1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3
4 #include <glib.h>
5
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"); */
10
11 #define SIZE 50
12 #define NUMBER_MIN 0000
13 #define NUMBER_MAX 9999
14
15
16 static guint32 array[SIZE];
17
18
19 static gint
sort(gconstpointer p1,gconstpointer p2)20 sort (gconstpointer p1, gconstpointer p2)
21 {
22 gint32 a, b;
23
24 a = GPOINTER_TO_INT (p1);
25 b = GPOINTER_TO_INT (p2);
26
27 return (a > b ? +1 : a == b ? 0 : -1);
28 }
29
30 /*
31 * gslist sort tests
32 */
33 static void
test_slist_sort(void)34 test_slist_sort (void)
35 {
36 GSList *slist = NULL;
37 gint i;
38
39 PRINT_MSG (("testing g_slist_sort()"));
40
41 for (i = 0; i < SIZE; i++) {
42 slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
43 }
44
45 slist = g_slist_sort (slist, sort);
46 for (i = 0; i < SIZE - 1; i++) {
47 gpointer p1, p2;
48
49 p1 = g_slist_nth_data (slist, i);
50 p2 = g_slist_nth_data (slist, i+1);
51
52 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
53 DEBUG_MSG (("slist_sort #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
54 }
55 }
56
57 static void
test_slist_sort_with_data(void)58 test_slist_sort_with_data (void)
59 {
60 GSList *slist = NULL;
61 gint i;
62
63 PRINT_MSG (("testing g_slist_sort_with_data()"));
64
65 for (i = 0; i < SIZE; i++) {
66 slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
67 }
68
69 slist = g_slist_sort_with_data (slist, (GCompareDataFunc)sort, NULL);
70 for (i = 0; i < SIZE - 1; i++) {
71 gpointer p1, p2;
72
73 p1 = g_slist_nth_data (slist, i);
74 p2 = g_slist_nth_data (slist, i+1);
75
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)));
78 }
79 }
80
81 static void
test_slist_insert_sorted(void)82 test_slist_insert_sorted (void)
83 {
84 GSList *slist = NULL;
85 gint i;
86
87 PRINT_MSG (("testing g_slist_insert_sorted()"));
88
89 for (i = 0; i < SIZE; i++) {
90 slist = g_slist_insert_sorted (slist, GINT_TO_POINTER (array[i]), sort);
91 }
92
93 for (i = 0; i < SIZE - 1; i++) {
94 gpointer p1, p2;
95
96 p1 = g_slist_nth_data (slist, i);
97 p2 = g_slist_nth_data (slist, i+1);
98
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)));
101 }
102 }
103
104 static void
test_slist_insert_sorted_with_data(void)105 test_slist_insert_sorted_with_data (void)
106 {
107 GSList *slist = NULL;
108 gint i;
109
110 PRINT_MSG (("testing g_slist_insert_sorted_with_data()"));
111
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,
116 NULL);
117 }
118
119 for (i = 0; i < SIZE - 1; i++) {
120 gpointer p1, p2;
121
122 p1 = g_slist_nth_data (slist, i);
123 p2 = g_slist_nth_data (slist, i+1);
124
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)));
127 }
128 }
129
130 static void
test_slist_reverse(void)131 test_slist_reverse (void)
132 {
133 GSList *slist = NULL;
134 GSList *st;
135 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
136 gint i;
137
138 PRINT_MSG (("testing g_slist_reverse()"));
139
140 for (i = 0; i < 10; i++) {
141 slist = g_slist_append (slist, &nums[i]);
142 }
143
144 slist = g_slist_reverse (slist);
145
146 for (i = 0; i < 10; i++) {
147 st = g_slist_nth (slist, i);
148 g_assert (*((gint*) st->data) == (9 - i));
149 }
150
151 g_slist_free (slist);
152 }
153
154 static void
test_slist_nth(void)155 test_slist_nth (void)
156 {
157 GSList *slist = NULL;
158 GSList *st;
159 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
160 gint i;
161
162 PRINT_MSG (("testing g_slist_nth()"));
163
164 for (i = 0; i < 10; i++) {
165 slist = g_slist_append (slist, &nums[i]);
166 }
167
168 for (i = 0; i < 10; i++) {
169 st = g_slist_nth (slist, i);
170 g_assert (*((gint*) st->data) == i);
171 }
172
173 g_slist_free (slist);
174 }
175
176 int
main(int argc,char * argv[])177 main (int argc, char *argv[])
178 {
179 gint i;
180
181 DEBUG_MSG (("debugging messages turned on"));
182
183 DEBUG_MSG (("creating %d random numbers", SIZE));
184
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]));
189 }
190
191 /* Start tests. */
192 test_slist_sort ();
193 test_slist_sort_with_data ();
194
195 test_slist_insert_sorted ();
196 test_slist_insert_sorted_with_data ();
197
198 test_slist_reverse ();
199 test_slist_nth ();
200
201 PRINT_MSG (("testing finished"));
202
203 return 0;
204 }
205