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>
28 #define MAX_LINES 0xFFF
29 #define MAX_LINES_BUFF 0xFFFFFF
30 #define MAX_BYTES_BINARY 0x100
35 GOutputStream
*stream
;
36 GOutputStream
*base_stream
;
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
);
57 test_read_lines (GDataStreamNewlineType newline_type
)
59 GOutputStream
*stream
;
60 GOutputStream
*base_stream
;
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
));
81 for (i
= 0; i
< MAX_LINES
; i
++)
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
);
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
);
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
);
110 test_read_lines_LF (void)
112 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF
);
116 test_read_lines_CR (void)
118 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR
);
122 test_read_lines_CR_LF (void)
124 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF
);
138 test_data_array (guchar
*buffer
, gsize len
,
139 enum TestDataType data_type
, GDataStreamByteOrder byte_order
)
141 GOutputStream
*stream
;
142 GOutputStream
*base_stream
;
145 GError
*error
= NULL
;
147 GDataStreamByteOrder native
;
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 */
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);
178 g_assert_not_reached ();
182 /* Write data to the file */
183 for (pos
= 0; pos
< len
; pos
++)
188 res
= g_data_output_stream_put_byte (G_DATA_OUTPUT_STREAM (stream
), buffer
[pos
], NULL
, &error
);
190 case TEST_DATA_INT16
:
191 res
= g_data_output_stream_put_int16 (G_DATA_OUTPUT_STREAM (stream
), ((gint16
*) buffer
)[pos
], NULL
, &error
);
193 case TEST_DATA_UINT16
:
194 res
= g_data_output_stream_put_uint16 (G_DATA_OUTPUT_STREAM (stream
), ((guint16
*) buffer
)[pos
], NULL
, &error
);
196 case TEST_DATA_INT32
:
197 res
= g_data_output_stream_put_int32 (G_DATA_OUTPUT_STREAM (stream
), ((gint32
*) buffer
)[pos
], NULL
, &error
);
199 case TEST_DATA_UINT32
:
200 res
= g_data_output_stream_put_uint32 (G_DATA_OUTPUT_STREAM (stream
), ((guint32
*) buffer
)[pos
], NULL
, &error
);
202 case TEST_DATA_INT64
:
203 res
= g_data_output_stream_put_int64 (G_DATA_OUTPUT_STREAM (stream
), ((gint64
*) buffer
)[pos
], NULL
, &error
);
205 case TEST_DATA_UINT64
:
206 res
= g_data_output_stream_put_uint64 (G_DATA_OUTPUT_STREAM (stream
), ((guint64
*) buffer
)[pos
], NULL
, &error
);
209 g_assert_not_reached ();
212 g_assert_no_error (error
);
213 g_assert_cmpint (res
, ==, TRUE
);
216 /* Compare data back */
217 for (pos
= 0; pos
< len
; pos
++)
222 /* swapping unnecessary */
223 g_assert_cmpint (buffer
[pos
], ==, stream_data
[pos
]);
225 case TEST_DATA_UINT16
:
227 g_assert_cmpint (GUINT16_SWAP_LE_BE (((guint16
*) buffer
)[pos
]), ==, ((guint16
*) stream_data
)[pos
]);
229 g_assert_cmpint (((guint16
*) buffer
)[pos
], ==, ((guint16
*) stream_data
)[pos
]);
231 case TEST_DATA_INT16
:
233 g_assert_cmpint ((gint16
) GUINT16_SWAP_LE_BE (((gint16
*) buffer
)[pos
]), ==, ((gint16
*) stream_data
)[pos
]);
235 g_assert_cmpint (((gint16
*) buffer
)[pos
], ==, ((gint16
*) stream_data
)[pos
]);
237 case TEST_DATA_UINT32
:
239 g_assert_cmpint (GUINT32_SWAP_LE_BE (((guint32
*) buffer
)[pos
]), ==, ((guint32
*) stream_data
)[pos
]);
241 g_assert_cmpint (((guint32
*) buffer
)[pos
], ==, ((guint32
*) stream_data
)[pos
]);
243 case TEST_DATA_INT32
:
245 g_assert_cmpint ((gint32
) GUINT32_SWAP_LE_BE (((gint32
*) buffer
)[pos
]), ==, ((gint32
*) stream_data
)[pos
]);
247 g_assert_cmpint (((gint32
*) buffer
)[pos
], ==, ((gint32
*) stream_data
)[pos
]);
249 case TEST_DATA_UINT64
:
251 g_assert_cmpint (GUINT64_SWAP_LE_BE (((guint64
*) buffer
)[pos
]), ==, ((guint64
*) stream_data
)[pos
]);
253 g_assert_cmpint (((guint64
*) buffer
)[pos
], ==, ((guint64
*) stream_data
)[pos
]);
255 case TEST_DATA_INT64
:
257 g_assert_cmpint ((gint64
) GUINT64_SWAP_LE_BE (((gint64
*) buffer
)[pos
]), ==, ((gint64
*) stream_data
)[pos
]);
259 g_assert_cmpint (((gint64
*) buffer
)[pos
], ==, ((gint64
*) stream_data
)[pos
]);
262 g_assert_not_reached ();
267 g_object_unref (base_stream
);
268 g_object_unref (stream
);
269 g_free (stream_data
);
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
++)
286 while (! x
) x
= (guchar
)g_rand_int (randomizer
);
287 *(guchar
*)((guchar
*)buffer
+ sizeof (guchar
) * i
) = x
;
290 for (i
= 0; i
< 3; i
++)
293 for (j
= 0; j
<= TEST_DATA_UINT64
; j
++)
294 test_data_array (buffer
, MAX_BYTES_BINARY
, j
, i
);
297 g_rand_free (randomizer
);
304 GDataOutputStream
*stream
;
305 GMemoryOutputStream
*base_stream
;
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
));
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
);
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
);
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
);
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
);
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
);
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);
376 res
= g_seekable_seek (seekable
, 4, G_SEEK_SET
, NULL
, &error
);
377 g_assert_no_error (error
);
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
);
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);
396 res
= g_seekable_seek (seekable
, -4, G_SEEK_END
, NULL
, &error
);
397 g_assert_no_error (error
);
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
);
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
);
423 GDataOutputStream
*stream
;
424 GMemoryOutputStream
*base_stream
;
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
);
440 g_assert (g_seekable_can_truncate (seekable
));
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
);
448 res
= g_data_output_stream_put_uint16 (stream
, 0x4567, NULL
, NULL
);
449 g_assert_no_error (error
);
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
);
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
);
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
);
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
);
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
);