2006-08-17 Aaron Bockover <abockover@novell.com>
authorAaron Bockover <abockover@novell.com>
Thu, 17 Aug 2006 19:32:01 +0000 (19:32 -0000)
committerAaron Bockover <abockover@novell.com>
Thu, 17 Aug 2006 19:32:01 +0000 (19:32 -0000)
    * 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

14 files changed:
eglib/ChangeLog
eglib/src/Makefile.am
eglib/src/glib.h
eglib/src/gstr.c
eglib/test/Makefile.am
eglib/test/driver.c
eglib/test/hash.c
eglib/test/hashtable.h [new file with mode: 0644]
eglib/test/slist.c
eglib/test/slist.h [new file with mode: 0644]
eglib/test/str.c
eglib/test/string-util.h [new file with mode: 0644]
eglib/test/test.c [new file with mode: 0644]
eglib/test/test.h

index 96deeda81541d2ca426442abfec25c2706fb770c..d9dff20e7064073b7445ee13eb92322fee674ba6 100644 (file)
@@ -1,3 +1,26 @@
+2006-08-17  Aaron Bockover  <abockover@novell.com>
+       
+       * 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  <duncan@a-chinaman.com>
 
        * src/gslist.c (g_slist_remove_link): I misread the function
index 5af41f57aa73fe9d571cf0f0af20df0c372eeb81..f5604c9ec4cddb21af91f7bd0800c13b44128a7c 100644 (file)
@@ -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)
 
index f6230e807b7f0eaec072c23cae7fcc86e1439e4f..60aea1688b1936da0fdfa20572578c6a5dabc1cf 100644 (file)
@@ -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)
 
 /*
index a41921726b7a0779c20b14b68a824a46ca5870e5..d1c49e79bf7cc54730acbf0cd215442047222b95 100644 (file)
@@ -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)
 {
index 7b31df754364a3299a679ac9c6a29ba9e2235766..5f544456912d072c65909af49c90427ba313ffd3 100644 (file)
@@ -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
index eed57d3c894af09b867dd6ac0a8a9e98bd107452..59996d28f54e4fd9e1aa5c7586c0ccc6dcae1f06 100644 (file)
@@ -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;
 }
+
index a68b5838125aed6679096e030a3527757140bac8..426797279e040bbb69beee32b7936d35b7b89790 100644 (file)
@@ -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 (file)
index 0000000..2638665
--- /dev/null
@@ -0,0 +1,4 @@
+#include "test.h"
+
+DEFINE_TEST_GROUP_INIT_H(hashtable_tests_init);
+
index 92a4a47434e4473a938e561142e227f8000dc50c..6008e46e3975c4bebdf1591d23fd4cc387ea8eab 100644 (file)
@@ -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 (file)
index 0000000..398be8b
--- /dev/null
@@ -0,0 +1,4 @@
+#include "test.h"
+
+DEFINE_TEST_GROUP_INIT_H(slist_tests_init);
+
index abeb1fe3daa6b27c69c4401993ad54f063048842..b4f90c42ccd94103c987950cf1015e69268b4ae1 100644 (file)
@@ -1,5 +1,6 @@
 #include <glib.h>
 #include <stdio.h>
+#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 (file)
index 0000000..29a1ca2
--- /dev/null
@@ -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 (file)
index 0000000..82430a8
--- /dev/null
@@ -0,0 +1,60 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+
+#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);
+}
+
index 85a68a6aa3fc699835126da812880c62e5e7c0b0..8d69bb500568866976048a0b7bdebb79bf91f5da 100644 (file)
@@ -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 <stdarg.h>
 
-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 */