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
35 GOutputStream *stream;
36 GOutputStream *base_stream;
40 data = g_malloc0 (MAX_LINES_BUFF);
42 /* initialize objects */
43 base_stream = g_memory_output_stream_new (data, MAX_LINES_BUFF, NULL, NULL);
44 stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
46 g_object_get (stream, "byte-order", &val, NULL);
47 g_assert_cmpint (val, ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
48 g_object_set (stream, "byte-order", G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN, NULL);
49 g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
51 g_object_unref (stream);
52 g_object_unref (base_stream);
56 test_read_lines (GDataStreamNewlineType newline_type)
58 GOutputStream *stream;
59 GOutputStream *base_stream;
66 #define TEST_STRING "some_text"
68 const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
71 data = g_malloc0 (MAX_LINES_BUFF);
72 lines = g_malloc0 ((strlen (TEST_STRING) + strlen (endl[newline_type])) * MAX_LINES + 1);
74 /* initialize objects */
75 base_stream = g_memory_output_stream_new (data, MAX_LINES_BUFF, NULL, NULL);
76 stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
80 for (i = 0; i < MAX_LINES; i++)
83 char *s = g_strconcat (TEST_STRING, endl[newline_type], NULL);
84 res = g_data_output_stream_put_string (G_DATA_OUTPUT_STREAM (stream), s, NULL, &error);
85 g_stpcpy ((char*)(lines + i*strlen(s)), s);
86 g_assert_no_error (error);
87 g_assert (res == TRUE);
90 /* Byte order testing */
91 g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
92 g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
93 g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
94 g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
98 g_assert_cmpint (size, <, MAX_LINES_BUFF);
99 g_assert_cmpstr ((char*)data, ==, lines);
101 g_object_unref (base_stream);
102 g_object_unref (stream);
108 test_read_lines_LF (void)
110 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
114 test_read_lines_CR (void)
116 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
120 test_read_lines_CR_LF (void)
122 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
136 test_data_array (guchar *buffer, gsize len,
137 enum TestDataType data_type, GDataStreamByteOrder byte_order)
139 GOutputStream *stream;
140 GOutputStream *base_stream;
143 GError *error = NULL;
145 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);
155 /* Set flag to swap bytes if needed */
156 native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
157 swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
159 /* set len to length of buffer cast to actual type */
164 case TEST_DATA_INT16:
165 case TEST_DATA_UINT16:
166 g_assert_cmpint (len % 2, ==, 0);
167 case TEST_DATA_INT32:
168 case TEST_DATA_UINT32:
169 g_assert_cmpint (len % 4, ==, 0);
170 case TEST_DATA_INT64:
171 case TEST_DATA_UINT64:
172 g_assert_cmpint (len % 8, ==, 0);
176 g_assert_not_reached ();
180 /* Write data to the file */
181 for (pos = 0; pos < len; pos++)
186 res = g_data_output_stream_put_byte (G_DATA_OUTPUT_STREAM (stream), buffer[pos], NULL, &error);
188 case TEST_DATA_INT16:
189 res = g_data_output_stream_put_int16 (G_DATA_OUTPUT_STREAM (stream), ((gint16 *) buffer)[pos], NULL, &error);
191 case TEST_DATA_UINT16:
192 res = g_data_output_stream_put_uint16 (G_DATA_OUTPUT_STREAM (stream), ((guint16 *) buffer)[pos], NULL, &error);
194 case TEST_DATA_INT32:
195 res = g_data_output_stream_put_int32 (G_DATA_OUTPUT_STREAM (stream), ((gint32 *) buffer)[pos], NULL, &error);
197 case TEST_DATA_UINT32:
198 res = g_data_output_stream_put_uint32 (G_DATA_OUTPUT_STREAM (stream), ((guint32 *) buffer)[pos], NULL, &error);
200 case TEST_DATA_INT64:
201 res = g_data_output_stream_put_int64 (G_DATA_OUTPUT_STREAM (stream), ((gint64 *) buffer)[pos], NULL, &error);
203 case TEST_DATA_UINT64:
204 res = g_data_output_stream_put_uint64 (G_DATA_OUTPUT_STREAM (stream), ((guint64 *) buffer)[pos], NULL, &error);
207 g_assert_not_reached ();
210 g_assert_no_error (error);
211 g_assert_cmpint (res, ==, TRUE);
214 /* Compare data back */
215 for (pos = 0; pos < len; pos++)
220 /* swapping unnecessary */
221 g_assert_cmpint (buffer[pos], ==, stream_data[pos]);
223 case TEST_DATA_UINT16:
225 g_assert_cmpint (GUINT16_SWAP_LE_BE (((guint16 *) buffer)[pos]), ==, ((guint16 *) stream_data)[pos]);
227 g_assert_cmpint (((guint16 *) buffer)[pos], ==, ((guint16 *) stream_data)[pos]);
229 case TEST_DATA_INT16:
231 g_assert_cmpint ((gint16) GUINT16_SWAP_LE_BE (((gint16 *) buffer)[pos]), ==, ((gint16 *) stream_data)[pos]);
233 g_assert_cmpint (((gint16 *) buffer)[pos], ==, ((gint16 *) stream_data)[pos]);
235 case TEST_DATA_UINT32:
237 g_assert_cmpint (GUINT32_SWAP_LE_BE (((guint32 *) buffer)[pos]), ==, ((guint32 *) stream_data)[pos]);
239 g_assert_cmpint (((guint32 *) buffer)[pos], ==, ((guint32 *) stream_data)[pos]);
241 case TEST_DATA_INT32:
243 g_assert_cmpint ((gint32) GUINT32_SWAP_LE_BE (((gint32 *) buffer)[pos]), ==, ((gint32 *) stream_data)[pos]);
245 g_assert_cmpint (((gint32 *) buffer)[pos], ==, ((gint32 *) stream_data)[pos]);
247 case TEST_DATA_UINT64:
249 g_assert_cmpint (GUINT64_SWAP_LE_BE (((guint64 *) buffer)[pos]), ==, ((guint64 *) stream_data)[pos]);
251 g_assert_cmpint (((guint64 *) buffer)[pos], ==, ((guint64 *) stream_data)[pos]);
253 case TEST_DATA_INT64:
255 g_assert_cmpint ((gint64) GUINT64_SWAP_LE_BE (((gint64 *) buffer)[pos]), ==, ((gint64 *) stream_data)[pos]);
257 g_assert_cmpint (((gint64 *) buffer)[pos], ==, ((gint64 *) stream_data)[pos]);
260 g_assert_not_reached ();
265 g_object_unref (base_stream);
266 g_object_unref (stream);
267 g_free (stream_data);
277 randomizer = g_rand_new ();
278 buffer = g_malloc0(MAX_BYTES_BINARY);
280 /* Fill in some random data */
281 for (i = 0; i < MAX_BYTES_BINARY; i++)
284 while (! x) x = (guchar)g_rand_int (randomizer);
285 *(guchar*)((guchar*)buffer + sizeof (guchar) * i) = x;
288 for (i = 0; i < 3; i++)
291 for (j = 0; j <= TEST_DATA_UINT64; j++)
292 test_data_array (buffer, MAX_BYTES_BINARY, j, i);
295 g_rand_free (randomizer);
304 g_test_init (&argc, &argv, NULL);
306 g_test_add_func ("/data-output-stream/basic", test_basic);
307 g_test_add_func ("/data-output-stream/write-lines-LF", test_read_lines_LF);
308 g_test_add_func ("/data-output-stream/write-lines-CR", test_read_lines_CR);
309 g_test_add_func ("/data-output-stream/write-lines-CR-LF", test_read_lines_CR_LF);
310 g_test_add_func ("/data-output-stream/write-int", test_read_int);