Initial commit
[platform/upstream/glib2.0.git] / gio / tests / data-output-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_LINES_BUFF          0xFFFFFF
30 #define MAX_BYTES_BINARY        0x100   
31
32 static void
33 test_read_lines (GDataStreamNewlineType newline_type)
34 {
35   GOutputStream *stream;
36   GOutputStream *base_stream;
37   GError *error = NULL;
38   gpointer data;
39   char *lines;
40   int size;
41   int i;
42
43 #define TEST_STRING     "some_text"
44   
45   const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
46   
47   
48   data = g_malloc0 (MAX_LINES_BUFF);
49   lines = g_malloc0 ((strlen (TEST_STRING) + strlen (endl[newline_type])) * MAX_LINES + 1);
50   
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));
54
55   
56   /*  fill data */
57   for (i = 0; i < MAX_LINES; i++)
58     {
59       gboolean res;
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);
65     }
66
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);
72   
73   /*  compare data */
74   size = strlen (data);
75   g_assert_cmpint (size, <, MAX_LINES_BUFF);
76   g_assert_cmpstr ((char*)data, ==, lines);
77   
78   g_object_unref (base_stream);
79   g_object_unref (stream);
80   g_free (data);
81   g_free (lines);
82 }
83
84 static void
85 test_read_lines_LF (void)
86 {
87   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
88 }
89
90 static void
91 test_read_lines_CR (void)
92 {
93   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
94 }
95
96 static void
97 test_read_lines_CR_LF (void)
98 {
99   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
100 }
101
102 enum TestDataType {
103   TEST_DATA_BYTE = 0,
104   TEST_DATA_INT16,
105   TEST_DATA_UINT16,
106   TEST_DATA_INT32,
107   TEST_DATA_UINT32,
108   TEST_DATA_INT64,
109   TEST_DATA_UINT64
110 };
111
112 static void
113 test_data_array (guchar *buffer, gsize len,
114                  enum TestDataType data_type, GDataStreamByteOrder byte_order)
115 {
116   GOutputStream *stream;
117   GOutputStream *base_stream;
118   guchar *stream_data;
119   
120   GError *error = NULL;
121   guint pos;
122   GDataStreamByteOrder native;
123   gboolean swap;
124   gboolean res;
125   
126   /*  create objects */
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);
131   
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);
135
136   /* set len to length of buffer cast to actual type */
137   switch (data_type)
138     {
139     case TEST_DATA_BYTE:
140       break;
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);
150       len /= 8;
151       break;
152     default:
153       g_assert_not_reached ();
154       break;
155     }
156
157   /*  Write data to the file */
158   for (pos = 0; pos < len; pos++)
159     {
160       switch (data_type)
161         {
162         case TEST_DATA_BYTE:
163           res = g_data_output_stream_put_byte (G_DATA_OUTPUT_STREAM (stream), buffer[pos], NULL, &error);
164           break;
165         case TEST_DATA_INT16:
166           res = g_data_output_stream_put_int16 (G_DATA_OUTPUT_STREAM (stream), ((gint16 *) buffer)[pos], NULL, &error);
167           break;
168         case TEST_DATA_UINT16:
169           res = g_data_output_stream_put_uint16 (G_DATA_OUTPUT_STREAM (stream), ((guint16 *) buffer)[pos], NULL, &error);
170           break;
171         case TEST_DATA_INT32:
172           res = g_data_output_stream_put_int32 (G_DATA_OUTPUT_STREAM (stream), ((gint32 *) buffer)[pos], NULL, &error);
173           break;
174         case TEST_DATA_UINT32:
175           res = g_data_output_stream_put_uint32 (G_DATA_OUTPUT_STREAM (stream), ((guint32 *) buffer)[pos], NULL, &error);
176           break;
177         case TEST_DATA_INT64:
178           res = g_data_output_stream_put_int64 (G_DATA_OUTPUT_STREAM (stream), ((gint64 *) buffer)[pos], NULL, &error);
179           break;
180         case TEST_DATA_UINT64:
181           res = g_data_output_stream_put_uint64 (G_DATA_OUTPUT_STREAM (stream), ((guint64 *) buffer)[pos], NULL, &error);
182           break;
183         default:
184           g_assert_not_reached ();
185           break;
186         }
187       g_assert_no_error (error);
188       g_assert_cmpint (res, ==, TRUE);
189     }
190   
191   /*  Compare data back */
192   for (pos = 0; pos < len; pos++)
193     {
194       switch (data_type)
195         {
196         case TEST_DATA_BYTE:
197           /* swapping unnecessary */
198           g_assert_cmpint (buffer[pos], ==, stream_data[pos]);
199           break;
200         case TEST_DATA_UINT16:
201           if (swap)
202             g_assert_cmpint (GUINT16_SWAP_LE_BE (((guint16 *) buffer)[pos]), ==, ((guint16 *) stream_data)[pos]);
203           else
204             g_assert_cmpint (((guint16 *) buffer)[pos], ==, ((guint16 *) stream_data)[pos]);
205           break;
206         case TEST_DATA_INT16:
207           if (swap)
208             g_assert_cmpint ((gint16) GUINT16_SWAP_LE_BE (((gint16 *) buffer)[pos]), ==, ((gint16 *) stream_data)[pos]);
209           else
210             g_assert_cmpint (((gint16 *) buffer)[pos], ==, ((gint16 *) stream_data)[pos]);
211           break;
212         case TEST_DATA_UINT32:
213           if (swap)
214             g_assert_cmpint (GUINT32_SWAP_LE_BE (((guint32 *) buffer)[pos]), ==, ((guint32 *) stream_data)[pos]);
215           else
216             g_assert_cmpint (((guint32 *) buffer)[pos], ==, ((guint32 *) stream_data)[pos]);
217           break;
218         case TEST_DATA_INT32:
219           if (swap)
220             g_assert_cmpint ((gint32) GUINT32_SWAP_LE_BE (((gint32 *) buffer)[pos]), ==, ((gint32 *) stream_data)[pos]);
221           else
222             g_assert_cmpint (((gint32 *) buffer)[pos], ==, ((gint32 *) stream_data)[pos]);
223           break;
224         case TEST_DATA_UINT64:
225           if (swap)
226             g_assert_cmpint (GUINT64_SWAP_LE_BE (((guint64 *) buffer)[pos]), ==, ((guint64 *) stream_data)[pos]);
227           else
228             g_assert_cmpint (((guint64 *) buffer)[pos], ==, ((guint64 *) stream_data)[pos]);
229           break;
230         case TEST_DATA_INT64:
231           if (swap)
232             g_assert_cmpint ((gint64) GUINT64_SWAP_LE_BE (((gint64 *) buffer)[pos]), ==, ((gint64 *) stream_data)[pos]);
233           else
234             g_assert_cmpint (((gint64 *) buffer)[pos], ==, ((gint64 *) stream_data)[pos]);
235           break;
236         default:
237             g_assert_not_reached ();
238           break;
239         }
240     }
241   
242   g_object_unref (base_stream);
243   g_object_unref (stream);
244   g_free (stream_data);
245 }
246
247 static void
248 test_read_int (void)
249 {
250   GRand *randomizer;
251   gpointer buffer;
252   int i;
253   
254   randomizer = g_rand_new ();
255   buffer = g_malloc0(MAX_BYTES_BINARY);
256   
257   /*  Fill in some random data */
258   for (i = 0; i < MAX_BYTES_BINARY; i++)
259     {
260       guchar x = 0;
261       while (! x)  x = (guchar)g_rand_int (randomizer);
262       *(guchar*)((guchar*)buffer + sizeof (guchar) * i) = x; 
263     }
264
265   for (i = 0; i < 3; i++)
266     {
267       int j;
268       for (j = 0; j <= TEST_DATA_UINT64; j++)
269         test_data_array (buffer, MAX_BYTES_BINARY, j, i);
270     }
271   
272   g_rand_free (randomizer);
273   g_free (buffer);
274 }
275
276 int
277 main (int   argc,
278       char *argv[])
279 {
280   g_type_init ();
281   g_test_init (&argc, &argv, NULL);
282
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);
287
288   return g_test_run();
289 }