1 /* GLib testing framework examples and tests
2 * Copyright (C) 2009 Red Hat, Inc.
3 * Authors: Alexander Larsson <alexl@redhat.com>
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.
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.
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.
23 #include <glib/glib.h>
28 #define G_TYPE_EXPANDER_CONVERTER (g_expander_converter_get_type ())
29 #define G_EXPANDER_CONVERTER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_EXPANDER_CONVERTER, GExpanderConverter))
30 #define G_EXPANDER_CONVERTER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), G_TYPE_EXPANDER_CONVERTER, GExpanderConverterClass))
31 #define G_IS_EXPANDER_CONVERTER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_EXPANDER_CONVERTER))
32 #define G_IS_EXPANDER_CONVERTER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_EXPANDER_CONVERTER))
33 #define G_EXPANDER_CONVERTER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_EXPANDER_CONVERTER, GExpanderConverterClass))
35 typedef struct _GExpanderConverter GExpanderConverter;
36 typedef struct _GExpanderConverterClass GExpanderConverterClass;
38 struct _GExpanderConverterClass
40 GObjectClass parent_class;
43 GType g_expander_converter_get_type (void) G_GNUC_CONST;
44 GConverter *g_expander_converter_new (void);
48 static void g_expander_converter_iface_init (GConverterIface *iface);
50 struct _GExpanderConverter
52 GObject parent_instance;
55 G_DEFINE_TYPE_WITH_CODE (GExpanderConverter, g_expander_converter, G_TYPE_OBJECT,
56 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
57 g_expander_converter_iface_init))
60 g_expander_converter_class_init (GExpanderConverterClass *klass)
65 g_expander_converter_init (GExpanderConverter *local)
70 g_expander_converter_new (void)
74 conv = g_object_new (G_TYPE_EXPANDER_CONVERTER, NULL);
80 g_expander_converter_reset (GConverter *converter)
84 static GConverterResult
85 g_expander_converter_convert (GConverter *converter,
90 GConverterFlags flags,
95 GExpanderConverter *conv;
96 const guint8 *in, *in_end;
101 conv = G_EXPANDER_CONVERTER (converter);
105 in_end = in + inbuf_size;
114 block_size = v * 1000;
116 if (outbuf_size < block_size)
119 return G_CONVERTER_CONVERTED;
121 g_set_error_literal (error, G_IO_ERROR,
124 return G_CONVERTER_ERROR;
129 *bytes_written += block_size;
130 outbuf_size -= block_size;
131 for (i = 0; i < block_size; i++)
135 if (in == in_end && (flags & G_CONVERTER_INPUT_AT_END))
136 return G_CONVERTER_FINISHED;
137 return G_CONVERTER_CONVERTED;
141 g_expander_converter_iface_init (GConverterIface *iface)
143 iface->convert = g_expander_converter_convert;
144 iface->reset = g_expander_converter_reset;
147 #define G_TYPE_COMPRESSOR_CONVERTER (g_compressor_converter_get_type ())
148 #define G_COMPRESSOR_CONVERTER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_COMPRESSOR_CONVERTER, GCompressorConverter))
149 #define G_COMPRESSOR_CONVERTER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), G_TYPE_COMPRESSOR_CONVERTER, GCompressorConverterClass))
150 #define G_IS_COMPRESSOR_CONVERTER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_COMPRESSOR_CONVERTER))
151 #define G_IS_COMPRESSOR_CONVERTER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_COMPRESSOR_CONVERTER))
152 #define G_COMPRESSOR_CONVERTER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_COMPRESSOR_CONVERTER, GCompressorConverterClass))
154 typedef struct _GCompressorConverter GCompressorConverter;
155 typedef struct _GCompressorConverterClass GCompressorConverterClass;
157 struct _GCompressorConverterClass
159 GObjectClass parent_class;
162 GType g_compressor_converter_get_type (void) G_GNUC_CONST;
163 GConverter *g_compressor_converter_new (void);
167 static void g_compressor_converter_iface_init (GConverterIface *iface);
169 struct _GCompressorConverter
171 GObject parent_instance;
174 G_DEFINE_TYPE_WITH_CODE (GCompressorConverter, g_compressor_converter, G_TYPE_OBJECT,
175 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
176 g_compressor_converter_iface_init))
179 g_compressor_converter_class_init (GCompressorConverterClass *klass)
184 g_compressor_converter_init (GCompressorConverter *local)
189 g_compressor_converter_new (void)
193 conv = g_object_new (G_TYPE_COMPRESSOR_CONVERTER, NULL);
199 g_compressor_converter_reset (GConverter *converter)
203 static GConverterResult
204 g_compressor_converter_convert (GConverter *converter,
209 GConverterFlags flags,
211 gsize *bytes_written,
214 GCompressorConverter *conv;
215 const guint8 *in, *in_end;
220 conv = G_COMPRESSOR_CONVERTER (converter);
224 in_end = in + inbuf_size;
233 while (in+block_size < in_end && *(in+block_size) == 0)
237 block_size = v * 1000;
239 /* Not enough data */
240 if (in_end - in < block_size)
244 g_set_error_literal (error, G_IO_ERROR,
245 G_IO_ERROR_PARTIAL_INPUT,
247 return G_CONVERTER_ERROR;
250 for (i = 0; i < block_size; i++)
256 g_set_error_literal (error, G_IO_ERROR,
257 G_IO_ERROR_INVALID_DATA,
259 return G_CONVERTER_ERROR;
263 if (v == 0 && in_end - in == block_size && (flags & G_CONVERTER_INPUT_AT_END) == 0)
267 g_set_error_literal (error, G_IO_ERROR,
268 G_IO_ERROR_PARTIAL_INPUT,
270 return G_CONVERTER_ERROR;
275 *bytes_read += block_size;
279 if (in == in_end && (flags & G_CONVERTER_INPUT_AT_END))
280 return G_CONVERTER_FINISHED;
281 return G_CONVERTER_CONVERTED;
285 g_compressor_converter_iface_init (GConverterIface *iface)
287 iface->convert = g_compressor_converter_convert;
288 iface->reset = g_compressor_converter_reset;
291 guint8 unexpanded_data[] = { 0,1,3,4,5,6,7,3,12,0,0};
296 guint8 *converted1, *converted2, *ptr;
297 gsize n_read, n_written;
300 GConverterResult cres;
302 GConverterInputStream *cstream;
303 GOutputStream *mem_out;
304 GConverterOutputStream *cstream_out;
305 GConverter *expander;
309 expander = g_expander_converter_new ();
311 converted1 = g_malloc (100*1000); /* Large enough */
312 converted2 = g_malloc (100*1000); /* Large enough */
314 cres = g_converter_convert (expander,
315 unexpanded_data, sizeof(unexpanded_data),
316 converted1, 100*1000,
317 G_CONVERTER_INPUT_AT_END,
318 &n_read, &n_written, NULL);
320 g_assert (cres == G_CONVERTER_FINISHED);
321 g_assert (n_read == 11);
322 g_assert (n_written == 41030);
324 g_converter_reset (expander);
326 mem = g_memory_input_stream_new_from_data (unexpanded_data,
327 sizeof (unexpanded_data),
329 cstream = g_converter_input_stream_new (mem, expander);
330 g_object_unref (mem);
337 res = g_input_stream_read (G_INPUT_STREAM (cstream),
340 g_assert (res != -1);
347 g_assert (total_read == n_written);
348 g_assert (memcmp (converted1, converted2, n_written) == 0);
350 g_converter_reset (expander);
352 mem_out = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
353 cstream_out = g_converter_output_stream_new (mem_out, expander);
354 g_object_unref (mem_out);
356 for (i = 0; i < sizeof(unexpanded_data); i++)
359 res = g_output_stream_write (G_OUTPUT_STREAM (cstream_out),
360 unexpanded_data + i, 1,
362 g_assert (res != -1);
365 g_assert (i == sizeof(unexpanded_data) -1);
371 g_output_stream_close (G_OUTPUT_STREAM (cstream_out), NULL, NULL);
373 g_assert (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out)) == n_written);
374 g_assert (memcmp (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mem_out)),
380 g_object_unref (cstream);
381 g_object_unref (cstream_out);
382 g_object_unref (expander);
386 test_compressor (void)
388 guint8 *converted, *expanded, *ptr;
389 gsize n_read, expanded_size;
392 GConverterResult cres;
394 GOutputStream *mem_out;
395 GConverterInputStream *cstream;
396 GConverterOutputStream *cstream_out;
397 GConverter *expander, *compressor;
401 expander = g_expander_converter_new ();
402 expanded = g_malloc (100*1000); /* Large enough */
403 cres = g_converter_convert (expander,
404 unexpanded_data, sizeof(unexpanded_data),
406 G_CONVERTER_INPUT_AT_END,
407 &n_read, &expanded_size, NULL);
408 g_assert (cres == G_CONVERTER_FINISHED);
409 g_assert (n_read == 11);
410 g_assert (expanded_size == 41030);
412 compressor = g_compressor_converter_new ();
414 converted = g_malloc (100*1000); /* Large enough */
416 mem = g_memory_input_stream_new_from_data (expanded,
419 cstream = g_converter_input_stream_new (mem, compressor);
420 g_object_unref (mem);
427 res = g_input_stream_read (G_INPUT_STREAM (cstream),
430 g_assert (res != -1);
437 g_assert (total_read == n_read - 1); /* Last 2 zeros are combined */
438 g_assert (memcmp (converted, unexpanded_data, total_read) == 0);
440 g_object_unref (cstream);
442 g_converter_reset (compressor);
444 mem_out = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
445 cstream_out = g_converter_output_stream_new (mem_out, compressor);
446 g_object_unref (mem_out);
448 for (i = 0; i < expanded_size; i++)
451 res = g_output_stream_write (G_OUTPUT_STREAM (cstream_out),
454 g_assert (res != -1);
457 g_assert (i == expanded_size -1);
463 g_output_stream_close (G_OUTPUT_STREAM (cstream_out), NULL, NULL);
465 g_assert (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out)) == n_read - 1); /* Last 2 zeros are combined */
466 g_assert (memcmp (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mem_out)),
468 g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out))) == 0);
470 g_object_unref (cstream_out);
472 g_converter_reset (compressor);
474 memset (expanded, 5, 5*1000*2);
476 mem = g_memory_input_stream_new_from_data (expanded,
479 cstream = g_converter_input_stream_new (mem, compressor);
480 g_object_unref (mem);
487 res = g_input_stream_read (G_INPUT_STREAM (cstream),
490 g_assert (res != -1);
497 g_assert (total_read == 1);
498 g_assert (*converted == 5);
500 mem = g_memory_input_stream_new_from_data (expanded,
503 cstream = g_converter_input_stream_new (mem, compressor);
504 g_object_unref (mem);
511 res = g_input_stream_read (G_INPUT_STREAM (cstream),
514 g_assert (res != -1);
521 g_assert (total_read == 2);
522 g_assert (converted[0] == 5);
523 g_assert (converted[1] == 5);
525 g_object_unref (cstream);
527 g_converter_reset (compressor);
529 mem = g_memory_input_stream_new_from_data (expanded,
532 cstream = g_converter_input_stream_new (mem, compressor);
533 g_object_unref (mem);
540 res = g_input_stream_read (G_INPUT_STREAM (cstream),
545 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT);
554 g_assert (total_read == 1);
555 g_assert (converted[0] == 5);
557 g_object_unref (cstream);
561 g_object_unref (expander);
562 g_object_unref (compressor);
570 g_test_init (&argc, &argv, NULL);
572 g_test_add_func ("/converter-input-stream/expander", test_expander);
573 g_test_add_func ("/converter-input-stream/compressor", test_compressor);