GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 67 / 0 / 67
Functions: 100.0% 7 / 0 / 7
Branches: 50.0% 33 / 0 / 66

tests/memory-cache.c
Line Branch Exec Source
1 #undef G_DISABLE_ASSERT
2
3 #include <shumate/shumate.h>
4 #include "shumate/shumate-memory-cache-private.h"
5
6 static GdkPaintable *
7 8 create_paintable ()
8 {
9 8 g_autoptr(GdkPixbuf) pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 256, 256);
10
1/2
✓ Branch 4 → 5 taken 8 times.
✗ Branch 4 → 6 not taken.
8 return GDK_PAINTABLE (gdk_texture_new_for_pixbuf (pixbuf));
11 }
12
13
14 /* Test that storing and retrieving a texture from the cache works */
15 static void
16 2 test_memory_cache_store_retrieve ()
17 {
18 2 g_autoptr(ShumateMemoryCache) cache = shumate_memory_cache_new_full (100);
19
1/2
✓ Branch 17 → 18 taken 2 times.
✗ Branch 17 → 19 not taken.
4 g_autoptr(ShumateTile) tile = shumate_tile_new_full (0, 0, 256, 0);
20
1/2
✓ Branch 15 → 16 taken 2 times.
✗ Branch 15 → 17 not taken.
4 g_autoptr(GdkPaintable) paintable = create_paintable ();
21
22 /* Store the tile */
23 2 shumate_tile_set_paintable (tile, paintable);
24 2 shumate_memory_cache_store_tile (cache, tile, "A");
25
26 /* Now retrieve it */
27
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 2 times.
2 g_assert_true (shumate_memory_cache_try_fill_tile (cache, tile, "A"));
28
2/4
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 2 times.
✓ Branch 13 → 14 taken 2 times.
✗ Branch 13 → 15 not taken.
2 g_assert_true (paintable == shumate_tile_get_paintable (tile));
29 2 }
30
31
32 /* Test that cache misses work properly */
33 static void
34 2 test_memory_cache_miss ()
35 {
36 2 g_autoptr(ShumateMemoryCache) cache = shumate_memory_cache_new_full (100);
37
1/2
✓ Branch 20 → 21 taken 2 times.
✗ Branch 20 → 22 not taken.
4 g_autoptr(ShumateTile) tile1 = shumate_tile_new_full (0, 0, 256, 0);
38
1/2
✓ Branch 18 → 19 taken 2 times.
✗ Branch 18 → 20 not taken.
4 g_autoptr(ShumateTile) tile2 = shumate_tile_new_full (0, 0, 256, 1);
39
1/2
✓ Branch 16 → 17 taken 2 times.
✗ Branch 16 → 18 not taken.
4 g_autoptr(GdkPaintable) paintable = create_paintable ();
40
41 /* Store a tile */
42 2 shumate_tile_set_paintable (tile1, paintable);
43 2 shumate_memory_cache_store_tile (cache, tile1, "A");
44
45 /* Now retrieve a different one */
46
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 2 times.
2 g_assert_false (shumate_memory_cache_try_fill_tile (cache, tile2, "A"));
47
2/4
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 2 times.
✓ Branch 14 → 15 taken 2 times.
✗ Branch 14 → 16 not taken.
2 g_assert_null (shumate_tile_get_paintable (tile2));
48 2 }
49
50
51 /* Test that multiple sources can be cached in parallel */
52 static void
53 2 test_memory_cache_source_id ()
54 {
55 2 g_autoptr(ShumateMemoryCache) cache = shumate_memory_cache_new_full (100);
56
1/2
✓ Branch 31 → 32 taken 2 times.
✗ Branch 31 → 33 not taken.
4 g_autoptr(ShumateTile) tile1 = shumate_tile_new_full (0, 0, 256, 0);
57
1/2
✓ Branch 29 → 30 taken 2 times.
✗ Branch 29 → 31 not taken.
4 g_autoptr(ShumateTile) tile2 = shumate_tile_new_full (0, 0, 256, 0);
58
1/2
✓ Branch 27 → 28 taken 2 times.
✗ Branch 27 → 29 not taken.
4 g_autoptr(GdkPaintable) paintable1 = create_paintable ();
59
1/2
✓ Branch 25 → 26 taken 2 times.
✗ Branch 25 → 27 not taken.
4 g_autoptr(GdkPaintable) paintable2 = create_paintable ();
60
61 /* Store the tiles */
62 2 shumate_tile_set_paintable (tile1, paintable1);
63 2 shumate_tile_set_paintable (tile2, paintable2);
64 2 shumate_memory_cache_store_tile (cache, tile1, "A");
65 2 shumate_memory_cache_store_tile (cache, tile2, "B");
66
67 /* Now retrieve them */
68
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 2 times.
2 g_assert_true (shumate_memory_cache_try_fill_tile (cache, tile1, "A"));
69
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 2 times.
2 g_assert_true (paintable1 == shumate_tile_get_paintable (tile1));
70
71
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 2 times.
2 g_assert_true (shumate_memory_cache_try_fill_tile (cache, tile2, "B"));
72
2/4
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 2 times.
✓ Branch 23 → 24 taken 2 times.
✗ Branch 23 → 25 not taken.
2 g_assert_true (paintable2 == shumate_tile_get_paintable (tile2));
73 2 }
74
75
76 /* Test that the cache is purged properly */
77 static void
78 2 test_memory_cache_purge ()
79 {
80 2 g_autoptr(ShumateMemoryCache) cache = shumate_memory_cache_new_full (3);
81
1/2
✓ Branch 31 → 32 taken 2 times.
✗ Branch 31 → 33 not taken.
4 g_autoptr(ShumateTile) tile = shumate_tile_new_full (0, 0, 256, 0);
82
83 /* Store a few tiles */
84 2 shumate_memory_cache_store_tile (cache, tile, "A");
85 2 shumate_memory_cache_store_tile (cache, tile, "B");
86 2 shumate_memory_cache_store_tile (cache, tile, "C");
87
88 /* Make sure they're all still cached */
89
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 2 times.
2 g_assert_true (shumate_memory_cache_try_fill_tile (cache, tile, "B"));
90
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 2 times.
2 g_assert_true (shumate_memory_cache_try_fill_tile (cache, tile, "A"));
91
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 2 times.
2 g_assert_true (shumate_memory_cache_try_fill_tile (cache, tile, "C"));
92
93 /* Store another one */
94 2 shumate_memory_cache_store_tile (cache, tile, "D");
95
96 /* Since B was the least recently accessed, it should be the one that was
97 * dropped */
98
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 2 times.
2 g_assert_false (shumate_memory_cache_try_fill_tile (cache, tile, "B"));
99
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 2 times.
2 g_assert_true (shumate_memory_cache_try_fill_tile (cache, tile, "A"));
100
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 2 times.
2 g_assert_true (shumate_memory_cache_try_fill_tile (cache, tile, "C"));
101
2/4
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 2 times.
✓ Branch 29 → 30 taken 2 times.
✗ Branch 29 → 31 not taken.
2 g_assert_true (shumate_memory_cache_try_fill_tile (cache, tile, "D"));
102 2 }
103
104
105 /* Test that cleaning the cache works */
106 static void
107 2 test_memory_cache_clean ()
108 {
109 2 g_autoptr(ShumateMemoryCache) cache = shumate_memory_cache_new_full (100);
110
1/2
✓ Branch 11 → 12 taken 2 times.
✗ Branch 11 → 13 not taken.
4 g_autoptr(ShumateTile) tile = shumate_tile_new_full (0, 0, 256, 0);
111
112 /* Store a tile */
113 2 shumate_memory_cache_store_tile (cache, tile, "A");
114
115 /* Clean the cache */
116 2 shumate_memory_cache_clean (cache);
117
118 /* Make sure it worked */
119
2/4
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 2 times.
✓ Branch 9 → 10 taken 2 times.
✗ Branch 9 → 11 not taken.
2 g_assert_false (shumate_memory_cache_try_fill_tile (cache, tile, "A"));
120 2 }
121
122
123 int
124 2 main (int argc, char *argv[])
125 {
126 2 g_test_init (&argc, &argv, NULL);
127
128 2 g_test_add_func ("/file-cache/store-retrieve", test_memory_cache_store_retrieve);
129 2 g_test_add_func ("/file-cache/miss", test_memory_cache_miss);
130 2 g_test_add_func ("/file-cache/source-id", test_memory_cache_source_id);
131 2 g_test_add_func ("/file-cache/purge", test_memory_cache_purge);
132 2 g_test_add_func ("/file-cache/clean", test_memory_cache_clean);
133
134 2 return g_test_run ();
135 }
136