nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 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_cmpmem (data, 4, g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes));
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_bytes_unref (bytes);
88  
89 g_assert_cmpmem (g_bytes_get_data (sub, NULL), g_bytes_get_size (sub), "wave", 4);
90 g_bytes_unref (sub);
91 }
92  
93 static void
94 on_destroy_increment (gpointer data)
95 {
96 gint *count = data;
97 g_assert (count != NULL);
98 (*count)++;
99 }
100  
101 static void
102 test_new_with_free_func (void)
103 {
104 GBytes *bytes;
105 gchar *data;
106 gint count = 0;
107 gsize size;
108  
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);
116  
117 g_bytes_unref (bytes);
118 g_assert_cmpuint (count, ==, 1);
119 }
120  
121 static void
122 test_hash (void)
123 {
124 GBytes *bytes1;
125 GBytes *bytes2;
126 guint hash1;
127 guint hash2;
128  
129 bytes1 = g_bytes_new ("blah", 4);
130 bytes2 = g_bytes_new ("blah", 4);
131  
132 hash1 = g_bytes_hash (bytes1);
133 hash2 = g_bytes_hash (bytes2);
134 g_assert (hash1 == hash2);
135  
136 g_bytes_unref (bytes1);
137 g_bytes_unref (bytes2);
138 }
139  
140 static void
141 test_equal (void)
142 {
143 GBytes *bytes;
144 GBytes *bytes2;
145  
146 bytes = g_bytes_new ("blah", 4);
147  
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);
152  
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);
157  
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);
162  
163 g_bytes_unref (bytes);
164 }
165  
166 static void
167 test_compare (void)
168 {
169 GBytes *bytes;
170 GBytes *bytes2;
171  
172 bytes = g_bytes_new ("blah", 4);
173  
174 bytes2 = g_bytes_new ("blah", 4);
175 g_assert_cmpint (g_bytes_compare (bytes, bytes2), ==, 0);
176 g_bytes_unref (bytes2);
177  
178 bytes2 = g_bytes_new ("bla", 3);
179 g_assert_cmpint (g_bytes_compare (bytes, bytes2), >, 0);
180 g_bytes_unref (bytes2);
181  
182 bytes2 = g_bytes_new ("abcd", 4);
183 g_assert_cmpint (g_bytes_compare (bytes, bytes2), >, 0);
184 g_bytes_unref (bytes2);
185  
186 bytes2 = g_bytes_new ("blahblah", 8);
187 g_assert_cmpint (g_bytes_compare (bytes, bytes2), <, 0);
188 g_bytes_unref (bytes2);
189  
190 bytes2 = g_bytes_new ("zyx", 3);
191 g_assert_cmpint (g_bytes_compare (bytes, bytes2), <, 0);
192 g_bytes_unref (bytes2);
193  
194 bytes2 = g_bytes_new ("zyxw", 4);
195 g_assert_cmpint (g_bytes_compare (bytes, bytes2), <, 0);
196 g_bytes_unref (bytes2);
197  
198 g_bytes_unref (bytes);
199 }
200  
201 static void
202 test_to_data_transferred (void)
203 {
204 gconstpointer memory;
205 gpointer data;
206 gsize size;
207 GBytes *bytes;
208  
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);
216 }
217  
218 static void
219 test_to_data_two_refs (void)
220 {
221 gconstpointer memory;
222 gpointer data;
223 gsize size;
224 GBytes *bytes;
225  
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);
238 }
239  
240 static void
241 test_to_data_non_malloc (void)
242 {
243 gpointer data;
244 gsize size;
245 GBytes *bytes;
246  
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);
254 }
255  
256 static void
257 test_to_array_transferred (void)
258 {
259 gconstpointer memory;
260 GByteArray *array;
261 GBytes *bytes;
262  
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);
271 }
272  
273 static void
274 test_to_array_two_refs (void)
275 {
276 gconstpointer memory;
277 GByteArray *array;
278 GBytes *bytes;
279 gsize size;
280  
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);
294 }
295  
296 static void
297 test_to_array_non_malloc (void)
298 {
299 GByteArray *array;
300 GBytes *bytes;
301  
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);
310 }
311  
312 static void
313 test_null (void)
314 {
315 GBytes *bytes;
316 gpointer data;
317 gsize size;
318  
319 bytes = g_bytes_new (NULL, 0);
320  
321 data = g_bytes_unref_to_data (bytes, &size);
322  
323 g_assert (data == NULL);
324 g_assert (size == 0);
325 }
326  
327 int
328 main (int argc, char *argv[])
329 {
330 g_test_init (&argc, &argv, NULL);
331  
332 g_test_bug_base ("http://bugs.gnome.org/");
333  
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);
349  
350 return g_test_run ();
351 }