Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / tests / memory-output-stream.c
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2008 Red Hat, Inc.
3  * Author: Matthias Clasen
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 #include <glib/glib.h>
23 #include <gio/gio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 static void
28 test_truncate (void)
29 {
30   GOutputStream *mo;
31   GDataOutputStream *o;
32   int i;
33   GError *error = NULL;
34
35   g_test_bug ("540423");
36
37   mo = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
38   o = g_data_output_stream_new (mo);
39   for (i = 0; i < 1000; i++)
40     {
41       g_data_output_stream_put_byte (o, 1, NULL, &error);
42       g_assert_no_error (error);
43     }
44   g_seekable_truncate (G_SEEKABLE (mo), 0, NULL, &error);
45   g_assert_no_error (error);
46   for (i = 0; i < 2000; i++)
47     {
48       g_data_output_stream_put_byte (o, 1, NULL, &error);
49       g_assert_no_error (error);
50     }
51
52   g_object_unref (o);
53   g_object_unref (mo);
54 }
55
56 static void
57 test_data_size (void)
58 {
59   GOutputStream *mo;
60   GDataOutputStream *o;
61   int pos;
62
63   g_test_bug ("540459");
64
65   mo = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
66   o = g_data_output_stream_new (mo);
67   g_data_output_stream_put_byte (o, 1, NULL, NULL);
68   pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
69   g_assert_cmpint (pos, ==, 1);
70
71   g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_CUR, NULL, NULL);
72   pos = g_seekable_tell (G_SEEKABLE (mo));
73   g_assert_cmpint (pos, ==, 1);
74
75   g_test_bug ("540461");
76   
77   g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_SET, NULL, NULL);
78   pos = g_seekable_tell (G_SEEKABLE (mo));
79   g_assert_cmpint (pos, ==, 0);
80   
81   pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
82   g_assert_cmpint (pos, ==, 1);
83   
84   g_object_unref (o);
85   g_object_unref (mo);
86 }
87
88 static void
89 test_properties (void)
90 {
91   GOutputStream *mo;
92   GDataOutputStream *o;
93   int i;
94   GError *error = NULL;
95
96   g_test_bug ("605733");
97
98   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
99                                       "realloc-function", g_realloc,
100                                       "destroy-function", g_free,
101                                       NULL);
102   o = g_data_output_stream_new (mo);
103
104   for (i = 0; i < 1000; i++)
105     {
106       g_data_output_stream_put_byte (o, 1, NULL, &error);
107       g_assert_no_error (error);
108     }
109
110   gsize data_size_fun = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
111   gsize data_size_prop;
112   g_object_get (mo, "data-size", &data_size_prop, NULL);
113   g_assert_cmpint (data_size_fun, ==, data_size_prop);
114
115   gpointer data_fun = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
116   gpointer data_prop;
117   g_object_get (mo, "data", &data_prop, NULL);
118   g_assert_cmphex ((guint) data_fun, ==, (guint) data_prop);
119
120   g_object_unref (o);
121   g_object_unref (mo);
122 }
123
124 int
125 main (int   argc,
126       char *argv[])
127 {
128   g_type_init ();
129   g_test_init (&argc, &argv, NULL);
130   g_test_bug_base ("http://bugzilla.gnome.org/");
131
132   g_test_add_func ("/memory-output-stream/truncate", test_truncate);
133   g_test_add_func ("/memory-output-stream/get-data-size", test_data_size);
134   g_test_add_func ("/memory-output-stream/properties", test_properties);
135
136   return g_test_run();
137 }