2 * Copyright 2011 Collabora Ltd.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * See the included COPYING file for more information.
12 #undef G_DISABLE_ASSERT
20 static const gchar
*NYAN
= "nyannyan";
21 static const gsize N_NYAN
= 8;
31 bytes
= g_bytes_new (data
, 4);
32 g_assert (bytes
!= NULL
);
33 g_assert (g_bytes_get_data (bytes
, &size
) != data
);
34 g_assert_cmpuint (size
, ==, 4);
35 g_assert_cmpuint (g_bytes_get_size (bytes
), ==, 4);
36 g_assert_cmpmem (data
, 4, g_bytes_get_data (bytes
, NULL
), g_bytes_get_size (bytes
));
38 g_bytes_unref (bytes
);
48 data
= g_strdup ("test");
49 bytes
= g_bytes_new_take (data
, 4);
50 g_assert (bytes
!= NULL
);
51 g_assert (g_bytes_get_data (bytes
, &size
) == data
);
52 g_assert_cmpuint (size
, ==, 4);
53 g_assert_cmpuint (g_bytes_get_size (bytes
), ==, 4);
55 g_bytes_unref (bytes
);
59 test_new_static (void)
66 bytes
= g_bytes_new_static (data
, 4);
67 g_assert (bytes
!= NULL
);
68 g_assert (g_bytes_get_data (bytes
, &size
) == data
);
69 g_assert_cmpuint (size
, ==, 4);
70 g_assert_cmpuint (g_bytes_get_size (bytes
), ==, 4);
72 g_bytes_unref (bytes
);
76 test_new_from_bytes (void)
78 const gchar
*data
= "smile and wave";
82 bytes
= g_bytes_new (data
, 14);
83 sub
= g_bytes_new_from_bytes (bytes
, 10, 4);
85 g_assert (sub
!= NULL
);
86 g_assert (g_bytes_get_data (sub
, NULL
) == ((gchar
*)g_bytes_get_data (bytes
, NULL
)) + 10);
87 g_bytes_unref (bytes
);
89 g_assert_cmpmem (g_bytes_get_data (sub
, NULL
), g_bytes_get_size (sub
), "wave", 4);
94 on_destroy_increment (gpointer data
)
97 g_assert (count
!= NULL
);
102 test_new_with_free_func (void)
110 bytes
= g_bytes_new_with_free_func (data
, 4, on_destroy_increment
, &count
);
111 g_assert (bytes
!= NULL
);
112 g_assert_cmpint (count
, ==, 0);
113 g_assert (g_bytes_get_data (bytes
, &size
) == data
);
114 g_assert_cmpuint (size
, ==, 4);
115 g_assert_cmpuint (g_bytes_get_size (bytes
), ==, 4);
117 g_bytes_unref (bytes
);
118 g_assert_cmpuint (count
, ==, 1);
129 bytes1
= g_bytes_new ("blah", 4);
130 bytes2
= g_bytes_new ("blah", 4);
132 hash1
= g_bytes_hash (bytes1
);
133 hash2
= g_bytes_hash (bytes2
);
134 g_assert (hash1
== hash2
);
136 g_bytes_unref (bytes1
);
137 g_bytes_unref (bytes2
);
146 bytes
= g_bytes_new ("blah", 4);
148 bytes2
= g_bytes_new ("blah", 4);
149 g_assert (g_bytes_equal (bytes
, bytes2
));
150 g_assert (g_bytes_equal (bytes2
, bytes
));
151 g_bytes_unref (bytes2
);
153 bytes2
= g_bytes_new ("bla", 3);
154 g_assert (!g_bytes_equal (bytes
, bytes2
));
155 g_assert (!g_bytes_equal (bytes2
, bytes
));
156 g_bytes_unref (bytes2
);
158 bytes2
= g_bytes_new ("true", 4);
159 g_assert (!g_bytes_equal (bytes
, bytes2
));
160 g_assert (!g_bytes_equal (bytes2
, bytes
));
161 g_bytes_unref (bytes2
);
163 g_bytes_unref (bytes
);
172 bytes
= g_bytes_new ("blah", 4);
174 bytes2
= g_bytes_new ("blah", 4);
175 g_assert_cmpint (g_bytes_compare (bytes
, bytes2
), ==, 0);
176 g_bytes_unref (bytes2
);
178 bytes2
= g_bytes_new ("bla", 3);
179 g_assert_cmpint (g_bytes_compare (bytes
, bytes2
), >, 0);
180 g_bytes_unref (bytes2
);
182 bytes2
= g_bytes_new ("abcd", 4);
183 g_assert_cmpint (g_bytes_compare (bytes
, bytes2
), >, 0);
184 g_bytes_unref (bytes2
);
186 bytes2
= g_bytes_new ("blahblah", 8);
187 g_assert_cmpint (g_bytes_compare (bytes
, bytes2
), <, 0);
188 g_bytes_unref (bytes2
);
190 bytes2
= g_bytes_new ("zyx", 3);
191 g_assert_cmpint (g_bytes_compare (bytes
, bytes2
), <, 0);
192 g_bytes_unref (bytes2
);
194 bytes2
= g_bytes_new ("zyxw", 4);
195 g_assert_cmpint (g_bytes_compare (bytes
, bytes2
), <, 0);
196 g_bytes_unref (bytes2
);
198 g_bytes_unref (bytes
);
202 test_to_data_transferred (void)
204 gconstpointer memory
;
209 /* Memory transferred: one reference, and allocated with g_malloc */
210 bytes
= g_bytes_new (NYAN
, N_NYAN
);
211 memory
= g_bytes_get_data (bytes
, NULL
);
212 data
= g_bytes_unref_to_data (bytes
, &size
);
213 g_assert (data
== memory
);
214 g_assert_cmpmem (data
, size
, NYAN
, N_NYAN
);
219 test_to_data_two_refs (void)
221 gconstpointer memory
;
226 /* Memory copied: two references */
227 bytes
= g_bytes_new (NYAN
, N_NYAN
);
228 bytes
= g_bytes_ref (bytes
);
229 memory
= g_bytes_get_data (bytes
, NULL
);
230 data
= g_bytes_unref_to_data (bytes
, &size
);
231 g_assert (data
!= memory
);
232 g_assert_cmpmem (data
, size
, NYAN
, N_NYAN
);
234 g_assert (g_bytes_get_data (bytes
, &size
) == memory
);
235 g_assert_cmpuint (size
, ==, N_NYAN
);
236 g_assert_cmpuint (g_bytes_get_size (bytes
), ==, N_NYAN
);
237 g_bytes_unref (bytes
);
241 test_to_data_non_malloc (void)
247 /* Memory copied: non malloc memory */
248 bytes
= g_bytes_new_static (NYAN
, N_NYAN
);
249 g_assert (g_bytes_get_data (bytes
, NULL
) == NYAN
);
250 data
= g_bytes_unref_to_data (bytes
, &size
);
251 g_assert (data
!= (gpointer
)NYAN
);
252 g_assert_cmpmem (data
, size
, NYAN
, N_NYAN
);
257 test_to_array_transferred (void)
259 gconstpointer memory
;
263 /* Memory transferred: one reference, and allocated with g_malloc */
264 bytes
= g_bytes_new (NYAN
, N_NYAN
);
265 memory
= g_bytes_get_data (bytes
, NULL
);
266 array
= g_bytes_unref_to_array (bytes
);
267 g_assert (array
!= NULL
);
268 g_assert (array
->data
== memory
);
269 g_assert_cmpmem (array
->data
, array
->len
, NYAN
, N_NYAN
);
270 g_byte_array_unref (array
);
274 test_to_array_two_refs (void)
276 gconstpointer memory
;
281 /* Memory copied: two references */
282 bytes
= g_bytes_new (NYAN
, N_NYAN
);
283 bytes
= g_bytes_ref (bytes
);
284 memory
= g_bytes_get_data (bytes
, NULL
);
285 array
= g_bytes_unref_to_array (bytes
);
286 g_assert (array
!= NULL
);
287 g_assert (array
->data
!= memory
);
288 g_assert_cmpmem (array
->data
, array
->len
, NYAN
, N_NYAN
);
289 g_byte_array_unref (array
);
290 g_assert (g_bytes_get_data (bytes
, &size
) == memory
);
291 g_assert_cmpuint (size
, ==, N_NYAN
);
292 g_assert_cmpuint (g_bytes_get_size (bytes
), ==, N_NYAN
);
293 g_bytes_unref (bytes
);
297 test_to_array_non_malloc (void)
302 /* Memory copied: non malloc memory */
303 bytes
= g_bytes_new_static (NYAN
, N_NYAN
);
304 g_assert (g_bytes_get_data (bytes
, NULL
) == NYAN
);
305 array
= g_bytes_unref_to_array (bytes
);
306 g_assert (array
!= NULL
);
307 g_assert (array
->data
!= (gpointer
)NYAN
);
308 g_assert_cmpmem (array
->data
, array
->len
, NYAN
, N_NYAN
);
309 g_byte_array_unref (array
);
319 bytes
= g_bytes_new (NULL
, 0);
321 data
= g_bytes_unref_to_data (bytes
, &size
);
323 g_assert (data
== NULL
);
324 g_assert (size
== 0);
328 main (int argc
, char *argv
[])
330 g_test_init (&argc
, &argv
, NULL
);
332 g_test_bug_base ("http://bugs.gnome.org/");
334 g_test_add_func ("/bytes/new", test_new
);
335 g_test_add_func ("/bytes/new-take", test_new_take
);
336 g_test_add_func ("/bytes/new-static", test_new_static
);
337 g_test_add_func ("/bytes/new-with-free-func", test_new_with_free_func
);
338 g_test_add_func ("/bytes/new-from-bytes", test_new_from_bytes
);
339 g_test_add_func ("/bytes/hash", test_hash
);
340 g_test_add_func ("/bytes/equal", test_equal
);
341 g_test_add_func ("/bytes/compare", test_compare
);
342 g_test_add_func ("/bytes/to-data/transfered", test_to_data_transferred
);
343 g_test_add_func ("/bytes/to-data/two-refs", test_to_data_two_refs
);
344 g_test_add_func ("/bytes/to-data/non-malloc", test_to_data_non_malloc
);
345 g_test_add_func ("/bytes/to-array/transfered", test_to_array_transferred
);
346 g_test_add_func ("/bytes/to-array/two-refs", test_to_array_two_refs
);
347 g_test_add_func ("/bytes/to-array/non-malloc", test_to_array_non_malloc
);
348 g_test_add_func ("/bytes/null", test_null
);
350 return g_test_run ();