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
32 test_seek_to_start (GInputStream *stream)
35 gboolean res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
36 g_assert_cmpint (res, ==, TRUE);
37 g_assert_no_error (error);
41 test_read_lines (GDataStreamNewlineType newline_type)
44 GInputStream *base_stream;
48 const char* lines[MAX_LINES];
49 const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
53 for (i = 0; i < MAX_LINES; i++)
54 lines[i] = "some_text";
56 base_stream = g_memory_input_stream_new ();
57 g_assert (base_stream != NULL);
58 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
59 g_assert(stream != NULL);
61 /* Byte order testing */
62 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
63 g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
64 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
65 g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
67 /* Line ends testing */
68 g_data_input_stream_set_newline_type (G_DATA_INPUT_STREAM (stream), newline_type);
69 g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream)), ==, newline_type);
73 for (i = 0; i < MAX_LINES; i++)
74 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
75 g_strconcat (lines[i], endl[newline_type], NULL), -1, NULL);
77 /* Seek to the start */
78 test_seek_to_start (base_stream);
87 data = g_data_input_stream_read_line (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
92 expected = g_strconcat (lines[line], endl[newline_type], NULL);
93 g_assert_cmpstr (data, ==, expected);
94 g_assert_no_error (error);
99 g_assert_cmpint (line, ==, MAX_LINES);
102 g_object_unref (base_stream);
103 g_object_unref (stream);
107 test_read_lines_LF (void)
109 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
113 test_read_lines_CR (void)
115 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
119 test_read_lines_CR_LF (void)
121 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
126 test_read_until (void)
128 GInputStream *stream;
129 GInputStream *base_stream;
130 GError *error = NULL;
135 #define REPEATS 10 /* number of rounds */
136 #define DATA_STRING " part1 # part2 $ part3 % part4 ^"
137 #define DATA_PART_LEN 7 /* number of characters between separators */
138 #define DATA_SEP "#$%^"
139 const int DATA_PARTS_NUM = strlen (DATA_SEP) * REPEATS;
141 base_stream = g_memory_input_stream_new ();
142 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
144 for (i = 0; i < REPEATS; i++)
145 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, -1, NULL);
147 /* Test stop characters */
154 data = g_data_input_stream_read_until (G_DATA_INPUT_STREAM (stream), DATA_SEP, &length, NULL, &error);
157 g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
158 g_assert_no_error (error);
162 g_assert_no_error (error);
163 g_assert_cmpint (line, ==, DATA_PARTS_NUM);
166 g_object_unref (base_stream);
167 g_object_unref (stream);
180 #define TEST_DATA_RETYPE_BUFF(a, v) \
181 (a == TEST_DATA_BYTE ? *(guchar*)v : \
182 (a == TEST_DATA_INT16 ? *(gint16*)v : \
183 (a == TEST_DATA_UINT16 ? *(guint16*)v : \
184 (a == TEST_DATA_INT32 ? *(gint32*)v : \
185 (a == TEST_DATA_UINT32 ? *(guint32*)v : \
186 (a == TEST_DATA_INT64 ? *(gint64*)v : \
191 test_data_array (GInputStream *stream, GInputStream *base_stream,
192 gpointer buffer, int len,
193 enum TestDataType data_type, GDataStreamByteOrder byte_order)
195 GError *error = NULL;
199 GDataStreamByteOrder native;
203 test_seek_to_start (base_stream);
205 /* Set correct data size */
211 case TEST_DATA_INT16:
212 case TEST_DATA_UINT16:
215 case TEST_DATA_INT32:
216 case TEST_DATA_UINT32:
219 case TEST_DATA_INT64:
220 case TEST_DATA_UINT64:
225 /* Set flag to swap bytes if needed */
226 native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
227 swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
235 data = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
237 case TEST_DATA_INT16:
238 data = g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
240 data = (gint16)GUINT16_SWAP_LE_BE((gint16)data);
242 case TEST_DATA_UINT16:
243 data = g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
245 data = (guint16)GUINT16_SWAP_LE_BE((guint16)data);
247 case TEST_DATA_INT32:
248 data = g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
250 data = (gint32)GUINT32_SWAP_LE_BE((gint32)data);
252 case TEST_DATA_UINT32:
253 data = g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
255 data = (guint32)GUINT32_SWAP_LE_BE((guint32)data);
257 case TEST_DATA_INT64:
258 data = g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
260 data = (gint64)GUINT64_SWAP_LE_BE((gint64)data);
262 case TEST_DATA_UINT64:
263 data = g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
265 data = (guint64)GUINT64_SWAP_LE_BE((guint64)data);
268 if ((data) && (! error))
269 g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, ((guchar*)buffer + pos)));
274 g_assert_no_error (error);
275 g_assert_cmpint (pos - data_size, ==, len);
281 GInputStream *stream;
282 GInputStream *base_stream;
287 rand = g_rand_new ();
288 buffer = g_malloc0 (MAX_BYTES);
290 /* Fill in some random data */
291 for (i = 0; i < MAX_BYTES; i++)
295 x = (guchar)g_rand_int (rand);
296 *(guchar*)((guchar*)buffer + sizeof(guchar) * i) = x;
299 base_stream = g_memory_input_stream_new ();
300 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
301 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), buffer, MAX_BYTES, NULL);
304 for (i = 0; i < 3; i++)
307 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), i);
309 for (j = 0; j <= TEST_DATA_UINT64; j++)
310 test_data_array (stream, base_stream, buffer, MAX_BYTES, j, i);
313 g_object_unref (base_stream);
314 g_object_unref (stream);
325 g_test_init (&argc, &argv, NULL);
327 g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
328 g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
329 g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
330 g_test_add_func ("/data-input-stream/read-until", test_read_until);
331 g_test_add_func ("/data-input-stream/read-int", test_read_int);