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, NULL);
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]);
115 g_assert_no_error (error);
119 g_assert_cmpint (line, ==, MAX_LINES);
122 g_object_unref (base_stream);
123 g_object_unref (stream);
127 test_read_lines_LF (void)
129 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
133 test_read_lines_CR (void)
135 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
139 test_read_lines_CR_LF (void)
141 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
145 test_read_lines_any (void)
147 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_ANY);
151 test_read_until (void)
153 GInputStream *stream;
154 GInputStream *base_stream;
155 GError *error = NULL;
160 #define REPEATS 10 /* number of rounds */
161 #define DATA_STRING " part1 # part2 $ part3 % part4 ^"
162 #define DATA_PART_LEN 7 /* number of characters between separators */
163 #define DATA_SEP "#$%^"
164 const int DATA_PARTS_NUM = strlen (DATA_SEP) * REPEATS;
166 base_stream = g_memory_input_stream_new ();
167 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
169 for (i = 0; i < REPEATS; i++)
170 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, -1, NULL);
172 /* Test stop characters */
179 data = g_data_input_stream_read_until (G_DATA_INPUT_STREAM (stream), DATA_SEP, &length, NULL, &error);
182 g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
183 g_assert_no_error (error);
187 g_assert_no_error (error);
188 g_assert_cmpint (line, ==, DATA_PARTS_NUM);
191 g_object_unref (base_stream);
192 g_object_unref (stream);
205 #define TEST_DATA_RETYPE_BUFF(a, t, v) \
206 (a == TEST_DATA_BYTE ? (t) *(guchar*)v : \
207 (a == TEST_DATA_INT16 ? (t) *(gint16*)v : \
208 (a == TEST_DATA_UINT16 ? (t) *(guint16*)v : \
209 (a == TEST_DATA_INT32 ? (t) *(gint32*)v : \
210 (a == TEST_DATA_UINT32 ? (t) *(guint32*)v : \
211 (a == TEST_DATA_INT64 ? (t) *(gint64*)v : \
212 (t) *(guint64*)v ))))))
216 test_data_array (GInputStream *stream, GInputStream *base_stream,
217 gpointer buffer, int len,
218 enum TestDataType data_type, GDataStreamByteOrder byte_order)
220 GError *error = NULL;
224 GDataStreamByteOrder native;
228 test_seek_to_start (base_stream);
230 /* Set correct data size */
236 case TEST_DATA_INT16:
237 case TEST_DATA_UINT16:
240 case TEST_DATA_INT32:
241 case TEST_DATA_UINT32:
244 case TEST_DATA_INT64:
245 case TEST_DATA_UINT64:
249 g_assert_not_reached ();
253 /* Set flag to swap bytes if needed */
254 native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
255 swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
263 data = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
265 case TEST_DATA_INT16:
266 data = g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
268 data = (gint16)GUINT16_SWAP_LE_BE((gint16)data);
270 case TEST_DATA_UINT16:
271 data = g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
273 data = (guint16)GUINT16_SWAP_LE_BE((guint16)data);
275 case TEST_DATA_INT32:
276 data = g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
278 data = (gint32)GUINT32_SWAP_LE_BE((gint32)data);
280 case TEST_DATA_UINT32:
281 data = g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
283 data = (guint32)GUINT32_SWAP_LE_BE((guint32)data);
285 case TEST_DATA_INT64:
286 data = g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
288 data = (gint64)GUINT64_SWAP_LE_BE((gint64)data);
290 case TEST_DATA_UINT64:
291 data = g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
293 data = (guint64)GUINT64_SWAP_LE_BE((guint64)data);
296 g_assert_not_reached ();
299 if ((data) && (! error))
300 g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, gint64, ((guchar*)buffer + pos)));
305 g_assert_no_error (error);
306 g_assert_cmpint (pos - data_size, ==, len);
312 GInputStream *stream;
313 GInputStream *base_stream;
318 randomizer = g_rand_new ();
319 buffer = g_malloc0 (MAX_BYTES);
321 /* Fill in some random data */
322 for (i = 0; i < MAX_BYTES; i++)
326 x = (guchar)g_rand_int (randomizer);
327 *(guchar*)((guchar*)buffer + sizeof(guchar) * i) = x;
330 base_stream = g_memory_input_stream_new ();
331 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
332 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), buffer, MAX_BYTES, NULL);
335 for (i = 0; i < 3; i++)
338 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), i);
340 for (j = 0; j <= TEST_DATA_UINT64; j++)
341 test_data_array (stream, base_stream, buffer, MAX_BYTES, j, i);
344 g_object_unref (base_stream);
345 g_object_unref (stream);
346 g_rand_free (randomizer);
356 g_test_init (&argc, &argv, NULL);
358 g_test_add_func ("/data-input-stream/basic", test_basic);
359 g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
360 g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
361 g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
362 g_test_add_func ("/data-input-stream/read-lines-any", test_read_lines_any);
363 g_test_add_func ("/data-input-stream/read-until", test_read_until);
364 g_test_add_func ("/data-input-stream/read-int", test_read_int);