docs: Add GIOModuleScope and GIOModuleScopeFlags
[glib.git] / gio / tests / memory-input-stream.c
blob753dfe9c6b9d18424e8654428f40cde34fef6d94
1 /* GLib testing framework examples and tests
2 * Copyright (C) 2007 Imendio AB
3 * Authors: Tim Janik
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>
24 #include <gio/gio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 static void
29 test_read_chunks (void)
31 const char *data1 = "abcdefghijklmnopqrstuvwxyz";
32 const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
33 const char *result = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
34 char buffer[128];
35 gsize bytes_read, pos, len, chunk_size;
36 GError *error = NULL;
37 GInputStream *stream;
38 gboolean res;
40 stream = g_memory_input_stream_new ();
42 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
43 data1, -1, NULL);
44 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
45 data2, -1, NULL);
46 len = strlen (data1) + strlen (data2);
48 for (chunk_size = 1; chunk_size < len - 1; chunk_size++)
50 pos = 0;
51 while (pos < len)
53 bytes_read = g_input_stream_read (stream, buffer, chunk_size, NULL, &error);
54 g_assert_no_error (error);
55 g_assert_cmpint (bytes_read, ==, MIN (chunk_size, len - pos));
56 g_assert (strncmp (buffer, result + pos, bytes_read) == 0);
58 pos += bytes_read;
61 g_assert_cmpint (pos, ==, len);
62 res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
63 g_assert_cmpint (res, ==, TRUE);
64 g_assert_no_error (error);
67 g_object_unref (stream);
70 GMainLoop *loop;
72 static void
73 async_read_chunk (GObject *object,
74 GAsyncResult *result,
75 gpointer user_data)
77 gsize *bytes_read = user_data;
78 GError *error = NULL;
80 *bytes_read = g_input_stream_read_finish (G_INPUT_STREAM (object),
81 result, &error);
82 g_assert_no_error (error);
84 g_main_loop_quit (loop);
87 static void
88 test_async (void)
90 const char *data1 = "abcdefghijklmnopqrstuvwxyz";
91 const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
92 const char *result = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
93 char buffer[128];
94 gsize bytes_read, pos, len, chunk_size;
95 GError *error = NULL;
96 GInputStream *stream;
97 gboolean res;
99 loop = g_main_loop_new (NULL, FALSE);
101 stream = g_memory_input_stream_new ();
103 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
104 data1, -1, NULL);
105 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
106 data2, -1, NULL);
107 len = strlen (data1) + strlen (data2);
109 for (chunk_size = 1; chunk_size < len - 1; chunk_size++)
111 pos = 0;
112 while (pos < len)
114 g_input_stream_read_async (stream, buffer, chunk_size,
115 G_PRIORITY_DEFAULT, NULL,
116 async_read_chunk, &bytes_read);
117 g_main_loop_run (loop);
119 g_assert_cmpint (bytes_read, ==, MIN (chunk_size, len - pos));
120 g_assert (strncmp (buffer, result + pos, bytes_read) == 0);
122 pos += bytes_read;
125 g_assert_cmpint (pos, ==, len);
126 res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
127 g_assert_cmpint (res, ==, TRUE);
128 g_assert_no_error (error);
131 g_object_unref (stream);
132 g_main_loop_unref (loop);
135 static void
136 test_seek (void)
138 const char *data1 = "abcdefghijklmnopqrstuvwxyz";
139 const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
140 GInputStream *stream;
141 GError *error;
142 char buffer[10];
144 stream = g_memory_input_stream_new ();
146 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
147 data1, -1, NULL);
148 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
149 data2, -1, NULL);
151 g_assert (G_IS_SEEKABLE (stream));
152 g_assert (g_seekable_can_seek (G_SEEKABLE (stream)));
154 error = NULL;
155 g_assert (g_seekable_seek (G_SEEKABLE (stream), 26, G_SEEK_SET, NULL, &error));
156 g_assert_no_error (error);
157 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 26);
159 g_assert (g_input_stream_read (stream, buffer, 1, NULL, &error) == 1);
160 g_assert_no_error (error);
162 g_assert (buffer[0] == 'A');
164 g_assert (!g_seekable_seek (G_SEEKABLE (stream), 26, G_SEEK_CUR, NULL, &error));
165 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
166 g_error_free (error);
168 g_object_unref (stream);
171 static void
172 test_truncate (void)
174 const char *data1 = "abcdefghijklmnopqrstuvwxyz";
175 const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
176 GInputStream *stream;
177 GError *error;
179 stream = g_memory_input_stream_new ();
181 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
182 data1, -1, NULL);
183 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
184 data2, -1, NULL);
186 g_assert (G_IS_SEEKABLE (stream));
187 g_assert (!g_seekable_can_truncate (G_SEEKABLE (stream)));
189 error = NULL;
190 g_assert (!g_seekable_truncate (G_SEEKABLE (stream), 26, NULL, &error));
191 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
192 g_error_free (error);
194 g_object_unref (stream);
198 main (int argc,
199 char *argv[])
201 g_type_init ();
202 g_test_init (&argc, &argv, NULL);
204 g_test_add_func ("/memory-input-stream/read-chunks", test_read_chunks);
205 g_test_add_func ("/memory-input-stream/async", test_async);
206 g_test_add_func ("/memory-input-stream/seek", test_seek);
207 g_test_add_func ("/memory-input-stream/truncate", test_truncate);
209 return g_test_run();