Plug a mem leak in data-input-stream test
[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   const int DATA_PARTS_NUM = strlen (DATA_SEP) * REPEATS;
168   
169   base_stream = g_memory_input_stream_new ();
170   stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
171   
172   for (i = 0; i < REPEATS; i++)
173     g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, -1, NULL);
174   
175   /*  Test stop characters */
176   error = NULL;
177   data = (char*)1;
178   line = 0;
179   while (data)
180     {
181       gsize length = -1;
182       data = g_data_input_stream_read_until (G_DATA_INPUT_STREAM (stream), DATA_SEP, &length, NULL, &error);
183       if (data)
184         {
185           g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
186           g_free (data);
187           g_assert_no_error (error);
188           line++;
189         }
190     }
191   g_assert_no_error (error);
192   g_assert_cmpint (line, ==, DATA_PARTS_NUM);
193         
194         
195   g_object_unref (base_stream);
196   g_object_unref (stream);
197 }
198
199 enum TestDataType {
200   TEST_DATA_BYTE = 0,
201   TEST_DATA_INT16,
202   TEST_DATA_UINT16,
203   TEST_DATA_INT32,
204   TEST_DATA_UINT32,
205   TEST_DATA_INT64,
206   TEST_DATA_UINT64
207 };
208
209 #define TEST_DATA_RETYPE_BUFF(a, t, v)  \
210          (a == TEST_DATA_BYTE   ? (t) *(guchar*)v : \
211          (a == TEST_DATA_INT16  ? (t) *(gint16*)v :      \
212          (a == TEST_DATA_UINT16 ? (t) *(guint16*)v : \
213          (a == TEST_DATA_INT32  ? (t) *(gint32*)v :      \
214          (a == TEST_DATA_UINT32 ? (t) *(guint32*)v : \
215          (a == TEST_DATA_INT64  ? (t) *(gint64*)v :      \
216          (t) *(guint64*)v )))))) 
217
218
219 static void
220 test_data_array (GInputStream *stream, GInputStream *base_stream,
221                  gpointer buffer, int len,
222                  enum TestDataType data_type, GDataStreamByteOrder byte_order)
223 {
224   GError *error = NULL;
225   int pos = 0;
226   int data_size = 1;
227   gint64 data;
228   GDataStreamByteOrder native;
229   gboolean swap;
230   
231   /*  Seek to start */
232   test_seek_to_start (base_stream);
233
234   /*  Set correct data size */
235   switch (data_type)
236     {
237     case TEST_DATA_BYTE:
238       data_size = 1;
239       break;
240     case TEST_DATA_INT16:
241     case TEST_DATA_UINT16:
242       data_size = 2;
243       break;
244     case TEST_DATA_INT32:
245     case TEST_DATA_UINT32:
246       data_size = 4;
247       break;
248     case TEST_DATA_INT64:
249     case TEST_DATA_UINT64:
250       data_size = 8;
251       break; 
252     default:
253       g_assert_not_reached ();
254       break;
255     }
256
257   /*  Set flag to swap bytes if needed */
258   native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
259   swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
260
261   data = 1;
262   while (data != 0)
263     {
264       switch (data_type)
265         {
266         case TEST_DATA_BYTE:
267           data = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
268           break;
269         case TEST_DATA_INT16:
270           data = g_data_input_stream_read_int16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
271           if (swap)
272             data = (gint16)GUINT16_SWAP_LE_BE((gint16)data);
273           break;
274         case TEST_DATA_UINT16:
275           data = g_data_input_stream_read_uint16 (G_DATA_INPUT_STREAM (stream), NULL, &error);
276           if (swap)
277             data = (guint16)GUINT16_SWAP_LE_BE((guint16)data);
278           break;
279         case TEST_DATA_INT32:
280           data = g_data_input_stream_read_int32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
281           if (swap)
282             data = (gint32)GUINT32_SWAP_LE_BE((gint32)data);
283           break;
284         case TEST_DATA_UINT32:
285           data = g_data_input_stream_read_uint32 (G_DATA_INPUT_STREAM (stream), NULL, &error);
286           if (swap)
287             data = (guint32)GUINT32_SWAP_LE_BE((guint32)data);
288           break;
289         case TEST_DATA_INT64:
290           data = g_data_input_stream_read_int64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
291           if (swap)
292             data = (gint64)GUINT64_SWAP_LE_BE((gint64)data);
293           break;
294         case TEST_DATA_UINT64:
295           data = g_data_input_stream_read_uint64 (G_DATA_INPUT_STREAM (stream), NULL, &error);
296           if (swap)
297             data = (guint64)GUINT64_SWAP_LE_BE((guint64)data);
298           break;
299         default:
300           g_assert_not_reached ();
301           break;
302         }
303       if (!error)
304         g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, gint64, ((guchar*)buffer + pos)));
305       
306       pos += data_size;
307     }
308   if (pos < len + 1)
309     g_assert_no_error (error);
310   if (error)
311     g_error_free (error);
312   g_assert_cmpint (pos - data_size, ==, len);
313 }
314
315 static void
316 test_read_int (void)
317 {
318   GInputStream *stream;
319   GInputStream *base_stream;
320   GRand *randomizer;
321   int i;
322   gpointer buffer;
323   
324   randomizer = g_rand_new ();
325   buffer = g_malloc0 (MAX_BYTES);
326   
327   /*  Fill in some random data */
328   for (i = 0; i < MAX_BYTES; i++)
329     {
330       guchar x = 0;
331       while (! x)
332         x = (guchar)g_rand_int (randomizer);
333       *(guchar*)((guchar*)buffer + sizeof(guchar) * i) = x; 
334     }
335
336   base_stream = g_memory_input_stream_new ();
337   stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
338   g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), buffer, MAX_BYTES, NULL);
339   
340   
341   for (i = 0; i < 3; i++)
342     {
343       int j;
344       g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), i);
345       
346       for (j = 0; j <= TEST_DATA_UINT64; j++)
347         test_data_array (stream, base_stream, buffer, MAX_BYTES, j, i);
348     }
349
350   g_object_unref (base_stream);
351   g_object_unref (stream);
352   g_rand_free (randomizer);
353   g_free (buffer);
354 }
355
356
357 int
358 main (int   argc,
359       char *argv[])
360 {
361   g_type_init ();
362   g_test_init (&argc, &argv, NULL);
363
364   g_test_add_func ("/data-input-stream/basic", test_basic);
365   g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
366   g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
367   g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
368   g_test_add_func ("/data-input-stream/read-lines-any", test_read_lines_any);
369   g_test_add_func ("/data-input-stream/read-until", test_read_until);
370   g_test_add_func ("/data-input-stream/read-int", test_read_int);
371
372   return g_test_run();
373 }