Fix non-srcdir builds
[platform/upstream/glib.git] / glib / tests / mappedfile.c
1 #include <glib.h>
2 #include <string.h>
3
4 static void
5 test_empty (void)
6 {
7   GMappedFile *file;
8   GError *error;
9
10   error = NULL;
11   file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
12   g_assert_no_error (error);
13
14   g_assert (g_mapped_file_get_contents (file) == NULL);
15
16   g_mapped_file_free (file);
17 }
18
19 static void
20 test_nonexisting (void)
21 {
22   GMappedFile *file;
23   GError *error;
24
25   error = NULL;
26   file = g_mapped_file_new ("no-such-file", FALSE, &error);
27   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
28   g_clear_error (&error);
29   g_assert (file == NULL);
30 }
31
32 static void
33 test_writable (void)
34 {
35   GMappedFile *file;
36   GError *error;
37   gchar *contents;
38   const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
39   const gchar *new = "abcdefghijklmnopqrstuvxyz";
40
41   error = NULL;
42   file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
43   g_assert_no_error (error);
44
45   contents = g_mapped_file_get_contents (file);
46   g_assert (strncmp (contents, old, strlen (old)) == 0);
47
48   memcpy (contents, new, strlen (new));
49   g_assert (strncmp (contents, new, strlen (new)) == 0);
50
51   g_mapped_file_free (file);
52
53   error = NULL;
54   file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
55   g_assert_no_error (error);
56
57   contents = g_mapped_file_get_contents (file);
58   g_assert (strncmp (contents, old, strlen (old)) == 0);
59
60   g_mapped_file_free (file);
61 }
62
63 int
64 main (int argc, char *argv[])
65 {
66   g_test_init (&argc, &argv, NULL);
67
68   g_test_add_func ("/mappedfile/empty", test_empty);
69   g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
70   g_test_add_func ("/mappedfile/writable", test_writable);
71
72   return g_test_run ();
73 }