2006-08-18 Aaron Bockover <abockover@novell.com>
authorAaron Bockover <abockover@novell.com>
Sat, 19 Aug 2006 00:10:39 +0000 (00:10 -0000)
committerAaron Bockover <abockover@novell.com>
Sat, 19 Aug 2006 00:10:39 +0000 (00:10 -0000)
    * test/driver.c: Added getopt support and code timing, among other
    nice features to make testing/profiling easier

    * test/test.c: Add support for suppressing output (quiet) and
    define RESULT, FAILED, and OK

    * src/glib.h:
    * src/glist.c: Added g_list_nth_data implementation

    * test/slist.c:
    * test/string-util.c:
    * test/ptrarray.c:
    * test/string.c:
    * test/hashtable.c:
    * test/list.c: Use RESULT, FAILURE, and OK for tests

svn path=/trunk/mono/; revision=64032

12 files changed:
eglib/ChangeLog
eglib/src/glib.h
eglib/src/glist.c
eglib/test/driver.c
eglib/test/hashtable.c
eglib/test/list.c
eglib/test/ptrarray.c
eglib/test/slist.c
eglib/test/string-util.c
eglib/test/string.c
eglib/test/test.c
eglib/test/test.h

index 4fbe104b441b690890d4cc393b9520b08feabbdc..e3cf2df2530a6c94270083bdc26394314fe94eea 100644 (file)
@@ -1,3 +1,21 @@
+2006-08-18  Aaron Bockover  <abockover@novell.com>
+
+       * test/driver.c: Added getopt support and code timing, among other 
+       nice features to make testing/profiling easier
+
+       * test/test.c: Add support for suppressing output (quiet) and
+       define RESULT, FAILED, and OK
+
+       * src/glib.h:
+       * src/glist.c: Added g_list_nth_data implementation
+
+       * test/slist.c:
+       * test/string-util.c:
+       * test/ptrarray.c:
+       * test/string.c:
+       * test/hashtable.c:
+       * test/list.c: Use RESULT, FAILURE, and OK for tests
+
 2006-08-18  Miguel de Icaza  <miguel@novell.com>
 
        * src/eglib-config.h (G_OS_): Add the G_OS_UNIX and G_OS_WIN32
index 0cead189a7d52f2349dc4bc6e9ace1097a68033b..41b93fc65f10cd0e4d8b3e8e9501ad6ee65f0ee4 100644 (file)
@@ -236,6 +236,8 @@ gint   g_list_index         (GList         *list,
                             gconstpointer  data);
 GList *g_list_nth           (GList         *list,
                             guint          n);
+gpointer g_list_nth_data      (GList         *list,
+                            guint          n);
 GList *g_list_last          (GList         *list);
 GList *g_list_concat        (GList         *list1,
                             GList         *list2);
index a462b5455daa722db20977343a8b68d7d05dd9ca..98f447d0e6c233a89f1fb770db3e4c86ea3551d3 100644 (file)
@@ -284,6 +284,12 @@ g_list_nth (GList *list, guint n)
        return value;
 }
 
+gpointer
+g_list_nth_data (GList *list, guint n)
+{
+       return g_list_nth(list, n)->data;
+}
+
 GList*
 g_list_copy (GList *list)
 {
index 49ddfb8bc5f3951e3acc287fead17dc705ddbc2c..dea1cd21d3a3882355c64a6c8573ce2b69c48b2e 100644 (file)
@@ -28,6 +28,9 @@
  
 #include <stdio.h>
 #include <glib.h>
+#include <stddef.h>
+#include <sys/time.h>
+#include <getopt.h>
 
 #include "test.h"
 #include "tests.h"
@@ -36,10 +39,13 @@ static void print_help(char *s)
 {
        gint i;
        
-       printf("Usage: %s [options] [iterations [test1 test2 ... testN]]\n\n", s);
+       printf("Usage: %s [options] [test1 test2 ... testN]\n\n", s);
        printf(" options are:\n");
-       printf("   --help      show this help\n\n");
-       printf(" iterations:   number of times to run tests\n");
+       printf("   --help|-h          show this help\n");
+       printf("   --time|-t          time the tests\n");
+       printf("   --iterations|-i    number of times to run tests\n");
+       printf("   --quiet|-q         do not print test results; if -t\n");
+       printf("                      is passed, the time will print\n\n");
        printf(" test1..testN  name of test to run (all run by default)\n\n");
        printf(" available tests:\n");
 
@@ -52,39 +58,57 @@ static void print_help(char *s)
 
 gint main(gint argc, gchar **argv)
 {
-       gint i, j, k, iterations = 1, tests_to_run_count = 0;
-       gchar **tests_to_run = NULL;
+       gint i, j, c, iterations = 1;
+       GList *tests_to_run = NULL;
+       double time_start, time_end;
+       struct timeval tp;
+       gboolean report_time = FALSE;
+       gboolean quiet = FALSE;
        
-       if(argc > 1) {
-               for(i = 1; i < argc; i++) {
-                       if(strcmp(argv[i], "--help") == 0) {
+       static struct option long_options [] = {
+               {"help", no_argument, 0, 'h'},
+               {"time", no_argument, 0, 't'},
+               {"iterations", required_argument, 0, 'i'},
+               {0, 0, 0, 0}
+       };
+
+       while((c = getopt_long(argc, argv, "htqi:", long_options, NULL)) != -1) {                       switch(c) {
+                       case 'h':
                                print_help(argv[0]);
                                return 1;
-                       }
+                       case 't':
+                               report_time = TRUE;
+                               break;
+                       case 'i':
+                               iterations = atoi(optarg);
+                               break;
+                       case 'q':
+                               quiet = TRUE;
+                               break;
                }
-       
-               iterations = atoi(argv[1]);
-               tests_to_run_count = argc - 2;
-
-               if(tests_to_run_count > 0) {
-                       tests_to_run = (gchar **)g_new0(gchar *, tests_to_run_count + 1);
-
-                       for(i = 0; i < tests_to_run_count; i++) {
-                               tests_to_run[i] = argv[i + 2];
-                       }
+       }
 
-                       tests_to_run[tests_to_run_count] = NULL;
+       for(i = optind; i < argc; i++) {
+               if(argv[i][0] == '-') {
+                       continue;
                }
+
+               tests_to_run = g_list_append(tests_to_run, argv[i]);
        }
 
+       gettimeofday(&tp, NULL);
+       time_start = (double)tp.tv_sec + (1.e-6) * tp.tv_usec;
+
        for(i = 0; i < iterations; i++) {
                for(j = 0; test_groups[j].name != NULL; j++) {
                        gboolean run = TRUE;
                        
                        if(tests_to_run != NULL) {
+                               gint k, n;
                                run = FALSE;
-                               for(k = 0; tests_to_run[k] != NULL; k++) {
-                                       if(strcmp(tests_to_run[k], test_groups[j].name) == 0) {
+                               for(k = 0, n = g_list_length(tests_to_run); k < n; k++) {
+                                       if(strcmp((char *)g_list_nth_data(tests_to_run, k), 
+                                               test_groups[j].name) == 0) {
                                                run = TRUE;
                                                break;
                                        }
@@ -93,15 +117,25 @@ gint main(gint argc, gchar **argv)
                        
                        if(run) {
                                gint total, passed;
-                               run_group(&(test_groups[j]), &total, &passed);
-                               printf("  -- %d / %d (%g%%) --\n", passed, total,
-                                       ((gdouble)passed / (gdouble)total) * 100.0);
+                               run_group(&(test_groups[j]), &total, &passed, quiet);
+                               if(!quiet) {
+                                       printf("  -- %d / %d (%g%%) --\n", passed, total,
+                                               ((gdouble)passed / (gdouble)total) * 100.0);
+                               }
                        }
                }
        }
+       
+       gettimeofday(&tp, NULL);
+       time_end = (double)tp.tv_sec + (1.e-6) * tp.tv_usec;
+       
+       if(report_time) {
+               gdouble duration = time_end - time_start;
+               printf("Total Time: %gs\n", duration);
+       }
 
        if(tests_to_run != NULL) {
-               g_free(tests_to_run);
+               g_list_free(tests_to_run);
        }
 
        return 0;
index f76523603427de6939d38225c791ac339a424127..4547cbd631842a1c48c729ade0d55ff1222d2450 100644 (file)
@@ -13,7 +13,7 @@ void foreach (gpointer key, gpointer value, gpointer user_data)
                foreach_fail = 1;
 }
 
-char *hash_t1 (void)
+RESULT hash_t1 (void)
 {
        GHashTable *t = g_hash_table_new (g_str_hash, g_str_equal);
 
@@ -40,12 +40,12 @@ char *hash_t1 (void)
                return "unexpected size";
        g_hash_table_destroy (t);
 
-       return NULL;
+       return OK;
 }
 
-char *hash_t2 (void)
+RESULT hash_t2 (void)
 {
-       return NULL;
+       return OK;
 }
 
 static Test hashtable_tests [] = {
index 5f5bd8a74c482d7595832eebf290cea0f158a360..a0fc70cd2722e7a8aca3e1b47ca4ef96d0ddb698 100644 (file)
@@ -3,7 +3,7 @@
 #include <glib.h>
 #include "test.h"
 
-char*
+RESULT
 test_list_length ()
 {
        GList *list = g_list_prepend (NULL, "foo");
@@ -22,7 +22,7 @@ test_list_length ()
        return NULL;
 }
 
-char*
+RESULT
 test_list_nth ()
 {
        char *foo = "foo";
@@ -45,10 +45,10 @@ test_list_nth ()
        if (nth->data != baz)
                return "nth failed. #3";
 
-       return NULL;
+       return OK;
 }
 
-char*
+RESULT
 test_list_index ()
 {
        int i;
@@ -72,10 +72,10 @@ test_list_index ()
        if (i != 2)
                return "index failed. #3";
 
-       return NULL;
+       return OK;
 }
 
-char*
+RESULT
 test_list_append ()
 {
        GList *list = g_list_prepend (NULL, "first");
@@ -87,9 +87,10 @@ test_list_append ()
        if (g_list_length (list) != 2)
                return "Append failed";
 
-       return NULL;
+       return OK;
 }
-char *
+
+RESULT
 test_list_last ()
 {
        GList *foo = g_list_prepend (NULL, "foo");
@@ -109,10 +110,10 @@ test_list_last ()
        if (strcmp ("quux", last->data))
                return "last failed. #2";
 
-       return NULL;
+       return OK;
 }
 
-char *
+RESULT
 test_list_concat ()
 {
        GList *foo = g_list_prepend (NULL, "foo");
@@ -134,7 +135,7 @@ test_list_concat ()
        if (g_list_last (list) != bar)
                return "Concat failed. #5";
 
-       return NULL;
+       return OK;
 }
 
 
@@ -150,7 +151,7 @@ compare (gconstpointer a, gconstpointer b)
        return 1;
 }
 
-char*
+RESULT
 test_list_insert_sorted ()
 {
        GList *list = g_list_prepend (NULL, "a");
@@ -159,23 +160,23 @@ test_list_insert_sorted ()
        /* insert at the middle */
        list = g_list_insert_sorted (list, "aa", compare);
        if (strcmp ("aa", list->next->data))
-               return result ("insert_sorted failed. #1");
+               return FAILED ("insert_sorted failed. #1");
 
        /* insert at the beginning */
        list = g_list_insert_sorted (list, "", compare);
        
        if (strcmp ("", list->data))
-               return result ("insert_sorted failed. #2");             
+               return FAILED ("insert_sorted failed. #2");             
 
        /* insert at the end */
        list = g_list_insert_sorted (list, "aaaa", compare);
        if (strcmp ("aaaa", g_list_last (list)->data))
-               return result ("insert_sorted failed. #3");
+               return FAILED ("insert_sorted failed. #3");
 
-       return NULL;
+       return OK;
 }
 
-char *
+RESULT
 test_list_copy ()
 {
        int i, length;
@@ -192,10 +193,10 @@ test_list_copy ()
                if (strcmp (g_list_nth (list, i)->data,
                            g_list_nth (copy, i)->data))
                        return "copy failed.";
-       return NULL;
+       return OK;
 }
 
-char *
+RESULT
 test_list_reverse ()
 {
        guint i, length;
@@ -217,7 +218,7 @@ test_list_reverse ()
                            g_list_nth (reverse, j)->data))
                        return "reverse failed. #2";
        }
-       return NULL;
+       return OK;
 }
 
 static Test list_tests [] = {
index 33d3c3a6ab8250c201ae8af6a0dc8dc342f3aee2..778eb1f9a4b1989e5d6edfb22cebd5674d900cc0 100644 (file)
@@ -44,7 +44,7 @@ static gint guess_size(gint length)
        return size;
 }
 
-char *ptrarray_alloc()
+RESULT ptrarray_alloc()
 {
        GPtrArrayPriv *array;
        gint i;
@@ -52,20 +52,20 @@ char *ptrarray_alloc()
        array = (GPtrArrayPriv *)ptrarray_alloc_and_fill(&i);
        
        if(array->size != guess_size(array->len)) {
-               return result("Size should be %d, but it is %d", 
+               return FAILED("Size should be %d, but it is %d", 
                        guess_size(array->len), array->size);
        }
        
        if(array->len != i) {
-               return result("Expected %d node(s) in the array", i);
+               return FAILED("Expected %d node(s) in the array", i);
        }
        
        g_ptr_array_free((GPtrArray *)array, TRUE);
 
-       return NULL;
+       return OK;
 }
 
-char *ptrarray_for_iterate()
+RESULT ptrarray_for_iterate()
 {
        GPtrArray *array = ptrarray_alloc_and_fill(NULL);
        gint i;
@@ -73,7 +73,7 @@ char *ptrarray_for_iterate()
        for(i = 0; i < array->len; i++) {
                char *item = (char *)g_ptr_array_index(array, i);
                if(item != items[i]) {
-                       return result(
+                       return FAILED(
                                "Expected item at %d to be %s, but it was %s", 
                                i, items[i], item);
                }
@@ -81,7 +81,7 @@ char *ptrarray_for_iterate()
 
        g_ptr_array_free(array, TRUE);
 
-       return NULL;
+       return OK;
 }
 
 static gint foreach_iterate_index = 0;
@@ -97,13 +97,13 @@ void foreach_callback(gpointer data, gpointer user_data)
        }
 
        if(item != item_cmp) {
-               foreach_iterate_error = result(
+               foreach_iterate_error = FAILED(
                        "Expected item at %d to be %s, but it was %s", 
                                foreach_iterate_index - 1, item_cmp, item);
        }
 }
 
-char *ptrarray_foreach_iterate()
+RESULT ptrarray_foreach_iterate()
 {
        GPtrArray *array = ptrarray_alloc_and_fill(NULL);
        
@@ -117,7 +117,7 @@ char *ptrarray_foreach_iterate()
        return foreach_iterate_error;
 }
 
-char *ptrarray_set_size()
+RESULT ptrarray_set_size()
 {
        GPtrArray *array = g_ptr_array_new();
        gint i, grow_length = 50;
@@ -127,25 +127,25 @@ char *ptrarray_set_size()
        g_ptr_array_set_size(array, grow_length);
 
        if(array->len != grow_length) {
-               return result("Array length should be 50, it is %d", array->len);
+               return FAILED("Array length should be 50, it is %d", array->len);
        } else if(array->pdata[0] != items[0]) {
-               return result("Item 0 was overwritten, should be %s", items[0]);
+               return FAILED("Item 0 was overwritten, should be %s", items[0]);
        } else if(array->pdata[1] != items[1]) {
-               return result("Item 1 was overwritten, should be %s", items[1]);
+               return FAILED("Item 1 was overwritten, should be %s", items[1]);
        }
 
        for(i = 2; i < array->len; i++) {
                if(array->pdata[i] != NULL) {
-                       return result("Item %d is not NULL, it is %p", i, array->pdata[i]);
+                       return FAILED("Item %d is not NULL, it is %p", i, array->pdata[i]);
                }
        }
 
        g_ptr_array_free(array, TRUE);
 
-       return NULL;
+       return OK;
 }
 
-char *ptrarray_remove_index()
+RESULT ptrarray_remove_index()
 {
        GPtrArray *array;
        gint i;
@@ -154,21 +154,21 @@ char *ptrarray_remove_index()
        
        g_ptr_array_remove_index(array, 0);
        if(array->pdata[0] != items[1]) {
-               return result("First item is not %s, it is %s", items[1],
+               return FAILED("First item is not %s, it is %s", items[1],
                        array->pdata[0]);
        }
 
        g_ptr_array_remove_index(array, array->len - 1);
        
        if(array->pdata[array->len - 1] != items[array->len]) {
-               return result("Last item is not %s, it is %s", 
+               return FAILED("Last item is not %s, it is %s", 
                        items[array->len - 2], array->pdata[array->len - 1]);
        }
 
-       return NULL;
+       return OK;
 }
 
-char *ptrarray_remove()
+RESULT ptrarray_remove()
 {
        GPtrArray *array;
        gint i;
@@ -178,18 +178,18 @@ char *ptrarray_remove()
        g_ptr_array_remove(array, (gpointer)items[7]);
 
        if(!g_ptr_array_remove(array, (gpointer)items[4])) {
-               return result("Item %s not removed", items[4]);
+               return FAILED("Item %s not removed", items[4]);
        }
 
        if(g_ptr_array_remove(array, (gpointer)items[4])) {
-               return result("Item %s still in array after removal", items[4]);
+               return FAILED("Item %s still in array after removal", items[4]);
        }
 
        if(array->pdata[array->len - 1] != items[array->len + 1]) {
-               return result("Last item in GPtrArray not correct");
+               return FAILED("Last item in GPtrArray not correct");
        }
 
-       return NULL;
+       return OK;
 }
 
 static gint ptrarray_sort_compare(gconstpointer a, gconstpointer b)
@@ -197,7 +197,7 @@ static gint ptrarray_sort_compare(gconstpointer a, gconstpointer b)
        return strcmp(a, b);
 }
 
-char *ptrarray_sort()
+RESULT ptrarray_sort()
 {
        GPtrArray *array = g_ptr_array_new();
        gint i;
@@ -213,14 +213,14 @@ char *ptrarray_sort()
 
        for(i = 0; i < array->len; i++) {
                if(strcmp((gchar *)array->pdata[i], letters[i]) != 0) {
-                       return result("Array out of order, expected %s got %s", 
+                       return FAILED("Array out of order, expected %s got %s", 
                                (gchar *)array->pdata[i], letters[i]);
                }
        }
 
        g_ptr_array_free(array, TRUE);
        
-       return NULL;
+       return OK;
 }
 
 static Test ptrarray_tests [] = {
index e590120d7678829e221e0238815695877f265e1a..e5b06dfe23260a4307cec5d254286def89b2544c 100644 (file)
@@ -3,7 +3,7 @@
 #include <glib.h>
 #include "test.h"
 
-char*
+RESULT
 test_slist_append ()
 {
        GSList *list = g_slist_prepend (NULL, "first");
@@ -15,10 +15,10 @@ test_slist_append ()
        if (g_slist_length (list) != 2)
                return "Append failed";
 
-       return NULL;
+       return OK;
 }
 
-char *
+RESULT
 test_slist_concat ()
 {
        GSList *foo = g_slist_prepend (NULL, "foo");
@@ -29,10 +29,10 @@ test_slist_concat ()
        if (g_slist_length (list) != 2)
                return "Concat failed.";
 
-       return NULL;
+       return OK;
 }
 
-char*
+RESULT
 test_slist_find ()
 {
        GSList *list = g_slist_prepend (NULL, "three");
@@ -50,10 +50,10 @@ test_slist_find ()
        if (found->data != data)
                return "Find failed";
 
-       return NULL;
+       return OK;
 }
 
-char*
+RESULT
 test_slist_remove ()
 {
        GSList *list = g_slist_prepend (NULL, "three");
@@ -69,10 +69,10 @@ test_slist_remove ()
        if (strcmp ("two", list->data) != 0)
                return "Remove failed";
 
-       return NULL;
+       return OK;
 }
 
-char*
+RESULT
 test_slist_remove_link ()
 {
        GSList *foo = g_slist_prepend (NULL, "a");
@@ -86,12 +86,12 @@ test_slist_remove_link ()
        list = g_slist_remove_link (list, bar);
 
        if (g_slist_length (list) != 2)
-               return g_strdup ("remove_link failed #1");
+               return FAILED ("remove_link failed #1");
 
        if (bar->next != NULL)
-               return g_strdup ("remove_link failed #2");
+               return FAILED ("remove_link failed #2");
 
-       return NULL;
+       return OK;
 }
 
 static gint
@@ -106,7 +106,7 @@ compare (gconstpointer a, gconstpointer b)
        return 1;
 }
 
-char*
+RESULT
 test_slist_insert_sorted ()
 {
        GSList *list = g_slist_prepend (NULL, "a");
@@ -115,19 +115,19 @@ test_slist_insert_sorted ()
        /* insert at the middle */
        list = g_slist_insert_sorted (list, "aa", compare);
        if (strcmp ("aa", list->next->data))
-               return g_strdup("insert_sorted failed #1");
+               return FAILED("insert_sorted failed #1");
 
        /* insert at the beginning */
        list = g_slist_insert_sorted (list, "", compare);
        if (strcmp ("", list->data))
-               return g_strdup ("insert_sorted failed #2");
+               return FAILED ("insert_sorted failed #2");
 
        /* insert at the end */
        list = g_slist_insert_sorted (list, "aaaa", compare);
        if (strcmp ("aaaa", g_slist_last (list)->data))
-               return g_strdup ("insert_sorted failed #3");
+               return FAILED ("insert_sorted failed #3");
 
-       return NULL;
+       return OK;
 }
 
 static Test slist_tests [] = {
index 3d69b0a9ff320159fc44a59e6d5e97979ebc9539..6b00ef4da9a36b3f48569a4bdbdb18f4bb73f828 100644 (file)
@@ -4,7 +4,7 @@
 #include "test.h"
 
 /* This test is just to be used with valgrind */
-char *
+RESULT
 test_strfreev ()
 {
        gchar **array = g_new (gchar *, 4);
@@ -16,20 +16,20 @@ test_strfreev ()
        g_strfreev (array);
        g_strfreev (NULL);
 
-       return NULL;
+       return OK;
 }
 
-char *
+RESULT
 test_concat ()
 {
        gchar *x = g_strconcat ("Hello", ", ", "world", NULL);
        if (strcmp (x, "Hello, world") != 0)
-               return result("concat failed, got: %s", x);
+               return FAILED("concat failed, got: %s", x);
        g_free (x);
-       return NULL;
+       return OK;
 }
 
-char *
+RESULT
 test_split ()
 {
        gchar **v = g_strsplit("Hello world, how are we doing today?", " ", 0);
@@ -40,15 +40,15 @@ test_split ()
        } else {
                for(i = 0; v[i] != NULL; i++);
                if(i != 7) {
-                       return result("split failed, expected 7 tokens, got %d\n", i);
+                       return FAILED("split failed, expected 7 tokens, got %d\n", i);
                }
        }
        
        g_strfreev(v);
-       return NULL;
+       return OK;
 }
 
-char *
+RESULT
 test_strreverse ()
 {
        gchar *a = g_strdup ("onetwothree");
@@ -60,21 +60,21 @@ test_strreverse ()
        if (strcmp (a, a_target)) {
                g_free (b);
                g_free (a);
-               return result("strreverse failed. Expecting: '%s' and got '%s'\n", a, a_target);
+               return FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", a, a_target);
        }
 
        g_strreverse (b);
        if (strcmp (b, b_target)) {
                g_free (b);
                g_free (a);
-               return result("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
+               return FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
        }
        g_free (b);
        g_free (a);
-       return NULL;
+       return OK;
 }
 
-char *
+RESULT
 test_strjoin ()
 {
        char *s;
@@ -103,10 +103,10 @@ test_strjoin ()
        if (s == NULL || (strcmp (s, "") != 0))
                return "Failed to join empty arguments";
 
-       return NULL;
+       return OK;
 }
 
-char *
+RESULT
 test_strchug ()
 {
        char *str = g_strdup (" \t\n hola");
@@ -118,10 +118,10 @@ test_strchug ()
                return "Failed.";
        }
        g_free (str);
-       return NULL;
+       return OK;
 }
 
-char *
+RESULT
 test_strchomp ()
 {
        char *str = g_strdup ("hola  \t");
@@ -133,10 +133,10 @@ test_strchomp ()
                return "Failed.";
        }
        g_free (str);
-       return NULL;
+       return OK;
 }
 
-char *
+RESULT
 test_strstrip ()
 {
        char *str = g_strdup (" \t hola   ");
@@ -148,7 +148,7 @@ test_strstrip ()
                return "Failed.";
        }
        g_free (str);
-       return NULL;
+       return OK;
 }
 
 static Test strutil_tests [] = {
index 6ab873c7a687df6526d8e498856d0cfe7549fefd..802400d236c8ccc0f65f2e2f8ca6fed2791035c9 100644 (file)
@@ -3,9 +3,9 @@
 #include <stdio.h>
 #include "test.h"
 
-#define sfail(k,p) if (s->str [p] != k) { g_string_free (s,TRUE); return result ("Got %s, Failed at %d, expected '%c'", s->str, p, k);}
+#define sfail(k,p) if (s->str [p] != k) { g_string_free (s,TRUE); return FAILED("Got %s, Failed at %d, expected '%c'", s->str, p, k);}
 
-char *
+RESULT
 test_gstring ()
 {
        GString *s = g_string_new_len ("My stuff", 2);
@@ -27,7 +27,7 @@ test_gstring ()
        if (s->str [2] != 0)
                return "Null as not copied";
        if (strncmp (s->str+4, "Relala", 6) != 0){
-               return result("Did not copy correctly, got: %s", s->str+4);
+               return FAILED("Did not copy correctly, got: %s", s->str+4);
        }
 
        g_string_free (s, TRUE);
@@ -36,7 +36,7 @@ test_gstring ()
                g_string_append (s, "x");
        }
        if (strlen (s->str) != 1024){
-               return result("Incorrect string size, got: %s %d", s->str, strlen (s->str));
+               return FAILED("Incorrect string size, got: %s %d", s->str, strlen (s->str));
        }
        g_string_free (s, TRUE);
 
@@ -45,14 +45,14 @@ test_gstring ()
                g_string_append_c (s, 'x');
        }
        if (strlen (s->str) != 1024){
-               return result("Incorrect string size, got: %s %d\n", s->str, strlen (s->str));
+               return FAILED("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){
-               return result("Incorrect data, got: %s\n", s->str);
+               return FAILED("Incorrect data, got: %s\n", s->str);
        }
        g_string_free (s, TRUE);
 
@@ -73,7 +73,7 @@ test_gstring ()
        sfail ('2', 5);
        g_string_free (s, TRUE);
        
-       return NULL;
+       return OK;
 }
 
 static Test string_tests [] = {
index e9c43a4b2615665327617607965bbda58fcbb075..9e6e87707df3a6d901041d98047bc326d7e35f75 100644 (file)
 static gchar *last_result = NULL;
 
 gboolean 
-run_test(Test *test)
+run_test(Test *test, gboolean quiet)
 {
        gchar *result; 
-       printf("  %s: ", test->name);
-       fflush(stdout);
+       
+       if(!quiet) {
+               printf("  %s: ", test->name);
+               fflush(stdout);
+       }
+       
        if((result = test->handler()) == NULL) {
-               printf("OK\n");
+               if(!quiet) {
+                       printf("OK\n");
+               }
+               
                return TRUE;
        } else {
-               printf("FAILED (%s)\n", result);
+               if(!quiet) {
+                       printf("FAILED (%s)\n", result);
+               }
+               
                if(last_result == result) {
                        last_result = NULL;
                        g_free(result);
@@ -57,15 +67,17 @@ run_test(Test *test)
 }
 
 void
-run_group(Group *group, gint *total, gint *passed)
+run_group(Group *group, gint *total, gint *passed, gboolean quiet)
 {
        Test *tests = group->handler();
        gint i, _passed = 0;
 
-       printf("[%s]\n", group->name);
+       if(!quiet) {
+               printf("[%s]\n", group->name);
+       }
 
        for(i = 0; tests[i].name != NULL; i++) {
-               _passed += run_test(&(tests[i]));
+               _passed += run_test(&(tests[i]), quiet);
        }
 
        if(total != NULL) {
@@ -77,8 +89,8 @@ run_group(Group *group, gint *total, gint *passed)
        }
 }
 
-gchar *
-result(const gchar *format, ...)
+RESULT
+FAILED(const gchar *format, ...)
 {
        gchar *ret;
        va_list args;
index 1befe7255ef915d1df8890c6981c2c7964187294..cfe81ca82595d0b6818c7588b094058fa7f3034e 100644 (file)
@@ -32,6 +32,8 @@
 #include <stdarg.h>
 #include <glib.h>
 
+typedef gchar * RESULT;
+
 typedef struct _Test Test;
 typedef struct _Group Group;
 
@@ -48,9 +50,11 @@ struct _Group {
        LoadGroupHandler handler;
 };
 
-gboolean run_test(Test *test);
-void run_group(Group *group, gint *total, gint *passed);
-gchar *result(const gchar *format, ...);
+gboolean run_test(Test *test, gboolean quiet);
+void run_group(Group *group, gint *total, gint *passed, gboolean quiet);
+RESULT FAILED(const gchar *format, ...);
+
+#define OK NULL
 
 #define DEFINE_TEST_GROUP_INIT(name, table) \
        Test * (name)() { return table; }