Initial commit
[platform/upstream/glib2.0.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_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
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
103 test_read_lines_LF (void)
104 {
105   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
106 }
107
108 static void
109 test_read_lines_CR (void)
110 {
111   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
112 }
113
114 static 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
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, t, v)  \
177          (a == TEST_DATA_BYTE   ? (t) *(guchar*)v : \
178          (a == TEST_DATA_INT16  ? (t) *(gint16*)v :      \
179          (a == TEST_DATA_UINT16 ? (t) *(guint16*)v : \
180          (a == TEST_DATA_INT32  ? (t) *(gint32*)v :      \
181          (a == TEST_DATA_UINT32 ? (t) *(guint32*)v : \
182          (a == TEST_DATA_INT64  ? (t) *(gint64*)v :      \
183          (t) *(guint64*)v )))))) 
184
185
186 static void
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     default:
220       g_assert_not_reached ();
221       break;
222     }
223
224   /*  Set flag to swap bytes if needed */
225   native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
226   swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
227
228   data = 1;
229   while (data != 0)
230     {
231       switch (data_type)
232         {
233         case TEST_DATA_BYTE:
234           data = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
235           break;
236         case TEST_DATA_INT16:
237           data = g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
238           if (swap)
239             data = (gint16)GUINT16_SWAP_LE_BE((gint16)data);
240           break;
241         case TEST_DATA_UINT16:
242           data = g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
243           if (swap)
244             data = (guint16)GUINT16_SWAP_LE_BE((guint16)data);
245           break;
246         case TEST_DATA_INT32:
247           data = g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
248           if (swap)
249             data = (gint32)GUINT32_SWAP_LE_BE((gint32)data);
250           break;
251         case TEST_DATA_UINT32:
252           data = g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
253           if (swap)
254             data = (guint32)GUINT32_SWAP_LE_BE((guint32)data);
255           break;
256         case TEST_DATA_INT64:
257           data = g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
258           if (swap)
259             data = (gint64)GUINT64_SWAP_LE_BE((gint64)data);
260           break;
261         case TEST_DATA_UINT64:
262           data = g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
263           if (swap)
264             data = (guint64)GUINT64_SWAP_LE_BE((guint64)data);
265           break;
266         default:
267           g_assert_not_reached ();
268           break;
269         }
270       if ((data) && (! error))  
271         g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, gint64, ((guchar*)buffer + pos)));
272       
273       pos += data_size;
274     }
275   if (pos < len + 1)
276     g_assert_no_error (error);
277   g_assert_cmpint (pos - data_size, ==, len);
278 }
279
280 static void
281 test_read_int (void)
282 {
283   GInputStream *stream;
284   GInputStream *base_stream;
285   GRand *randomizer;
286   int i;
287   gpointer buffer;
288   
289   randomizer = g_rand_new ();
290   buffer = g_malloc0 (MAX_BYTES);
291   
292   /*  Fill in some random data */
293   for (i = 0; i < MAX_BYTES; i++)
294     {
295       guchar x = 0;
296       while (! x)
297         x = (guchar)g_rand_int (randomizer);
298       *(guchar*)((guchar*)buffer + sizeof(guchar) * i) = x; 
299     }
300
301   base_stream = g_memory_input_stream_new ();
302   stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
303   g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), buffer, MAX_BYTES, NULL);
304   
305   
306   for (i = 0; i < 3; i++)
307     {
308       int j;
309       g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), i);
310       
311       for (j = 0; j <= TEST_DATA_UINT64; j++)
312         test_data_array (stream, base_stream, buffer, MAX_BYTES, j, i);
313     }
314
315   g_object_unref (base_stream);
316   g_object_unref (stream);
317   g_rand_free (randomizer);
318   g_free (buffer);
319 }
320
321
322 int
323 main (int   argc,
324       char *argv[])
325 {
326   g_type_init ();
327   g_test_init (&argc, &argv, NULL);
328
329   g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
330   g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
331   g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
332   g_test_add_func ("/data-input-stream/read-until", test_read_until);
333   g_test_add_func ("/data-input-stream/read-int", test_read_int);
334
335   return g_test_run();
336 }