From: Aaron Bockover Date: Thu, 17 Aug 2006 19:32:01 +0000 (-0000) Subject: 2006-08-17 Aaron Bockover X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=000f6579f2d952fbbe68697b331bfefeff04ee61;p=mono.git 2006-08-17 Aaron Bockover * test/test.h: * test/test.c: Added group iterator/test driver functionality * test/driver.c: Added groups to run using new test functionality * test/slist.h: * test/hashtable.h: * test/string-util.h: Test group definitions for string util/hashtable * test/slist.c: * test/str.c: * test/hash.c: Added test definition table * test/Makefile.am: Added -Wall -Werror -D_FORTIFY_SOURCE=2 * src/gstr.c: Added implementation for g_str_has_prefix, g_str_has_suffix * src/glib.h: Added missing function signatures * src/Makefile.am: added -D_FORTIFY_SOURCE=2 svn path=/trunk/mono/; revision=63937 --- diff --git a/eglib/ChangeLog b/eglib/ChangeLog index 96deeda8154..d9dff20e706 100644 --- a/eglib/ChangeLog +++ b/eglib/ChangeLog @@ -1,3 +1,26 @@ +2006-08-17 Aaron Bockover + + * test/test.h: + * test/test.c: Added group iterator/test driver functionality + + * test/driver.c: Added groups to run using new test functionality + + * test/slist.h: + * test/hashtable.h: + * test/string-util.h: Test group definitions for string util/hashtable + + * test/slist.c: + * test/str.c: + * test/hash.c: Added test definition table + + * test/Makefile.am: Added -Wall -Werror -D_FORTIFY_SOURCE=2 + + * src/gstr.c: Added implementation for g_str_has_prefix, g_str_has_suffix + + * src/glib.h: Added missing function signatures + + * src/Makefile.am: added -D_FORTIFY_SOURCE=2 + 2006-08-17 Duncan Mak * src/gslist.c (g_slist_remove_link): I misread the function diff --git a/eglib/src/Makefile.am b/eglib/src/Makefile.am index 5af41f57aa7..f5604c9ec4c 100644 --- a/eglib/src/Makefile.am +++ b/eglib/src/Makefile.am @@ -9,7 +9,7 @@ libeglib_la_SOURCES = \ gslist.c \ gstring.c -libeglib_la_CFLAGS = -Wall -Werror +libeglib_la_CFLAGS = -Wall -Werror -D_FORTIFY_SOURCE=2 INCLUDES = -I$(srcdir) diff --git a/eglib/src/glib.h b/eglib/src/glib.h index f6230e807b7..60aea1688b1 100644 --- a/eglib/src/glib.h +++ b/eglib/src/glib.h @@ -116,6 +116,8 @@ void g_strfreev (gchar **str_array); gchar *g_strconcat (const gchar *first, ...); gchar **g_strsplit (const gchar *string, const gchar *delimiter, gint max_tokens); gchar *g_strreverse (gchar *str); +gboolean g_str_has_prefix (const gchar *str, const gchar *prefix); +gboolean g_str_has_suffix (const gchar *str, const gchar *suffix); /* * String type @@ -135,7 +137,7 @@ void g_string_printf (GString *string, const gchar *format, ...); void g_string_append_printf (GString *string, const gchar *format, ...); GString *g_string_append_c (GString *string, gchar c); GString *g_string_append (GString *string, const gchar *val); - +GString *g_string_append_len (GString *string, const gchar *val, gsize len); #define g_string_sprintfa g_string_append_printf /* @@ -162,7 +164,7 @@ GSList *g_slist_reverse (GSList *list); GSList *g_slist_remove_link (GSList *list, GSList *link); GSList *g_slist_delete_link (GSList *list, GSList *link); GSList *g_slist_insert_sorted (GSList *list, gpointer data, GCompareFunc func); - +guint g_slist_length (GSList *list); #define g_slist_next (slist) ((slist) ? (((GSList *) slist)->next) : NULL) /* diff --git a/eglib/src/gstr.c b/eglib/src/gstr.c index a41921726b7..d1c49e79bf7 100644 --- a/eglib/src/gstr.c +++ b/eglib/src/gstr.c @@ -51,6 +51,49 @@ g_strfreev (gchar **str_array) g_free (orig); } +gint +g_strv_length(gchar **str_array) +{ + gint length = 0; + g_return_val_if_fail(str_array != NULL, 0); + for(length = 0; str_array[length] != NULL; length++); + return length; +} + +gboolean +g_str_has_suffix(const gchar *str, const gchar *suffix) +{ + gint str_length; + gint suffix_length; + + g_return_val_if_fail(str != NULL, FALSE); + g_return_val_if_fail(suffix != NULL, FALSE); + + str_length = strlen(str); + suffix_length = strlen(suffix); + + return suffix_length <= str_length ? + strncmp(str + str_length - suffix_length, suffix, suffix_length) == 0 : + FALSE; +} + +gboolean +g_str_has_prefix(const gchar *str, const gchar *prefix) +{ + gint str_length; + gint prefix_length; + + g_return_val_if_fail(str != NULL, FALSE); + g_return_val_if_fail(prefix != NULL, FALSE); + + str_length = strlen(str); + prefix_length = strlen(prefix); + + return prefix_length <= str_length ? + strncmp(str, prefix, prefix_length) == 0 : + FALSE; +} + gchar * g_strdup_vprintf (const gchar *format, va_list args) { diff --git a/eglib/test/Makefile.am b/eglib/test/Makefile.am index 7b31df75436..5f544456912 100644 --- a/eglib/test/Makefile.am +++ b/eglib/test/Makefile.am @@ -1,12 +1,13 @@ noinst_PROGRAMS = test test_SOURCES = \ - test.h \ + test.c \ driver.c \ hash.c \ str.c \ slist.c +test_CFLAGS = -Wall -Werror -D_FORTIFY_SOURCE=2 INCLUDES = -I../src test_LDADD = -L../src -leglib diff --git a/eglib/test/driver.c b/eglib/test/driver.c index eed57d3c894..59996d28f54 100644 --- a/eglib/test/driver.c +++ b/eglib/test/driver.c @@ -2,19 +2,19 @@ #include "test.h" -int main () +#include "string-util.h" +#include "hashtable.h" +#include "slist.h" + +int main() { - printf ("hashtable\n"); - test ("hash-1", hash_t1); - test ("s-freev", test_strfreev); - test ("s-concat", test_concat); - test ("s-split", test_split); - test ("s-gstring", test_gstring); - test ("s-gstrreverse", test_strreverse); - test ("s-slist-append", test_slist_append); - test ("s-slist-concat", test_slist_concat); - test ("s-slist-find", test_slist_find); - test ("s-slist-remove", test_slist_remove); - test ("s-slist-remove-link", test_slist_remove_link); - test ("s-slist-insert-sorted", test_slist_insert_sorted); + run_groups( + "string", string_tests_init, + "hashtable", hashtable_tests_init, + "slist", slist_tests_init, + NULL + ); + + return 0; } + diff --git a/eglib/test/hash.c b/eglib/test/hash.c index a68b5838125..426797279e0 100644 --- a/eglib/test/hash.c +++ b/eglib/test/hash.c @@ -44,5 +44,14 @@ char *hash_t1 (void) char *hash_t2 (void) { - + return RESULT("test body not defined"); } + +static Test hashtable_tests [] = { + {"hash_t1", hash_t1}, + {"hash_t2", hash_t2}, + {NULL, NULL} +}; + +DEFINE_TEST_GROUP_INIT(hashtable_tests_init, hashtable_tests) + diff --git a/eglib/test/hashtable.h b/eglib/test/hashtable.h new file mode 100644 index 00000000000..26386654332 --- /dev/null +++ b/eglib/test/hashtable.h @@ -0,0 +1,4 @@ +#include "test.h" + +DEFINE_TEST_GROUP_INIT_H(hashtable_tests_init); + diff --git a/eglib/test/slist.c b/eglib/test/slist.c index 92a4a47434e..6008e46e397 100644 --- a/eglib/test/slist.c +++ b/eglib/test/slist.c @@ -128,3 +128,16 @@ test_slist_insert_sorted () return NULL; } + +static Test slist_tests [] = { + {"slist_append", test_slist_append}, + {"slist_concat", test_slist_concat}, + {"slist_find", test_slist_find}, + {"slist_remove", test_slist_remove}, + {"slist_remove_link", test_slist_remove_link}, + {"slist_insert_sorted", test_slist_insert_sorted}, + {NULL, NULL} +}; + +DEFINE_TEST_GROUP_INIT(slist_tests_init, slist_tests) + diff --git a/eglib/test/slist.h b/eglib/test/slist.h new file mode 100644 index 00000000000..398be8bd0ea --- /dev/null +++ b/eglib/test/slist.h @@ -0,0 +1,4 @@ +#include "test.h" + +DEFINE_TEST_GROUP_INIT_H(slist_tests_init); + diff --git a/eglib/test/str.c b/eglib/test/str.c index abeb1fe3daa..b4f90c42ccd 100644 --- a/eglib/test/str.c +++ b/eglib/test/str.c @@ -1,5 +1,6 @@ #include #include +#include "test.h" /* This test is just to be used with valgrind */ char * @@ -27,7 +28,7 @@ test_concat () return NULL; } -#define sfail(k,p) if (s->str [p] != k) { g_string_free (s,TRUE); return g_strdup_printf ("Failed at %d, expected '%c'", p, k);} +#define sfail(k,p) if (s->str [p] != k) { g_string_free (s,TRUE); return g_strdup_printf ("Got %s, Failed at %d, expected '%c'", s, p, k);} char * test_gstring () @@ -37,22 +38,21 @@ test_gstring () int i; if (strcmp (s->str, "My") != 0) - return "Expected only 'My' on the string"; + return RESULT("Expected only 'My' on the string"); g_string_free (s, TRUE); s = g_string_new_len ("My\0\0Rest", 6); if (s->str [2] != 0) - return "Null was not copied"; + return RESULT("Null was not copied"); if (strcmp (s->str+4, "Re") != 0){ - return "Did not find the 'Re' part"; + return RESULT("Did not find the 'Re' part"); } g_string_append (s, "lalalalalalalalalalalalalalalalalalalalalalal"); if (s->str [2] != 0) - return "Null as not copied"; + return RESULT("Null as not copied"); if (strncmp (s->str+4, "Relala", 6) != 0){ - printf ("got: %s\n", s->str+4); - return "Did not copy correctly"; + return g_strdup_printf("Did not copy correctly, got: %s", s->str+4); } g_string_free (s, TRUE); @@ -61,8 +61,7 @@ test_gstring () g_string_append (s, "x"); } if (strlen (s->str) != 1024){ - printf ("got: %s %d\n", s->str, strlen (s->str)); - return "Incorrect string size"; + return g_strdup_printf("Incorrect string size, got: %s %d", s->str, strlen (s->str)); } g_string_free (s, TRUE); @@ -71,16 +70,14 @@ test_gstring () g_string_append_c (s, 'x'); } if (strlen (s->str) != 1024){ - printf ("got: %s %d\n", s->str, strlen (s->str)); - return "Incorrect string size"; + return g_strdup_printf("Incorrect string size, got: %s %d\n", s->str, strlen (s->str)); } g_string_free (s, TRUE); s = g_string_new ("hola"); g_string_sprintfa (s, "%s%d", ", bola", 5); if (strcmp (s->str, "hola, bola5") != 0){ - printf ("got: %s\n", s->str); - return "Got incorrect data"; + return g_strdup_printf("Incorrect data, got: %s\n", s->str); } g_string_free (s, TRUE); @@ -94,10 +91,10 @@ test_gstring () s = g_string_new_len ("H\000H", 3); g_string_append_len (s, "1\0002", 3); sfail ('H', 0); - sfail (0, 1); + sfail ( 0, 1); sfail ('H', 2); sfail ('1', 3); - sfail (0, 4); + sfail ( 0, 4); sfail ('2', 5); g_string_free (s, TRUE); @@ -111,7 +108,7 @@ test_split () int i = 0; if(v == NULL) { - return g_strdup_printf("split failed, got NULL vector"); + return RESULT("split failed, got NULL vector"); } else { for(i = 0; v[i] != NULL; i++); if(i != 7) { @@ -149,3 +146,14 @@ test_strreverse () return NULL; } +static Test string_tests [] = { + {"g_strfreev", test_strfreev}, + {"g_strconcat", test_concat}, + {"GString", test_gstring}, + {"g_strsplit", test_split}, + {"g_strreverse", test_strreverse}, + {NULL, NULL} +}; + +DEFINE_TEST_GROUP_INIT(string_tests_init, string_tests) + diff --git a/eglib/test/string-util.h b/eglib/test/string-util.h new file mode 100644 index 00000000000..29a1ca26b24 --- /dev/null +++ b/eglib/test/string-util.h @@ -0,0 +1,4 @@ +#include "test.h" + +DEFINE_TEST_GROUP_INIT_H(string_tests_init); + diff --git a/eglib/test/test.c b/eglib/test/test.c new file mode 100644 index 00000000000..82430a85014 --- /dev/null +++ b/eglib/test/test.c @@ -0,0 +1,60 @@ +#include +#include +#include + +#include "test.h" + +void +run_test(Test *test) +{ + char *result; + printf(" %s: ", test->name); + fflush(stdout); + if((result = test->handler()) == NULL) { + printf("OK\n"); + } else { + printf("FAILED (%s)\n", result); + free(result); + } +} + +void +run_group(const char *name, LoadGroupHandler group_handler) +{ + Test *tests = group_handler(); + int i; + + printf("[%s]\n", name); + + for(i = 0; tests[i].name != NULL; i++) { + run_test(&(tests[i])); + } +} + +void +run_groups(const char *first_name, LoadGroupHandler first_group_handler, ...) +{ + va_list args; + va_start(args, first_group_handler); + + run_group(first_name, first_group_handler); + + while(1) { + const char *name; + LoadGroupHandler group_handler; + + if((name = (const char *)va_arg(args, const char **)) == NULL) { + break; + } + + if((group_handler = (LoadGroupHandler)va_arg(args, + LoadGroupHandler)) == NULL) { + break; + } + + run_group(name, group_handler); + } + + va_end(args); +} + diff --git a/eglib/test/test.h b/eglib/test/test.h index 85a68a6aa3f..8d69bb50056 100644 --- a/eglib/test/test.h +++ b/eglib/test/test.h @@ -1,23 +1,29 @@ -#define test(name,func) do { char *r; printf (" test: %s: ", name); fflush (stdout);r = func (); if (r){printf ("failure (%s)\n",r); free (r);} else printf ("OK\n");} while (0); +#ifndef _TEST_H +#define _TEST_H +#include -char *test_concat (); -char *test_strfreev (); -char *test_gstring (); -char *test_split (); -char *test_strreverse (); -char *hash_t1 (void); -char *hash_t2 (void); -char *test_slist_append (); -char *test_slist_concat (); -char *test_slist_find (); -char *test_slist_remove (); -char *test_slist_remove_link (); +typedef struct _Test Test; +typedef char * (* RunTestHandler)(); +typedef Test * (* LoadGroupHandler)(); +struct _Test { + const char *name; + RunTestHandler handler; +}; +void run_test(Test *test); +void run_group(const char *name, LoadGroupHandler group_handler); +void run_groups(const char *first_name, LoadGroupHandler first_group_handler, ...); +#define DEFINE_TEST_GROUP_INIT(name, table) \ + Test * (name)() { return table; } +#define DEFINE_TEST_GROUP_INIT_H(name) \ + Test * (name)(); +#define RESULT(x) g_strdup_printf(x); +#endif /* _TEST_H */