1 #define GLIB_DISABLE_DEPRECATION_WARNINGS
8 #include <glib/gstdio.h>
10 #include <sys/types.h>
20 file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
21 g_assert_no_error (error);
23 g_mapped_file_ref (file);
24 g_mapped_file_unref (file);
26 g_mapped_file_unref (file);
36 file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
37 g_assert_no_error (error);
39 g_assert (g_mapped_file_get_contents (file) == NULL);
41 g_mapped_file_free (file);
51 file = g_mapped_file_new ("/dev/null", FALSE, &error);
52 g_assert (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL) ||
53 g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM));
54 g_assert (file == NULL);
60 test_nonexisting (void)
66 file = g_mapped_file_new ("no-such-file", FALSE, &error);
67 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
68 g_clear_error (&error);
69 g_assert (file == NULL);
79 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
80 const gchar *new = "abcdefghijklmnopqrstuvxyz";
83 tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
85 g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
86 g_assert_no_error (error);
87 g_file_set_contents (tmp_copy_path, contents, len, &error);
88 g_assert_no_error (error);
92 file = g_mapped_file_new (tmp_copy_path, TRUE, &error);
93 g_assert_no_error (error);
95 contents = g_mapped_file_get_contents (file);
96 g_assert (strncmp (contents, old, strlen (old)) == 0);
98 memcpy (contents, new, strlen (new));
99 g_assert (strncmp (contents, new, strlen (new)) == 0);
101 g_mapped_file_free (file);
104 file = g_mapped_file_new (tmp_copy_path, FALSE, &error);
105 g_assert_no_error (error);
107 contents = g_mapped_file_get_contents (file);
108 g_assert (strncmp (contents, old, strlen (old)) == 0);
110 g_mapped_file_free (file);
112 g_free (tmp_copy_path);
116 test_writable_fd (void)
119 GError *error = NULL;
121 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
122 const gchar *new = "abcdefghijklmnopqrstuvxyz";
125 gchar *tmp_copy_path;
127 tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
129 g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
130 g_assert_no_error (error);
131 g_file_set_contents (tmp_copy_path, contents, len, &error);
132 g_assert_no_error (error);
136 fd = g_open (tmp_copy_path, O_RDWR, 0);
138 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
139 g_assert_no_error (error);
141 contents = g_mapped_file_get_contents (file);
142 g_assert (strncmp (contents, old, strlen (old)) == 0);
144 memcpy (contents, new, strlen (new));
145 g_assert (strncmp (contents, new, strlen (new)) == 0);
147 g_mapped_file_free (file);
151 fd = g_open (tmp_copy_path, O_RDWR, 0);
153 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
154 g_assert_no_error (error);
156 contents = g_mapped_file_get_contents (file);
157 g_assert (strncmp (contents, old, strlen (old)) == 0);
159 g_mapped_file_free (file);
161 g_free (tmp_copy_path);
172 file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
173 g_assert_no_error (error);
175 bytes = g_mapped_file_get_bytes (file);
176 g_mapped_file_unref (file);
178 g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
179 g_bytes_unref (bytes);
183 main (int argc, char *argv[])
185 g_test_init (&argc, &argv, NULL);
187 g_test_add_func ("/mappedfile/basic", test_basic);
188 g_test_add_func ("/mappedfile/empty", test_empty);
190 g_test_add_func ("/mappedfile/device", test_device);
192 g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
193 g_test_add_func ("/mappedfile/writable", test_writable);
194 g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
195 g_test_add_func ("/mappedfile/gbytes", test_gbytes);
197 return g_test_run ();