Branch data Line data Source code
1 : : #undef G_DISABLE_ASSERT
2 : :
3 : : #include <shumate/shumate.h>
4 : : #include "shumate/vector/shumate-vector-index-private.h"
5 : : #include "shumate/vector/shumate-vector-render-scope-private.h"
6 : : #include "shumate/vector/shumate-vector-expression-private.h"
7 : : #include "shumate/shumate-vector-reader-iter-private.h"
8 : :
9 : : static void
10 : 3 : test_vector_index_bitset (void)
11 : : {
12 : 3 : g_autoptr(ShumateVectorIndexBitset) bitset = shumate_vector_index_bitset_new (100);
13 : 3 : g_autoptr(ShumateVectorIndexBitset) bitset2 = NULL;
14 [ - + ]: 3 : g_assert_nonnull (bitset);
15 [ - + ]: 3 : g_assert_cmpint (bitset->len, ==, 100);
16 [ - + ]: 3 : g_assert_nonnull (bitset->bits);
17 : :
18 [ - + ]: 3 : g_assert_cmpint (shumate_vector_index_bitset_next (bitset, -1), ==, -1);
19 : :
20 : 3 : shumate_vector_index_bitset_set (bitset, 0);
21 [ - + ]: 3 : g_assert_cmpint (shumate_vector_index_bitset_next (bitset, -1), ==, 0);
22 : :
23 : 3 : shumate_vector_index_bitset_set (bitset, 32);
24 [ - + ]: 3 : g_assert_cmpint (shumate_vector_index_bitset_next (bitset, 0), ==, 32);
25 : :
26 : 3 : shumate_vector_index_bitset_clear (bitset, 32);
27 : 3 : shumate_vector_index_bitset_set (bitset, 50);
28 [ - + ]: 3 : g_assert_cmpint (shumate_vector_index_bitset_next (bitset, 0), ==, 50);
29 : :
30 : 3 : shumate_vector_index_bitset_set (bitset, 99);
31 [ - + ]: 3 : g_assert_cmpint (shumate_vector_index_bitset_next (bitset, 98), ==, 99);
32 [ - + ]: 3 : g_assert_cmpint (shumate_vector_index_bitset_next (bitset, 99), ==, -1);
33 : :
34 : 3 : bitset2 = shumate_vector_index_bitset_copy (bitset);
35 [ - + ]: 3 : g_assert_nonnull (bitset2);
36 [ - + ]: 3 : g_assert_cmpint (bitset2->len, ==, 100);
37 [ - + ]: 3 : g_assert_nonnull (bitset2->bits);
38 [ - + ]: 3 : g_assert_cmpint (shumate_vector_index_bitset_next (bitset2, 0), ==, 50);
39 : :
40 : 3 : shumate_vector_index_bitset_set (bitset2, 49);
41 : 3 : shumate_vector_index_bitset_or (bitset, bitset2);
42 [ - + ]: 3 : g_assert_cmpint (shumate_vector_index_bitset_next (bitset, 0), ==, 49);
43 : :
44 : 3 : shumate_vector_index_bitset_clear (bitset2, 49);
45 : 3 : shumate_vector_index_bitset_and (bitset, bitset2);
46 [ - + ]: 3 : g_assert_cmpint (shumate_vector_index_bitset_next (bitset, 0), ==, 50);
47 : 3 : }
48 : :
49 : : static void
50 : 3 : test_vector_index_description (void)
51 : : {
52 : 3 : ShumateVectorIndexDescription *desc = shumate_vector_index_description_new ();
53 : 6 : g_auto(ShumateVectorValue) value = SHUMATE_VECTOR_VALUE_INIT;
54 : :
55 : 3 : shumate_vector_value_set_string (&value, "Hello, world!");
56 : :
57 [ - + ]: 3 : g_assert_nonnull (desc);
58 : :
59 [ - + ]: 3 : g_assert_false (shumate_vector_index_description_has_layer (desc, "foo"));
60 : :
61 : 3 : shumate_vector_index_description_add_broad_geometry_type (desc, "foo");
62 [ - + ]: 3 : g_assert_true (shumate_vector_index_description_has_layer (desc, "foo"));
63 [ - + ]: 3 : g_assert_true (shumate_vector_index_description_has_broad_geometry_type (desc, "foo"));
64 [ - + ]: 3 : g_assert_false (shumate_vector_index_description_has_geometry_type (desc, "foo"));
65 : :
66 : 3 : shumate_vector_index_description_add_geometry_type (desc, "bar");
67 [ - + ]: 3 : g_assert_true (shumate_vector_index_description_has_layer (desc, "bar"));
68 [ - + ]: 3 : g_assert_false (shumate_vector_index_description_has_broad_geometry_type (desc, "bar"));
69 [ - + ]: 3 : g_assert_true (shumate_vector_index_description_has_geometry_type (desc, "bar"));
70 : :
71 : 3 : shumate_vector_index_description_add_has_index (desc, "baz", "qux");
72 [ - + ]: 3 : g_assert_true (shumate_vector_index_description_has_layer (desc, "baz"));
73 [ - + ]: 3 : g_assert_true (shumate_vector_index_description_has_field (desc, "baz", "qux"));
74 [ - + ]: 3 : g_assert_true (shumate_vector_index_description_has_field_has_index (desc, "baz", "qux"));
75 [ - + ]: 3 : g_assert_false (shumate_vector_index_description_has_value (desc, "baz", "qux", &value));
76 : :
77 : 3 : shumate_vector_index_description_add (desc, "baz", "aaa", &value);
78 [ - + ]: 3 : g_assert_true (shumate_vector_index_description_has_field (desc, "baz", "aaa"));
79 [ - + ]: 3 : g_assert_false (shumate_vector_index_description_has_field_has_index (desc, "baz", "aaa"));
80 [ - + ]: 3 : g_assert_true (shumate_vector_index_description_has_value (desc, "baz", "aaa", &value));
81 : :
82 : 3 : shumate_vector_index_description_free (desc);
83 : 3 : }
84 : :
85 : : static ShumateVectorIndexBitset *
86 : 21 : compute_bitset (ShumateVectorReader *reader, char *json, const char *layer, ShumateVectorIndexDescription **index_description_out)
87 : : {
88 : 21 : GError *error = NULL;
89 : 21 : ShumateVectorRenderScope scope;
90 : 42 : g_autoptr(JsonNode) node1 = NULL;
91 [ + - ]: 21 : g_autoptr(ShumateVectorExpression) expr1 = NULL;
92 [ + - ]: 21 : g_autoptr(ShumateVectorIndexBitset) bitset1 = NULL;
93 : :
94 : 21 : scope.reader = shumate_vector_reader_iterate (reader);
95 [ - + ]: 21 : g_assert_nonnull (scope.reader);
96 : :
97 : 21 : scope.index = NULL;
98 : 21 : scope.index_description = shumate_vector_index_description_new ();
99 : :
100 : 21 : scope.zoom_level = 12;
101 : :
102 : 21 : node1 = json_from_string (json, &error);
103 [ - + ]: 21 : g_assert_no_error (error);
104 : 21 : expr1 = shumate_vector_expression_from_json (node1, &error);
105 [ - + ]: 21 : g_assert_no_error (error);
106 : :
107 : 21 : shumate_vector_expression_collect_indexes (expr1, layer, scope.index_description);
108 : :
109 : 21 : shumate_vector_reader_iter_read_layer_by_name (scope.reader, layer);
110 : 21 : scope.source_layer_idx = shumate_vector_reader_iter_get_layer_index (scope.reader);
111 : 21 : shumate_vector_render_scope_index_layer (&scope);
112 : 21 : bitset1 = shumate_vector_expression_eval_bitset (expr1, &scope, NULL);
113 [ - + ]: 21 : g_assert_nonnull (bitset1);
114 : :
115 [ + + ]: 21 : if (index_description_out)
116 : 6 : *index_description_out = g_steal_pointer (&scope.index_description);
117 : : else
118 : 15 : shumate_vector_index_description_free (scope.index_description);
119 : :
120 [ + - ]: 21 : g_clear_pointer (&scope.index, shumate_vector_index_free);
121 [ + - ]: 21 : g_clear_object (&scope.reader);
122 : :
123 [ + - ]: 21 : return g_steal_pointer (&bitset1);
124 : : }
125 : :
126 : : static void
127 : 3 : test_vector_index_eval (void)
128 : : {
129 : 3 : GError *error = NULL;
130 : 6 : g_autoptr(GBytes) vector_data = NULL;
131 [ + - ]: 3 : g_autoptr(ShumateVectorReader) reader = NULL;
132 : 3 : ShumateVectorIndexBitset *bitset = NULL;
133 : 3 : ShumateVectorIndexDescription *index_description = NULL;
134 : :
135 : 3 : vector_data = g_resources_lookup_data ("/org/gnome/shumate/Tests/0.pbf", G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
136 : 3 : g_assert_no_error (error);
137 : :
138 : 3 : reader = shumate_vector_reader_new (vector_data);
139 : :
140 : : /* Test literals in eval_bitset() */
141 : 3 : bitset = compute_bitset (reader, "[\"all\", true, false]", "lines", NULL);
142 [ - + ]: 3 : g_assert_false (shumate_vector_index_bitset_get (bitset, 0));
143 [ - + ]: 3 : g_assert_false (shumate_vector_index_bitset_get (bitset, 1));
144 : 3 : shumate_vector_index_bitset_free (bitset);
145 : :
146 : 3 : bitset = compute_bitset (reader, "[\"any\", true, false]", "lines", NULL);
147 [ - + ]: 3 : g_assert_true (shumate_vector_index_bitset_get (bitset, 0));
148 [ - + ]: 3 : g_assert_true (shumate_vector_index_bitset_get (bitset, 1));
149 : 3 : shumate_vector_index_bitset_free (bitset);
150 : :
151 : : /* Test geometry type indexes, both broad and not*/
152 : 3 : bitset = compute_bitset (reader, "[\"in\", [\"geometry-type\"], [\"literal\", [\"LineString\", \"MultiLineString\"]]]", "lines", &index_description);
153 [ - + ]: 3 : g_assert_true (shumate_vector_index_bitset_get (bitset, 0));
154 [ - + ]: 3 : g_assert_true (shumate_vector_index_bitset_get (bitset, 1));
155 [ - + ]: 3 : g_assert_false (shumate_vector_index_description_has_geometry_type (index_description, "lines"));
156 [ - + ]: 3 : g_assert_true (shumate_vector_index_description_has_broad_geometry_type (index_description, "lines"));
157 : 3 : shumate_vector_index_bitset_free (bitset);
158 : 3 : shumate_vector_index_description_free (index_description);
159 : :
160 : 3 : bitset = compute_bitset (reader, "[\"==\", [\"geometry-type\"], \"LineString\"]", "lines", &index_description);
161 [ - + ]: 3 : g_assert_true (shumate_vector_index_bitset_get (bitset, 0));
162 [ - + ]: 3 : g_assert_true (shumate_vector_index_bitset_get (bitset, 1));
163 [ - + ]: 3 : g_assert_true (shumate_vector_index_description_has_geometry_type (index_description, "lines"));
164 [ - + ]: 3 : g_assert_false (shumate_vector_index_description_has_broad_geometry_type (index_description, "lines"));
165 : 3 : shumate_vector_index_bitset_free (bitset);
166 : 3 : shumate_vector_index_description_free (index_description);
167 : :
168 : 3 : bitset = compute_bitset (reader, "[\"in\", [\"get\", \"name\"], [\"literal\", [\"test1\", \"helloworld\"]]]", "lines", NULL);
169 [ - + ]: 3 : g_assert_true (shumate_vector_index_bitset_get (bitset, 0));
170 [ - + ]: 3 : g_assert_false (shumate_vector_index_bitset_get (bitset, 1));
171 : 3 : shumate_vector_index_bitset_free (bitset);
172 : :
173 : 3 : bitset = compute_bitset (reader, "[\"has\", \"name\"]", "lines", NULL);
174 [ - + ]: 3 : g_assert_true (shumate_vector_index_bitset_get (bitset, 0));
175 [ - + ]: 3 : g_assert_false (shumate_vector_index_bitset_get (bitset, 1));
176 : 3 : shumate_vector_index_bitset_free (bitset);
177 : :
178 : : /* Test fallback with an expression that isn't indexed */
179 : 3 : bitset = compute_bitset (reader, "[\">=\", [\"get\", \"number\"], [\"zoom\"]]", "polygons", NULL);
180 [ - + ]: 3 : g_assert_true (shumate_vector_index_bitset_get (bitset, 0));
181 [ - + ]: 3 : g_assert_false (shumate_vector_index_bitset_get (bitset, 1));
182 [ + - ]: 3 : shumate_vector_index_bitset_free (bitset);
183 : 3 : }
184 : :
185 : : int
186 : 3 : main (int argc, char *argv[])
187 : : {
188 : 3 : g_test_init (&argc, &argv, NULL);
189 : :
190 : 3 : g_test_add_func ("/vector-index/bitset", test_vector_index_bitset);
191 : 3 : g_test_add_func ("/vector-index/description", test_vector_index_description);
192 : 3 : g_test_add_func ("/vector-index/eval", test_vector_index_eval);
193 : :
194 : 3 : return g_test_run ();
195 : : }
|