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