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 (memcmp (data
, g_bytes_get_data (bytes
, NULL
), g_bytes_get_size (bytes
)) == 0);
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_assert (g_bytes_get_size (sub
) == 4);
88 g_bytes_unref (bytes
);
90 g_assert (memcmp (g_bytes_get_data (sub
, NULL
), "wave", 4) == 0);
95 on_destroy_increment (gpointer data
)
98 g_assert (count
!= NULL
);
103 test_new_with_free_func (void)
111 bytes
= g_bytes_new_with_free_func (data
, 4, on_destroy_increment
, &count
);
112 g_assert (bytes
!= NULL
);
113 g_assert_cmpint (count
, ==, 0);
114 g_assert (g_bytes_get_data (bytes
, &size
) == data
);
115 g_assert_cmpuint (size
, ==, 4);
116 g_assert_cmpuint (g_bytes_get_size (bytes
), ==, 4);
118 g_bytes_unref (bytes
);
119 g_assert_cmpuint (count
, ==, 1);
130 bytes1
= g_bytes_new ("blah", 4);
131 bytes2
= g_bytes_new ("blah", 4);
133 hash1
= g_bytes_hash (bytes1
);
134 hash2
= g_bytes_hash (bytes2
);
135 g_assert (hash1
== hash2
);
137 g_bytes_unref (bytes1
);
138 g_bytes_unref (bytes2
);
147 bytes
= g_bytes_new ("blah", 4);
149 bytes2
= g_bytes_new ("blah", 4);
150 g_assert (g_bytes_equal (bytes
, bytes2
));
151 g_assert (g_bytes_equal (bytes2
, bytes
));
152 g_bytes_unref (bytes2
);
154 bytes2
= g_bytes_new ("bla", 3);
155 g_assert (!g_bytes_equal (bytes
, bytes2
));
156 g_assert (!g_bytes_equal (bytes2
, bytes
));
157 g_bytes_unref (bytes2
);
159 bytes2
= g_bytes_new ("true", 4);
160 g_assert (!g_bytes_equal (bytes
, bytes2
));
161 g_assert (!g_bytes_equal (bytes2
, bytes
));
162 g_bytes_unref (bytes2
);
164 g_bytes_unref (bytes
);
173 bytes
= g_bytes_new ("blah", 4);
175 bytes2
= g_bytes_new ("blah", 4);
176 g_assert_cmpint (g_bytes_compare (bytes
, bytes2
), ==, 0);
177 g_bytes_unref (bytes2
);
179 bytes2
= g_bytes_new ("bla", 3);
180 g_assert_cmpint (g_bytes_compare (bytes
, bytes2
), >, 0);
181 g_bytes_unref (bytes2
);
183 bytes2
= g_bytes_new ("abcd", 4);
184 g_assert_cmpint (g_bytes_compare (bytes
, bytes2
), >, 0);
185 g_bytes_unref (bytes2
);
187 bytes2
= g_bytes_new ("blahblah", 8);
188 g_assert_cmpint (g_bytes_compare (bytes
, bytes2
), <, 0);
189 g_bytes_unref (bytes2
);
191 bytes2
= g_bytes_new ("zyx", 3);
192 g_assert_cmpint (g_bytes_compare (bytes
, bytes2
), <, 0);
193 g_bytes_unref (bytes2
);
195 bytes2
= g_bytes_new ("zyxw", 4);
196 g_assert_cmpint (g_bytes_compare (bytes
, bytes2
), <, 0);
197 g_bytes_unref (bytes2
);
199 g_bytes_unref (bytes
);
203 test_to_data_transferred (void)
205 gconstpointer memory
;
210 /* Memory transferred: one reference, and allocated with g_malloc */
211 bytes
= g_bytes_new (NYAN
, N_NYAN
);
212 memory
= g_bytes_get_data (bytes
, NULL
);
213 data
= g_bytes_unref_to_data (bytes
, &size
);
214 g_assert (data
== memory
);
215 g_assert_cmpuint (size
, ==, N_NYAN
);
216 g_assert (memcmp (data
, NYAN
, N_NYAN
) == 0);
221 test_to_data_two_refs (void)
223 gconstpointer memory
;
228 /* Memory copied: two references */
229 bytes
= g_bytes_new (NYAN
, N_NYAN
);
230 bytes
= g_bytes_ref (bytes
);
231 memory
= g_bytes_get_data (bytes
, NULL
);
232 data
= g_bytes_unref_to_data (bytes
, &size
);
233 g_assert (data
!= memory
);
234 g_assert_cmpuint (size
, ==, N_NYAN
);
235 g_assert (memcmp (data
, NYAN
, N_NYAN
) == 0);
237 g_assert (g_bytes_get_data (bytes
, &size
) == memory
);
238 g_assert_cmpuint (size
, ==, N_NYAN
);
239 g_assert_cmpuint (g_bytes_get_size (bytes
), ==, N_NYAN
);
240 g_bytes_unref (bytes
);
244 test_to_data_non_malloc (void)
250 /* Memory copied: non malloc memory */
251 bytes
= g_bytes_new_static (NYAN
, N_NYAN
);
252 g_assert (g_bytes_get_data (bytes
, NULL
) == NYAN
);
253 data
= g_bytes_unref_to_data (bytes
, &size
);
254 g_assert (data
!= (gpointer
)NYAN
);
255 g_assert_cmpuint (size
, ==, N_NYAN
);
256 g_assert (memcmp (data
, NYAN
, N_NYAN
) == 0);
261 test_to_array_transferred (void)
263 gconstpointer memory
;
267 /* Memory transferred: one reference, and allocated with g_malloc */
268 bytes
= g_bytes_new (NYAN
, N_NYAN
);
269 memory
= g_bytes_get_data (bytes
, NULL
);
270 array
= g_bytes_unref_to_array (bytes
);
271 g_assert (array
!= NULL
);
272 g_assert (array
->data
== memory
);
273 g_assert_cmpuint (array
->len
, ==, N_NYAN
);
274 g_assert (memcmp (array
->data
, NYAN
, N_NYAN
) == 0);
275 g_byte_array_unref (array
);
279 test_to_array_two_refs (void)
281 gconstpointer memory
;
286 /* Memory copied: two references */
287 bytes
= g_bytes_new (NYAN
, N_NYAN
);
288 bytes
= g_bytes_ref (bytes
);
289 memory
= g_bytes_get_data (bytes
, NULL
);
290 array
= g_bytes_unref_to_array (bytes
);
291 g_assert (array
!= NULL
);
292 g_assert (array
->data
!= memory
);
293 g_assert_cmpuint (array
->len
, ==, N_NYAN
);
294 g_assert (memcmp (array
->data
, NYAN
, N_NYAN
) == 0);
295 g_byte_array_unref (array
);
296 g_assert (g_bytes_get_data (bytes
, &size
) == memory
);
297 g_assert_cmpuint (size
, ==, N_NYAN
);
298 g_assert_cmpuint (g_bytes_get_size (bytes
), ==, N_NYAN
);
299 g_bytes_unref (bytes
);
303 test_to_array_non_malloc (void)
308 /* Memory copied: non malloc memory */
309 bytes
= g_bytes_new_static (NYAN
, N_NYAN
);
310 g_assert (g_bytes_get_data (bytes
, NULL
) == NYAN
);
311 array
= g_bytes_unref_to_array (bytes
);
312 g_assert (array
!= NULL
);
313 g_assert (array
->data
!= (gpointer
)NYAN
);
314 g_assert_cmpuint (array
->len
, ==, N_NYAN
);
315 g_assert (memcmp (array
->data
, NYAN
, N_NYAN
) == 0);
316 g_byte_array_unref (array
);
326 bytes
= g_bytes_new (NULL
, 0);
328 data
= g_bytes_unref_to_data (bytes
, &size
);
330 g_assert (data
== NULL
);
331 g_assert (size
== 0);
335 main (int argc
, char *argv
[])
337 g_test_init (&argc
, &argv
, NULL
);
339 g_test_bug_base ("http://bugs.gnome.org/");
341 g_test_add_func ("/bytes/new", test_new
);
342 g_test_add_func ("/bytes/new-take", test_new_take
);
343 g_test_add_func ("/bytes/new-static", test_new_static
);
344 g_test_add_func ("/bytes/new-with-free-func", test_new_with_free_func
);
345 g_test_add_func ("/bytes/new-from-bytes", test_new_from_bytes
);
346 g_test_add_func ("/bytes/hash", test_hash
);
347 g_test_add_func ("/bytes/equal", test_equal
);
348 g_test_add_func ("/bytes/compare", test_compare
);
349 g_test_add_func ("/bytes/to-data/transfered", test_to_data_transferred
);
350 g_test_add_func ("/bytes/to-data/two-refs", test_to_data_two_refs
);
351 g_test_add_func ("/bytes/to-data/non-malloc", test_to_data_non_malloc
);
352 g_test_add_func ("/bytes/to-array/transfered", test_to_array_transferred
);
353 g_test_add_func ("/bytes/to-array/two-refs", test_to_array_two_refs
);
354 g_test_add_func ("/bytes/to-array/non-malloc", test_to_array_non_malloc
);
355 g_test_add_func ("/bytes/null", test_null
);
357 return g_test_run ();