+2006-08-18 Aaron Bockover <abockover@novell.com>
+
+ * src/gptrarray.c: Implemented g_ptr_array_sort
+
+ * test/ptrarray.c: Added sort test
+
+ * test/driver.c: Added --help; support running N iterations and allow
+ selecting which test groups to run; uses the test group table in tests.h
+
+ * test/tests.h: Added group table
+
+ * test/test.h:
+ * test/test.c: Using a Group structure and table, removed run_groups
+ as we only need run_group now
+
+ * test/slist.h: Removed, not needed, tests/groups defined in tests.h
+
2006-08-18 Miguel de Icaza <miguel@novell.com>
* src/unicode.c: New file, to host unicode code, it will throw as
* test/list.c: Tests for GList.
+>>>>>>> .r63982
2006-08-17 Aaron Bockover <abockover@novell.com>
* src/gptrarray.c: Implemented g_ptr_array_remove and
(array->len - index - 1) * sizeof(gpointer));
}
- array->len -= 1;
-// array->pdata[array->len - 1] = NULL;
+ array->len--;
+ array->pdata[array->len] = NULL;
return removed_node;
}
}
}
+void
+g_ptr_array_sort(GPtrArray *array, GCompareFunc compare_func)
+{
+ g_return_if_fail(array != NULL);
+ qsort(array->pdata, array->len, sizeof(gpointer), compare_func);
+}
+
+void
+g_ptr_array_sort_with_data(GPtrArray *array, GCompareDataFunc compare_func,
+ gpointer user_data)
+{
+}
+
+
#include <stdio.h>
+#include <glib.h>
#include "test.h"
#include "tests.h"
-int main()
+static void print_help(char *s)
{
- run_groups(
- "string", strutil_tests_init,
- "hashtable", hashtable_tests_init,
- "slist", slist_tests_init,
- "list", list_tests_init,
- "ptrarray", ptrarray_tests_init,
- "gstring", string_tests_init,
- NULL
- );
+ gint i;
+
+ printf("Usage: %s [options] [iterations [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(" test1..testN name of test to run (all run by default)\n\n");
+ printf(" available tests:\n");
+
+ for(i = 0; test_groups[i].name != NULL; i++) {
+ printf(" %s\n", test_groups[i].name);
+ }
+
+ printf("\n");
+}
+
+gint main(gint argc, gchar **argv)
+{
+ gint i, j, k, iterations = 1, tests_to_run_count = 0;
+ gchar **tests_to_run = NULL;
+
+ if(argc > 1) {
+ for(i = 1; i < argc; i++) {
+ if(strcmp(argv[i], "--help") == 0) {
+ print_help(argv[0]);
+ return 1;
+ }
+ }
+
+ 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 = 0; i < iterations; i++) {
+ for(j = 0; test_groups[j].name != NULL; j++) {
+ gboolean run = TRUE;
+
+ if(tests_to_run != NULL) {
+ run = FALSE;
+ for(k = 0; tests_to_run[k] != NULL; k++) {
+ if(strcmp(tests_to_run[k], test_groups[j].name) == 0) {
+ run = TRUE;
+ break;
+ }
+ }
+ }
+
+ if(run) {
+ run_group(&(test_groups[j]));
+ }
+ }
+ }
+
+ if(tests_to_run != NULL) {
+ g_free(tests_to_run);
+ }
return 0;
}
return NULL;
}
+static gint ptrarray_sort_compare(gconstpointer a, gconstpointer b)
+{
+ return strcmp(a, b);
+}
+
+char *ptrarray_sort()
+{
+ GPtrArray *array = g_ptr_array_new();
+ gint i;
+ static const gchar *letters [] = { "A", "B", "C", "D", "E" };
+
+ g_ptr_array_add(array, "E");
+ g_ptr_array_add(array, "C");
+ g_ptr_array_add(array, "A");
+ g_ptr_array_add(array, "D");
+ g_ptr_array_add(array, "B");
+
+ g_ptr_array_sort(array, ptrarray_sort_compare);
+
+ 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",
+ (gchar *)array->pdata[i], letters[i]);
+ }
+ }
+
+ g_ptr_array_free(array, TRUE);
+
+ return NULL;
+}
+
static Test ptrarray_tests [] = {
{"ptrarray_alloc", ptrarray_alloc},
{"ptrarray_for_iterate", ptrarray_for_iterate},
{"ptrarray_set_size", ptrarray_set_size},
{"ptrarray_remove_index", ptrarray_remove_index},
{"ptrarray_remove", ptrarray_remove},
+ {"ptrarray_sort", ptrarray_sort},
{NULL, NULL}
};
+++ /dev/null
-#include "test.h"
-
-DEFINE_TEST_GROUP_INIT_H(slist_tests_init);
-
}
void
-run_group(const char *name, LoadGroupHandler group_handler)
+run_group(Group *group)
{
- Test *tests = group_handler();
+ Test *tests = group->handler();
int i;
- printf("[%s]\n", name);
+ printf("[%s]\n", group->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);
-}
-
#include <stdarg.h>
typedef struct _Test Test;
+typedef struct _Group Group;
typedef char * (* RunTestHandler)();
typedef Test * (* LoadGroupHandler)();
RunTestHandler handler;
};
+struct _Group {
+ const char *name;
+ LoadGroupHandler 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, ...);
+void run_group(Group *group);
#define DEFINE_TEST_GROUP_INIT(name, table) \
Test * (name)() { return table; }
+#include "test.h"
+
DEFINE_TEST_GROUP_INIT_H(string_tests_init);
DEFINE_TEST_GROUP_INIT_H(strutil_tests_init);
DEFINE_TEST_GROUP_INIT_H(slist_tests_init);
DEFINE_TEST_GROUP_INIT_H(hashtable_tests_init);
DEFINE_TEST_GROUP_INIT_H(ptrarray_tests_init);
+static Group test_groups [] = {
+ {"string", string_tests_init},
+ {"strutil", strutil_tests_init},
+ {"ptrarray", ptrarray_tests_init},
+ {"slist", slist_tests_init},
+ {"list", list_tests_init},
+ {"hashtable", hashtable_tests_init},
+ {NULL, NULL}
+};
+