gconvert: add note to avoid transliteration
[glib.git] / tests / mapping-test.c
bloba85cf8038294a03799e226174f3ec5d3dbef0422
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 2005 Matthias Clasen
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include <signal.h>
22 #include "glib.h"
23 #include "gstdio.h"
25 #ifdef G_OS_UNIX
26 #include <unistd.h>
27 #endif
29 static gchar *dir, *filename, *displayname, *childname;
31 static gboolean stop = FALSE;
33 #ifndef G_OS_WIN32
35 static void
36 handle_usr1 (int signum)
38 stop = TRUE;
41 #endif
43 static gboolean
44 check_stop (gpointer data)
46 GMainLoop *loop = data;
48 #ifdef G_OS_WIN32
49 stop = g_file_test ("STOP", G_FILE_TEST_EXISTS);
50 #endif
52 if (stop)
53 g_main_loop_quit (loop);
55 return TRUE;
58 static void
59 write_or_die (const gchar *filename,
60 const gchar *contents,
61 gssize length)
63 GError *error = NULL;
64 gchar *displayname;
66 if (!g_file_set_contents (filename, contents, length, &error))
68 displayname = g_filename_display_name (childname);
69 g_print ("failed to write '%s': %s\n",
70 displayname, error->message);
71 exit (1);
75 static GMappedFile *
76 map_or_die (const gchar *filename,
77 gboolean writable)
79 GError *error = NULL;
80 GMappedFile *map;
81 gchar *displayname;
83 map = g_mapped_file_new (filename, writable, &error);
84 if (!map)
86 displayname = g_filename_display_name (childname);
87 g_print ("failed to map '%s' non-writable, shared: %s\n",
88 displayname, error->message);
89 exit (1);
92 return map;
95 static int
96 child_main (int argc, char *argv[])
98 GMappedFile *map;
99 GMainLoop *loop;
101 map = map_or_die (filename, FALSE);
103 loop = g_main_loop_new (NULL, FALSE);
105 #ifndef G_OS_WIN32
106 signal (SIGUSR1, handle_usr1);
107 #endif
108 g_idle_add (check_stop, loop);
109 g_main_loop_run (loop);
111 write_or_die (childname,
112 g_mapped_file_get_contents (map),
113 g_mapped_file_get_length (map));
115 return 0;
118 static void
119 test_mapping (void)
121 GMappedFile *map;
123 write_or_die (filename, "ABC", -1);
125 map = map_or_die (filename, FALSE);
126 g_assert (g_mapped_file_get_length (map) == 3);
127 g_mapped_file_free (map);
129 map = map_or_die (filename, TRUE);
130 g_assert (g_mapped_file_get_length (map) == 3);
131 g_mapped_file_free (map);
134 static void
135 test_private (void)
137 GError *error = NULL;
138 GMappedFile *map;
139 gchar *buffer;
140 gsize len;
142 write_or_die (filename, "ABC", -1);
143 map = map_or_die (filename, TRUE);
145 buffer = (gchar *)g_mapped_file_get_contents (map);
146 buffer[0] = '1';
147 buffer[1] = '2';
148 buffer[2] = '3';
149 g_mapped_file_free (map);
151 if (!g_file_get_contents (filename, &buffer, &len, &error))
153 g_print ("failed to read '%s': %s\n",
154 displayname, error->message);
155 exit (1);
158 g_assert (len == 3);
159 g_assert (strcmp (buffer, "ABC") == 0);
160 g_free (buffer);
164 static void
165 test_child_private (gchar *argv0)
167 GError *error = NULL;
168 GMappedFile *map;
169 gchar *buffer;
170 gsize len;
171 gchar *child_argv[3];
172 GPid child_pid;
174 #ifdef G_OS_WIN32
175 g_remove ("STOP");
176 g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS));
177 #endif
179 write_or_die (filename, "ABC", -1);
180 map = map_or_die (filename, TRUE);
182 child_argv[0] = argv0;
183 child_argv[1] = "mapchild";
184 child_argv[2] = NULL;
185 if (!g_spawn_async (dir, child_argv, NULL,
186 0, NULL, NULL, &child_pid, &error))
188 g_print ("failed to spawn child: %s\n",
189 error->message);
190 exit (1);
193 /* give the child some time to set up its mapping */
194 g_usleep (2000000);
196 buffer = (gchar *)g_mapped_file_get_contents (map);
197 buffer[0] = '1';
198 buffer[1] = '2';
199 buffer[2] = '3';
200 g_mapped_file_free (map);
202 #ifndef G_OS_WIN32
203 kill (child_pid, SIGUSR1);
204 #else
205 g_file_set_contents ("STOP", "Hey there\n", -1, NULL);
206 #endif
208 /* give the child some time to write the file */
209 g_usleep (2000000);
211 if (!g_file_get_contents (childname, &buffer, &len, &error))
213 gchar *name;
215 name = g_filename_display_name (childname);
216 g_print ("failed to read '%s': %s\n", name, error->message);
217 exit (1);
219 g_assert (len == 3);
220 g_assert (strcmp (buffer, "ABC") == 0);
221 g_free (buffer);
224 static int
225 parent_main (int argc,
226 char *argv[])
228 /* test mapping with various flag combinations */
229 test_mapping ();
231 /* test private modification */
232 test_private ();
234 /* test multiple clients, non-shared */
235 test_child_private (argv[0]);
237 return 0;
241 main (int argc,
242 char *argv[])
244 int ret;
246 dir = g_get_current_dir ();
247 filename = g_build_filename (dir, "maptest", NULL);
248 displayname = g_filename_display_name (filename);
249 childname = g_build_filename (dir, "mapchild", NULL);
251 if (argc > 1)
252 ret = child_main (argc, argv);
253 else
254 ret = parent_main (argc, argv);
256 g_free (childname);
257 g_free (filename);
258 g_free (displayname);
259 g_free (dir);
261 return ret;