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_no_error (error);
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);
113 test_data_array (guchar *buffer, gsize len,
114 enum TestDataType data_type, GDataStreamByteOrder byte_order)
116 GOutputStream *stream;
117 GOutputStream *base_stream;
120 GError *error = NULL;
122 GDataStreamByteOrder native;
127 stream_data = g_malloc0 (len);
128 base_stream = g_memory_output_stream_new (stream_data, len, NULL, NULL);
129 stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
130 g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), byte_order);
132 /* Set flag to swap bytes if needed */
133 native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
134 swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
136 /* set len to length of buffer cast to actual type */
141 case TEST_DATA_INT16:
142 case TEST_DATA_UINT16:
143 g_assert_cmpint (len % 2, ==, 0);
144 case TEST_DATA_INT32:
145 case TEST_DATA_UINT32:
146 g_assert_cmpint (len % 4, ==, 0);
147 case TEST_DATA_INT64:
148 case TEST_DATA_UINT64:
149 g_assert_cmpint (len % 8, ==, 0);
153 g_assert_not_reached ();
157 /* Write data to the file */
158 for (pos = 0; pos < len; pos++)
163 res = g_data_output_stream_put_byte (G_DATA_OUTPUT_STREAM (stream), buffer[pos], NULL, &error);
165 case TEST_DATA_INT16:
166 res = g_data_output_stream_put_int16 (G_DATA_OUTPUT_STREAM (stream), ((gint16 *) buffer)[pos], NULL, &error);
168 case TEST_DATA_UINT16:
169 res = g_data_output_stream_put_uint16 (G_DATA_OUTPUT_STREAM (stream), ((guint16 *) buffer)[pos], NULL, &error);
171 case TEST_DATA_INT32:
172 res = g_data_output_stream_put_int32 (G_DATA_OUTPUT_STREAM (stream), ((gint32 *) buffer)[pos], NULL, &error);
174 case TEST_DATA_UINT32:
175 res = g_data_output_stream_put_uint32 (G_DATA_OUTPUT_STREAM (stream), ((guint32 *) buffer)[pos], NULL, &error);
177 case TEST_DATA_INT64:
178 res = g_data_output_stream_put_int64 (G_DATA_OUTPUT_STREAM (stream), ((gint64 *) buffer)[pos], NULL, &error);
180 case TEST_DATA_UINT64:
181 res = g_data_output_stream_put_uint64 (G_DATA_OUTPUT_STREAM (stream), ((guint64 *) buffer)[pos], NULL, &error);
184 g_assert_not_reached ();
187 g_assert_no_error (error);
188 g_assert_cmpint (res, ==, TRUE);
191 /* Compare data back */
192 for (pos = 0; pos < len; pos++)
197 /* swapping unnecessary */
198 g_assert_cmpint (buffer[pos], ==, stream_data[pos]);
200 case TEST_DATA_UINT16:
202 g_assert_cmpint (GUINT16_SWAP_LE_BE (((guint16 *) buffer)[pos]), ==, ((guint16 *) stream_data)[pos]);
204 g_assert_cmpint (((guint16 *) buffer)[pos], ==, ((guint16 *) stream_data)[pos]);
206 case TEST_DATA_INT16:
208 g_assert_cmpint ((gint16) GUINT16_SWAP_LE_BE (((gint16 *) buffer)[pos]), ==, ((gint16 *) stream_data)[pos]);
210 g_assert_cmpint (((gint16 *) buffer)[pos], ==, ((gint16 *) stream_data)[pos]);
212 case TEST_DATA_UINT32:
214 g_assert_cmpint (GUINT32_SWAP_LE_BE (((guint32 *) buffer)[pos]), ==, ((guint32 *) stream_data)[pos]);
216 g_assert_cmpint (((guint32 *) buffer)[pos], ==, ((guint32 *) stream_data)[pos]);
218 case TEST_DATA_INT32:
220 g_assert_cmpint ((gint32) GUINT32_SWAP_LE_BE (((gint32 *) buffer)[pos]), ==, ((gint32 *) stream_data)[pos]);
222 g_assert_cmpint (((gint32 *) buffer)[pos], ==, ((gint32 *) stream_data)[pos]);
224 case TEST_DATA_UINT64:
226 g_assert_cmpint (GUINT64_SWAP_LE_BE (((guint64 *) buffer)[pos]), ==, ((guint64 *) stream_data)[pos]);
228 g_assert_cmpint (((guint64 *) buffer)[pos], ==, ((guint64 *) stream_data)[pos]);
230 case TEST_DATA_INT64:
232 g_assert_cmpint ((gint64) GUINT64_SWAP_LE_BE (((gint64 *) buffer)[pos]), ==, ((gint64 *) stream_data)[pos]);
234 g_assert_cmpint (((gint64 *) buffer)[pos], ==, ((gint64 *) stream_data)[pos]);
237 g_assert_not_reached ();
242 g_object_unref (base_stream);
243 g_object_unref (stream);
244 g_free (stream_data);
254 randomizer = g_rand_new ();
255 buffer = g_malloc0(MAX_BYTES_BINARY);
257 /* Fill in some random data */
258 for (i = 0; i < MAX_BYTES_BINARY; i++)
261 while (! x) x = (guchar)g_rand_int (randomizer);
262 *(guchar*)((guchar*)buffer + sizeof (guchar) * i) = x;
265 for (i = 0; i < 3; i++)
268 for (j = 0; j <= TEST_DATA_UINT64; j++)
269 test_data_array (buffer, MAX_BYTES_BINARY, j, i);
272 g_rand_free (randomizer);
281 g_test_init (&argc, &argv, NULL);
283 g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
284 g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
285 g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
286 g_test_add_func ("/data-input-stream/read-int", test_read_int);