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_NODEV
) ||
58 g_error_matches (error
, G_FILE_ERROR
, G_FILE_ERROR_NOMEM
));
59 g_assert (file
== NULL
);
65 test_nonexisting (void)
71 file
= g_mapped_file_new ("no-such-file", FALSE
, &error
);
72 g_assert_error (error
, G_FILE_ERROR
, G_FILE_ERROR_NOENT
);
73 g_clear_error (&error
);
74 g_assert (file
== NULL
);
84 const gchar
*old
= "MMMMMMMMMMMMMMMMMMMMMMMMM";
85 const gchar
*new = "abcdefghijklmnopqrstuvxyz";
88 tmp_copy_path
= g_build_filename (g_get_tmp_dir (), "glib-test-4096-random-bytes", NULL
);
90 g_file_get_contents (g_test_get_filename (G_TEST_DIST
, "4096-random-bytes", NULL
), &contents
, &len
, &error
);
91 g_assert_no_error (error
);
92 g_file_set_contents (tmp_copy_path
, contents
, len
, &error
);
93 g_assert_no_error (error
);
97 file
= g_mapped_file_new (tmp_copy_path
, TRUE
, &error
);
98 g_assert_no_error (error
);
100 contents
= g_mapped_file_get_contents (file
);
101 g_assert (strncmp (contents
, old
, strlen (old
)) == 0);
103 memcpy (contents
, new, strlen (new));
104 g_assert (strncmp (contents
, new, strlen (new)) == 0);
106 g_mapped_file_free (file
);
109 file
= g_mapped_file_new (tmp_copy_path
, FALSE
, &error
);
110 g_assert_no_error (error
);
112 contents
= g_mapped_file_get_contents (file
);
113 g_assert (strncmp (contents
, old
, strlen (old
)) == 0);
115 g_mapped_file_free (file
);
117 g_free (tmp_copy_path
);
121 test_writable_fd (void)
124 GError
*error
= NULL
;
126 const gchar
*old
= "MMMMMMMMMMMMMMMMMMMMMMMMM";
127 const gchar
*new = "abcdefghijklmnopqrstuvxyz";
130 gchar
*tmp_copy_path
;
132 tmp_copy_path
= g_build_filename (g_get_tmp_dir (), "glib-test-4096-random-bytes", NULL
);
134 g_file_get_contents (g_test_get_filename (G_TEST_DIST
, "4096-random-bytes", NULL
), &contents
, &len
, &error
);
135 g_assert_no_error (error
);
136 g_file_set_contents (tmp_copy_path
, contents
, len
, &error
);
137 g_assert_no_error (error
);
141 fd
= g_open (tmp_copy_path
, O_RDWR
, 0);
143 file
= g_mapped_file_new_from_fd (fd
, TRUE
, &error
);
144 g_assert_no_error (error
);
146 contents
= g_mapped_file_get_contents (file
);
147 g_assert (strncmp (contents
, old
, strlen (old
)) == 0);
149 memcpy (contents
, new, strlen (new));
150 g_assert (strncmp (contents
, new, strlen (new)) == 0);
152 g_mapped_file_free (file
);
156 fd
= g_open (tmp_copy_path
, O_RDWR
, 0);
158 file
= g_mapped_file_new_from_fd (fd
, TRUE
, &error
);
159 g_assert_no_error (error
);
161 contents
= g_mapped_file_get_contents (file
);
162 g_assert (strncmp (contents
, old
, strlen (old
)) == 0);
164 g_mapped_file_free (file
);
166 g_free (tmp_copy_path
);
177 file
= g_mapped_file_new (g_test_get_filename (G_TEST_DIST
, "empty", NULL
), FALSE
, &error
);
178 g_assert_no_error (error
);
180 bytes
= g_mapped_file_get_bytes (file
);
181 g_mapped_file_unref (file
);
183 g_assert_cmpint (g_bytes_get_size (bytes
), ==, 0);
184 g_bytes_unref (bytes
);
188 main (int argc
, char *argv
[])
190 g_test_init (&argc
, &argv
, NULL
);
192 g_test_add_func ("/mappedfile/basic", test_basic
);
193 g_test_add_func ("/mappedfile/empty", test_empty
);
195 g_test_add_func ("/mappedfile/device", test_device
);
197 g_test_add_func ("/mappedfile/nonexisting", test_nonexisting
);
198 g_test_add_func ("/mappedfile/writable", test_writable
);
199 g_test_add_func ("/mappedfile/writable_fd", test_writable_fd
);
200 g_test_add_func ("/mappedfile/gbytes", test_gbytes
);
202 return g_test_run ();