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.
22 #include <glib/gtestutils.h>
23 #include <glib/glib.h>
28 #define MAX_LINES 0xFFF
29 #define MAX_LINES_BUFF 0xFFFFFF
30 #define MAX_BYTES_BINARY 0x100
33 test_read_lines (GDataStreamNewlineType newline_type)
35 GOutputStream *stream;
36 GOutputStream *base_stream;
42 #define TEST_STRING "some_text"
44 const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
47 data = g_malloc0 (MAX_LINES_BUFF);
48 lines = g_malloc0 ((strlen (TEST_STRING) + strlen (endl[newline_type])) * MAX_LINES + 1);
50 /* initialize objects */
51 base_stream = g_memory_output_stream_new (data, MAX_LINES_BUFF, NULL, NULL);
52 stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
56 for (i = 0; i < MAX_LINES; i++)
59 char *s = g_strconcat (TEST_STRING, endl[newline_type], NULL);
60 res = g_data_output_stream_put_string (G_DATA_OUTPUT_STREAM (stream), s, NULL, &error);
61 g_stpcpy ((char*)(lines + i*strlen(s)), s);
62 g_assert (error == NULL);
63 g_assert (res == TRUE);
66 /* Byte order testing */
67 g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
68 g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
69 g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
70 g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
73 int size = strlen(data);
74 g_assert_cmpint (size, <, MAX_LINES_BUFF);
75 g_assert_cmpstr ((char*)data, ==, lines);
77 g_object_unref (base_stream);
78 g_object_unref (stream);
84 test_read_lines_LF (void)
86 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
90 test_read_lines_CR (void)
92 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
96 test_read_lines_CR_LF (void)
98 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
111 #define TEST_DATA_RETYPE(a, v) \
112 (a == TEST_DATA_BYTE ? (guchar)v : \
113 (a == TEST_DATA_INT16 ? (gint16)v : \
114 (a == TEST_DATA_UINT16 ? (guint16)v : \
115 (a == TEST_DATA_INT32 ? (gint32)v : \
116 (a == TEST_DATA_UINT32 ? (guint32)v : \
117 (a == TEST_DATA_INT64 ? (gint64)v : \
120 #define TEST_DATA_RETYPE_BUFF(a, v) \
121 (a == TEST_DATA_BYTE ? *(guchar*)v : \
122 (a == TEST_DATA_INT16 ? *(gint16*)v : \
123 (a == TEST_DATA_UINT16 ? *(guint16*)v : \
124 (a == TEST_DATA_INT32 ? *(gint32*)v : \
125 (a == TEST_DATA_UINT32 ? *(guint32*)v : \
126 (a == TEST_DATA_INT64 ? *(gint64*)v : \
133 test_data_array (gpointer buffer, int len,
134 enum TestDataType data_type, GDataStreamByteOrder byte_order)
136 GOutputStream *stream;
137 GOutputStream *base_stream;
138 gpointer stream_data;
140 GError *error = NULL;
143 GDataStreamByteOrder native;
149 stream_data = g_malloc0 (len);
150 base_stream = g_memory_output_stream_new (stream_data, len, NULL, NULL);
151 stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
152 g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), byte_order);
155 /* Set correct data size */
161 case TEST_DATA_INT16:
162 case TEST_DATA_UINT16:
165 case TEST_DATA_INT32:
166 case TEST_DATA_UINT32:
169 case TEST_DATA_INT64:
170 case TEST_DATA_UINT64:
175 /* Set flag to swap bytes if needed */
176 native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
177 swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
179 /* Write data to the file */
186 res = g_data_output_stream_put_byte (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
188 case TEST_DATA_INT16:
189 res = g_data_output_stream_put_int16 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
191 case TEST_DATA_UINT16:
192 res = g_data_output_stream_put_uint16 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
194 case TEST_DATA_INT32:
195 res = g_data_output_stream_put_int32 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
197 case TEST_DATA_UINT32:
198 res = g_data_output_stream_put_uint32 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
200 case TEST_DATA_INT64:
201 res = g_data_output_stream_put_int64 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
203 case TEST_DATA_UINT64:
204 res = g_data_output_stream_put_uint64 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, (buffer + pos)), NULL, &error);
207 g_assert (error == NULL);
208 g_assert_cmpint (res, ==, TRUE);
213 /* Compare data back */
218 data = TEST_DATA_RETYPE_BUFF(data_type, (stream_data + pos));
225 case TEST_DATA_UINT16:
226 case TEST_DATA_INT16:
227 data = TEST_DATA_RETYPE(data_type, GUINT16_SWAP_LE_BE(TEST_DATA_RETYPE(data_type, data)));
229 case TEST_DATA_UINT32:
230 case TEST_DATA_INT32:
231 data = TEST_DATA_RETYPE(data_type, GUINT32_SWAP_LE_BE(TEST_DATA_RETYPE(data_type, data)));
233 case TEST_DATA_UINT64:
234 case TEST_DATA_INT64:
235 data = TEST_DATA_RETYPE(data_type, GUINT64_SWAP_LE_BE(TEST_DATA_RETYPE(data_type, data)));
239 g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, (buffer + pos)));
245 g_object_unref (base_stream);
246 g_object_unref (stream);
247 g_free (stream_data);
256 rand = g_rand_new ();
257 buffer = g_malloc0(MAX_BYTES_BINARY);
259 /* Fill in some random data */
261 for (i = 0; i < MAX_BYTES_BINARY; i++)
264 while (! x) x = (guchar)g_rand_int (rand);
265 *(guchar*)(buffer + sizeof(guchar) * i) = x;
268 for (i = 0; i < 3; i++)
271 for (j = 0; j <= TEST_DATA_UINT64; j++)
272 test_data_array (buffer, MAX_BYTES_BINARY, j, i);
284 g_test_init (&argc, &argv, NULL);
286 g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
287 g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
288 g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
289 g_test_add_func ("/data-input-stream/read-int", test_read_int);