1 #define GLIB_DISABLE_DEPRECATION_WARNINGS
5 #include <glib/gstdio.h>
24 file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
25 g_assert_no_error (error);
27 g_mapped_file_ref (file);
28 g_mapped_file_unref (file);
30 g_mapped_file_unref (file);
40 file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
41 g_assert_no_error (error);
43 g_assert (g_mapped_file_get_contents (file) == NULL);
45 g_mapped_file_free (file);
55 file = g_mapped_file_new ("/dev/null", FALSE, &error);
56 g_assert (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL) ||
57 g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM));
58 g_assert (file == NULL);
64 test_nonexisting (void)
70 file = g_mapped_file_new ("no-such-file", FALSE, &error);
71 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
72 g_clear_error (&error);
73 g_assert (file == NULL);
83 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
84 const gchar *new = "abcdefghijklmnopqrstuvxyz";
87 tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
89 g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
90 g_assert_no_error (error);
91 g_file_set_contents (tmp_copy_path, contents, len, &error);
92 g_assert_no_error (error);
96 file = g_mapped_file_new (tmp_copy_path, TRUE, &error);
97 g_assert_no_error (error);
99 contents = g_mapped_file_get_contents (file);
100 g_assert (strncmp (contents, old, strlen (old)) == 0);
102 memcpy (contents, new, strlen (new));
103 g_assert (strncmp (contents, new, strlen (new)) == 0);
105 g_mapped_file_free (file);
108 file = g_mapped_file_new (tmp_copy_path, FALSE, &error);
109 g_assert_no_error (error);
111 contents = g_mapped_file_get_contents (file);
112 g_assert (strncmp (contents, old, strlen (old)) == 0);
114 g_mapped_file_free (file);
116 g_free (tmp_copy_path);
120 test_writable_fd (void)
123 GError *error = NULL;
125 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
126 const gchar *new = "abcdefghijklmnopqrstuvxyz";
129 gchar *tmp_copy_path;
131 tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
133 g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
134 g_assert_no_error (error);
135 g_file_set_contents (tmp_copy_path, contents, len, &error);
136 g_assert_no_error (error);
140 fd = g_open (tmp_copy_path, O_RDWR, 0);
142 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
143 g_assert_no_error (error);
145 contents = g_mapped_file_get_contents (file);
146 g_assert (strncmp (contents, old, strlen (old)) == 0);
148 memcpy (contents, new, strlen (new));
149 g_assert (strncmp (contents, new, strlen (new)) == 0);
151 g_mapped_file_free (file);
155 fd = g_open (tmp_copy_path, O_RDWR, 0);
157 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
158 g_assert_no_error (error);
160 contents = g_mapped_file_get_contents (file);
161 g_assert (strncmp (contents, old, strlen (old)) == 0);
163 g_mapped_file_free (file);
165 g_free (tmp_copy_path);
176 file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
177 g_assert_no_error (error);
179 bytes = g_mapped_file_get_bytes (file);
180 g_mapped_file_unref (file);
182 g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
183 g_bytes_unref (bytes);
187 main (int argc, char *argv[])
189 g_test_init (&argc, &argv, NULL);
191 g_test_add_func ("/mappedfile/basic", test_basic);
192 g_test_add_func ("/mappedfile/empty", test_empty);
194 g_test_add_func ("/mappedfile/device", test_device);
196 g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
197 g_test_add_func ("/mappedfile/writable", test_writable);
198 g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
199 g_test_add_func ("/mappedfile/gbytes", test_gbytes);
201 return g_test_run ();