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_LINES_BUFF 0xFFFFFF
30 #define MAX_BYTES_BINARY 0x100
33 test_read_lines (GDataStreamNewlineType newline_type)
35 GOutputStream *stream;
36 GOutputStream *base_stream;
43 #define TEST_STRING "some_text"
45 const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
48 data = g_malloc0 (MAX_LINES_BUFF);
49 lines = g_malloc0 ((strlen (TEST_STRING) + strlen (endl[newline_type])) * MAX_LINES + 1);
51 /* initialize objects */
52 base_stream = g_memory_output_stream_new (data, MAX_LINES_BUFF, NULL, NULL);
53 stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
57 for (i = 0; i < MAX_LINES; i++)
60 char *s = g_strconcat (TEST_STRING, endl[newline_type], NULL);
61 res = g_data_output_stream_put_string (G_DATA_OUTPUT_STREAM (stream), s, NULL, &error);
62 g_stpcpy ((char*)(lines + i*strlen(s)), s);
63 g_assert (error == NULL);
64 g_assert (res == TRUE);
67 /* Byte order testing */
68 g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
69 g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
70 g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
71 g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
75 g_assert_cmpint (size, <, MAX_LINES_BUFF);
76 g_assert_cmpstr ((char*)data, ==, lines);
78 g_object_unref (base_stream);
79 g_object_unref (stream);
85 test_read_lines_LF (void)
87 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
91 test_read_lines_CR (void)
93 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
97 test_read_lines_CR_LF (void)
99 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
112 #define TEST_DATA_RETYPE(a, v) \
113 (a == TEST_DATA_BYTE ? (guchar)v : \
114 (a == TEST_DATA_INT16 ? (gint16)v : \
115 (a == TEST_DATA_UINT16 ? (guint16)v : \
116 (a == TEST_DATA_INT32 ? (gint32)v : \
117 (a == TEST_DATA_UINT32 ? (guint32)v : \
118 (a == TEST_DATA_INT64 ? (gint64)v : \
121 #define TEST_DATA_RETYPE_BUFF(a, v) \
122 (a == TEST_DATA_BYTE ? *(guchar*)v : \
123 (a == TEST_DATA_INT16 ? *(gint16*)v : \
124 (a == TEST_DATA_UINT16 ? *(guint16*)v : \
125 (a == TEST_DATA_INT32 ? *(gint32*)v : \
126 (a == TEST_DATA_UINT32 ? *(guint32*)v : \
127 (a == TEST_DATA_INT64 ? *(gint64*)v : \
134 test_data_array (gpointer buffer, int len,
135 enum TestDataType data_type, GDataStreamByteOrder byte_order)
137 GOutputStream *stream;
138 GOutputStream *base_stream;
139 gpointer stream_data;
141 GError *error = NULL;
144 GDataStreamByteOrder native;
150 stream_data = g_malloc0 (len);
151 base_stream = g_memory_output_stream_new (stream_data, len, NULL, NULL);
152 stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
153 g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), byte_order);
156 /* Set correct data size */
162 case TEST_DATA_INT16:
163 case TEST_DATA_UINT16:
166 case TEST_DATA_INT32:
167 case TEST_DATA_UINT32:
170 case TEST_DATA_INT64:
171 case TEST_DATA_UINT64:
176 /* Set flag to swap bytes if needed */
177 native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
178 swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
180 /* Write data to the file */
187 res = g_data_output_stream_put_byte (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, ((guchar*)buffer + pos)), NULL, &error);
189 case TEST_DATA_INT16:
190 res = g_data_output_stream_put_int16 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, ((guchar*)buffer + pos)), NULL, &error);
192 case TEST_DATA_UINT16:
193 res = g_data_output_stream_put_uint16 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, ((guchar*)buffer + pos)), NULL, &error);
195 case TEST_DATA_INT32:
196 res = g_data_output_stream_put_int32 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, ((guchar*)buffer + pos)), NULL, &error);
198 case TEST_DATA_UINT32:
199 res = g_data_output_stream_put_uint32 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, ((guchar*)buffer + pos)), NULL, &error);
201 case TEST_DATA_INT64:
202 res = g_data_output_stream_put_int64 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, ((guchar*)buffer + pos)), NULL, &error);
204 case TEST_DATA_UINT64:
205 res = g_data_output_stream_put_uint64 (G_DATA_OUTPUT_STREAM (stream), TEST_DATA_RETYPE_BUFF (data_type, ((guchar*)buffer + pos)), NULL, &error);
208 g_assert (error == NULL);
209 g_assert_cmpint (res, ==, TRUE);
214 /* Compare data back */
219 data = TEST_DATA_RETYPE_BUFF(data_type, ((guchar*)stream_data + pos));
226 case TEST_DATA_UINT16:
227 case TEST_DATA_INT16:
228 data = TEST_DATA_RETYPE(data_type, GUINT16_SWAP_LE_BE(TEST_DATA_RETYPE(data_type, data)));
230 case TEST_DATA_UINT32:
231 case TEST_DATA_INT32:
232 data = TEST_DATA_RETYPE(data_type, GUINT32_SWAP_LE_BE(TEST_DATA_RETYPE(data_type, data)));
234 case TEST_DATA_UINT64:
235 case TEST_DATA_INT64:
236 data = TEST_DATA_RETYPE(data_type, GUINT64_SWAP_LE_BE(TEST_DATA_RETYPE(data_type, data)));
240 g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, ((guchar*)buffer + pos)));
246 g_object_unref (base_stream);
247 g_object_unref (stream);
248 g_free (stream_data);
258 rand = g_rand_new ();
259 buffer = g_malloc0(MAX_BYTES_BINARY);
261 /* Fill in some random data */
262 for (i = 0; i < MAX_BYTES_BINARY; i++)
265 while (! x) x = (guchar)g_rand_int (rand);
266 *(guchar*)((guchar*)buffer + sizeof (guchar) * i) = x;
269 for (i = 0; i < 3; i++)
272 for (j = 0; j <= TEST_DATA_UINT64; j++)
273 test_data_array (buffer, MAX_BYTES_BINARY, j, i);
285 g_test_init (&argc, &argv, NULL);
287 g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
288 g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
289 g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
290 g_test_add_func ("/data-input-stream/read-int", test_read_int);