1 #define GLIB_DISABLE_DEPRECATION_WARNINGS
9 #include <glib/gstdio.h>
11 #include <sys/types.h>
21 file = g_mapped_file_new (g_test_get_filename (G_TEST_DISTED, "empty", NULL), FALSE, &error);
22 g_assert_no_error (error);
24 g_mapped_file_ref (file);
25 g_mapped_file_unref (file);
27 g_mapped_file_unref (file);
37 file = g_mapped_file_new (g_test_get_filename (G_TEST_DISTED, "empty", NULL), FALSE, &error);
38 g_assert_no_error (error);
40 g_assert (g_mapped_file_get_contents (file) == NULL);
42 g_mapped_file_free (file);
52 file = g_mapped_file_new ("/dev/null", FALSE, &error);
53 g_assert (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL) ||
54 g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM));
55 g_assert (file == NULL);
61 test_nonexisting (void)
67 file = g_mapped_file_new ("no-such-file", FALSE, &error);
68 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
69 g_clear_error (&error);
70 g_assert (file == NULL);
80 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
81 const gchar *new = "abcdefghijklmnopqrstuvxyz";
84 tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
86 g_file_get_contents (g_test_get_filename (G_TEST_DISTED, "4096-random-bytes", NULL), &contents, &len, &error);
87 g_assert_no_error (error);
88 g_file_set_contents (tmp_copy_path, contents, len, &error);
89 g_assert_no_error (error);
93 file = g_mapped_file_new (tmp_copy_path, TRUE, &error);
94 g_assert_no_error (error);
96 contents = g_mapped_file_get_contents (file);
97 g_assert (strncmp (contents, old, strlen (old)) == 0);
99 memcpy (contents, new, strlen (new));
100 g_assert (strncmp (contents, new, strlen (new)) == 0);
102 g_mapped_file_free (file);
105 file = g_mapped_file_new (tmp_copy_path, FALSE, &error);
106 g_assert_no_error (error);
108 contents = g_mapped_file_get_contents (file);
109 g_assert (strncmp (contents, old, strlen (old)) == 0);
111 g_mapped_file_free (file);
113 g_free (tmp_copy_path);
117 test_writable_fd (void)
120 GError *error = NULL;
122 const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
123 const gchar *new = "abcdefghijklmnopqrstuvxyz";
126 gchar *tmp_copy_path;
128 tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL);
130 g_file_get_contents (g_test_get_filename (G_TEST_DISTED, "4096-random-bytes", NULL), &contents, &len, &error);
131 g_assert_no_error (error);
132 g_file_set_contents (tmp_copy_path, contents, len, &error);
133 g_assert_no_error (error);
137 fd = g_open (tmp_copy_path, O_RDWR, 0);
139 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
140 g_assert_no_error (error);
142 contents = g_mapped_file_get_contents (file);
143 g_assert (strncmp (contents, old, strlen (old)) == 0);
145 memcpy (contents, new, strlen (new));
146 g_assert (strncmp (contents, new, strlen (new)) == 0);
148 g_mapped_file_free (file);
152 fd = g_open (tmp_copy_path, O_RDWR, 0);
154 file = g_mapped_file_new_from_fd (fd, TRUE, &error);
155 g_assert_no_error (error);
157 contents = g_mapped_file_get_contents (file);
158 g_assert (strncmp (contents, old, strlen (old)) == 0);
160 g_mapped_file_free (file);
162 g_free (tmp_copy_path);
173 file = g_mapped_file_new (g_test_get_filename (G_TEST_DISTED, "empty", NULL), FALSE, &error);
174 g_assert_no_error (error);
176 bytes = g_mapped_file_get_bytes (file);
177 g_mapped_file_unref (file);
179 g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
180 g_bytes_unref (bytes);
184 main (int argc, char *argv[])
186 g_test_init (&argc, &argv, NULL);
188 g_test_add_func ("/mappedfile/basic", test_basic);
189 g_test_add_func ("/mappedfile/empty", test_empty);
191 g_test_add_func ("/mappedfile/device", test_device);
193 g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
194 g_test_add_func ("/mappedfile/writable", test_writable);
195 g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
196 g_test_add_func ("/mappedfile/gbytes", test_gbytes);
198 return g_test_run ();