Merge remote-tracking branch 'gvdb/master'
[platform/upstream/glib.git] / gio / tests / data-input-stream.c
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
32 test_basic (void)
33 {
34   GInputStream *stream;
35   GInputStream *base_stream;
36   gint val;
37
38   base_stream = g_memory_input_stream_new ();
39   stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
40
41   g_object_get (stream, "byte-order", &val, NULL);
42   g_assert_cmpint (val, ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
43   g_object_set (stream, "byte-order", G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN, NULL);
44   g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
45
46   g_object_get (stream, "newline-type", &val, NULL);
47   g_assert_cmpint (val, ==, G_DATA_STREAM_NEWLINE_TYPE_LF);
48   g_object_set (stream, "newline-type", G_DATA_STREAM_NEWLINE_TYPE_CR_LF, NULL);
49   g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
50
51   g_object_unref (stream);
52   g_object_unref (base_stream);
53 }
54
55 static void
56 test_seek_to_start (GInputStream *stream)
57 {
58   GError *error = NULL;
59   gboolean res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
60   g_assert_cmpint (res, ==, TRUE);
61   g_assert_no_error (error);
62 }
63
64 static void
65 test_read_lines (GDataStreamNewlineType newline_type)
66 {
67   GInputStream *stream;
68   GInputStream *base_stream;
69   GError *error = NULL;
70   char *data;
71   int line;
72   const char* lines[MAX_LINES];
73   const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
74   
75   /*  prepare data */
76   int i;
77   for (i = 0; i < MAX_LINES; i++)
78     lines[i] = "some_text";
79         
80   base_stream = g_memory_input_stream_new ();
81   g_assert (base_stream != NULL);
82   stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
83   g_assert(stream != NULL);
84         
85   /*  Byte order testing */
86   g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
87   g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
88   g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
89   g_assert_cmpint (g_data_input_stream_get_byte_order (G_DATA_INPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
90   
91   /*  Line ends testing */
92   g_data_input_stream_set_newline_type (G_DATA_INPUT_STREAM (stream), newline_type);
93   g_assert_cmpint (g_data_input_stream_get_newline_type (G_DATA_INPUT_STREAM (stream)), ==, newline_type);
94         
95
96   /*  Add sample data */
97   for (i = 0; i < MAX_LINES; i++) 
98     g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
99                                     g_strconcat (lines[i], endl[newline_type], NULL), -1, g_free);
100
101   /*  Seek to the start */
102   test_seek_to_start (base_stream);
103         
104   /*  Test read line */
105   error = NULL;
106   data = (char*)1;
107   line = 0;
108   while (data)
109     {
110       gsize length = -1;
111       data = g_data_input_stream_read_line (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
112       if (data)
113         {
114           g_assert_cmpstr (data, ==, lines[line]);
115           g_free (data);
116           g_assert_no_error (error);
117           line++;
118         }
119       if (error)
120         g_error_free (error);
121     }
122   g_assert_cmpint (line, ==, MAX_LINES);
123   
124   
125   g_object_unref (base_stream);
126   g_object_unref (stream);
127 }
128
129 static void
130 test_read_lines_LF (void)
131 {
132   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
133 }
134
135 static void
136 test_read_lines_CR (void)
137 {
138   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
139 }
140
141 static void
142 test_read_lines_CR_LF (void)
143 {
144   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
145 }
146
147 static void
148 test_read_lines_any (void)
149 {
150   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_ANY);
151 }
152
153 static void
154 test_read_until (void)
155 {
156   GInputStream *stream;
157   GInputStream *base_stream;
158   GError *error = NULL;
159   char *data;
160   int line;
161   int i;
162   
163 #define REPEATS                 10   /* number of rounds */
164 #define DATA_STRING             " part1 # part2 $ part3 % part4 ^"
165 #define DATA_PART_LEN           7    /* number of characters between separators */
166 #define DATA_SEP                "#$%^"
167 #define DATA_SEP_LEN            4
168   const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;
169   
170   base_stream = g_memory_input_stream_new ();
171   stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
172   
173   for (i = 0; i < REPEATS; i++)
174     g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, -1, NULL);
175   
176   /*  Test stop characters */
177   error = NULL;
178   data = (char*)1;
179   line = 0;
180   while (data)
181     {
182       gsize length = -1;
183       data = g_data_input_stream_read_until (G_DATA_INPUT_STREAM (stream), DATA_SEP, &length, NULL, &error);
184       if (data)
185         {
186           g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
187           g_free (data);
188           g_assert_no_error (error);
189           line++;
190         }
191     }
192   g_assert_no_error (error);
193   g_assert_cmpint (line, ==, DATA_PARTS_NUM);
194
195   g_object_unref (base_stream);
196   g_object_unref (stream);
197 }
198
199 static void
200 test_read_upto (void)
201 {
202   GInputStream *stream;
203   GInputStream *base_stream;
204   GError *error = NULL;
205   char *data;
206   int line;
207   int i;
208   guchar stop_char;
209
210 #undef REPEATS
211 #undef DATA_STRING
212 #undef DATA_PART_LEN
213 #undef DATA_SEP
214 #undef DATA_SEP_LEN
215 #define REPEATS                 10   /* number of rounds */
216 #define DATA_STRING             " part1 # part2 $ part3 \0 part4 ^"
217 #define DATA_PART_LEN           7    /* number of characters between separators */
218 #define DATA_SEP                "#$\0^"
219 #define DATA_SEP_LEN            4
220   const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;
221
222   base_stream = g_memory_input_stream_new ();
223   stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
224
225   for (i = 0; i < REPEATS; i++)
226     g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, 32, NULL);
227
228   /*  Test stop characters */
229   error = NULL;
230   data = (char*)1;
231   line = 0;
232   while (data)
233     {
234       gsize length = -1;
235       data = g_data_input_stream_read_upto (G_DATA_INPUT_STREAM (stream), DATA_SEP, DATA_SEP_LEN, &length, NULL, &error);
236       if (data)
237         {
238           g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
239           g_assert_no_error (error);
240           line++;
241
242           stop_char = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
243           g_assert (memchr (DATA_SEP, stop_char, DATA_SEP_LEN) != NULL);
244           g_assert_no_error (error);
245         }
246     }
247   g_assert_no_error (error);
248   g_assert_cmpint (line, ==, DATA_PARTS_NUM);
249
250   g_object_unref (base_stream);
251   g_object_unref (stream);
252 }
253 enum TestDataType {
254   TEST_DATA_BYTE = 0,
255   TEST_DATA_INT16,
256   TEST_DATA_UINT16,
257   TEST_DATA_INT32,
258   TEST_DATA_UINT32,
259   TEST_DATA_INT64,
260   TEST_DATA_UINT64
261 };
262
263 #define TEST_DATA_RETYPE_BUFF(a, t, v)  \
264          (a == TEST_DATA_BYTE   ? (t) *(guchar*)v : \
265          (a == TEST_DATA_INT16  ? (t) *(gint16*)v :      \
266          (a == TEST_DATA_UINT16 ? (t) *(guint16*)v : \
267          (a == TEST_DATA_INT32  ? (t) *(gint32*)v :      \
268          (a == TEST_DATA_UINT32 ? (t) *(guint32*)v : \
269          (a == TEST_DATA_INT64  ? (t) *(gint64*)v :      \
270          (t) *(guint64*)v )))))) 
271
272
273 static void
274 test_data_array (GInputStream *stream, GInputStream *base_stream,
275                  gpointer buffer, int len,
276                  enum TestDataType data_type, GDataStreamByteOrder byte_order)
277 {
278   GError *error = NULL;
279   int pos = 0;
280   int data_size = 1;
281   gint64 data;
282   GDataStreamByteOrder native;
283   gboolean swap;
284   
285   /*  Seek to start */
286   test_seek_to_start (base_stream);
287
288   /*  Set correct data size */
289   switch (data_type)
290     {
291     case TEST_DATA_BYTE:
292       data_size = 1;
293       break;
294     case TEST_DATA_INT16:
295     case TEST_DATA_UINT16:
296       data_size = 2;
297       break;
298     case TEST_DATA_INT32:
299     case TEST_DATA_UINT32:
300       data_size = 4;
301       break;
302     case TEST_DATA_INT64:
303     case TEST_DATA_UINT64:
304       data_size = 8;
305       break; 
306     default:
307       g_assert_not_reached ();
308       break;
309     }
310
311   /*  Set flag to swap bytes if needed */
312   native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
313   swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
314
315   data = 1;
316   while (data != 0)
317     {
318       switch (data_type)
319         {
320         case TEST_DATA_BYTE:
321           data = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
322           break;
323         case TEST_DATA_INT16:
324           data = g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
325           if (swap)
326             data = (gint16)GUINT16_SWAP_LE_BE((gint16)data);
327           break;
328         case TEST_DATA_UINT16:
329           data = g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
330           if (swap)
331             data = (guint16)GUINT16_SWAP_LE_BE((guint16)data);
332           break;
333         case TEST_DATA_INT32:
334           data = g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
335           if (swap)
336             data = (gint32)GUINT32_SWAP_LE_BE((gint32)data);
337           break;
338         case TEST_DATA_UINT32:
339           data = g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
340           if (swap)
341             data = (guint32)GUINT32_SWAP_LE_BE((guint32)data);
342           break;
343         case TEST_DATA_INT64:
344           data = g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
345           if (swap)
346             data = (gint64)GUINT64_SWAP_LE_BE((gint64)data);
347           break;
348         case TEST_DATA_UINT64:
349           data = g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
350           if (swap)
351             data = (guint64)GUINT64_SWAP_LE_BE((guint64)data);
352           break;
353         default:
354           g_assert_not_reached ();
355           break;
356         }
357       if (!error)
358         g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, gint64, ((guchar*)buffer + pos)));
359       
360       pos += data_size;
361     }
362   if (pos < len + 1)
363     g_assert_no_error (error);
364   if (error)
365     g_error_free (error);
366   g_assert_cmpint (pos - data_size, ==, len);
367 }
368
369 static void
370 test_read_int (void)
371 {
372   GInputStream *stream;
373   GInputStream *base_stream;
374   GRand *randomizer;
375   int i;
376   gpointer buffer;
377   
378   randomizer = g_rand_new ();
379   buffer = g_malloc0 (MAX_BYTES);
380   
381   /*  Fill in some random data */
382   for (i = 0; i < MAX_BYTES; i++)
383     {
384       guchar x = 0;
385       while (! x)
386         x = (guchar)g_rand_int (randomizer);
387       *(guchar*)((guchar*)buffer + sizeof(guchar) * i) = x; 
388     }
389
390   base_stream = g_memory_input_stream_new ();
391   stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
392   g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), buffer, MAX_BYTES, NULL);
393   
394   
395   for (i = 0; i < 3; i++)
396     {
397       int j;
398       g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), i);
399       
400       for (j = 0; j <= TEST_DATA_UINT64; j++)
401         test_data_array (stream, base_stream, buffer, MAX_BYTES, j, i);
402     }
403
404   g_object_unref (base_stream);
405   g_object_unref (stream);
406   g_rand_free (randomizer);
407   g_free (buffer);
408 }
409
410
411 int
412 main (int   argc,
413       char *argv[])
414 {
415   g_type_init ();
416   g_test_init (&argc, &argv, NULL);
417
418   g_test_add_func ("/data-input-stream/basic", test_basic);
419   g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
420   g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
421   g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
422   g_test_add_func ("/data-input-stream/read-lines-any", test_read_lines_any);
423   g_test_add_func ("/data-input-stream/read-until", test_read_until);
424   g_test_add_func ("/data-input-stream/read-upto", test_read_upto);
425   g_test_add_func ("/data-input-stream/read-int", test_read_int);
426
427   return g_test_run();
428 }