+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
+/*
+ * 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>
/* 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;
}
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);
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);
}
}
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);
}
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]);
}
}
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]);
}
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;
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]);
}
}
{
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;
}
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);
}
}
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);
#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 ()
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);
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);
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);
+/*
+ * 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);
+ }
}
}
run_group(Group *group)
{
Test *tests = group->handler();
- int i;
+ gint i;
printf("[%s]\n", group->name);
}
}
+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;
+}
+
+/*
+ * 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; }
#define DEFINE_TEST_GROUP_INIT_H(name) \
Test * (name)();
-#define RESULT(x) g_strdup_printf(x);
-
#endif /* _TEST_H */