Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / tests / data-output-stream.c
blobb656b27446a26db637dc20ec128b2af4eacb4792
1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc.
3 * Authors: Tomas Bzatek <tbzatek@redhat.com>
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
23 #include <glib/glib.h>
24 #include <gio/gio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #define MAX_LINES 0xFFF
29 #define MAX_LINES_BUFF 0xFFFFFF
30 #define MAX_BYTES_BINARY 0x100
32 static void
33 test_basic (void)
35 GOutputStream *stream;
36 GOutputStream *base_stream;
37 gpointer data;
38 gint val;
40 data = g_malloc0 (MAX_LINES_BUFF);
42 /* initialize objects */
43 base_stream = g_memory_output_stream_new (data, MAX_LINES_BUFF, NULL, NULL);
44 stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
46 g_object_get (stream, "byte-order", &val, NULL);
47 g_assert_cmpint (val, ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
48 g_object_set (stream, "byte-order", G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN, NULL);
49 g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
51 g_object_unref (stream);
52 g_object_unref (base_stream);
53 g_free (data);
56 static void
57 test_read_lines (GDataStreamNewlineType newline_type)
59 GOutputStream *stream;
60 GOutputStream *base_stream;
61 GError *error = NULL;
62 gpointer data;
63 char *lines;
64 int size;
65 int i;
67 #define TEST_STRING "some_text"
69 const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
72 data = g_malloc0 (MAX_LINES_BUFF);
73 lines = g_malloc0 ((strlen (TEST_STRING) + strlen (endl[newline_type])) * MAX_LINES + 1);
75 /* initialize objects */
76 base_stream = g_memory_output_stream_new (data, MAX_LINES_BUFF, NULL, NULL);
77 stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
80 /* fill data */
81 for (i = 0; i < MAX_LINES; i++)
83 gboolean res;
84 char *s = g_strconcat (TEST_STRING, endl[newline_type], NULL);
85 res = g_data_output_stream_put_string (G_DATA_OUTPUT_STREAM (stream), s, NULL, &error);
86 g_stpcpy ((char*)(lines + i*strlen(s)), s);
87 g_assert_no_error (error);
88 g_assert (res == TRUE);
89 g_free (s);
92 /* Byte order testing */
93 g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
94 g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
95 g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
96 g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
98 /* compare data */
99 size = strlen (data);
100 g_assert_cmpint (size, <, MAX_LINES_BUFF);
101 g_assert_cmpstr ((char*)data, ==, lines);
103 g_object_unref (base_stream);
104 g_object_unref (stream);
105 g_free (data);
106 g_free (lines);
109 static void
110 test_read_lines_LF (void)
112 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
115 static void
116 test_read_lines_CR (void)
118 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
121 static void
122 test_read_lines_CR_LF (void)
124 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
127 enum TestDataType {
128 TEST_DATA_BYTE = 0,
129 TEST_DATA_INT16,
130 TEST_DATA_UINT16,
131 TEST_DATA_INT32,
132 TEST_DATA_UINT32,
133 TEST_DATA_INT64,
134 TEST_DATA_UINT64
137 static void
138 test_data_array (guchar *buffer, gsize len,
139 enum TestDataType data_type, GDataStreamByteOrder byte_order)
141 GOutputStream *stream;
142 GOutputStream *base_stream;
143 guchar *stream_data;
145 GError *error = NULL;
146 guint pos;
147 GDataStreamByteOrder native;
148 gboolean swap;
149 gboolean res;
151 /* create objects */
152 stream_data = g_malloc0 (len);
153 base_stream = g_memory_output_stream_new (stream_data, len, NULL, NULL);
154 stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
155 g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), byte_order);
157 /* Set flag to swap bytes if needed */
158 native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
159 swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
161 /* set len to length of buffer cast to actual type */
162 switch (data_type)
164 case TEST_DATA_BYTE:
165 break;
166 case TEST_DATA_INT16:
167 case TEST_DATA_UINT16:
168 g_assert_cmpint (len % 2, ==, 0);
169 case TEST_DATA_INT32:
170 case TEST_DATA_UINT32:
171 g_assert_cmpint (len % 4, ==, 0);
172 case TEST_DATA_INT64:
173 case TEST_DATA_UINT64:
174 g_assert_cmpint (len % 8, ==, 0);
175 len /= 8;
176 break;
177 default:
178 g_assert_not_reached ();
179 break;
182 /* Write data to the file */
183 for (pos = 0; pos < len; pos++)
185 switch (data_type)
187 case TEST_DATA_BYTE:
188 res = g_data_output_stream_put_byte (G_DATA_OUTPUT_STREAM (stream), buffer[pos], NULL, &error);
189 break;
190 case TEST_DATA_INT16:
191 res = g_data_output_stream_put_int16 (G_DATA_OUTPUT_STREAM (stream), ((gint16 *) buffer)[pos], NULL, &error);
192 break;
193 case TEST_DATA_UINT16:
194 res = g_data_output_stream_put_uint16 (G_DATA_OUTPUT_STREAM (stream), ((guint16 *) buffer)[pos], NULL, &error);
195 break;
196 case TEST_DATA_INT32:
197 res = g_data_output_stream_put_int32 (G_DATA_OUTPUT_STREAM (stream), ((gint32 *) buffer)[pos], NULL, &error);
198 break;
199 case TEST_DATA_UINT32:
200 res = g_data_output_stream_put_uint32 (G_DATA_OUTPUT_STREAM (stream), ((guint32 *) buffer)[pos], NULL, &error);
201 break;
202 case TEST_DATA_INT64:
203 res = g_data_output_stream_put_int64 (G_DATA_OUTPUT_STREAM (stream), ((gint64 *) buffer)[pos], NULL, &error);
204 break;
205 case TEST_DATA_UINT64:
206 res = g_data_output_stream_put_uint64 (G_DATA_OUTPUT_STREAM (stream), ((guint64 *) buffer)[pos], NULL, &error);
207 break;
208 default:
209 g_assert_not_reached ();
210 break;
212 g_assert_no_error (error);
213 g_assert_cmpint (res, ==, TRUE);
216 /* Compare data back */
217 for (pos = 0; pos < len; pos++)
219 switch (data_type)
221 case TEST_DATA_BYTE:
222 /* swapping unnecessary */
223 g_assert_cmpint (buffer[pos], ==, stream_data[pos]);
224 break;
225 case TEST_DATA_UINT16:
226 if (swap)
227 g_assert_cmpint (GUINT16_SWAP_LE_BE (((guint16 *) buffer)[pos]), ==, ((guint16 *) stream_data)[pos]);
228 else
229 g_assert_cmpint (((guint16 *) buffer)[pos], ==, ((guint16 *) stream_data)[pos]);
230 break;
231 case TEST_DATA_INT16:
232 if (swap)
233 g_assert_cmpint ((gint16) GUINT16_SWAP_LE_BE (((gint16 *) buffer)[pos]), ==, ((gint16 *) stream_data)[pos]);
234 else
235 g_assert_cmpint (((gint16 *) buffer)[pos], ==, ((gint16 *) stream_data)[pos]);
236 break;
237 case TEST_DATA_UINT32:
238 if (swap)
239 g_assert_cmpint (GUINT32_SWAP_LE_BE (((guint32 *) buffer)[pos]), ==, ((guint32 *) stream_data)[pos]);
240 else
241 g_assert_cmpint (((guint32 *) buffer)[pos], ==, ((guint32 *) stream_data)[pos]);
242 break;
243 case TEST_DATA_INT32:
244 if (swap)
245 g_assert_cmpint ((gint32) GUINT32_SWAP_LE_BE (((gint32 *) buffer)[pos]), ==, ((gint32 *) stream_data)[pos]);
246 else
247 g_assert_cmpint (((gint32 *) buffer)[pos], ==, ((gint32 *) stream_data)[pos]);
248 break;
249 case TEST_DATA_UINT64:
250 if (swap)
251 g_assert_cmpint (GUINT64_SWAP_LE_BE (((guint64 *) buffer)[pos]), ==, ((guint64 *) stream_data)[pos]);
252 else
253 g_assert_cmpint (((guint64 *) buffer)[pos], ==, ((guint64 *) stream_data)[pos]);
254 break;
255 case TEST_DATA_INT64:
256 if (swap)
257 g_assert_cmpint ((gint64) GUINT64_SWAP_LE_BE (((gint64 *) buffer)[pos]), ==, ((gint64 *) stream_data)[pos]);
258 else
259 g_assert_cmpint (((gint64 *) buffer)[pos], ==, ((gint64 *) stream_data)[pos]);
260 break;
261 default:
262 g_assert_not_reached ();
263 break;
267 g_object_unref (base_stream);
268 g_object_unref (stream);
269 g_free (stream_data);
272 static void
273 test_read_int (void)
275 GRand *randomizer;
276 gpointer buffer;
277 int i;
279 randomizer = g_rand_new ();
280 buffer = g_malloc0(MAX_BYTES_BINARY);
282 /* Fill in some random data */
283 for (i = 0; i < MAX_BYTES_BINARY; i++)
285 guchar x = 0;
286 while (! x) x = (guchar)g_rand_int (randomizer);
287 *(guchar*)((guchar*)buffer + sizeof (guchar) * i) = x;
290 for (i = 0; i < 3; i++)
292 int j;
293 for (j = 0; j <= TEST_DATA_UINT64; j++)
294 test_data_array (buffer, MAX_BYTES_BINARY, j, i);
297 g_rand_free (randomizer);
298 g_free (buffer);
301 static void
302 test_seek (void)
304 GDataOutputStream *stream;
305 GMemoryOutputStream *base_stream;
306 GSeekable *seekable;
307 GError *error;
308 guchar *stream_data;
309 gsize len;
310 gboolean res;
312 len = 8;
314 /* create objects */
315 stream_data = g_malloc0 (len);
316 base_stream = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (stream_data, len, NULL, NULL));
317 stream = g_data_output_stream_new (G_OUTPUT_STREAM (base_stream));
318 g_data_output_stream_set_byte_order (stream, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
319 seekable = G_SEEKABLE (stream);
320 g_assert (!g_seekable_can_truncate (seekable));
321 error = NULL;
323 /* Write */
324 g_assert_cmpint (g_seekable_tell (seekable), ==, 0);
325 res = g_data_output_stream_put_uint16 (stream, 0x0123, NULL, &error);
326 g_assert_no_error (error);
327 g_assert (res);
328 g_data_output_stream_put_uint16 (stream, 0x4567, NULL, NULL);
329 g_assert_cmpint (g_seekable_tell (seekable), ==, 4);
330 g_assert_cmpint (stream_data[0], ==, 0x01);
331 g_assert_cmpint (stream_data[1], ==, 0x23);
332 g_assert_cmpint (stream_data[2], ==, 0x45);
333 g_assert_cmpint (stream_data[3], ==, 0x67);
334 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
336 /* Forward relative seek */
337 res = g_seekable_seek (seekable, 2, G_SEEK_CUR, NULL, &error);
338 g_assert_no_error (error);
339 g_assert (res);
340 g_assert_cmpint (g_seekable_tell (seekable), ==, 6);
341 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
342 res = g_data_output_stream_put_uint16 (stream, 0x89AB, NULL, &error);
343 g_assert (res);
344 g_assert_cmpint (g_seekable_tell (seekable), ==, 8);
345 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
346 g_assert_cmpint (stream_data[0], ==, 0x01);
347 g_assert_cmpint (stream_data[1], ==, 0x23);
348 g_assert_cmpint (stream_data[2], ==, 0x45);
349 g_assert_cmpint (stream_data[3], ==, 0x67);
350 g_assert_cmpint (stream_data[4], ==, 0x00);
351 g_assert_cmpint (stream_data[5], ==, 0x00);
352 g_assert_cmpint (stream_data[6], ==, 0x89);
353 g_assert_cmpint (stream_data[7], ==, 0xAB);
355 /* Backward relative seek */
356 res = g_seekable_seek (seekable, -3, G_SEEK_CUR, NULL, &error);
357 g_assert_no_error (error);
358 g_assert (res);
359 g_assert_cmpint (g_seekable_tell (seekable), ==, 5);
360 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
361 res = g_data_output_stream_put_uint16 (stream, 0xCDEF, NULL, &error);
362 g_assert_no_error (error);
363 g_assert (res);
364 g_assert_cmpint (g_seekable_tell (seekable), ==, 7);
365 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
366 g_assert_cmpint (stream_data[0], ==, 0x01);
367 g_assert_cmpint (stream_data[1], ==, 0x23);
368 g_assert_cmpint (stream_data[2], ==, 0x45);
369 g_assert_cmpint (stream_data[3], ==, 0x67);
370 g_assert_cmpint (stream_data[4], ==, 0x00);
371 g_assert_cmpint (stream_data[5], ==, 0xCD);
372 g_assert_cmpint (stream_data[6], ==, 0xEF);
373 g_assert_cmpint (stream_data[7], ==, 0xAB);
375 /* From start */
376 res = g_seekable_seek (seekable, 4, G_SEEK_SET, NULL, &error);
377 g_assert_no_error (error);
378 g_assert (res);
379 g_assert_cmpint (g_seekable_tell (seekable), ==, 4);
380 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
381 res = g_data_output_stream_put_uint16 (stream, 0xFEDC, NULL, &error);
382 g_assert_no_error (error);
383 g_assert (res);
384 g_assert_cmpint (g_seekable_tell (seekable), ==, 6);
385 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
386 g_assert_cmpint (stream_data[0], ==, 0x01);
387 g_assert_cmpint (stream_data[1], ==, 0x23);
388 g_assert_cmpint (stream_data[2], ==, 0x45);
389 g_assert_cmpint (stream_data[3], ==, 0x67);
390 g_assert_cmpint (stream_data[4], ==, 0xFE);
391 g_assert_cmpint (stream_data[5], ==, 0xDC);
392 g_assert_cmpint (stream_data[6], ==, 0xEF);
393 g_assert_cmpint (stream_data[7], ==, 0xAB);
395 /* From end */
396 res = g_seekable_seek (seekable, -4, G_SEEK_END, NULL, &error);
397 g_assert_no_error (error);
398 g_assert (res);
399 g_assert_cmpint (g_seekable_tell (seekable), ==, 4);
400 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
401 res = g_data_output_stream_put_uint16 (stream, 0xBA87, NULL, &error);
402 g_assert_no_error (error);
403 g_assert (res);
404 g_assert_cmpint (g_seekable_tell (seekable), ==, 6);
405 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
406 g_assert_cmpint (stream_data[0], ==, 0x01);
407 g_assert_cmpint (stream_data[1], ==, 0x23);
408 g_assert_cmpint (stream_data[2], ==, 0x45);
409 g_assert_cmpint (stream_data[3], ==, 0x67);
410 g_assert_cmpint (stream_data[4], ==, 0xBA);
411 g_assert_cmpint (stream_data[5], ==, 0x87);
412 g_assert_cmpint (stream_data[6], ==, 0xEF);
413 g_assert_cmpint (stream_data[7], ==, 0xAB);
415 g_object_unref (stream);
416 g_object_unref (base_stream);
417 g_free (stream_data);
420 static void
421 test_truncate (void)
423 GDataOutputStream *stream;
424 GMemoryOutputStream *base_stream;
425 GSeekable *seekable;
426 GError *error;
427 guchar *stream_data;
428 gsize len;
429 gboolean res;
431 len = 8;
433 /* Create objects */
434 stream_data = g_malloc0 (len);
435 base_stream = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (stream_data, len, g_realloc, g_free));
436 stream = g_data_output_stream_new (G_OUTPUT_STREAM (base_stream));
437 g_data_output_stream_set_byte_order (stream, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
438 seekable = G_SEEKABLE (stream);
439 error = NULL;
440 g_assert (g_seekable_can_truncate (seekable));
442 /* Write */
443 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, len);
444 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 0);
445 res = g_data_output_stream_put_uint16 (stream, 0x0123, NULL, &error);
446 g_assert_no_error (error);
447 g_assert (res);
448 res = g_data_output_stream_put_uint16 (stream, 0x4567, NULL, NULL);
449 g_assert_no_error (error);
450 g_assert (res);
451 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, len);
452 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
453 stream_data = g_memory_output_stream_get_data (base_stream);
454 g_assert_cmpint (stream_data[0], ==, 0x01);
455 g_assert_cmpint (stream_data[1], ==, 0x23);
456 g_assert_cmpint (stream_data[2], ==, 0x45);
457 g_assert_cmpint (stream_data[3], ==, 0x67);
459 /* Truncate at position */
460 res = g_seekable_truncate (seekable, 4, NULL, &error);
461 g_assert_no_error (error);
462 g_assert (res);
463 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 4);
464 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
465 stream_data = g_memory_output_stream_get_data (base_stream);
466 g_assert_cmpint (stream_data[0], ==, 0x01);
467 g_assert_cmpint (stream_data[1], ==, 0x23);
468 g_assert_cmpint (stream_data[2], ==, 0x45);
469 g_assert_cmpint (stream_data[3], ==, 0x67);
471 /* Truncate beyond position */
472 res = g_seekable_truncate (seekable, 6, NULL, &error);
473 g_assert_no_error (error);
474 g_assert (res);
475 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 6);
476 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 6);
477 stream_data = g_memory_output_stream_get_data (base_stream);
478 g_assert_cmpint (stream_data[0], ==, 0x01);
479 g_assert_cmpint (stream_data[1], ==, 0x23);
480 g_assert_cmpint (stream_data[2], ==, 0x45);
481 g_assert_cmpint (stream_data[3], ==, 0x67);
483 /* Truncate before position */
484 res = g_seekable_truncate (seekable, 2, NULL, &error);
485 g_assert_no_error (error);
486 g_assert (res);
487 g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 2);
488 g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 2);
489 stream_data = g_memory_output_stream_get_data (base_stream);
490 g_assert_cmpint (stream_data[0], ==, 0x01);
491 g_assert_cmpint (stream_data[1], ==, 0x23);
493 g_object_unref (stream);
494 g_object_unref (base_stream);
498 main (int argc,
499 char *argv[])
501 g_test_init (&argc, &argv, NULL);
503 g_test_add_func ("/data-output-stream/basic", test_basic);
504 g_test_add_func ("/data-output-stream/write-lines-LF", test_read_lines_LF);
505 g_test_add_func ("/data-output-stream/write-lines-CR", test_read_lines_CR);
506 g_test_add_func ("/data-output-stream/write-lines-CR-LF", test_read_lines_CR_LF);
507 g_test_add_func ("/data-output-stream/write-int", test_read_int);
508 g_test_add_func ("/data-output-stream/seek", test_seek);
509 g_test_add_func ("/data-output-stream/truncate", test_truncate);
511 return g_test_run();