From bf7b2fbe64c6fa27b094dd7e0b739a9674d9c17c Mon Sep 17 00:00:00 2001 From: Nathan Angelacos Date: Tue, 3 Jun 2008 02:12:16 +0000 Subject: minor test changes --- src/Makefile.am | 2 +- src/common.c | 1 + src/h_script.c | 1 + src/haserl.c | 33 +---------- src/rfc2388.c | 1 + tests/utest_common.c | 65 +++++++++++---------- tests/utest_llist.c | 156 ++++++++++++++++++++++++++++----------------------- 7 files changed, 127 insertions(+), 132 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index c12fc80..a2b26ca 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -46,7 +46,7 @@ bin_PROGRAMS = haserl haserl_SOURCES = common.c common.h sliding_buffer.c sliding_buffer.h \ h_error.c h_error.h h_script.c h_script.h rfc2388.c rfc2388.h \ - llist.c \ + llist.c xmalloc.c \ $(BASHSOURCE) $(LUASOURCE) haserl.c haserl.h install-strip: diff --git a/src/common.c b/src/common.c index 2ee4afb..bf877b5 100644 --- a/src/common.c +++ b/src/common.c @@ -30,6 +30,7 @@ #include #include "common.h" +#include "xmalloc.h" #include "h_error.h" diff --git a/src/h_script.c b/src/h_script.c index 029fc41..019cbf2 100644 --- a/src/h_script.c +++ b/src/h_script.c @@ -32,6 +32,7 @@ #include #include "common.h" +#include "xmalloc.h" #include "h_error.h" #include "h_script.h" #include "h_bash.h" diff --git a/src/haserl.c b/src/haserl.c index 3126c41..59b38d6 100644 --- a/src/haserl.c +++ b/src/haserl.c @@ -55,6 +55,7 @@ #include "h_script.h" #include "sliding_buffer.h" #include "rfc2388.h" +#include "xmalloc.h" #ifdef INCLUDE_BASHSHELL #include "h_bash.h" #endif @@ -179,38 +180,6 @@ unescape_url (char *url) } - - -/* - * allocate memory or die, busybox style. - */ -void * -xmalloc (size_t size) -{ - void *buf; - if ((buf = malloc (size)) == NULL) - { - die_with_message (NULL, NULL, g_err_msg[E_MALLOC_FAIL]); - } - memset (buf, 0, size); - return buf; -} - - -/* - * realloc memory, or die xmalloc style. - */ -void * -xrealloc (void *buf, size_t size) -{ - if ((buf = realloc (buf, size)) == NULL) - { - die_with_message (NULL, NULL, g_err_msg[E_MALLOC_FAIL]); - } - return buf; -} - - /* * adds or replaces the "key=value" value in the env_list chain * prefix is appended to the key (e.g. FORM_key=value) diff --git a/src/rfc2388.c b/src/rfc2388.c index 8aa5d19..4a261b3 100644 --- a/src/rfc2388.c +++ b/src/rfc2388.c @@ -42,6 +42,7 @@ #include "h_bash.h" #include "sliding_buffer.h" #include "rfc2388.h" +#include "xmalloc.h" #include "haserl.h" diff --git a/tests/utest_common.c b/tests/utest_common.c index 0085cb4..c7a34b8 100644 --- a/tests/utest_common.c +++ b/tests/utest_common.c @@ -1,5 +1,5 @@ /* file minunit_example.c */ - + #include #include #include @@ -9,38 +9,45 @@ int tests_run = 0; -void * xmalloc=malloc; -void * xrealloc=realloc; +void *xmalloc = malloc; +void *xrealloc = realloc; -char *test_lowercase() { +char * +test_lowercase () +{ char source[] = "This is a Test!"; int result; - - lowercase(source); - result=memcmp("this is a test!", source, strlen(source)); - mu_assert("lowercase failed",result == 0); + lowercase (source); + result = memcmp ("this is a test!", source, strlen (source)); + + mu_assert ("lowercase failed", result == 0); return 0; - } - - -char *all_tests() { - mu_run_test(test_lowercase); - return 0; - } - -int main(int argc, char **argv) { - char *result = all_tests(); - printf ("%d\n", (int) result); - if (result != 0) { - printf("%s\n", result); - } - else { - printf("ALL TESTS PASSED\n"); - } - printf("Tests run: %d\n", tests_run); - - return result != 0; - } +} + +char * +all_tests () +{ + mu_run_test (test_lowercase); + return 0; +} + +int +main (int argc, char **argv) +{ + char *result = all_tests (); + printf ("%d\n", (int) result); + if (result != 0) + { + printf ("%s\n", result); + } + else + { + printf ("ALL TESTS PASSED\n"); + } + printf ("Tests run: %d\n", tests_run); + + return result != 0; +} diff --git a/tests/utest_llist.c b/tests/utest_llist.c index b84134d..6ec1a70 100644 --- a/tests/utest_llist.c +++ b/tests/utest_llist.c @@ -17,24 +17,32 @@ void llist_free(llist_t *elm, void (*freeit)(void *data)); */ char *elem[] = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", - "Golf", "Hotel", "India", "Juliet", "Kilo", NULL }; + "Golf", "Hotel", "India", "Juliet", "Kilo", NULL +}; char *freemelist = NULL; -void freeme ( void *elem) { - if (!freemelist) { - freemelist = xmalloc (strlen(elem)+1); - memcpy(freemelist,elem,strlen(elem)+1); - } else { - freemelist= xrealloc ( freemelist, strlen(freemelist) + - strlen(elem)+1); - memcpy(freemelist+strlen(freemelist),elem,strlen(elem)+1); - } +void +freeme (void *elem) +{ + if (!freemelist) + { + freemelist = xmalloc (strlen (elem) + 1); + memcpy (freemelist, elem, strlen (elem) + 1); + } + else + { + freemelist = xrealloc (freemelist, strlen (freemelist) + + strlen (elem) + 1); + memcpy (freemelist + strlen (freemelist), elem, strlen (elem) + 1); + } } -char *test_basic_api() { - int result,count; +char * +test_basic_api () +{ + int result, count; llist_t *my_list = NULL; llist_t *client_list = NULL; char *data; @@ -42,94 +50,102 @@ char *test_basic_api() { /* Check adding to a new list */ llist_add_to (&my_list, elem[2]); llist_add_to (&my_list, elem[1]); - - result=memcmp(elem[1], my_list->data, strlen(elem[1])); - mu_assert("add_to failed",result == 0); - - + + result = memcmp (elem[1], my_list->data, strlen (elem[1])); + mu_assert ("add_to failed", result == 0); + + /* Check adding to the end of an existing list */ llist_add_to_end (&my_list, elem[3]); - mu_assert("add_to_end failed",result == 0); - + mu_assert ("add_to_end (1) failed", + memcmp (elem[1], my_list->data, strlen (elem[1])) == 0); + client_list = my_list; - while ( client_list->link ) { - client_list = client_list->link; - } - result=memcmp(elem[3], client_list->data, strlen(elem[3])); - mu_assert("add_to_end failed",result == 0); - + while (client_list->link) + { + client_list = client_list->link; + } + result = memcmp (elem[3], client_list->data, strlen (elem[3])); + mu_assert ("add_to_end (2) failed", result == 0); + /* verify add_to on an existing list pushes to top */ llist_add_to (&my_list, elem[0]); - result=memcmp(elem[0], my_list->data, strlen(elem[0])); - mu_assert("add_to_end (pass 2) failed",result == 0); - + mu_assert ("add_to (pass 2) failed", + memcmp (elem[0], my_list->data, strlen (elem[0])) == 0); + /* check pop pops the top */ client_list = my_list->link; data = llist_pop (&my_list); - result=memcmp(elem[0], data, strlen(elem[0])); - mu_assert("llist pop failed",result == 0); - mu_assert("llist pop (2) failed", client_list == my_list); - result=memcmp(elem[1], my_list->data, strlen(elem[1])); - mu_assert("llist pop (3) failed",result == 0); + result = memcmp (elem[0], data, strlen (elem[0])); + mu_assert ("llist pop failed", result == 0); + mu_assert ("llist pop (2) failed", client_list == my_list); + result = memcmp (elem[1], my_list->data, strlen (elem[1])); + mu_assert ("llist pop (3) failed", result == 0); /* check unlinking */ llist_add_to (&my_list, elem[0]); client_list = my_list->link; - + llist_unlink (&my_list, my_list->link); - result=memcmp(elem[0], my_list->data, strlen(elem[0])); - mu_assert("llist unlink failed",result == 0); + result = memcmp (elem[0], my_list->data, strlen (elem[0])); + mu_assert ("llist unlink failed", result == 0); client_list = my_list->link; - result=memcmp(elem[2], client_list->data, strlen(elem[2])); - mu_assert("llist unlink (2) failed",result == 0); + result = memcmp (elem[2], client_list->data, strlen (elem[2])); + mu_assert ("llist unlink (2) failed", result == 0); llist_unlink (&my_list, my_list); - result=memcmp(elem[2], my_list->data, strlen(elem[2])); - mu_assert("llist unlink (3) failed",result == 0); + result = memcmp (elem[2], my_list->data, strlen (elem[2])); + mu_assert ("llist unlink (3) failed", result == 0); /* check freeing the entire list */ llist_add_to (&my_list, elem[1]); llist_add_to (&my_list, elem[0]); - result=1; + result = 1; client_list = my_list; - while (client_list->link) { - result++; - client_list = client_list->link; - } + while (client_list->link) + { + result++; + client_list = client_list->link; + } llist_free (my_list, &freeme); data = freemelist; freemelist = NULL; - for (count=0; count < result; count++) { - freeme(elem[count]); - } - result=memcmp(data, freemelist, strlen(freemelist)); - mu_assert("llist free failed",result == 0); + for (count = 0; count < result; count++) + { + freeme (elem[count]); + } + result = memcmp (data, freemelist, strlen (freemelist)); + mu_assert ("llist free failed", result == 0); free (data); free (freemelist); freemelist = NULL; data = NULL; return 0; - } - - -char *all_tests() { - mu_run_test(test_basic_api); - return 0; - } - -int main(int argc, char **argv) { - char *result = all_tests(); - // printf ("%d\n", (int) result); - if (result != 0) { - printf("%s\n", result); - } - /* +} + + +char * +all_tests () +{ + mu_run_test (test_basic_api); + return 0; +} + +int +main (int argc, char **argv) +{ + char *result = all_tests (); + // printf ("%d\n", (int) result); + if (result != 0) + { + printf ("%s\n", result); + } + /* else { - printf("ALL TESTS PASSED\n"); + printf("ALL TESTS PASSED\n"); } printf("Tests run: %d\n", tests_run); - */ - return result != 0; - } - + */ + return result != 0; +} -- cgit v1.2.3