GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 59 / 0 / 59
Functions: 100.0% 6 / 0 / 6
Branches: 50.0% 23 / 0 / 46

tests/file-cache.c
Line Branch Exec Source
1 #undef G_DISABLE_ASSERT
2
3 #include <shumate/shumate.h>
4
5 #define TEST_ETAG "0123456789ABCDEFG"
6 #define TEST_DATA "The quick brown fox \0 jumps over the lazy dog"
7
8
9 static void
10 1 on_tile_stored (GObject *object, GAsyncResult *res, gpointer user_data)
11 {
12 2 g_autoptr(GError) error = NULL;
13 1 GMainLoop *loop = user_data;
14
15 1 shumate_file_cache_store_tile_finish ((ShumateFileCache *) object, res, &error);
16
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1 time.
1 g_assert_no_error (error);
17
18
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 1 time.
1 g_main_loop_quit (loop);
19 1 }
20
21 static void
22 1 on_tile_retrieved (GObject *object, GAsyncResult *res, gpointer user_data)
23 {
24 1 GMainLoop *loop = user_data;
25 2 g_autoptr(GError) error = NULL;
26
1/2
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 1 time.
1 g_autoptr(GBytes) bytes = NULL;
27
1/2
✓ Branch 24 → 25 taken 1 time.
✗ Branch 24 → 26 not taken.
1 g_autofree char *etag = NULL;
28 2 g_autoptr(GBytes) expected_bytes = g_bytes_new_static (TEST_DATA, sizeof TEST_DATA);
29
1/2
✓ Branch 21 → 22 taken 1 time.
✗ Branch 21 → 23 not taken.
1 g_autoptr(GDateTime) modtime = NULL;
30
1/2
✓ Branch 19 → 20 taken 1 time.
✗ Branch 19 → 21 not taken.
2 g_autoptr(GDateTime) now = g_date_time_new_now_utc ();
31
32 1 bytes = shumate_file_cache_get_tile_finish ((ShumateFileCache *) object, &etag, &modtime, res, &error);
33
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1 time.
1 g_assert_no_error (error);
34
35
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 1 time.
1 g_assert_true (g_bytes_equal (bytes, expected_bytes));
36
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 1 time.
1 g_assert_cmpstr (etag, ==, TEST_ETAG);
37 /* the modification time should be very, very recent */
38
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 1 time.
1 g_assert_true (g_date_time_difference (now, modtime) < G_TIME_SPAN_SECOND * 10);
39
40
1/2
✓ Branch 17 → 18 taken 1 time.
✗ Branch 17 → 19 not taken.
1 g_main_loop_quit (loop);
41 1 }
42
43 /* Test that storing and retrieving a file from the cache works */
44 static void
45 1 test_file_cache_store_retrieve ()
46 {
47 1 g_autoptr(ShumateFileCache) cache = shumate_file_cache_new_full (100000000, "test", NULL);
48
1/2
✓ Branch 15 → 16 taken 1 time.
✗ Branch 15 → 17 not taken.
2 g_autoptr(GBytes) bytes = g_bytes_new_static (TEST_DATA, sizeof TEST_DATA);
49
1/2
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 15 not taken.
1 g_autoptr(GMainLoop) loop = NULL;
50
51 /* Store the tile */
52 1 loop = g_main_loop_new (NULL, TRUE);
53 1 shumate_file_cache_store_tile_async (cache, 0, 0, 256, bytes, TEST_ETAG, NULL, on_tile_stored, loop);
54 1 g_main_loop_run (loop);
55
56 /* Now retrieve it */
57 1 g_main_loop_unref (loop);
58 1 loop = g_main_loop_new (NULL, TRUE);
59 1 shumate_file_cache_get_tile_async (cache, 0, 0, 256, NULL, on_tile_retrieved, loop);
60
1/2
✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 13 not taken.
1 g_main_loop_run (loop);
61 1 }
62
63
64 static void
65 1 on_no_tile_retrieved (GObject *object, GAsyncResult *res, gpointer user_data)
66 {
67 1 GMainLoop *loop = user_data;
68 2 g_autoptr(GError) error = NULL;
69
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 1 time.
1 g_autoptr(GBytes) bytes = NULL;
70
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 1 time.
1 g_autofree char *etag = NULL;
71 1 g_autoptr(GDateTime) modtime = NULL;
72
73 /* Make sure retrieving the tile returns NULL */
74 1 bytes = shumate_file_cache_get_tile_finish ((ShumateFileCache *) object, &etag, &modtime, res, &error);
75
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1 time.
1 g_assert_no_error (error);
76
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1 time.
1 g_assert_null (bytes);
77
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 1 time.
1 g_assert_null (etag);
78
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 1 time.
1 g_assert_null (modtime);
79
80
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 1 time.
1 g_main_loop_quit (loop);
81 1 }
82
83 /* Test that cache misses work properly */
84 static void
85 1 test_file_cache_miss ()
86 {
87 1 g_autoptr(ShumateFileCache) cache = shumate_file_cache_new_full (100000000, "test", NULL);
88
1/2
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 10 not taken.
1 g_autoptr(GMainLoop) loop = NULL;
89
90 1 loop = g_main_loop_new (NULL, TRUE);
91 1 shumate_file_cache_get_tile_async (cache, 0, 0, 256, NULL, on_no_tile_retrieved, loop);
92
1/2
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 8 not taken.
1 g_main_loop_run (loop);
93 1 }
94
95
96 int
97 1 main (int argc, char *argv[])
98 {
99 1 g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
100
101 1 g_test_add_func ("/file-cache/store-retrieve", test_file_cache_store_retrieve);
102 1 g_test_add_func ("/file-cache/miss", test_file_cache_miss);
103
104 1 return g_test_run ();
105 }
106