diff options
author | Dmitri Pal <dpal@redhat.com> | 2010-11-25 20:51:49 -0500 |
---|---|---|
committer | Stephen Gallagher <sgallagh@redhat.com> | 2010-12-21 11:16:09 -0500 |
commit | 94e5ad22e13733a21ac612b601eb1ba5873d2b1e (patch) | |
tree | 4aa905d7d1ff026b58bcc712deed860005e7c5fa /ini/ini_comment_ut.c | |
parent | 8c201905d5f0720b62d036eb2308f81f4530cfad (diff) | |
download | ding-libs-94e5ad22e13733a21ac612b601eb1ba5873d2b1e.tar.gz ding-libs-94e5ad22e13733a21ac612b601eb1ba5873d2b1e.tar.xz ding-libs-94e5ad22e13733a21ac612b601eb1ba5873d2b1e.zip |
Refactoring comment object
* Added copy coment functionality
* Changed the internal implementation to
use array simple buffer instead of
the two arrays one for lines and one for lengths
* Realigned comments in the header file
* Changed the trace macros to use
simplified entry and exit macros.
* Corrected unit test to return error codes.
* Refactored unit test to use array
of test functions intead of the direct calls
* Added unit test for copy functionality.
Diffstat (limited to 'ini/ini_comment_ut.c')
-rw-r--r-- | ini/ini_comment_ut.c | 115 |
1 files changed, 102 insertions, 13 deletions
diff --git a/ini/ini_comment_ut.c b/ini/ini_comment_ut.c index 49c56fc..56474f5 100644 --- a/ini/ini_comment_ut.c +++ b/ini/ini_comment_ut.c @@ -22,6 +22,7 @@ #include <stdio.h> #include <string.h> #include <errno.h> +#include <stdlib.h> #define TRACE_HOME #include "trace.h" #include "ini_comment.h" @@ -33,6 +34,7 @@ int verbose = 0; if (verbose) foo; \ } while(0) +typedef int (*test_fn)(void); int file_test(void) { @@ -46,7 +48,7 @@ int file_test(void) printf("Failed to create comment object %d\n", error); ini_comment_destroy(ic); - return -1; + return error; } INIOUT(printf("<==== Comment ====>\n")); @@ -81,7 +83,7 @@ int alter_test(void) (error = ini_comment_build(ic, ""))) { printf("Failed to create comment object\n"); ini_comment_destroy(ic); - return -1; + return error; } INIOUT(printf("<==== Comment ====>\n")); @@ -96,7 +98,7 @@ int alter_test(void) (error = ini_comment_insert(ic, 1, ";Line 1 inserted"))) { printf("Failed to create comment object\n"); ini_comment_destroy(ic); - return -1; + return error; } INIOUT(printf("<==== Comment ====>\n")); @@ -107,7 +109,7 @@ int alter_test(void) if (error) { printf("Failed to get number of lines.\n"); ini_comment_destroy(ic); - return -1; + return error; } for (i = 0; i < num; i++) { @@ -115,14 +117,14 @@ int alter_test(void) if (error) { printf("Failed to get line.\n"); ini_comment_destroy(ic); - return -1; + return error; } if (strcmp(line, expected[i]) != 0) { printf("Lines do not match.\n"); printf("GOT: %s\n", line); printf("EXP: %s\n", expected[i]); ini_comment_destroy(ic); - return -1; + return error; } } @@ -133,7 +135,7 @@ int alter_test(void) (error = ini_comment_swap(ic, 2 , 4))) { printf("Failed to swap lines.\n"); ini_comment_destroy(ic); - return -1; + return error; } for (i = 0; i < num; i++) { @@ -141,14 +143,14 @@ int alter_test(void) if (error) { printf("Failed to get line.\n"); ini_comment_destroy(ic); - return -1; + return error; } if (strcmp(line, expected[6 - i]) != 0) { printf("Lines do not match.\n"); printf("GOT: %s\n", line); printf("EXP: %s\n", expected[6 - i]); ini_comment_destroy(ic); - return -1; + return error; } } @@ -156,19 +158,106 @@ int alter_test(void) return error; } +int copy_test(void) +{ + int error = EOK; + struct ini_comment *ic = NULL; + struct ini_comment *ic_copy = NULL; + char *line = NULL; + char *line_copy = NULL; + uint32_t i, num = 0; + + INIOUT(printf("\n\nCopy test\n\n")); + + if ((error = ini_comment_create(&ic)) || + (error = ini_comment_build(ic, ";Line 0")) || + (error = ini_comment_build(ic, ";Line 1")) || + (error = ini_comment_build(ic, ";Line 2"))) { + printf("Failed to create comment object %d\n", + error); + ini_comment_destroy(ic); + return error; + } + + INIOUT(printf("<==== Comment ====>\n")); + INIOUT(ini_comment_print(ic, stdout)); + INIOUT(printf("<=================>\n")); + + if ((error = ini_comment_copy(ic, &ic_copy))) { + printf("Failed to create comment object %d\n", + error); + ini_comment_destroy(ic); + return error; + } + + INIOUT(printf("<==== Comment Copy====>\n")); + INIOUT(ini_comment_print(ic_copy, stdout)); + INIOUT(printf("<=================>\n")); + + error = ini_comment_get_numlines(ic, &num); + if (error) { + printf("Failed to get number of lines.\n"); + ini_comment_destroy(ic); + ini_comment_destroy(ic_copy); + return error; + } + + for (i = 0; i < num; i++) { + error = ini_comment_get_line(ic, i, &line, NULL); + if (error) { + printf("Failed to get line.\n"); + ini_comment_destroy(ic); + ini_comment_destroy(ic_copy); + return error; + } + error = ini_comment_get_line(ic_copy, i, &line_copy, NULL); + if (error) { + printf("Failed to get line.\n"); + ini_comment_destroy(ic); + ini_comment_destroy(ic_copy); + return error; + } + if (strcmp(line, line_copy) != 0) { + printf("Lines do not match.\n"); + printf("Source: %s\n", line); + printf("Copy: %s\n", line_copy); + ini_comment_destroy(ic); + ini_comment_destroy(ic_copy); + return error; + } + } + + ini_comment_destroy(ic); + ini_comment_destroy(ic_copy); + + return error; +} int main(int argc, char *argv[]) { int error = EOK; + test_fn tests[] = { file_test, + alter_test, + copy_test, + NULL }; + test_fn t; + int i = 0; + char *var; if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1; + else { + var = getenv("COMMON_TEST_VERBOSE"); + if (var) verbose = 1; + } INIOUT(printf("Start\n")); - if ((error = file_test()) || - (error = alter_test())) { - printf("Test failed! Error %d.\n", error); - return -1; + while ((t = tests[i++])) { + error = t(); + if (error) { + INIOUT(printf("Failed with error %d!\n", error)); + return error; + } } INIOUT(printf("Success!\n")); |