GVariant test: add a new vector serialisation test
[platform/upstream/glib.git] / glib / tests / asyncqueue.c
1 /* Unit tests for GAsyncQueue
2  * Copyright (C) 2011 Red Hat, Inc
3  * Author: Matthias Clasen
4  *
5  * This work is provided "as is"; redistribution and modification
6  * in whole or in part, in any medium, physical or electronic is
7  * permitted without restriction.
8  *
9  * This work is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * In no event shall the authors or contributors be liable for any
14  * direct, indirect, incidental, special, exemplary, or consequential
15  * damages (including, but not limited to, procurement of substitute
16  * goods or services; loss of use, data, or profits; or business
17  * interruption) however caused and on any theory of liability, whether
18  * in contract, strict liability, or tort (including negligence or
19  * otherwise) arising in any way out of the use of this software, even
20  * if advised of the possibility of such damage.
21  */
22
23 /* We are testing some deprecated APIs here */
24 #define GLIB_DISABLE_DEPRECATION_WARNINGS
25
26 #include <glib.h>
27
28 static gint
29 compare_func (gconstpointer d1, gconstpointer d2, gpointer data)
30 {
31   gint i1, i2;
32
33   i1 = GPOINTER_TO_INT (d1);
34   i2 = GPOINTER_TO_INT (d2);
35
36   return i1 - i2;
37 }
38
39 static
40 void test_async_queue_sort (void)
41 {
42   GAsyncQueue *q;
43
44   q = g_async_queue_new ();
45
46   g_async_queue_push (q, GINT_TO_POINTER (10));
47   g_async_queue_push (q, GINT_TO_POINTER (2));
48   g_async_queue_push (q, GINT_TO_POINTER (7));
49
50   g_async_queue_sort (q, compare_func, NULL);
51
52   g_async_queue_push_sorted (q, GINT_TO_POINTER (1), compare_func, NULL);
53   g_async_queue_push_sorted (q, GINT_TO_POINTER (8), compare_func, NULL);
54
55   g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 1);
56   g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 2);
57   g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 7);
58   g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 8);
59   g_assert_cmpint (GPOINTER_TO_INT (g_async_queue_pop (q)), ==, 10);
60
61   g_assert (g_async_queue_try_pop (q) == NULL);
62
63   g_async_queue_unref (q);
64 }
65
66 static gint destroy_count;
67
68 static void
69 destroy_notify (gpointer item)
70 {
71   destroy_count++;
72 }
73
74 static void
75 test_async_queue_destroy (void)
76 {
77   GAsyncQueue *q;
78
79   q = g_async_queue_new_full (destroy_notify);
80
81   g_assert (destroy_count == 0);
82
83   g_async_queue_push (q, GINT_TO_POINTER (1));
84   g_async_queue_push (q, GINT_TO_POINTER (1));
85   g_async_queue_push (q, GINT_TO_POINTER (1));
86   g_async_queue_push (q, GINT_TO_POINTER (1));
87
88   g_assert (g_async_queue_length (q) == 4);
89
90   g_async_queue_unref (q);
91
92   g_assert (destroy_count == 4);
93 }
94
95 static GAsyncQueue *q;
96
97 static GThread *threads[10];
98 static gint counts[10];
99 static gint sums[10];
100 static gint total;
101
102 static gpointer
103 thread_func (gpointer data)
104 {
105   gint pos = GPOINTER_TO_INT (data);
106   gint value;
107
108   while (1)
109     {
110       value = GPOINTER_TO_INT (g_async_queue_pop (q));
111
112       if (value == -1)
113         break;
114
115       counts[pos]++;
116       sums[pos] += value;
117
118       g_usleep (1000);
119     }
120
121   return NULL;
122 }
123
124 static void
125 test_async_queue_threads (void)
126 {
127   gint i, j;
128   gint s, c;
129   gint value;
130
131   q = g_async_queue_new ();
132
133   for (i = 0; i < 10; i++)
134     threads[i] = g_thread_new ("test", thread_func, GINT_TO_POINTER (i));
135
136   for (i = 0; i < 100; i++)
137     {
138       g_async_queue_lock (q);
139       for (j = 0; j < 10; j++)
140         {
141           value = g_random_int_range (1, 100);
142           total += value;
143           g_async_queue_push_unlocked (q, GINT_TO_POINTER (value));
144         }
145       g_async_queue_unlock (q);
146
147       g_usleep (1000);
148     }
149
150   for (i = 0; i < 10; i++)
151     g_async_queue_push (q, GINT_TO_POINTER(-1));
152
153   for (i = 0; i < 10; i++)
154     g_thread_join (threads[i]);
155
156   g_assert_cmpint (g_async_queue_length (q), ==, 0);
157
158   s = c = 0;
159
160   for (i = 0; i < 10; i++)
161     {
162       g_assert_cmpint (sums[i], >, 0);
163       g_assert_cmpint (counts[i], >, 0);
164       s += sums[i];
165       c += counts[i];
166     }
167
168   g_assert_cmpint (s, ==, total);
169   g_assert_cmpint (c, ==, 1000);
170
171   g_async_queue_unref (q);
172 }
173
174 static void
175 test_async_queue_timed (void)
176 {
177   GAsyncQueue *q;
178   GTimeVal tv;
179   gint64 start, end, diff;
180   gpointer val;
181
182   q = g_async_queue_new ();
183
184   start = g_get_monotonic_time ();
185   val = g_async_queue_timeout_pop (q, G_USEC_PER_SEC / 10);
186   g_assert (val == NULL);
187
188   end = g_get_monotonic_time ();
189   diff = end - start;
190   g_assert_cmpint (diff, >=, G_USEC_PER_SEC / 10);
191   /* diff should be only a little bit more than G_USEC_PER_SEC/10, but
192    * we have to leave some wiggle room for heavily-loaded machines...
193    */
194   g_assert_cmpint (diff, <, G_USEC_PER_SEC);
195
196   start = end;
197   g_get_current_time (&tv);
198   g_time_val_add (&tv, G_USEC_PER_SEC / 10);
199   val = g_async_queue_timed_pop (q, &tv);
200   g_assert (val == NULL);
201
202   end = g_get_monotonic_time ();
203   diff = end - start;
204   g_assert_cmpint (diff, >=, G_USEC_PER_SEC / 10);
205   g_assert_cmpint (diff, <, G_USEC_PER_SEC);
206
207   start = end;
208   g_get_current_time (&tv);
209   g_time_val_add (&tv, G_USEC_PER_SEC / 10);
210   g_async_queue_lock (q);
211   val = g_async_queue_timed_pop_unlocked (q, &tv);
212   g_async_queue_unlock (q);
213   g_assert (val == NULL);
214
215   end = g_get_monotonic_time ();
216   diff = end - start;
217   g_assert_cmpint (diff, >=, G_USEC_PER_SEC / 10);
218   g_assert_cmpint (diff, <, G_USEC_PER_SEC);
219
220   g_async_queue_unref (q);
221 }
222
223 int
224 main (int argc, char *argv[])
225 {
226   g_test_init (&argc, &argv, NULL);
227
228   g_test_add_func ("/asyncqueue/sort", test_async_queue_sort);
229   g_test_add_func ("/asyncqueue/destroy", test_async_queue_destroy);
230   g_test_add_func ("/asyncqueue/threads", test_async_queue_threads);
231   g_test_add_func ("/asyncqueue/timed", test_async_queue_timed);
232
233   return g_test_run ();
234 }