1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc.
3 * Authors: Tomas Bzatek <tbzatek@redhat.com>
4 *
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.
8 *
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.
12 *
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.
21 */
22
23 #include <glib/glib.h>
24 #include <gio/gio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define MAX_LINES 0xFFF
29 #define MAX_BYTES 0x10000
30
31 static void
test_seek_to_start(GInputStream * stream)32 test_seek_to_start (GInputStream *stream)
33 {
34 GError *error = NULL;
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);
38 }
39
40 static void
test_read_lines(GDataStreamNewlineType newline_type)41 test_read_lines (GDataStreamNewlineType newline_type)
42 {
43 GInputStream *stream;
44 GInputStream *base_stream;
45 GError *error = NULL;
46 char *data;
47 int line;
48 const char* lines[MAX_LINES];
49 const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
50
51 /* prepare data */
52 int i;
53 for (i = 0; i < MAX_LINES; i++)
54 lines[i] = "some_text";
55
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);
60
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);
66
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);
70
71
72 /* Add sample data */
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);
76
77 /* Seek to the start */
78 test_seek_to_start (base_stream);
79
80 /* Test read line */
81 error = NULL;
82 data = (char*)1;
83 line = 0;
84 while (data)
85 {
86 gsize length = -1;
87 data = g_data_input_stream_read_line (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
88 if (data)
89 {
90 g_assert_cmpstr (data, ==, lines[line]);
91 g_assert_no_error (error);
92 line++;
93 }
94 }
95 g_assert_cmpint (line, ==, MAX_LINES);
96
97
98 g_object_unref (base_stream);
99 g_object_unref (stream);
100 }
101
102 static void
test_read_lines_LF(void)103 test_read_lines_LF (void)
104 {
105 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
106 }
107
108 static void
test_read_lines_CR(void)109 test_read_lines_CR (void)
110 {
111 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
112 }
113
114 static void
test_read_lines_CR_LF(void)115 test_read_lines_CR_LF (void)
116 {
117 test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
118 }
119
120
121 static void
test_read_until(void)122 test_read_until (void)
123 {
124 GInputStream *stream;
125 GInputStream *base_stream;
126 GError *error = NULL;
127 char *data;
128 int line;
129 int i;
130
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;
136
137 base_stream = g_memory_input_stream_new ();
138 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
139
140 for (i = 0; i < REPEATS; i++)
141 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, -1, NULL);
142
143 /* Test stop characters */
144 error = NULL;
145 data = (char*)1;
146 line = 0;
147 while (data)
148 {
149 gsize length = -1;
150 data = g_data_input_stream_read_until (G_DATA_INPUT_STREAM (stream), DATA_SEP, &length, NULL, &error);
151 if (data)
152 {
153 g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
154 g_assert_no_error (error);
155 line++;
156 }
157 }
158 g_assert_no_error (error);
159 g_assert_cmpint (line, ==, DATA_PARTS_NUM);
160
161
162 g_object_unref (base_stream);
163 g_object_unref (stream);
164 }
165
166 enum TestDataType {
167 TEST_DATA_BYTE = 0,
168 TEST_DATA_INT16,
169 TEST_DATA_UINT16,
170 TEST_DATA_INT32,
171 TEST_DATA_UINT32,
172 TEST_DATA_INT64,
173 TEST_DATA_UINT64
174 };
175
176 #define TEST_DATA_RETYPE_BUFF(a, v) \
177 (a == TEST_DATA_BYTE ? *(guchar*)v : \
178 (a == TEST_DATA_INT16 ? *(gint16*)v : \
179 (a == TEST_DATA_UINT16 ? *(guint16*)v : \
180 (a == TEST_DATA_INT32 ? *(gint32*)v : \
181 (a == TEST_DATA_UINT32 ? *(guint32*)v : \
182 (a == TEST_DATA_INT64 ? *(gint64*)v : \
183 *(guint64*)v ))))))
184
185
186 static void
test_data_array(GInputStream * stream,GInputStream * base_stream,gpointer buffer,int len,enum TestDataType data_type,GDataStreamByteOrder byte_order)187 test_data_array (GInputStream *stream, GInputStream *base_stream,
188 gpointer buffer, int len,
189 enum TestDataType data_type, GDataStreamByteOrder byte_order)
190 {
191 GError *error = NULL;
192 int pos = 0;
193 int data_size = 1;
194 gint64 data;
195 GDataStreamByteOrder native;
196 gboolean swap;
197
198 /* Seek to start */
199 test_seek_to_start (base_stream);
200
201 /* Set correct data size */
202 switch (data_type)
203 {
204 case TEST_DATA_BYTE:
205 data_size = 1;
206 break;
207 case TEST_DATA_INT16:
208 case TEST_DATA_UINT16:
209 data_size = 2;
210 break;
211 case TEST_DATA_INT32:
212 case TEST_DATA_UINT32:
213 data_size = 4;
214 break;
215 case TEST_DATA_INT64:
216 case TEST_DATA_UINT64:
217 data_size = 8;
218 break;
219 }
220
221 /* Set flag to swap bytes if needed */
222 native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
223 swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
224
225 data = 1;
226 while (data != 0)
227 {
228 switch (data_type)
229 {
230 case TEST_DATA_BYTE:
231 data = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
232 break;
233 case TEST_DATA_INT16:
234 data = g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
235 if (swap)
236 data = (gint16)GUINT16_SWAP_LE_BE((gint16)data);
237 break;
238 case TEST_DATA_UINT16:
239 data = g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
240 if (swap)
241 data = (guint16)GUINT16_SWAP_LE_BE((guint16)data);
242 break;
243 case TEST_DATA_INT32:
244 data = g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
245 if (swap)
246 data = (gint32)GUINT32_SWAP_LE_BE((gint32)data);
247 break;
248 case TEST_DATA_UINT32:
249 data = g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
250 if (swap)
251 data = (guint32)GUINT32_SWAP_LE_BE((guint32)data);
252 break;
253 case TEST_DATA_INT64:
254 data = g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
255 if (swap)
256 data = (gint64)GUINT64_SWAP_LE_BE((gint64)data);
257 break;
258 case TEST_DATA_UINT64:
259 data = g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
260 if (swap)
261 data = (guint64)GUINT64_SWAP_LE_BE((guint64)data);
262 break;
263 }
264 if ((data) && (! error))
265 g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, ((guchar*)buffer + pos)));
266
267 pos += data_size;
268 }
269 if (pos < len + 1)
270 g_assert_no_error (error);
271 g_assert_cmpint (pos - data_size, ==, len);
272 }
273
274 static void
test_read_int(void)275 test_read_int (void)
276 {
277 GInputStream *stream;
278 GInputStream *base_stream;
279 GRand *rand;
280 int i;
281 gpointer buffer;
282
283 rand = g_rand_new ();
284 buffer = g_malloc0 (MAX_BYTES);
285
286 /* Fill in some random data */
287 for (i = 0; i < MAX_BYTES; i++)
288 {
289 guchar x = 0;
290 while (! x)
291 x = (guchar)g_rand_int (rand);
292 *(guchar*)((guchar*)buffer + sizeof(guchar) * i) = x;
293 }
294
295 base_stream = g_memory_input_stream_new ();
296 stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
297 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), buffer, MAX_BYTES, NULL);
298
299
300 for (i = 0; i < 3; i++)
301 {
302 int j;
303 g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), i);
304
305 for (j = 0; j <= TEST_DATA_UINT64; j++)
306 test_data_array (stream, base_stream, buffer, MAX_BYTES, j, i);
307 }
308
309 g_object_unref (base_stream);
310 g_object_unref (stream);
311 g_rand_free (rand);
312 g_free (buffer);
313 }
314
315
316 int
main(int argc,char * argv[])317 main (int argc,
318 char *argv[])
319 {
320 g_type_init ();
321 g_test_init (&argc, &argv, NULL);
322
323 g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
324 g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
325 g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
326 g_test_add_func ("/data-input-stream/read-until", test_read_until);
327 g_test_add_func ("/data-input-stream/read-int", test_read_int);
328
329 return g_test_run();
330 }
331