File: list-test.c

package info (click to toggle)
glib2.0 2.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 46,108 kB
  • ctags: 31,166
  • sloc: ansic: 239,332; sh: 11,438; xml: 5,729; makefile: 2,551; perl: 1,259; python: 754; cpp: 9
file content (212 lines) | stat: -rw-r--r-- 4,269 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN

#include <glib.h>

#define DEBUG_MSG(args)
/* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");  */
#define PRINT_MSG(args)
/* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */

#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;

  PRINT_MSG (("testing g_list_sort()"));

  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));
    DEBUG_MSG (("list_sort #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
  }

  g_list_free (list);
}

static void
test_list_sort_with_data (void)
{
  GList *list = NULL;
  gint   i;

  PRINT_MSG (("testing g_list_sort_with_data()"));

  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));
    DEBUG_MSG (("list_sort_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
  }

  g_list_free (list);
}

static void
test_list_insert_sorted (void)
{
  GList *list = NULL;
  gint   i;

  PRINT_MSG (("testing g_list_insert_sorted()"));

  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));
    DEBUG_MSG (("list_insert_sorted #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
  }

  g_list_free (list);
}

static void
test_list_insert_sorted_with_data (void)
{
  GList *list = NULL;
  gint   i;

  PRINT_MSG (("testing g_list_insert_sorted_with_data()"));

  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));
    DEBUG_MSG (("list_insert_sorted_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
  }

  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;

  PRINT_MSG (("testing g_list_reverse()"));

  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;

  PRINT_MSG (("testing g_list_nth()"));

  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;

  DEBUG_MSG (("debugging messages turned on"));

  DEBUG_MSG (("creating %d random numbers", SIZE));

  /* Create an array of random numbers. */
  for (i = 0; i < SIZE; i++) {
    array[i] = g_random_int_range (NUMBER_MIN, NUMBER_MAX);
    DEBUG_MSG (("number #%3.3d ---> %d", i, array[i]));
  }

  /* Start tests. */
  test_list_sort ();
  test_list_sort_with_data ();

  test_list_insert_sorted ();
  test_list_insert_sorted_with_data ();

  test_list_reverse ();
  test_list_nth ();

  PRINT_MSG (("testing finished"));

  return 0;
}