Merge remote-tracking branch 'gvdb/master'
[platform/upstream/glib.git] / glib / tests / mappedfile.c
1 #include <config.h>
2 #include <glib.h>
3 #include <string.h>
4 #ifdef HAVE_UNISTD_H
5 #include <unistd.h>
6 #endif
7
8 static void
9 test_basic (void)
10 {
11   GMappedFile *file;
12   GError *error;
13
14   error = NULL;
15   file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
16   g_assert_no_error (error);
17
18   g_mapped_file_ref (file);
19   g_mapped_file_unref (file);
20
21   g_mapped_file_unref (file);
22 }
23
24 static void
25 test_empty (void)
26 {
27   GMappedFile *file;
28   GError *error;
29
30   error = NULL;
31   file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
32   g_assert_no_error (error);
33
34   g_assert (g_mapped_file_get_contents (file) == NULL);
35
36   g_mapped_file_free (file);
37 }
38
39 static void
40 test_nonexisting (void)
41 {
42   GMappedFile *file;
43   GError *error;
44
45   error = NULL;
46   file = g_mapped_file_new ("no-such-file", FALSE, &error);
47   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
48   g_clear_error (&error);
49   g_assert (file == NULL);
50 }
51
52 static void
53 test_writable (void)
54 {
55   GMappedFile *file;
56   GError *error;
57   gchar *contents;
58   const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
59   const gchar *new = "abcdefghijklmnopqrstuvxyz";
60
61   if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
62     {
63       g_test_message ("Skipping writable mapping test");
64       return;
65     }
66
67   error = NULL;
68   file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
69   g_assert_no_error (error);
70
71   contents = g_mapped_file_get_contents (file);
72   g_assert (strncmp (contents, old, strlen (old)) == 0);
73
74   memcpy (contents, new, strlen (new));
75   g_assert (strncmp (contents, new, strlen (new)) == 0);
76
77   g_mapped_file_free (file);
78
79   error = NULL;
80   file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
81   g_assert_no_error (error);
82
83   contents = g_mapped_file_get_contents (file);
84   g_assert (strncmp (contents, old, strlen (old)) == 0);
85
86   g_mapped_file_free (file);
87 }
88
89 int
90 main (int argc, char *argv[])
91 {
92   g_test_init (&argc, &argv, NULL);
93
94   g_test_add_func ("/mappedfile/basic", test_basic);
95   g_test_add_func ("/mappedfile/empty", test_empty);
96   g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
97   g_test_add_func ("/mappedfile/writable", test_writable);
98
99   return g_test_run ();
100 }