2006-08-18 Aaron Bockover <abockover@novell.com>
authorAaron Bockover <abockover@novell.com>
Fri, 18 Aug 2006 11:24:35 +0000 (11:24 -0000)
committerAaron Bockover <abockover@novell.com>
Fri, 18 Aug 2006 11:24:35 +0000 (11:24 -0000)
    * test/test.c:
    * test/test.h: Added result() to be used in place of g_strdup_printf(),
    it's shorter to write and allows the duped string to be freed safely;
    added license header to file

    * test/driver.c: Added license header to file

    * test/string-util.c:
    * test/ptrarray.c:
    * test/string.c:
    * test/list.c: Use result() in place of g_strdup_printf(), it's nicer

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

eglib/ChangeLog
eglib/test/driver.c
eglib/test/list.c
eglib/test/ptrarray.c
eglib/test/string-util.c
eglib/test/string.c
eglib/test/test.c
eglib/test/test.h

index 3c9665ca9ebbb451dabd1f79ff723e741afcb482..d1940ac2b3c3881c65c39baf528b12218e21c3f3 100644 (file)
@@ -1,3 +1,17 @@
+2006-08-18  Aaron Bockover  <abockover@novell.com>
+
+       * test/test.c: 
+       * test/test.h: Added result() to be used in place of g_strdup_printf(),
+       it's shorter to write and allows the duped string to be freed safely;
+       added license header to file
+
+       * test/driver.c: Added license header to file
+
+       * test/string-util.c:
+       * test/ptrarray.c:
+       * test/string.c:
+       * test/list.c: Use result() in place of g_strdup_printf(), it's nicer
+
 2006-08-18  Aaron Bockover  <abockover@novell.com>
 
        * src/gptrarray.c: Implemented g_ptr_array_sort
index 784ca864c11ba612d8ea63d2e685699cb09d4a22..b90ba415dab0fd90ebc14f93993994b3ee051afc 100644 (file)
@@ -1,3 +1,31 @@
+/*
+ * EGLib Unit Test Driver
+ *
+ * Author:
+ *   Aaron Bockover (abockover@novell.com)
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
 #include <stdio.h>
 #include <glib.h>
 
index 73d410e5ed45bb6dcd0d396a91bb31ffc75bba1b..5f5bd8a74c482d7595832eebf290cea0f158a360 100644 (file)
@@ -159,18 +159,18 @@ test_list_insert_sorted ()
        /* insert at the middle */
        list = g_list_insert_sorted (list, "aa", compare);
        if (strcmp ("aa", list->next->data))
-               return g_strdup("insert_sorted failed. #1");
+               return result ("insert_sorted failed. #1");
 
        /* insert at the beginning */
        list = g_list_insert_sorted (list, "", compare);
        
        if (strcmp ("", list->data))
-               return g_strdup ("insert_sorted failed. #2");           
+               return result ("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 g_strdup ("insert_sorted failed. #3");
+               return result ("insert_sorted failed. #3");
 
        return NULL;
 }
index b7dd042a5e46f2d6100d9678c47758e5b0bdfcf0..33d3c3a6ab8250c201ae8af6a0dc8dc342f3aee2 100644 (file)
@@ -52,12 +52,12 @@ char *ptrarray_alloc()
        array = (GPtrArrayPriv *)ptrarray_alloc_and_fill(&i);
        
        if(array->size != guess_size(array->len)) {
-               return g_strdup_printf("Size should be %d, but it is %d", 
+               return result("Size should be %d, but it is %d", 
                        guess_size(array->len), array->size);
        }
        
        if(array->len != i) {
-               return g_strdup_printf("Expected %d node(s) in the array", i);
+               return result("Expected %d node(s) in the array", i);
        }
        
        g_ptr_array_free((GPtrArray *)array, TRUE);
@@ -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 g_strdup_printf(
+                       return result(
                                "Expected item at %d to be %s, but it was %s", 
                                i, items[i], item);
                }
@@ -97,7 +97,7 @@ void foreach_callback(gpointer data, gpointer user_data)
        }
 
        if(item != item_cmp) {
-               foreach_iterate_error = g_strdup_printf(
+               foreach_iterate_error = result(
                        "Expected item at %d to be %s, but it was %s", 
                                foreach_iterate_index - 1, item_cmp, item);
        }
@@ -127,20 +127,16 @@ char *ptrarray_set_size()
        g_ptr_array_set_size(array, grow_length);
 
        if(array->len != grow_length) {
-               return g_strdup_printf("Array length should be 50, it is %d", 
-                       array->len);
+               return result("Array length should be 50, it is %d", array->len);
        } else if(array->pdata[0] != items[0]) {
-               return g_strdup_printf("Item 0 was overwritten, should be %s", 
-                       items[0]);
+               return result("Item 0 was overwritten, should be %s", items[0]);
        } else if(array->pdata[1] != items[1]) {
-               return g_strdup_printf("Item 1 was overwritten, should be %s",
-                       items[1]);
+               return result("Item 1 was overwritten, should be %s", items[1]);
        }
 
        for(i = 2; i < array->len; i++) {
                if(array->pdata[i] != NULL) {
-                       return g_strdup_printf("Item %d is not NULL, it is %p", 
-                               i, array->pdata[i]);
+                       return result("Item %d is not NULL, it is %p", i, array->pdata[i]);
                }
        }
 
@@ -158,14 +154,14 @@ char *ptrarray_remove_index()
        
        g_ptr_array_remove_index(array, 0);
        if(array->pdata[0] != items[1]) {
-               return g_strdup_printf("First item is not %s, it is %s", items[1],
+               return result("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 g_strdup_printf("Last item is not %s, it is %s", 
+               return result("Last item is not %s, it is %s", 
                        items[array->len - 2], array->pdata[array->len - 1]);
        }
 
@@ -182,16 +178,15 @@ char *ptrarray_remove()
        g_ptr_array_remove(array, (gpointer)items[7]);
 
        if(!g_ptr_array_remove(array, (gpointer)items[4])) {
-               return g_strdup_printf("Item %s not removed", items[4]);
+               return result("Item %s not removed", items[4]);
        }
 
        if(g_ptr_array_remove(array, (gpointer)items[4])) {
-               return g_strdup_printf("Item %s still in array after removal", 
-                       items[4]);
+               return result("Item %s still in array after removal", items[4]);
        }
 
        if(array->pdata[array->len - 1] != items[array->len + 1]) {
-               return g_strdup_printf("Last item in GPtrArray not correct");
+               return result("Last item in GPtrArray not correct");
        }
 
        return NULL;
@@ -218,7 +213,7 @@ char *ptrarray_sort()
 
        for(i = 0; i < array->len; i++) {
                if(strcmp((gchar *)array->pdata[i], letters[i]) != 0) {
-                       return g_strdup_printf("Array out of order, expected %s got %s", 
+                       return result("Array out of order, expected %s got %s", 
                                (gchar *)array->pdata[i], letters[i]);
                }
        }
index 3f96fe27280bb56e30a0880374bf01912f9763bd..3d69b0a9ff320159fc44a59e6d5e97979ebc9539 100644 (file)
@@ -24,7 +24,7 @@ test_concat ()
 {
        gchar *x = g_strconcat ("Hello", ", ", "world", NULL);
        if (strcmp (x, "Hello, world") != 0)
-               return g_strdup_printf ("concat failed, got: %s", x);
+               return result("concat failed, got: %s", x);
        g_free (x);
        return NULL;
 }
@@ -36,11 +36,11 @@ test_split ()
        int i = 0;
        
        if(v == NULL) {
-               return RESULT("split failed, got NULL vector");
+               return "split failed, got NULL vector";
        } else {
                for(i = 0; v[i] != NULL; i++);
                if(i != 7) {
-                       return g_strdup_printf("split failed, expected 7 tokens, got %d\n", i);
+                       return result("split failed, expected 7 tokens, got %d\n", i);
                }
        }
        
@@ -60,14 +60,14 @@ test_strreverse ()
        if (strcmp (a, a_target)) {
                g_free (b);
                g_free (a);
-               return g_strdup_printf ("strreverse failed. Expecting: '%s' and got '%s'\n", a, a_target);
+               return result("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 g_strdup_printf ("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
+               return result("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
        }
        g_free (b);
        g_free (a);
index d861ddde84114b62747694e9279813fd3091e5ca..6ab873c7a687df6526d8e498856d0cfe7549fefd 100644 (file)
@@ -3,7 +3,7 @@
 #include <stdio.h>
 #include "test.h"
 
-#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->str, p, k);}
+#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);}
 
 char *
 test_gstring ()
@@ -13,21 +13,21 @@ test_gstring ()
        int i;
 
        if (strcmp (s->str, "My") != 0)
-               return RESULT("Expected only 'My' on the string");
+               return "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 RESULT("Null was not copied");
+               return "Null was not copied";
        if (strcmp (s->str+4, "Re") != 0){
-               return RESULT("Did not find the 'Re' part");
+               return "Did not find the 'Re' part";
        }
 
        g_string_append (s, "lalalalalalalalalalalalalalalalalalalalalalal");
        if (s->str [2] != 0)
-               return RESULT("Null as not copied");
+               return "Null as not copied";
        if (strncmp (s->str+4, "Relala", 6) != 0){
-               return g_strdup_printf("Did not copy correctly, got: %s", s->str+4);
+               return result("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 g_strdup_printf("Incorrect string size, got: %s %d", s->str, strlen (s->str));
+               return result("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 g_strdup_printf("Incorrect string size, got: %s %d\n", s->str, strlen (s->str));
+               return result("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 g_strdup_printf("Incorrect data, got: %s\n", s->str);
+               return result("Incorrect data, got: %s\n", s->str);
        }
        g_string_free (s, TRUE);
 
index 393a4acb8ecc63ca70d689ae6bd75a8a0eefdc64..cb387fd198aa95dbab85e88fb5f30ca1ee5a935c 100644 (file)
@@ -1,20 +1,55 @@
+/*
+ * EGLib Unit Group/Test Runners
+ *
+ * Author:
+ *   Aaron Bockover (abockover@novell.com)
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#define _GNU_SOURCE
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <glib.h>
 
 #include "test.h"
 
+static gchar *last_result = NULL;
+
 void 
 run_test(Test *test)
 {
-       char *result; 
+       gchar *result; 
        printf("  %s: ", test->name);
        fflush(stdout);
        if((result = test->handler()) == NULL) {
                printf("OK\n");
        } else {
                printf("FAILED (%s)\n", result);
-               /* It is ok to leak if the test fails, so we dont force people to use g_strdup */
+               if(last_result == result) {
+                       last_result = NULL;
+                       g_free(result);
+               }
        }
 }
 
@@ -22,7 +57,7 @@ void
 run_group(Group *group)
 {
        Test *tests = group->handler();
-       int i;
+       gint i;
        
        printf("[%s]\n", group->name);
 
@@ -31,3 +66,23 @@ run_group(Group *group)
        }
 }
 
+gchar *
+result(const gchar *format, ...)
+{
+       gchar *ret;
+       va_list args;
+       gint n;
+
+       va_start(args, format);
+       n = vasprintf(&ret, format, args);
+       va_end(args);
+
+       if(n == -1) {
+               last_result = NULL;
+               return NULL;
+       }
+
+       last_result = ret;
+       return ret;
+}
+
index 3fa7f81e46a15adf2d60354c57abdc840099add9..3c6556d11d9084ce49aa2e1b2f78a6110677cb57 100644 (file)
@@ -1,26 +1,56 @@
+/*
+ * EGLib Unit Group/Test Runners
+ *
+ * Author:
+ *   Aaron Bockover (abockover@novell.com)
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
 #ifndef _TEST_H
 #define _TEST_H
 
 #include <stdarg.h>
+#include <glib.h>
 
 typedef struct _Test Test;
 typedef struct _Group Group;
 
-typedef char * (* RunTestHandler)();
+typedef gchar * (* RunTestHandler)();
 typedef Test * (* LoadGroupHandler)();
 
 struct _Test {
-       const char *name;
+       const gchar *name;
        RunTestHandler handler;
 };
 
 struct _Group {
-       const char *name;
+       const gchar *name;
        LoadGroupHandler handler;
 };
 
 void run_test(Test *test);
 void run_group(Group *group);
+gchar *result(const gchar *format, ...);
 
 #define DEFINE_TEST_GROUP_INIT(name, table) \
        Test * (name)() { return table; }
@@ -28,7 +58,5 @@ void run_group(Group *group);
 #define DEFINE_TEST_GROUP_INIT_H(name) \
        Test * (name)();
 
-#define RESULT(x) g_strdup_printf(x);
-
 #endif /* _TEST_H */