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
20 /* Keep in sync with glib/gbytes.c */
26 GDestroyNotify free_func
;
30 static const gchar
*NYAN
= "nyannyan";
31 static const gsize N_NYAN
= 8;
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
);
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
);
69 test_new_static (void)
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
);
86 test_new_from_bytes (void)
88 const gchar
*data
= "smile and wave";
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);
103 /* Verify that creating slices of GBytes reference the top-most bytes
104 * at the correct offset. Ensure that intermediate GBytes are not referenced.
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.
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
);
157 on_destroy_increment (gpointer data
)
160 g_assert (count
!= NULL
);
165 test_new_with_free_func (void)
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);
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
);
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
);
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
);
265 test_to_data_transferred (void)
267 gconstpointer memory
;
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
);
282 test_to_data_two_refs (void)
284 gconstpointer memory
;
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
);
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
);
304 test_to_data_non_malloc (void)
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
);
320 test_to_array_transferred (void)
322 gconstpointer memory
;
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
);
337 test_to_array_two_refs (void)
339 gconstpointer memory
;
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
);
360 test_to_array_non_malloc (void)
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
);
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 ();