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_BYTES 0x10000
35 GInputStream
*base_stream
;
38 base_stream
= g_memory_input_stream_new ();
39 stream
= G_INPUT_STREAM (g_data_input_stream_new (base_stream
));
41 g_object_get (stream
, "byte-order", &val
, NULL
);
42 g_assert_cmpint (val
, ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
);
43 g_object_set (stream
, "byte-order", G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
, NULL
);
44 g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream
)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
);
46 g_object_get (stream
, "newline-type", &val
, NULL
);
47 g_assert_cmpint (val
, ==, G_DATA_STREAM_NEWLINE_TYPE_LF
);
48 g_object_set (stream
, "newline-type", G_DATA_STREAM_NEWLINE_TYPE_CR_LF
, NULL
);
49 g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream
)), ==, G_DATA_STREAM_NEWLINE_TYPE_CR_LF
);
51 g_object_unref (stream
);
52 g_object_unref (base_stream
);
56 test_seek_to_start (GInputStream
*stream
)
59 gboolean res
= g_seekable_seek (G_SEEKABLE (stream
), 0, G_SEEK_SET
, NULL
, &error
);
60 g_assert_cmpint (res
, ==, TRUE
);
61 g_assert_no_error (error
);
65 test_read_lines (GDataStreamNewlineType newline_type
)
68 GInputStream
*base_stream
;
72 const char* lines
[MAX_LINES
];
73 const char* endl
[4] = {"\n", "\r", "\r\n", "\n"};
77 for (i
= 0; i
< MAX_LINES
; i
++)
78 lines
[i
] = "some_text";
80 base_stream
= g_memory_input_stream_new ();
81 g_assert (base_stream
!= NULL
);
82 stream
= G_INPUT_STREAM (g_data_input_stream_new (base_stream
));
83 g_assert(stream
!= NULL
);
85 /* Byte order testing */
86 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream
), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
);
87 g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream
)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
);
88 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream
), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
);
89 g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream
)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
);
91 /* Line ends testing */
92 g_data_input_stream_set_newline_type (G_DATA_INPUT_STREAM (stream
), newline_type
);
93 g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream
)), ==, newline_type
);
97 for (i
= 0; i
< MAX_LINES
; i
++)
98 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream
),
99 g_strconcat (lines
[i
], endl
[newline_type
], NULL
), -1, g_free
);
101 /* Seek to the start */
102 test_seek_to_start (base_stream
);
111 data
= g_data_input_stream_read_line (G_DATA_INPUT_STREAM (stream
), &length
, NULL
, &error
);
114 g_assert_cmpstr (data
, ==, lines
[line
]);
116 g_assert_no_error (error
);
120 g_error_free (error
);
122 g_assert_cmpint (line
, ==, MAX_LINES
);
125 g_object_unref (base_stream
);
126 g_object_unref (stream
);
130 test_read_lines_LF (void)
132 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF
);
136 test_read_lines_CR (void)
138 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR
);
142 test_read_lines_CR_LF (void)
144 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF
);
148 test_read_lines_any (void)
150 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_ANY
);
154 test_read_lines_LF_valid_utf8 (void)
156 GInputStream
*stream
;
157 GInputStream
*base_stream
;
158 GError
*error
= NULL
;
162 base_stream
= g_memory_input_stream_new ();
163 stream
= G_INPUT_STREAM (g_data_input_stream_new (base_stream
));
165 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream
),
166 "foo\nthis is valid UTF-8 ☺!\nbar\n", -1, NULL
);
173 line
= g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream
), &length
, NULL
, &error
);
174 g_assert_no_error (error
);
180 g_assert_cmpint (n_lines
, ==, 3);
182 g_object_unref (base_stream
);
183 g_object_unref (stream
);
187 test_read_lines_LF_invalid_utf8 (void)
189 GInputStream
*stream
;
190 GInputStream
*base_stream
;
191 GError
*error
= NULL
;
195 base_stream
= g_memory_input_stream_new ();
196 stream
= G_INPUT_STREAM (g_data_input_stream_new (base_stream
));
198 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream
),
199 "foo\nthis is not valid UTF-8 \xE5 =(\nbar\n", -1, NULL
);
206 line
= g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream
), &length
, NULL
, &error
);
208 g_assert_no_error (error
);
211 g_assert (error
!= NULL
);
212 g_clear_error (&error
);
219 g_assert_cmpint (n_lines
, ==, 1);
221 g_object_unref (base_stream
);
222 g_object_unref (stream
);
226 test_read_until (void)
228 GInputStream
*stream
;
229 GInputStream
*base_stream
;
230 GError
*error
= NULL
;
235 #define REPEATS 10 /* number of rounds */
236 #define DATA_STRING " part1 # part2 $ part3 % part4 ^"
237 #define DATA_PART_LEN 7 /* number of characters between separators */
238 #define DATA_SEP "#$%^"
239 #define DATA_SEP_LEN 4
240 const int DATA_PARTS_NUM
= DATA_SEP_LEN
* REPEATS
;
242 base_stream
= g_memory_input_stream_new ();
243 stream
= G_INPUT_STREAM (g_data_input_stream_new (base_stream
));
245 for (i
= 0; i
< REPEATS
; i
++)
246 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream
), DATA_STRING
, -1, NULL
);
248 /* Test stop characters */
255 data
= g_data_input_stream_read_until (G_DATA_INPUT_STREAM (stream
), DATA_SEP
, &length
, NULL
, &error
);
258 g_assert_cmpint (strlen (data
), ==, DATA_PART_LEN
);
260 g_assert_no_error (error
);
264 g_assert_no_error (error
);
265 g_assert_cmpint (line
, ==, DATA_PARTS_NUM
);
267 g_object_unref (base_stream
);
268 g_object_unref (stream
);
272 test_read_upto (void)
274 GInputStream
*stream
;
275 GInputStream
*base_stream
;
276 GError
*error
= NULL
;
287 #define REPEATS 10 /* number of rounds */
288 #define DATA_STRING " part1 # part2 $ part3 \0 part4 ^"
289 #define DATA_PART_LEN 7 /* number of characters between separators */
290 #define DATA_SEP "#$\0^"
291 #define DATA_SEP_LEN 4
292 const int DATA_PARTS_NUM
= DATA_SEP_LEN
* REPEATS
;
294 base_stream
= g_memory_input_stream_new ();
295 stream
= G_INPUT_STREAM (g_data_input_stream_new (base_stream
));
297 for (i
= 0; i
< REPEATS
; i
++)
298 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream
), DATA_STRING
, 32, NULL
);
300 /* Test stop characters */
307 data
= g_data_input_stream_read_upto (G_DATA_INPUT_STREAM (stream
), DATA_SEP
, DATA_SEP_LEN
, &length
, NULL
, &error
);
310 g_assert_cmpint (strlen (data
), ==, DATA_PART_LEN
);
311 g_assert_no_error (error
);
314 stop_char
= g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream
), NULL
, &error
);
315 g_assert (memchr (DATA_SEP
, stop_char
, DATA_SEP_LEN
) != NULL
);
316 g_assert_no_error (error
);
320 g_assert_no_error (error
);
321 g_assert_cmpint (line
, ==, DATA_PARTS_NUM
);
323 g_object_unref (base_stream
);
324 g_object_unref (stream
);
336 #define TEST_DATA_RETYPE_BUFF(a, t, v) \
337 (a == TEST_DATA_BYTE ? (t) *(guchar*)v : \
338 (a == TEST_DATA_INT16 ? (t) *(gint16*)v : \
339 (a == TEST_DATA_UINT16 ? (t) *(guint16*)v : \
340 (a == TEST_DATA_INT32 ? (t) *(gint32*)v : \
341 (a == TEST_DATA_UINT32 ? (t) *(guint32*)v : \
342 (a == TEST_DATA_INT64 ? (t) *(gint64*)v : \
343 (t) *(guint64*)v ))))))
347 test_data_array (GInputStream
*stream
, GInputStream
*base_stream
,
348 gpointer buffer
, int len
,
349 enum TestDataType data_type
, GDataStreamByteOrder byte_order
)
351 GError
*error
= NULL
;
355 GDataStreamByteOrder native
;
359 test_seek_to_start (base_stream
);
361 /* Set correct data size */
367 case TEST_DATA_INT16
:
368 case TEST_DATA_UINT16
:
371 case TEST_DATA_INT32
:
372 case TEST_DATA_UINT32
:
375 case TEST_DATA_INT64
:
376 case TEST_DATA_UINT64
:
380 g_assert_not_reached ();
384 /* Set flag to swap bytes if needed */
385 native
= (G_BYTE_ORDER
== G_BIG_ENDIAN
) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
: G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN
;
386 swap
= (byte_order
!= G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN
) && (byte_order
!= native
);
394 data
= g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream
), NULL
, &error
);
396 case TEST_DATA_INT16
:
397 data
= g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream
), NULL
, &error
);
399 data
= (gint16
)GUINT16_SWAP_LE_BE((gint16
)data
);
401 case TEST_DATA_UINT16
:
402 data
= g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream
), NULL
, &error
);
404 data
= (guint16
)GUINT16_SWAP_LE_BE((guint16
)data
);
406 case TEST_DATA_INT32
:
407 data
= g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream
), NULL
, &error
);
409 data
= (gint32
)GUINT32_SWAP_LE_BE((gint32
)data
);
411 case TEST_DATA_UINT32
:
412 data
= g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream
), NULL
, &error
);
414 data
= (guint32
)GUINT32_SWAP_LE_BE((guint32
)data
);
416 case TEST_DATA_INT64
:
417 data
= g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream
), NULL
, &error
);
419 data
= (gint64
)GUINT64_SWAP_LE_BE((gint64
)data
);
421 case TEST_DATA_UINT64
:
422 data
= g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream
), NULL
, &error
);
424 data
= (guint64
)GUINT64_SWAP_LE_BE((guint64
)data
);
427 g_assert_not_reached ();
431 g_assert_cmpint (data
, ==, TEST_DATA_RETYPE_BUFF(data_type
, gint64
, ((guchar
*)buffer
+ pos
)));
436 g_assert_no_error (error
);
438 g_error_free (error
);
439 g_assert_cmpint (pos
- data_size
, ==, len
);
445 GInputStream
*stream
;
446 GInputStream
*base_stream
;
451 randomizer
= g_rand_new ();
452 buffer
= g_malloc0 (MAX_BYTES
);
454 /* Fill in some random data */
455 for (i
= 0; i
< MAX_BYTES
; i
++)
459 x
= (guchar
)g_rand_int (randomizer
);
460 *(guchar
*)((guchar
*)buffer
+ sizeof(guchar
) * i
) = x
;
463 base_stream
= g_memory_input_stream_new ();
464 stream
= G_INPUT_STREAM (g_data_input_stream_new (base_stream
));
465 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream
), buffer
, MAX_BYTES
, NULL
);
468 for (i
= 0; i
< 3; i
++)
471 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream
), i
);
473 for (j
= 0; j
<= TEST_DATA_UINT64
; j
++)
474 test_data_array (stream
, base_stream
, buffer
, MAX_BYTES
, j
, i
);
477 g_object_unref (base_stream
);
478 g_object_unref (stream
);
479 g_rand_free (randomizer
);
488 g_test_init (&argc
, &argv
, NULL
);
490 g_test_add_func ("/data-input-stream/basic", test_basic
);
491 g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF
);
492 g_test_add_func ("/data-input-stream/read-lines-LF-valid-utf8", test_read_lines_LF_valid_utf8
);
493 g_test_add_func ("/data-input-stream/read-lines-LF-invalid-utf8", test_read_lines_LF_invalid_utf8
);
494 g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR
);
495 g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF
);
496 g_test_add_func ("/data-input-stream/read-lines-any", test_read_lines_any
);
497 g_test_add_func ("/data-input-stream/read-until", test_read_until
);
498 g_test_add_func ("/data-input-stream/read-upto", test_read_upto
);
499 g_test_add_func ("/data-input-stream/read-int", test_read_int
);