First public contribution.
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
2 #undef G_DISABLE_ASSERT
7 #define DEBUG_MSG(args)
8 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
9 #define PRINT_MSG(args)
10 /* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
13 #define NUMBER_MIN 0000
14 #define NUMBER_MAX 9999
17 #include "mrt2_glib2_test.h"
21 static guint32 array[SIZE];
25 sort (gconstpointer p1, gconstpointer p2)
29 a = GPOINTER_TO_INT (p1);
30 b = GPOINTER_TO_INT (p2);
32 return (a > b ? +1 : a == b ? 0 : -1);
39 test_slist_sort (void)
44 PRINT_MSG (("testing g_slist_sort()"));
46 for (i = 0; i < SIZE; i++) {
47 slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
50 slist = g_slist_sort (slist, sort);
51 for (i = 0; i < SIZE - 1; i++) {
54 p1 = g_slist_nth_data (slist, i);
55 p2 = g_slist_nth_data (slist, i+1);
57 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
58 DEBUG_MSG (("slist_sort #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
63 test_slist_sort_with_data (void)
68 PRINT_MSG (("testing g_slist_sort_with_data()"));
70 for (i = 0; i < SIZE; i++) {
71 slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
74 slist = g_slist_sort_with_data (slist, (GCompareDataFunc)sort, NULL);
75 for (i = 0; i < SIZE - 1; i++) {
78 p1 = g_slist_nth_data (slist, i);
79 p2 = g_slist_nth_data (slist, i+1);
81 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
82 DEBUG_MSG (("slist_sort_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
87 test_slist_insert_sorted (void)
92 PRINT_MSG (("testing g_slist_insert_sorted()"));
94 for (i = 0; i < SIZE; i++) {
95 slist = g_slist_insert_sorted (slist, GINT_TO_POINTER (array[i]), sort);
98 for (i = 0; i < SIZE - 1; i++) {
101 p1 = g_slist_nth_data (slist, i);
102 p2 = g_slist_nth_data (slist, i+1);
104 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
105 DEBUG_MSG (("slist_insert_sorted #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
110 test_slist_insert_sorted_with_data (void)
112 GSList *slist = NULL;
115 PRINT_MSG (("testing g_slist_insert_sorted_with_data()"));
117 for (i = 0; i < SIZE; i++) {
118 slist = g_slist_insert_sorted_with_data (slist,
119 GINT_TO_POINTER (array[i]),
120 (GCompareDataFunc)sort,
124 for (i = 0; i < SIZE - 1; i++) {
127 p1 = g_slist_nth_data (slist, i);
128 p2 = g_slist_nth_data (slist, i+1);
130 g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
134 test_slist_reverse (void)
136 GSList *slist = NULL;
138 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
141 PRINT_MSG (("testing g_slist_reverse()"));
143 for (i = 0; i < 10; i++) {
144 slist = g_slist_append (slist, &nums[i]);
147 slist = g_slist_reverse (slist);
149 for (i = 0; i < 10; i++) {
150 st = g_slist_nth (slist, i);
151 g_assert (*((gint*) st->data) == (9 - i));
154 g_slist_free (slist);
158 test_slist_nth (void)
160 GSList *slist = NULL;
162 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
165 PRINT_MSG (("testing g_slist_nth()"));
167 for (i = 0; i < 10; i++) {
168 slist = g_slist_append (slist, &nums[i]);
171 for (i = 0; i < 10; i++) {
172 st = g_slist_nth (slist, i);
173 g_assert (*((gint*) st->data) == i);
176 g_slist_free (slist);
180 main (int argc, char *argv[])
185 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
186 g_set_print_handler(mrtPrintHandler);
192 DEBUG_MSG (("debugging messages turned on"));
194 DEBUG_MSG (("creating %d random numbers", SIZE));
196 /* Create an array of random numbers. */
197 for (i = 0; i < SIZE; i++) {
198 array[i] = g_random_int_range (NUMBER_MIN, NUMBER_MAX);
199 DEBUG_MSG (("number #%3.3d ---> %d", i, array[i]));
204 test_slist_sort_with_data ();
206 test_slist_insert_sorted ();
207 test_slist_insert_sorted_with_data ();
209 test_slist_reverse ();
212 PRINT_MSG (("testing finished"));
215 testResultXml("slist-test");
216 #endif /* EMULATOR */