1 #undef G_DISABLE_ASSERT
6 #define DEBUG_MSG(args)
7 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
8 #define PRINT_MSG(args)
9 /* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
12 #define NUMBER_MIN 0000
13 #define NUMBER_MAX 9999
16 static guint32 array[SIZE];
20 sort (gconstpointer p1, gconstpointer p2)
24 a = GPOINTER_TO_INT (p1);
25 b = GPOINTER_TO_INT (p2);
27 return (a > b ? +1 : a == b ? 0 : -1);
39 PRINT_MSG (("testing g_list_sort()"));
41 for (i = 0; i < SIZE; i++) {
42 list = g_list_append (list, GINT_TO_POINTER (array[i]));
45 list = g_list_sort (list, sort);
46 for (i = 0; i < SIZE - 1; i++) {
49 p1 = g_list_nth_data (list, i);
50 p2 = g_list_nth_data (list, i+1);
52 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
53 DEBUG_MSG (("list_sort #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
60 test_list_sort_with_data (void)
65 PRINT_MSG (("testing g_list_sort_with_data()"));
67 for (i = 0; i < SIZE; i++) {
68 list = g_list_append (list, GINT_TO_POINTER (array[i]));
71 list = g_list_sort_with_data (list, (GCompareDataFunc)sort, NULL);
72 for (i = 0; i < SIZE - 1; i++) {
75 p1 = g_list_nth_data (list, i);
76 p2 = g_list_nth_data (list, i+1);
78 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
79 DEBUG_MSG (("list_sort_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
86 test_list_insert_sorted (void)
91 PRINT_MSG (("testing g_list_insert_sorted()"));
93 for (i = 0; i < SIZE; i++) {
94 list = g_list_insert_sorted (list, GINT_TO_POINTER (array[i]), sort);
97 for (i = 0; i < SIZE - 1; i++) {
100 p1 = g_list_nth_data (list, i);
101 p2 = g_list_nth_data (list, i+1);
103 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
104 DEBUG_MSG (("list_insert_sorted #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
111 test_list_insert_sorted_with_data (void)
116 PRINT_MSG (("testing g_list_insert_sorted_with_data()"));
118 for (i = 0; i < SIZE; i++) {
119 list = g_list_insert_sorted_with_data (list,
120 GINT_TO_POINTER (array[i]),
121 (GCompareDataFunc)sort,
125 for (i = 0; i < SIZE - 1; i++) {
128 p1 = g_list_nth_data (list, i);
129 p2 = g_list_nth_data (list, i+1);
131 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
132 DEBUG_MSG (("list_insert_sorted_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
139 test_list_reverse (void)
143 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
146 PRINT_MSG (("testing g_list_reverse()"));
148 for (i = 0; i < 10; i++) {
149 list = g_list_append (list, &nums[i]);
152 list = g_list_reverse (list);
154 for (i = 0; i < 10; i++) {
155 st = g_list_nth (list, i);
156 g_assert (*((gint*) st->data) == (9 - i));
167 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
170 PRINT_MSG (("testing g_list_nth()"));
172 for (i = 0; i < 10; i++) {
173 list = g_list_append (list, &nums[i]);
176 for (i = 0; i < 10; i++) {
177 st = g_list_nth (list, i);
178 g_assert (*((gint*) st->data) == i);
185 main (int argc, char *argv[])
189 DEBUG_MSG (("debugging messages turned on"));
191 DEBUG_MSG (("creating %d random numbers", SIZE));
193 /* Create an array of random numbers. */
194 for (i = 0; i < SIZE; i++) {
195 array[i] = g_random_int_range (NUMBER_MIN, NUMBER_MAX);
196 DEBUG_MSG (("number #%3.3d ---> %d", i, array[i]));
201 test_list_sort_with_data ();
203 test_list_insert_sorted ();
204 test_list_insert_sorted_with_data ();
206 test_list_reverse ();
209 PRINT_MSG (("testing finished"));