[glib] Move list tests to the test framework
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] Move list tests to the test framework
- Date: Tue, 6 Jul 2010 03:16:17 +0000 (UTC)
commit b61fd45ac3305ec20b156bbeeed5494b9a445c8a
Author: Matthias Clasen <mclasen redhat com>
Date: Mon Jul 5 20:51:29 2010 -0400
Move list tests to the test framework
glib/tests/Makefile.am | 3 +
glib/tests/list.c | 179 ++++++++++++++++++++++++++++++++++++++++
tests/list-test.c | 212 ------------------------------------------------
3 files changed, 182 insertions(+), 212 deletions(-)
---
diff --git a/glib/tests/Makefile.am b/glib/tests/Makefile.am
index b668166..626aa9b 100644
--- a/glib/tests/Makefile.am
+++ b/glib/tests/Makefile.am
@@ -99,6 +99,9 @@ node_LDADD = $(progs_ldadd)
TEST_PROGS += convert
convert_LDADD = $(progs_ldadd)
+TEST_PROGS += list
+list_LDADD = $(progs_ldadd)
+
if OS_UNIX
# some testing of gtester funcitonality
diff --git a/glib/tests/list.c b/glib/tests/list.c
new file mode 100644
index 0000000..15b46ee
--- /dev/null
+++ b/glib/tests/list.c
@@ -0,0 +1,179 @@
+#include <glib.h>
+
+#define SIZE 50
+#define NUMBER_MIN 0000
+#define NUMBER_MAX 9999
+
+
+static guint32 array[SIZE];
+
+
+static gint
+sort (gconstpointer p1, gconstpointer p2)
+{
+ gint32 a, b;
+
+ a = GPOINTER_TO_INT (p1);
+ b = GPOINTER_TO_INT (p2);
+
+ return (a > b ? +1 : a == b ? 0 : -1);
+}
+
+/*
+ * glist sort tests
+ */
+static void
+test_list_sort (void)
+{
+ GList *list = NULL;
+ gint i;
+
+ for (i = 0; i < SIZE; i++)
+ list = g_list_append (list, GINT_TO_POINTER (array[i]));
+
+ list = g_list_sort (list, sort);
+ for (i = 0; i < SIZE - 1; i++)
+ {
+ gpointer p1, p2;
+
+ p1 = g_list_nth_data (list, i);
+ p2 = g_list_nth_data (list, i+1);
+
+ g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
+ }
+
+ g_list_free (list);
+}
+
+static void
+test_list_sort_with_data (void)
+{
+ GList *list = NULL;
+ gint i;
+
+ for (i = 0; i < SIZE; i++)
+ list = g_list_append (list, GINT_TO_POINTER (array[i]));
+
+ list = g_list_sort_with_data (list, (GCompareDataFunc)sort, NULL);
+ for (i = 0; i < SIZE - 1; i++)
+ {
+ gpointer p1, p2;
+
+ p1 = g_list_nth_data (list, i);
+ p2 = g_list_nth_data (list, i+1);
+
+ g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
+ }
+
+ g_list_free (list);
+}
+
+static void
+test_list_insert_sorted (void)
+{
+ GList *list = NULL;
+ gint i;
+
+ for (i = 0; i < SIZE; i++)
+ list = g_list_insert_sorted (list, GINT_TO_POINTER (array[i]), sort);
+
+ for (i = 0; i < SIZE - 1; i++)
+ {
+ gpointer p1, p2;
+
+ p1 = g_list_nth_data (list, i);
+ p2 = g_list_nth_data (list, i+1);
+
+ g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
+ }
+
+ g_list_free (list);
+}
+
+static void
+test_list_insert_sorted_with_data (void)
+{
+ GList *list = NULL;
+ gint i;
+
+ for (i = 0; i < SIZE; i++)
+ list = g_list_insert_sorted_with_data (list,
+ GINT_TO_POINTER (array[i]),
+ (GCompareDataFunc)sort,
+ NULL);
+
+ for (i = 0; i < SIZE - 1; i++)
+ {
+ gpointer p1, p2;
+
+ p1 = g_list_nth_data (list, i);
+ p2 = g_list_nth_data (list, i+1);
+
+ g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
+ }
+
+ g_list_free (list);
+}
+
+static void
+test_list_reverse (void)
+{
+ GList *list = NULL;
+ GList *st;
+ gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+ gint i;
+
+ for (i = 0; i < 10; i++)
+ list = g_list_append (list, &nums[i]);
+
+ list = g_list_reverse (list);
+
+ for (i = 0; i < 10; i++)
+ {
+ st = g_list_nth (list, i);
+ g_assert (*((gint*) st->data) == (9 - i));
+ }
+
+ g_list_free (list);
+}
+
+static void
+test_list_nth (void)
+{
+ GList *list = NULL;
+ GList *st;
+ gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+ gint i;
+
+ for (i = 0; i < 10; i++)
+ list = g_list_append (list, &nums[i]);
+
+ for (i = 0; i < 10; i++)
+ {
+ st = g_list_nth (list, i);
+ g_assert (*((gint*) st->data) == i);
+ }
+
+ g_list_free (list);
+}
+
+int
+main (int argc, char *argv[])
+{
+ gint i;
+
+ g_test_init (&argc, &argv, NULL);
+
+ /* Create an array of random numbers. */
+ for (i = 0; i < SIZE; i++)
+ array[i] = g_test_rand_int_range (NUMBER_MIN, NUMBER_MAX);
+
+ g_test_add_func ("/list/sort", test_list_sort);
+ g_test_add_func ("/list/sort-with-data", test_list_sort_with_data);
+ g_test_add_func ("/list/insert-sorted", test_list_insert_sorted);
+ g_test_add_func ("/list/insert-sorted-with-data", test_list_insert_sorted_with_data);
+ g_test_add_func ("/list/reverse", test_list_reverse);
+ g_test_add_func ("/list/nth", test_list_nth);
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]