Add new internal header glib-linux.h
[platform/upstream/glib.git] / glib / tests / bytes.c
1 /*
2  * Copyright 2011 Collabora Ltd.
3  *
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.
8  *
9  * See the included COPYING file for more information.
10  */
11
12 #undef G_DISABLE_ASSERT
13 #undef G_LOG_DOMAIN
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "glib.h"
19
20 static const gchar *NYAN = "nyannyan";
21 static const gsize N_NYAN = 8;
22
23 static void
24 test_new (void)
25 {
26   const gchar *data;
27   GBytes *bytes;
28   gsize size;
29
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);
37
38   g_bytes_unref (bytes);
39 }
40
41 static void
42 test_new_take (void)
43 {
44   gchar *data;
45   GBytes *bytes;
46   gsize size;
47
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);
54
55   g_bytes_unref (bytes);
56 }
57
58 static void
59 test_new_static (void)
60 {
61   const gchar *data;
62   GBytes *bytes;
63   gsize size;
64
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);
71
72   g_bytes_unref (bytes);
73 }
74
75 static void
76 test_new_from_bytes (void)
77 {
78   const gchar *data = "smile and wave";
79   GBytes *bytes;
80   GBytes *sub;
81
82   bytes = g_bytes_new (data, 14);
83   sub = g_bytes_new_from_bytes (bytes, 10, 4);
84
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);
89
90   g_assert (memcmp (g_bytes_get_data (sub, NULL), "wave", 4) == 0);
91   g_bytes_unref (sub);
92 }
93
94 static void
95 on_destroy_increment (gpointer data)
96 {
97   gint *count = data;
98   g_assert (count != NULL);
99   (*count)++;
100 }
101
102 static void
103 test_new_with_free_func (void)
104 {
105   GBytes *bytes;
106   gchar *data;
107   gint count = 0;
108   gsize size;
109
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);
117
118   g_bytes_unref (bytes);
119   g_assert_cmpuint (count, ==, 1);
120 }
121
122 static void
123 test_hash (void)
124 {
125   GBytes *bytes1;
126   GBytes *bytes2;
127   guint hash1;
128   guint hash2;
129
130   bytes1 = g_bytes_new ("blah", 4);
131   bytes2 = g_bytes_new ("blah", 4);
132
133   hash1 = g_bytes_hash (bytes1);
134   hash2 = g_bytes_hash (bytes2);
135   g_assert (hash1 == hash2);
136
137   g_bytes_unref (bytes1);
138   g_bytes_unref (bytes2);
139 }
140
141 static void
142 test_equal (void)
143 {
144   GBytes *bytes;
145   GBytes *bytes2;
146
147   bytes = g_bytes_new ("blah", 4);
148
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);
153
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);
158
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);
163
164   g_bytes_unref (bytes);
165 }
166
167 static void
168 test_compare (void)
169 {
170   GBytes *bytes;
171   GBytes *bytes2;
172
173   bytes = g_bytes_new ("blah", 4);
174
175   bytes2 = g_bytes_new ("blah", 4);
176   g_assert_cmpint (g_bytes_compare (bytes, bytes2), ==, 0);
177   g_bytes_unref (bytes2);
178
179   bytes2 = g_bytes_new ("bla", 3);
180   g_assert_cmpint (g_bytes_compare (bytes, bytes2), >, 0);
181   g_bytes_unref (bytes2);
182
183   bytes2 = g_bytes_new ("abcd", 4);
184   g_assert_cmpint (g_bytes_compare (bytes, bytes2), >, 0);
185   g_bytes_unref (bytes2);
186
187   bytes2 = g_bytes_new ("blahblah", 8);
188   g_assert_cmpint (g_bytes_compare (bytes, bytes2), <, 0);
189   g_bytes_unref (bytes2);
190
191   bytes2 = g_bytes_new ("zyx", 3);
192   g_assert_cmpint (g_bytes_compare (bytes, bytes2), <, 0);
193   g_bytes_unref (bytes2);
194
195   bytes2 = g_bytes_new ("zyxw", 4);
196   g_assert_cmpint (g_bytes_compare (bytes, bytes2), <, 0);
197   g_bytes_unref (bytes2);
198
199   g_bytes_unref (bytes);
200 }
201
202 static void
203 test_to_data_transferred (void)
204 {
205   gconstpointer memory;
206   gpointer data;
207   gsize size;
208   GBytes *bytes;
209
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);
218 }
219
220 static void
221 test_to_data_two_refs (void)
222 {
223   gconstpointer memory;
224   gpointer data;
225   gsize size;
226   GBytes *bytes;
227
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);
241 }
242
243 static void
244 test_to_data_non_malloc (void)
245 {
246   gpointer data;
247   gsize size;
248   GBytes *bytes;
249
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);
258 }
259
260 static void
261 test_to_array_transferred (void)
262 {
263   gconstpointer memory;
264   GByteArray *array;
265   GBytes *bytes;
266
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);
276 }
277
278 static void
279 test_to_array_two_refs (void)
280 {
281   gconstpointer memory;
282   GByteArray *array;
283   GBytes *bytes;
284   gsize size;
285
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);
300 }
301
302 static void
303 test_to_array_non_malloc (void)
304 {
305   GByteArray *array;
306   GBytes *bytes;
307
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);
317 }
318
319 static void
320 test_null (void)
321 {
322   GBytes *bytes;
323   gpointer data;
324   gsize size;
325
326   bytes = g_bytes_new (NULL, 0);
327
328   data = g_bytes_unref_to_data (bytes, &size);
329
330   g_assert (data == NULL);
331   g_assert (size == 0);
332 }
333
334 int
335 main (int argc, char *argv[])
336 {
337   g_test_init (&argc, &argv, NULL);
338
339   g_test_bug_base ("http://bugs.gnome.org/");
340
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);
356
357   return g_test_run ();
358 }