Add some more cases to the app-id unit tests
[glib.git] / glib / tests / bytes.c
blob58f3bedea543fe3c87c65264a1f8cff1ea638a31
1 /*
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
13 #undef G_LOG_DOMAIN
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "glib.h"
20 static const gchar *NYAN = "nyannyan";
21 static const gsize N_NYAN = 8;
23 static void
24 test_new (void)
26 const gchar *data;
27 GBytes *bytes;
28 gsize size;
30 data = "test";
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);
41 static void
42 test_new_take (void)
44 gchar *data;
45 GBytes *bytes;
46 gsize size;
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);
58 static void
59 test_new_static (void)
61 const gchar *data;
62 GBytes *bytes;
63 gsize size;
65 data = "test";
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);
75 static void
76 test_new_from_bytes (void)
78 const gchar *data = "smile and wave";
79 GBytes *bytes;
80 GBytes *sub;
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);
90 g_bytes_unref (sub);
93 static void
94 on_destroy_increment (gpointer data)
96 gint *count = data;
97 g_assert (count != NULL);
98 (*count)++;
101 static void
102 test_new_with_free_func (void)
104 GBytes *bytes;
105 gchar *data;
106 gint count = 0;
107 gsize size;
109 data = "test";
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);
121 static void
122 test_hash (void)
124 GBytes *bytes1;
125 GBytes *bytes2;
126 guint hash1;
127 guint hash2;
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);
140 static void
141 test_equal (void)
143 GBytes *bytes;
144 GBytes *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);
166 static void
167 test_compare (void)
169 GBytes *bytes;
170 GBytes *bytes2;
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);
201 static void
202 test_to_data_transferred (void)
204 gconstpointer memory;
205 gpointer data;
206 gsize size;
207 GBytes *bytes;
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);
215 g_free (data);
218 static void
219 test_to_data_two_refs (void)
221 gconstpointer memory;
222 gpointer data;
223 gsize size;
224 GBytes *bytes;
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);
233 g_free (data);
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);
240 static void
241 test_to_data_non_malloc (void)
243 gpointer data;
244 gsize size;
245 GBytes *bytes;
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);
253 g_free (data);
256 static void
257 test_to_array_transferred (void)
259 gconstpointer memory;
260 GByteArray *array;
261 GBytes *bytes;
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);
273 static void
274 test_to_array_two_refs (void)
276 gconstpointer memory;
277 GByteArray *array;
278 GBytes *bytes;
279 gsize size;
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);
296 static void
297 test_to_array_non_malloc (void)
299 GByteArray *array;
300 GBytes *bytes;
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);
312 static void
313 test_null (void)
315 GBytes *bytes;
316 gpointer data;
317 gsize size;
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 ();