Increase the timeout for some GLib tests
[glib.git] / glib / tests / bytes.c
blob5ea5c2b35a486a5a34843655098c3626cd723d8f
1 /*
2 * Copyright 2011 Collabora Ltd.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * See the included COPYING file for more information.
12 #undef G_DISABLE_ASSERT
13 #undef G_LOG_DOMAIN
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "glib.h"
20 /* Keep in sync with glib/gbytes.c */
21 struct _GBytes
23 gconstpointer data;
24 gsize size;
25 gint ref_count;
26 GDestroyNotify free_func;
27 gpointer user_data;
30 static const gchar *NYAN = "nyannyan";
31 static const gsize N_NYAN = 8;
33 static void
34 test_new (void)
36 const gchar *data;
37 GBytes *bytes;
38 gsize size;
40 data = "test";
41 bytes = g_bytes_new (data, 4);
42 g_assert (bytes != NULL);
43 g_assert (g_bytes_get_data (bytes, &size) != data);
44 g_assert_cmpuint (size, ==, 4);
45 g_assert_cmpuint (g_bytes_get_size (bytes), ==, 4);
46 g_assert_cmpmem (data, 4, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes));
48 g_bytes_unref (bytes);
51 static void
52 test_new_take (void)
54 gchar *data;
55 GBytes *bytes;
56 gsize size;
58 data = g_strdup ("test");
59 bytes = g_bytes_new_take (data, 4);
60 g_assert (bytes != NULL);
61 g_assert (g_bytes_get_data (bytes, &size) == data);
62 g_assert_cmpuint (size, ==, 4);
63 g_assert_cmpuint (g_bytes_get_size (bytes), ==, 4);
65 g_bytes_unref (bytes);
68 static void
69 test_new_static (void)
71 const gchar *data;
72 GBytes *bytes;
73 gsize size;
75 data = "test";
76 bytes = g_bytes_new_static (data, 4);
77 g_assert (bytes != NULL);
78 g_assert (g_bytes_get_data (bytes, &size) == data);
79 g_assert_cmpuint (size, ==, 4);
80 g_assert_cmpuint (g_bytes_get_size (bytes), ==, 4);
82 g_bytes_unref (bytes);
85 static void
86 test_new_from_bytes (void)
88 const gchar *data = "smile and wave";
89 GBytes *bytes;
90 GBytes *sub;
92 bytes = g_bytes_new (data, 14);
93 sub = g_bytes_new_from_bytes (bytes, 10, 4);
95 g_assert (sub != NULL);
96 g_assert (g_bytes_get_data (sub, NULL) == ((gchar *)g_bytes_get_data (bytes, NULL)) + 10);
97 g_bytes_unref (bytes);
99 g_assert_cmpmem (g_bytes_get_data (sub, NULL), g_bytes_get_size (sub), "wave", 4);
100 g_bytes_unref (sub);
103 /* Verify that creating slices of GBytes reference the top-most bytes
104 * at the correct offset. Ensure that intermediate GBytes are not referenced.
106 static void
107 test_new_from_bytes_slice (void)
109 GBytes *bytes = g_bytes_new_static ("Some stupid data", strlen ("Some stupid data") + 1);
110 GBytes *bytes1 = g_bytes_new_from_bytes (bytes, 4, 13);
111 GBytes *bytes2 = g_bytes_new_from_bytes (bytes1, 1, 12);
112 GBytes *bytes3 = g_bytes_new_from_bytes (bytes2, 0, 6);
114 g_assert_cmpint (bytes->ref_count, ==, 4);
115 g_assert_cmpint (bytes1->ref_count, ==, 1);
116 g_assert_cmpint (bytes2->ref_count, ==, 1);
117 g_assert_cmpint (bytes3->ref_count, ==, 1);
119 g_assert_null (bytes->user_data);
120 g_assert (bytes1->user_data == bytes);
121 g_assert (bytes2->user_data == bytes);
122 g_assert (bytes3->user_data == bytes);
124 g_assert_cmpint (17, ==, g_bytes_get_size (bytes));
125 g_assert_cmpint (13, ==, g_bytes_get_size (bytes1));
126 g_assert_cmpint (12, ==, g_bytes_get_size (bytes2));
127 g_assert_cmpint (6, ==, g_bytes_get_size (bytes3));
129 g_assert_cmpint (0, ==, strncmp ("Some stupid data", (gchar *)bytes->data, 17));
130 g_assert_cmpint (0, ==, strncmp (" stupid data", (gchar *)bytes1->data, 13));
131 g_assert_cmpint (0, ==, strncmp ("stupid data", (gchar *)bytes2->data, 12));
132 g_assert_cmpint (0, ==, strncmp ("stupid", (gchar *)bytes3->data, 6));
134 g_bytes_unref (bytes);
135 g_bytes_unref (bytes1);
136 g_bytes_unref (bytes2);
137 g_bytes_unref (bytes3);
140 /* Ensure that referencing an entire GBytes just returns the same bytes
141 * instance (with incremented reference count) instead of a new instance.
143 static void
144 test_new_from_bytes_shared_ref (void)
146 GBytes *bytes = g_bytes_new_static ("Some data", strlen ("Some data") + 1);
147 GBytes *other = g_bytes_new_from_bytes (bytes, 0, g_bytes_get_size (bytes));
149 g_assert (bytes == other);
150 g_assert_cmpint (bytes->ref_count, ==, 2);
152 g_bytes_unref (bytes);
153 g_bytes_unref (other);
156 static void
157 on_destroy_increment (gpointer data)
159 gint *count = data;
160 g_assert (count != NULL);
161 (*count)++;
164 static void
165 test_new_with_free_func (void)
167 GBytes *bytes;
168 gchar *data;
169 gint count = 0;
170 gsize size;
172 data = "test";
173 bytes = g_bytes_new_with_free_func (data, 4, on_destroy_increment, &count);
174 g_assert (bytes != NULL);
175 g_assert_cmpint (count, ==, 0);
176 g_assert (g_bytes_get_data (bytes, &size) == data);
177 g_assert_cmpuint (size, ==, 4);
178 g_assert_cmpuint (g_bytes_get_size (bytes), ==, 4);
180 g_bytes_unref (bytes);
181 g_assert_cmpuint (count, ==, 1);
184 static void
185 test_hash (void)
187 GBytes *bytes1;
188 GBytes *bytes2;
189 guint hash1;
190 guint hash2;
192 bytes1 = g_bytes_new ("blah", 4);
193 bytes2 = g_bytes_new ("blah", 4);
195 hash1 = g_bytes_hash (bytes1);
196 hash2 = g_bytes_hash (bytes2);
197 g_assert (hash1 == hash2);
199 g_bytes_unref (bytes1);
200 g_bytes_unref (bytes2);
203 static void
204 test_equal (void)
206 GBytes *bytes;
207 GBytes *bytes2;
209 bytes = g_bytes_new ("blah", 4);
211 bytes2 = g_bytes_new ("blah", 4);
212 g_assert (g_bytes_equal (bytes, bytes2));
213 g_assert (g_bytes_equal (bytes2, bytes));
214 g_bytes_unref (bytes2);
216 bytes2 = g_bytes_new ("bla", 3);
217 g_assert (!g_bytes_equal (bytes, bytes2));
218 g_assert (!g_bytes_equal (bytes2, bytes));
219 g_bytes_unref (bytes2);
221 bytes2 = g_bytes_new ("true", 4);
222 g_assert (!g_bytes_equal (bytes, bytes2));
223 g_assert (!g_bytes_equal (bytes2, bytes));
224 g_bytes_unref (bytes2);
226 g_bytes_unref (bytes);
229 static void
230 test_compare (void)
232 GBytes *bytes;
233 GBytes *bytes2;
235 bytes = g_bytes_new ("blah", 4);
237 bytes2 = g_bytes_new ("blah", 4);
238 g_assert_cmpint (g_bytes_compare (bytes, bytes2), ==, 0);
239 g_bytes_unref (bytes2);
241 bytes2 = g_bytes_new ("bla", 3);
242 g_assert_cmpint (g_bytes_compare (bytes, bytes2), >, 0);
243 g_bytes_unref (bytes2);
245 bytes2 = g_bytes_new ("abcd", 4);
246 g_assert_cmpint (g_bytes_compare (bytes, bytes2), >, 0);
247 g_bytes_unref (bytes2);
249 bytes2 = g_bytes_new ("blahblah", 8);
250 g_assert_cmpint (g_bytes_compare (bytes, bytes2), <, 0);
251 g_bytes_unref (bytes2);
253 bytes2 = g_bytes_new ("zyx", 3);
254 g_assert_cmpint (g_bytes_compare (bytes, bytes2), <, 0);
255 g_bytes_unref (bytes2);
257 bytes2 = g_bytes_new ("zyxw", 4);
258 g_assert_cmpint (g_bytes_compare (bytes, bytes2), <, 0);
259 g_bytes_unref (bytes2);
261 g_bytes_unref (bytes);
264 static void
265 test_to_data_transferred (void)
267 gconstpointer memory;
268 gpointer data;
269 gsize size;
270 GBytes *bytes;
272 /* Memory transferred: one reference, and allocated with g_malloc */
273 bytes = g_bytes_new (NYAN, N_NYAN);
274 memory = g_bytes_get_data (bytes, NULL);
275 data = g_bytes_unref_to_data (bytes, &size);
276 g_assert (data == memory);
277 g_assert_cmpmem (data, size, NYAN, N_NYAN);
278 g_free (data);
281 static void
282 test_to_data_two_refs (void)
284 gconstpointer memory;
285 gpointer data;
286 gsize size;
287 GBytes *bytes;
289 /* Memory copied: two references */
290 bytes = g_bytes_new (NYAN, N_NYAN);
291 bytes = g_bytes_ref (bytes);
292 memory = g_bytes_get_data (bytes, NULL);
293 data = g_bytes_unref_to_data (bytes, &size);
294 g_assert (data != memory);
295 g_assert_cmpmem (data, size, NYAN, N_NYAN);
296 g_free (data);
297 g_assert (g_bytes_get_data (bytes, &size) == memory);
298 g_assert_cmpuint (size, ==, N_NYAN);
299 g_assert_cmpuint (g_bytes_get_size (bytes), ==, N_NYAN);
300 g_bytes_unref (bytes);
303 static void
304 test_to_data_non_malloc (void)
306 gpointer data;
307 gsize size;
308 GBytes *bytes;
310 /* Memory copied: non malloc memory */
311 bytes = g_bytes_new_static (NYAN, N_NYAN);
312 g_assert (g_bytes_get_data (bytes, NULL) == NYAN);
313 data = g_bytes_unref_to_data (bytes, &size);
314 g_assert (data != (gpointer)NYAN);
315 g_assert_cmpmem (data, size, NYAN, N_NYAN);
316 g_free (data);
319 static void
320 test_to_array_transferred (void)
322 gconstpointer memory;
323 GByteArray *array;
324 GBytes *bytes;
326 /* Memory transferred: one reference, and allocated with g_malloc */
327 bytes = g_bytes_new (NYAN, N_NYAN);
328 memory = g_bytes_get_data (bytes, NULL);
329 array = g_bytes_unref_to_array (bytes);
330 g_assert (array != NULL);
331 g_assert (array->data == memory);
332 g_assert_cmpmem (array->data, array->len, NYAN, N_NYAN);
333 g_byte_array_unref (array);
336 static void
337 test_to_array_two_refs (void)
339 gconstpointer memory;
340 GByteArray *array;
341 GBytes *bytes;
342 gsize size;
344 /* Memory copied: two references */
345 bytes = g_bytes_new (NYAN, N_NYAN);
346 bytes = g_bytes_ref (bytes);
347 memory = g_bytes_get_data (bytes, NULL);
348 array = g_bytes_unref_to_array (bytes);
349 g_assert (array != NULL);
350 g_assert (array->data != memory);
351 g_assert_cmpmem (array->data, array->len, NYAN, N_NYAN);
352 g_byte_array_unref (array);
353 g_assert (g_bytes_get_data (bytes, &size) == memory);
354 g_assert_cmpuint (size, ==, N_NYAN);
355 g_assert_cmpuint (g_bytes_get_size (bytes), ==, N_NYAN);
356 g_bytes_unref (bytes);
359 static void
360 test_to_array_non_malloc (void)
362 GByteArray *array;
363 GBytes *bytes;
365 /* Memory copied: non malloc memory */
366 bytes = g_bytes_new_static (NYAN, N_NYAN);
367 g_assert (g_bytes_get_data (bytes, NULL) == NYAN);
368 array = g_bytes_unref_to_array (bytes);
369 g_assert (array != NULL);
370 g_assert (array->data != (gpointer)NYAN);
371 g_assert_cmpmem (array->data, array->len, NYAN, N_NYAN);
372 g_byte_array_unref (array);
375 static void
376 test_null (void)
378 GBytes *bytes;
379 gpointer data;
380 gsize size;
382 bytes = g_bytes_new (NULL, 0);
384 data = g_bytes_unref_to_data (bytes, &size);
386 g_assert (data == NULL);
387 g_assert (size == 0);
391 main (int argc, char *argv[])
393 g_test_init (&argc, &argv, NULL);
395 g_test_bug_base ("https://bugzilla.gnome.org/");
397 g_test_add_func ("/bytes/new", test_new);
398 g_test_add_func ("/bytes/new-take", test_new_take);
399 g_test_add_func ("/bytes/new-static", test_new_static);
400 g_test_add_func ("/bytes/new-with-free-func", test_new_with_free_func);
401 g_test_add_func ("/bytes/new-from-bytes", test_new_from_bytes);
402 g_test_add_func ("/bytes/new-from-bytes-slice", test_new_from_bytes_slice);
403 g_test_add_func ("/bytes/new-from-bytes-shared-ref", test_new_from_bytes_shared_ref);
404 g_test_add_func ("/bytes/hash", test_hash);
405 g_test_add_func ("/bytes/equal", test_equal);
406 g_test_add_func ("/bytes/compare", test_compare);
407 g_test_add_func ("/bytes/to-data/transfered", test_to_data_transferred);
408 g_test_add_func ("/bytes/to-data/two-refs", test_to_data_two_refs);
409 g_test_add_func ("/bytes/to-data/non-malloc", test_to_data_non_malloc);
410 g_test_add_func ("/bytes/to-array/transfered", test_to_array_transferred);
411 g_test_add_func ("/bytes/to-array/two-refs", test_to_array_two_refs);
412 g_test_add_func ("/bytes/to-array/non-malloc", test_to_array_non_malloc);
413 g_test_add_func ("/bytes/null", test_null);
415 return g_test_run ();