Update German translation
[glib.git] / gio / tests / readwrite.c
blob9f674c39d200e908a625307bad7135e6eb0338f3
1 #include <glib/glib.h>
2 #include <glib/gstdio.h>
3 #include <gio/gio.h>
4 #include <string.h>
6 #ifdef G_OS_UNIX
7 #include <unistd.h>
8 #endif
10 static const char *original_data = "This is some test data that we can put in a file...";
11 static const char *new_data = "new data..";
13 static void
14 verify_pos (GIOStream *iostream, goffset expected_pos)
16 goffset pos;
18 pos = g_seekable_tell (G_SEEKABLE (iostream));
19 g_assert_cmpint (pos, ==, expected_pos);
21 pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_input_stream (iostream)));
22 g_assert_cmpint (pos, ==, expected_pos);
24 pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_output_stream (iostream)));
25 g_assert_cmpint (pos, ==, expected_pos);
28 static void
29 verify_iostream (GFileIOStream *file_iostream)
31 gboolean res;
32 GIOStream *iostream;
33 GError *error;
34 GInputStream *in;
35 GOutputStream *out;
36 char buffer[1024];
37 gsize n_bytes;
38 char *modified_data;
40 iostream = G_IO_STREAM (file_iostream);
42 verify_pos (iostream, 0);
44 in = g_io_stream_get_input_stream (iostream);
45 out = g_io_stream_get_output_stream (iostream);
47 res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
48 g_assert (res);
49 g_assert_cmpmem (buffer, n_bytes, original_data, 20);
51 verify_pos (iostream, 20);
53 res = g_seekable_seek (G_SEEKABLE (iostream),
54 -10, G_SEEK_END,
55 NULL, NULL);
56 g_assert (res);
57 verify_pos (iostream, strlen (original_data) - 10);
59 res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
60 g_assert (res);
61 g_assert_cmpmem (buffer, n_bytes, original_data + strlen (original_data) - 10, 10);
63 verify_pos (iostream, strlen (original_data));
65 res = g_seekable_seek (G_SEEKABLE (iostream),
66 10, G_SEEK_SET,
67 NULL, NULL);
69 res = g_input_stream_skip (in, 5, NULL, NULL);
70 g_assert (res == 5);
71 verify_pos (iostream, 15);
73 res = g_input_stream_skip (in, 10000, NULL, NULL);
74 g_assert (res == strlen (original_data) - 15);
75 verify_pos (iostream, strlen (original_data));
77 res = g_seekable_seek (G_SEEKABLE (iostream),
78 10, G_SEEK_SET,
79 NULL, NULL);
81 verify_pos (iostream, 10);
83 res = g_output_stream_write_all (out, new_data, strlen (new_data),
84 &n_bytes, NULL, NULL);
85 g_assert (res);
86 g_assert_cmpint (n_bytes, ==, strlen (new_data));
88 verify_pos (iostream, 10 + strlen (new_data));
90 res = g_seekable_seek (G_SEEKABLE (iostream),
91 0, G_SEEK_SET,
92 NULL, NULL);
93 g_assert (res);
94 verify_pos (iostream, 0);
96 res = g_input_stream_read_all (in, buffer, strlen (original_data), &n_bytes, NULL, NULL);
97 g_assert (res);
98 g_assert_cmpint ((int)n_bytes, ==, strlen (original_data));
99 buffer[n_bytes] = 0;
101 modified_data = g_strdup (original_data);
102 memcpy (modified_data + 10, new_data, strlen (new_data));
103 g_assert_cmpstr (buffer, ==, modified_data);
105 verify_pos (iostream, strlen (original_data));
107 res = g_seekable_seek (G_SEEKABLE (iostream),
108 0, G_SEEK_SET,
109 NULL, NULL);
110 g_assert (res);
111 verify_pos (iostream, 0);
113 res = g_output_stream_close (out, NULL, NULL);
114 g_assert (res);
116 res = g_input_stream_read_all (in, buffer, 15, &n_bytes, NULL, NULL);
117 g_assert (res);
118 g_assert_cmpmem (buffer, n_bytes, modified_data, 15);
120 error = NULL;
121 res = g_output_stream_write_all (out, new_data, strlen (new_data),
122 &n_bytes, NULL, &error);
123 g_assert (!res);
124 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
125 g_error_free (error);
127 error = NULL;
128 res = g_io_stream_close (iostream, NULL, &error);
129 g_assert (res);
130 g_assert_no_error (error);
132 g_free (modified_data);
135 static void
136 test_g_file_open_readwrite (void)
138 char *tmp_file;
139 int fd;
140 gboolean res;
141 GFileIOStream *file_iostream;
142 char *path;
143 GFile *file;
144 GError *error;
146 fd = g_file_open_tmp ("readwrite_XXXXXX",
147 &tmp_file, NULL);
148 g_assert (fd != -1);
149 close (fd);
151 res = g_file_set_contents (tmp_file,
152 original_data, -1, NULL);
153 g_assert (res);
155 path = g_build_filename (g_get_tmp_dir (), "g-a-nonexisting-file", NULL);
156 file = g_file_new_for_path (path);
157 g_free (path);
158 error = NULL;
159 file_iostream = g_file_open_readwrite (file, NULL, &error);
160 g_assert (file_iostream == NULL);
161 g_assert (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND));
162 g_error_free (error);
163 g_object_unref (file);
165 file = g_file_new_for_path (tmp_file);
166 error = NULL;
167 file_iostream = g_file_open_readwrite (file, NULL, &error);
168 g_assert (file_iostream != NULL);
169 g_object_unref (file);
171 verify_iostream (file_iostream);
173 g_object_unref (file_iostream);
175 g_unlink (tmp_file);
176 g_free (tmp_file);
179 static void
180 test_g_file_create_readwrite (void)
182 char *tmp_file;
183 int fd;
184 gboolean res;
185 GFileIOStream *file_iostream;
186 GOutputStream *out;
187 GFile *file;
188 GError *error;
189 gsize n_bytes;
191 fd = g_file_open_tmp ("readwrite_XXXXXX",
192 &tmp_file, NULL);
193 g_assert (fd != -1);
194 close (fd);
196 file = g_file_new_for_path (tmp_file);
197 error = NULL;
198 file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
199 g_assert (file_iostream == NULL);
200 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
201 g_error_free (error);
203 g_unlink (tmp_file);
204 file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
205 g_assert (file_iostream != NULL);
207 out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
208 res = g_output_stream_write_all (out, original_data, strlen (original_data),
209 &n_bytes, NULL, NULL);
210 g_assert (res);
211 g_assert_cmpint (n_bytes, ==, strlen (original_data));
213 res = g_seekable_seek (G_SEEKABLE (file_iostream),
214 0, G_SEEK_SET,
215 NULL, NULL);
216 g_assert (res);
218 verify_iostream (file_iostream);
220 g_object_unref (file_iostream);
221 g_object_unref (file);
223 g_unlink (tmp_file);
224 g_free (tmp_file);
227 static void
228 test_g_file_replace_readwrite (void)
230 char *tmp_file, *backup, *data;
231 int fd;
232 gboolean res;
233 GFileIOStream *file_iostream;
234 GInputStream *in;
235 GOutputStream *out;
236 GFile *file;
237 GError *error;
238 char buffer[1024];
239 gsize n_bytes;
241 fd = g_file_open_tmp ("readwrite_XXXXXX",
242 &tmp_file, NULL);
243 g_assert (fd != -1);
244 close (fd);
246 res = g_file_set_contents (tmp_file,
247 new_data, -1, NULL);
248 g_assert (res);
250 file = g_file_new_for_path (tmp_file);
251 error = NULL;
252 file_iostream = g_file_replace_readwrite (file, NULL,
253 TRUE, 0, NULL, &error);
254 g_assert (file_iostream != NULL);
256 in = g_io_stream_get_input_stream (G_IO_STREAM (file_iostream));
258 /* Ensure its empty */
259 res = g_input_stream_read_all (in, buffer, sizeof buffer, &n_bytes, NULL, NULL);
260 g_assert (res);
261 g_assert_cmpint ((int)n_bytes, ==, 0);
263 out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
264 res = g_output_stream_write_all (out, original_data, strlen (original_data),
265 &n_bytes, NULL, NULL);
266 g_assert (res);
267 g_assert_cmpint (n_bytes, ==, strlen (original_data));
269 res = g_seekable_seek (G_SEEKABLE (file_iostream),
270 0, G_SEEK_SET,
271 NULL, NULL);
272 g_assert (res);
274 verify_iostream (file_iostream);
276 g_object_unref (file_iostream);
277 g_object_unref (file);
279 backup = g_strconcat (tmp_file, "~", NULL);
280 res = g_file_get_contents (backup,
281 &data,
282 NULL, NULL);
283 g_assert (res);
284 g_assert_cmpstr (data, ==, new_data);
285 g_free (data);
286 g_unlink (backup);
287 g_free (backup);
289 g_unlink (tmp_file);
290 g_free (tmp_file);
295 main (int argc,
296 char *argv[])
298 g_test_init (&argc, &argv, NULL);
300 g_test_add_func ("/readwrite/test_g_file_open_readwrite",
301 test_g_file_open_readwrite);
302 g_test_add_func ("/readwrite/test_g_file_create_readwrite",
303 test_g_file_create_readwrite);
304 g_test_add_func ("/readwrite/test_g_file_replace_readwrite",
305 test_g_file_replace_readwrite);
307 return g_test_run();