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);
90 g_assert_cmpstr (data, ==, lines[line]);
91 g_assert_no_error (error);
95 g_assert_cmpint (line, ==, MAX_LINES);
98 g_object_unref (base_stream);
99 g_object_unref (stream);
103 test_read_lines_LF (void)
105 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
109 test_read_lines_CR (void)
111 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
115 test_read_lines_CR_LF (void)
117 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
122 test_read_until (void)
124 GInputStream *stream;
125 GInputStream *base_stream;
126 GError *error = NULL;
131 #define REPEATS 10 /* number of rounds */
132 #define DATA_STRING " part1 # part2 $ part3 % part4 ^"
133 #define DATA_PART_LEN 7 /* number of characters between separators */
134 #define DATA_SEP "#$%^"
135 const int DATA_PARTS_NUM = strlen (DATA_SEP) * REPEATS;
137 base_stream = g_memory_input_stream_new ();
138 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
140 for (i = 0; i < REPEATS; i++)
141 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, -1, NULL);
143 /* Test stop characters */
150 data = g_data_input_stream_read_until (G_DATA_INPUT_STREAM (stream), DATA_SEP, &length, NULL, &error);
153 g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
154 g_assert_no_error (error);
158 g_assert_no_error (error);
159 g_assert_cmpint (line, ==, DATA_PARTS_NUM);
162 g_object_unref (base_stream);
163 g_object_unref (stream);
176 #define TEST_DATA_RETYPE_BUFF(a, t, v) \
177 (a == TEST_DATA_BYTE ? (t) *(guchar*)v : \
178 (a == TEST_DATA_INT16 ? (t) *(gint16*)v : \
179 (a == TEST_DATA_UINT16 ? (t) *(guint16*)v : \
180 (a == TEST_DATA_INT32 ? (t) *(gint32*)v : \
181 (a == TEST_DATA_UINT32 ? (t) *(guint32*)v : \
182 (a == TEST_DATA_INT64 ? (t) *(gint64*)v : \
183 (t) *(guint64*)v ))))))
187 test_data_array (GInputStream *stream, GInputStream *base_stream,
188 gpointer buffer, int len,
189 enum TestDataType data_type, GDataStreamByteOrder byte_order)
191 GError *error = NULL;
195 GDataStreamByteOrder native;
199 test_seek_to_start (base_stream);
201 /* Set correct data size */
207 case TEST_DATA_INT16:
208 case TEST_DATA_UINT16:
211 case TEST_DATA_INT32:
212 case TEST_DATA_UINT32:
215 case TEST_DATA_INT64:
216 case TEST_DATA_UINT64:
220 g_assert_not_reached ();
224 /* Set flag to swap bytes if needed */
225 native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
226 swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
234 data = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
236 case TEST_DATA_INT16:
237 data = g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
239 data = (gint16)GUINT16_SWAP_LE_BE((gint16)data);
241 case TEST_DATA_UINT16:
242 data = g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
244 data = (guint16)GUINT16_SWAP_LE_BE((guint16)data);
246 case TEST_DATA_INT32:
247 data = g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
249 data = (gint32)GUINT32_SWAP_LE_BE((gint32)data);
251 case TEST_DATA_UINT32:
252 data = g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
254 data = (guint32)GUINT32_SWAP_LE_BE((guint32)data);
256 case TEST_DATA_INT64:
257 data = g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
259 data = (gint64)GUINT64_SWAP_LE_BE((gint64)data);
261 case TEST_DATA_UINT64:
262 data = g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
264 data = (guint64)GUINT64_SWAP_LE_BE((guint64)data);
267 g_assert_not_reached ();
270 if ((data) && (! error))
271 g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, gint64, ((guchar*)buffer + pos)));
276 g_assert_no_error (error);
277 g_assert_cmpint (pos - data_size, ==, len);
283 GInputStream *stream;
284 GInputStream *base_stream;
289 randomizer = g_rand_new ();
290 buffer = g_malloc0 (MAX_BYTES);
292 /* Fill in some random data */
293 for (i = 0; i < MAX_BYTES; i++)
297 x = (guchar)g_rand_int (randomizer);
298 *(guchar*)((guchar*)buffer + sizeof(guchar) * i) = x;
301 base_stream = g_memory_input_stream_new ();
302 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
303 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), buffer, MAX_BYTES, NULL);
306 for (i = 0; i < 3; i++)
309 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), i);
311 for (j = 0; j <= TEST_DATA_UINT64; j++)
312 test_data_array (stream, base_stream, buffer, MAX_BYTES, j, i);
315 g_object_unref (base_stream);
316 g_object_unref (stream);
317 g_rand_free (randomizer);
327 g_test_init (&argc, &argv, NULL);
329 g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
330 g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
331 g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
332 g_test_add_func ("/data-input-stream/read-until", test_read_until);
333 g_test_add_func ("/data-input-stream/read-int", test_read_int);