Simplify glib/glib/tests setup
[glib.git] / glib / tests / bytes.c
blob044450d527b165f69633c38d41adf83764a81064
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 (memcmp (data, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes)) == 0);
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_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);
91 g_bytes_unref (sub);
94 static void
95 on_destroy_increment (gpointer data)
97 gint *count = data;
98 g_assert (count != NULL);
99 (*count)++;
102 static void
103 test_new_with_free_func (void)
105 GBytes *bytes;
106 gchar *data;
107 gint count = 0;
108 gsize size;
110 data = "test";
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);
122 static void
123 test_hash (void)
125 GBytes *bytes1;
126 GBytes *bytes2;
127 guint hash1;
128 guint hash2;
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);
141 static void
142 test_equal (void)
144 GBytes *bytes;
145 GBytes *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);
167 static void
168 test_compare (void)
170 GBytes *bytes;
171 GBytes *bytes2;
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);
202 static void
203 test_to_data_transferred (void)
205 gconstpointer memory;
206 gpointer data;
207 gsize size;
208 GBytes *bytes;
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);
217 g_free (data);
220 static void
221 test_to_data_two_refs (void)
223 gconstpointer memory;
224 gpointer data;
225 gsize size;
226 GBytes *bytes;
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);
236 g_free (data);
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);
243 static void
244 test_to_data_non_malloc (void)
246 gpointer data;
247 gsize size;
248 GBytes *bytes;
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);
257 g_free (data);
260 static void
261 test_to_array_transferred (void)
263 gconstpointer memory;
264 GByteArray *array;
265 GBytes *bytes;
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);
278 static void
279 test_to_array_two_refs (void)
281 gconstpointer memory;
282 GByteArray *array;
283 GBytes *bytes;
284 gsize size;
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);
302 static void
303 test_to_array_non_malloc (void)
305 GByteArray *array;
306 GBytes *bytes;
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);
319 static void
320 test_null (void)
322 GBytes *bytes;
323 gpointer data;
324 gsize size;
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 ();