1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc.
3 * Authors: Tomas Bzatek <tbzatek@redhat.com>
5 * SPDX-License-Identifier: LicenseRef-old-glib-tests
7 * This work is provided "as is"; redistribution and modification
8 * in whole or in part, in any medium, physical or electronic is
9 * permitted without restriction.
11 * This work is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * In no event shall the authors or contributors be liable for any
16 * direct, indirect, incidental, special, exemplary, or consequential
17 * damages (including, but not limited to, procurement of substitute
18 * goods or services; loss of use, data, or profits; or business
19 * interruption) however caused and on any theory of liability, whether
20 * in contract, strict liability, or tort (including negligence or
21 * otherwise) arising in any way out of the use of this software, even
22 * if advised of the possibility of such damage.
25 #include <glib/glib.h>
30 #define MAX_LINES 0xFFF
31 #define MAX_BYTES 0x10000
37 GInputStream *base_stream;
40 base_stream = g_memory_input_stream_new ();
41 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
43 g_object_get (stream, "byte-order", &val, NULL);
44 g_assert_cmpint (val, ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
45 g_object_set (stream, "byte-order", G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN, NULL);
46 g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
48 g_object_get (stream, "newline-type", &val, NULL);
49 g_assert_cmpint (val, ==, G_DATA_STREAM_NEWLINE_TYPE_LF);
50 g_object_set (stream, "newline-type", G_DATA_STREAM_NEWLINE_TYPE_CR_LF, NULL);
51 g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
53 g_object_unref (stream);
54 g_object_unref (base_stream);
58 test_seek_to_start (GInputStream *stream)
61 gboolean res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
62 g_assert_cmpint (res, ==, TRUE);
63 g_assert_no_error (error);
67 test_read_lines (GDataStreamNewlineType newline_type)
70 GInputStream *base_stream;
74 const char* lines[MAX_LINES];
75 const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
79 for (i = 0; i < MAX_LINES; i++)
80 lines[i] = "some_text";
82 base_stream = g_memory_input_stream_new ();
83 g_assert (base_stream != NULL);
84 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
85 g_assert(stream != NULL);
87 /* Byte order testing */
88 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
89 g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
90 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
91 g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
93 /* Line ends testing */
94 g_data_input_stream_set_newline_type (G_DATA_INPUT_STREAM (stream), newline_type);
95 g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream)), ==, newline_type);
99 for (i = 0; i < MAX_LINES; i++)
100 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
101 g_strconcat (lines[i], endl[newline_type], NULL), -1, g_free);
103 /* Seek to the start */
104 test_seek_to_start (base_stream);
113 data = g_data_input_stream_read_line (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
116 g_assert_cmpstr (data, ==, lines[line]);
118 g_assert_no_error (error);
122 g_error_free (error);
124 g_assert_cmpint (line, ==, MAX_LINES);
127 g_object_unref (base_stream);
128 g_object_unref (stream);
132 test_read_lines_LF (void)
134 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
138 test_read_lines_CR (void)
140 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
144 test_read_lines_CR_LF (void)
146 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
150 test_read_lines_any (void)
152 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_ANY);
156 test_read_lines_LF_valid_utf8 (void)
158 GInputStream *stream;
159 GInputStream *base_stream;
160 GError *error = NULL;
164 base_stream = g_memory_input_stream_new ();
165 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
167 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
168 "foo\nthis is valid UTF-8 ☺!\nbar\n", -1, NULL);
175 line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
176 g_assert_no_error (error);
182 g_assert_cmpint (n_lines, ==, 3);
184 g_object_unref (base_stream);
185 g_object_unref (stream);
189 test_read_lines_LF_invalid_utf8 (void)
191 GInputStream *stream;
192 GInputStream *base_stream;
193 GError *error = NULL;
197 base_stream = g_memory_input_stream_new ();
198 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
200 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
201 "foo\nthis is not valid UTF-8 \xE5 =(\nbar\n", -1, NULL);
208 line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
210 g_assert_no_error (error);
213 g_assert (error != NULL);
214 g_clear_error (&error);
221 g_assert_cmpint (n_lines, ==, 1);
223 g_object_unref (base_stream);
224 g_object_unref (stream);
227 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
230 test_read_until (void)
232 GInputStream *stream;
233 GInputStream *base_stream;
234 GError *error = NULL;
239 #define REPEATS 10 /* number of rounds */
240 #define DATA_STRING " part1 # part2 $ part3 % part4 ^"
241 #define DATA_PART_LEN 7 /* number of characters between separators */
242 #define DATA_SEP "#$%^"
243 #define DATA_SEP_LEN 4
244 const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;
246 base_stream = g_memory_input_stream_new ();
247 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
249 for (i = 0; i < REPEATS; i++)
250 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, -1, NULL);
252 /* Test stop characters */
259 data = g_data_input_stream_read_until (G_DATA_INPUT_STREAM (stream), DATA_SEP, &length, NULL, &error);
262 g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
264 g_assert_no_error (error);
268 g_assert_no_error (error);
269 g_assert_cmpint (line, ==, DATA_PARTS_NUM);
271 g_object_unref (base_stream);
272 g_object_unref (stream);
275 G_GNUC_END_IGNORE_DEPRECATIONS
278 test_read_upto (void)
280 GInputStream *stream;
281 GInputStream *base_stream;
282 GError *error = NULL;
293 #define REPEATS 10 /* number of rounds */
294 #define DATA_STRING " part1 # part2 $ part3 \0 part4 ^"
295 #define DATA_PART_LEN 7 /* number of characters between separators */
296 #define DATA_SEP "#$\0^"
297 #define DATA_SEP_LEN 4
298 const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;
300 base_stream = g_memory_input_stream_new ();
301 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
303 for (i = 0; i < REPEATS; i++)
304 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, 32, NULL);
306 /* Test stop characters */
313 data = g_data_input_stream_read_upto (G_DATA_INPUT_STREAM (stream), DATA_SEP, DATA_SEP_LEN, &length, NULL, &error);
316 g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
317 g_assert_no_error (error);
320 stop_char = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
321 g_assert (memchr (DATA_SEP, stop_char, DATA_SEP_LEN) != NULL);
322 g_assert_no_error (error);
326 g_assert_no_error (error);
327 g_assert_cmpint (line, ==, DATA_PARTS_NUM);
329 g_object_unref (base_stream);
330 g_object_unref (stream);
342 /* The order is reversed to avoid -Wduplicated-branches. */
343 #define TEST_DATA_RETYPE_BUFF(a, t, v) \
344 (a == TEST_DATA_UINT64 ? (t) *(guint64*)v : \
345 (a == TEST_DATA_INT64 ? (t) *(gint64*)v : \
346 (a == TEST_DATA_UINT32 ? (t) *(guint32*)v : \
347 (a == TEST_DATA_INT32 ? (t) *(gint32*)v : \
348 (a == TEST_DATA_UINT16 ? (t) *(guint16*)v : \
349 (a == TEST_DATA_INT16 ? (t) *(gint16*)v : \
350 (t) *(guchar*)v ))))))
354 test_data_array (GInputStream *stream, GInputStream *base_stream,
355 gpointer buffer, int len,
356 enum TestDataType data_type, GDataStreamByteOrder byte_order)
358 GError *error = NULL;
362 GDataStreamByteOrder native;
366 test_seek_to_start (base_stream);
368 /* Set correct data size */
374 case TEST_DATA_INT16:
375 case TEST_DATA_UINT16:
378 case TEST_DATA_INT32:
379 case TEST_DATA_UINT32:
382 case TEST_DATA_INT64:
383 case TEST_DATA_UINT64:
387 g_assert_not_reached ();
391 /* Set flag to swap bytes if needed */
392 native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
393 swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
401 data = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
403 case TEST_DATA_INT16:
404 data = g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
406 data = (gint16)GUINT16_SWAP_LE_BE((gint16)data);
408 case TEST_DATA_UINT16:
409 data = g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
411 data = (guint16)GUINT16_SWAP_LE_BE((guint16)data);
413 case TEST_DATA_INT32:
414 data = g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
416 data = (gint32)GUINT32_SWAP_LE_BE((gint32)data);
418 case TEST_DATA_UINT32:
419 data = g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
421 data = (guint32)GUINT32_SWAP_LE_BE((guint32)data);
423 case TEST_DATA_INT64:
424 data = g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
426 data = (gint64)GUINT64_SWAP_LE_BE((gint64)data);
428 case TEST_DATA_UINT64:
429 data = g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
431 data = (guint64)GUINT64_SWAP_LE_BE((guint64)data);
434 g_assert_not_reached ();
438 g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, gint64, ((guchar*)buffer + pos)));
443 g_assert_no_error (error);
445 g_error_free (error);
446 g_assert_cmpint (pos - data_size, ==, len);
452 GInputStream *stream;
453 GInputStream *base_stream;
458 randomizer = g_rand_new ();
459 buffer = g_malloc0 (MAX_BYTES);
461 /* Fill in some random data */
462 for (i = 0; i < MAX_BYTES; i++)
466 x = (guchar)g_rand_int (randomizer);
467 *(guchar*)((guchar*)buffer + sizeof(guchar) * i) = x;
470 base_stream = g_memory_input_stream_new ();
471 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
472 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), buffer, MAX_BYTES, NULL);
475 for (i = 0; i < 3; i++)
478 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), i);
480 for (j = 0; j <= TEST_DATA_UINT64; j++)
481 test_data_array (stream, base_stream, buffer, MAX_BYTES, j, i);
484 g_object_unref (base_stream);
485 g_object_unref (stream);
486 g_rand_free (randomizer);
495 g_test_init (&argc, &argv, NULL);
497 g_test_add_func ("/data-input-stream/basic", test_basic);
498 g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
499 g_test_add_func ("/data-input-stream/read-lines-LF-valid-utf8", test_read_lines_LF_valid_utf8);
500 g_test_add_func ("/data-input-stream/read-lines-LF-invalid-utf8", test_read_lines_LF_invalid_utf8);
501 g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
502 g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
503 g_test_add_func ("/data-input-stream/read-lines-any", test_read_lines_any);
504 g_test_add_func ("/data-input-stream/read-until", test_read_until);
505 g_test_add_func ("/data-input-stream/read-upto", test_read_upto);
506 g_test_add_func ("/data-input-stream/read-int", test_read_int);