glib/gwin32.c: Silence a Deprecation Warning
[glib.git] / tests / mapping-test.c
blobc21b21f13754b961d4d6674019658e5a3fb8ab6d
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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <signal.h>
24 #include "glib.h"
25 #include "gstdio.h"
27 #ifdef G_OS_UNIX
28 #include <unistd.h>
29 #endif
31 static gchar *dir, *filename, *displayname, *childname;
33 static gboolean stop = FALSE;
35 #ifndef G_OS_WIN32
37 static void
38 handle_usr1 (int signum)
40 stop = TRUE;
43 #endif
45 static gboolean
46 check_stop (gpointer data)
48 GMainLoop *loop = data;
50 #ifdef G_OS_WIN32
51 stop = g_file_test ("STOP", G_FILE_TEST_EXISTS);
52 #endif
54 if (stop)
55 g_main_loop_quit (loop);
57 return TRUE;
60 static void
61 write_or_die (const gchar *filename,
62 const gchar *contents,
63 gssize length)
65 GError *error = NULL;
66 gchar *displayname;
68 if (!g_file_set_contents (filename, contents, length, &error))
70 displayname = g_filename_display_name (childname);
71 g_print ("failed to write '%s': %s\n",
72 displayname, error->message);
73 exit (1);
77 static GMappedFile *
78 map_or_die (const gchar *filename,
79 gboolean writable)
81 GError *error = NULL;
82 GMappedFile *map;
83 gchar *displayname;
85 map = g_mapped_file_new (filename, writable, &error);
86 if (!map)
88 displayname = g_filename_display_name (childname);
89 g_print ("failed to map '%s' non-writable, shared: %s\n",
90 displayname, error->message);
91 exit (1);
94 return map;
97 static int
98 child_main (int argc, char *argv[])
100 GMappedFile *map;
101 GMainLoop *loop;
103 map = map_or_die (filename, FALSE);
105 loop = g_main_loop_new (NULL, FALSE);
107 #ifndef G_OS_WIN32
108 signal (SIGUSR1, handle_usr1);
109 #endif
110 g_idle_add (check_stop, loop);
111 g_main_loop_run (loop);
113 write_or_die (childname,
114 g_mapped_file_get_contents (map),
115 g_mapped_file_get_length (map));
117 return 0;
120 static void
121 test_mapping (void)
123 GMappedFile *map;
125 write_or_die (filename, "ABC", -1);
127 map = map_or_die (filename, FALSE);
128 g_assert (g_mapped_file_get_length (map) == 3);
129 g_mapped_file_free (map);
131 map = map_or_die (filename, TRUE);
132 g_assert (g_mapped_file_get_length (map) == 3);
133 g_mapped_file_free (map);
136 static void
137 test_private (void)
139 GError *error = NULL;
140 GMappedFile *map;
141 gchar *buffer;
142 gsize len;
144 write_or_die (filename, "ABC", -1);
145 map = map_or_die (filename, TRUE);
147 buffer = (gchar *)g_mapped_file_get_contents (map);
148 buffer[0] = '1';
149 buffer[1] = '2';
150 buffer[2] = '3';
151 g_mapped_file_free (map);
153 if (!g_file_get_contents (filename, &buffer, &len, &error))
155 g_print ("failed to read '%s': %s\n",
156 displayname, error->message);
157 exit (1);
160 g_assert (len == 3);
161 g_assert (strcmp (buffer, "ABC") == 0);
162 g_free (buffer);
166 static void
167 test_child_private (gchar *argv0)
169 GError *error = NULL;
170 GMappedFile *map;
171 gchar *buffer;
172 gsize len;
173 gchar *child_argv[3];
174 GPid child_pid;
176 #ifdef G_OS_WIN32
177 g_remove ("STOP");
178 g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS));
179 #endif
181 write_or_die (filename, "ABC", -1);
182 map = map_or_die (filename, TRUE);
184 child_argv[0] = argv0;
185 child_argv[1] = "mapchild";
186 child_argv[2] = NULL;
187 if (!g_spawn_async (dir, child_argv, NULL,
188 0, NULL, NULL, &child_pid, &error))
190 g_print ("failed to spawn child: %s\n",
191 error->message);
192 exit (1);
195 /* give the child some time to set up its mapping */
196 g_usleep (2000000);
198 buffer = (gchar *)g_mapped_file_get_contents (map);
199 buffer[0] = '1';
200 buffer[1] = '2';
201 buffer[2] = '3';
202 g_mapped_file_free (map);
204 #ifndef G_OS_WIN32
205 kill (child_pid, SIGUSR1);
206 #else
207 g_file_set_contents ("STOP", "Hey there\n", -1, NULL);
208 #endif
210 /* give the child some time to write the file */
211 g_usleep (2000000);
213 if (!g_file_get_contents (childname, &buffer, &len, &error))
215 gchar *name;
217 name = g_filename_display_name (childname);
218 g_print ("failed to read '%s': %s\n", name, error->message);
219 exit (1);
221 g_assert (len == 3);
222 g_assert (strcmp (buffer, "ABC") == 0);
223 g_free (buffer);
226 static int
227 parent_main (int argc,
228 char *argv[])
230 /* test mapping with various flag combinations */
231 test_mapping ();
233 /* test private modification */
234 test_private ();
236 /* test multiple clients, non-shared */
237 test_child_private (argv[0]);
239 return 0;
243 main (int argc,
244 char *argv[])
246 int ret;
248 dir = g_get_current_dir ();
249 filename = g_build_filename (dir, "maptest", NULL);
250 displayname = g_filename_display_name (filename);
251 childname = g_build_filename (dir, "mapchild", NULL);
253 if (argc > 1)
254 ret = child_main (argc, argv);
255 else
256 ret = parent_main (argc, argv);
258 g_free (childname);
259 g_free (filename);
260 g_free (displayname);
261 g_free (dir);
263 return ret;