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);
34 test_slist_sort (void)
39 PRINT_MSG (("testing g_slist_sort()"));
41 for (i = 0; i < SIZE; i++) {
42 slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
45 slist = g_slist_sort (slist, sort);
46 for (i = 0; i < SIZE - 1; i++) {
49 p1 = g_slist_nth_data (slist, i);
50 p2 = g_slist_nth_data (slist, i+1);
52 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
53 DEBUG_MSG (("slist_sort #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
58 test_slist_sort_with_data (void)
63 PRINT_MSG (("testing g_slist_sort_with_data()"));
65 for (i = 0; i < SIZE; i++) {
66 slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
69 slist = g_slist_sort_with_data (slist, (GCompareDataFunc)sort, NULL);
70 for (i = 0; i < SIZE - 1; i++) {
73 p1 = g_slist_nth_data (slist, i);
74 p2 = g_slist_nth_data (slist, i+1);
76 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
77 DEBUG_MSG (("slist_sort_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
82 test_slist_insert_sorted (void)
87 PRINT_MSG (("testing g_slist_insert_sorted()"));
89 for (i = 0; i < SIZE; i++) {
90 slist = g_slist_insert_sorted (slist, GINT_TO_POINTER (array[i]), sort);
93 for (i = 0; i < SIZE - 1; i++) {
96 p1 = g_slist_nth_data (slist, i);
97 p2 = g_slist_nth_data (slist, i+1);
99 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
100 DEBUG_MSG (("slist_insert_sorted #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
105 test_slist_insert_sorted_with_data (void)
107 GSList *slist = NULL;
110 PRINT_MSG (("testing g_slist_insert_sorted_with_data()"));
112 for (i = 0; i < SIZE; i++) {
113 slist = g_slist_insert_sorted_with_data (slist,
114 GINT_TO_POINTER (array[i]),
115 (GCompareDataFunc)sort,
119 for (i = 0; i < SIZE - 1; i++) {
122 p1 = g_slist_nth_data (slist, i);
123 p2 = g_slist_nth_data (slist, i+1);
125 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
126 DEBUG_MSG (("slist_insert_sorted_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
131 test_slist_reverse (void)
133 GSList *slist = NULL;
135 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
138 PRINT_MSG (("testing g_slist_reverse()"));
140 for (i = 0; i < 10; i++) {
141 slist = g_slist_append (slist, &nums[i]);
144 slist = g_slist_reverse (slist);
146 for (i = 0; i < 10; i++) {
147 st = g_slist_nth (slist, i);
148 g_assert (*((gint*) st->data) == (9 - i));
151 g_slist_free (slist);
155 test_slist_nth (void)
157 GSList *slist = NULL;
159 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
162 PRINT_MSG (("testing g_slist_nth()"));
164 for (i = 0; i < 10; i++) {
165 slist = g_slist_append (slist, &nums[i]);
168 for (i = 0; i < 10; i++) {
169 st = g_slist_nth (slist, i);
170 g_assert (*((gint*) st->data) == i);
173 g_slist_free (slist);
177 main (int argc, char *argv[])
181 DEBUG_MSG (("debugging messages turned on"));
183 DEBUG_MSG (("creating %d random numbers", SIZE));
185 /* Create an array of random numbers. */
186 for (i = 0; i < SIZE; i++) {
187 array[i] = g_random_int_range (NUMBER_MIN, NUMBER_MAX);
188 DEBUG_MSG (("number #%3.3d ---> %d", i, array[i]));
193 test_slist_sort_with_data ();
195 test_slist_insert_sorted ();
196 test_slist_insert_sorted_with_data ();
198 test_slist_reverse ();
201 PRINT_MSG (("testing finished"));