summaryrefslogtreecommitdiffstats
path: root/src/test/test-hashmap.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2018-01-27 22:10:39 +0100
committerVito Caputo <vcaputo@pengaru.com>2018-01-27 22:11:58 +0100
commit647c7b74407f6d9ea6ae4a87d0d223d56f5e5d72 (patch)
tree3650c1351f1d69233f0facaccbc3fdede82a7b7c /src/test/test-hashmap.c
parentjournal: use IteratedCache in sd-journal (diff)
downloadsystemd-647c7b74407f6d9ea6ae4a87d0d223d56f5e5d72.tar.xz
systemd-647c7b74407f6d9ea6ae4a87d0d223d56f5e5d72.zip
test-hashmap: test IteratedCache
Add some rudimentary testing of the new IteratedCache
Diffstat (limited to 'src/test/test-hashmap.c')
-rw-r--r--src/test/test-hashmap.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c
index dd9195425e..16ca27cd5f 100644
--- a/src/test/test-hashmap.c
+++ b/src/test/test-hashmap.c
@@ -80,6 +80,63 @@ static void test_string_compare_func(void) {
assert_se(string_compare_func("fred", "fred") == 0);
}
+static void compare_cache(Hashmap *map, IteratedCache *cache) {
+ const void **keys = NULL, **values = NULL;
+ unsigned num, idx;
+ Iterator iter;
+ void *k, *v;
+
+ assert_se(iterated_cache_get(cache, &keys, &values, &num) == 0);
+ assert_se(num == 0 || keys);
+ assert_se(num == 0 || values);
+
+ idx = 0;
+ HASHMAP_FOREACH_KEY(v, k, map, iter) {
+ assert_se(v == values[idx]);
+ assert_se(k == keys[idx]);
+
+ idx++;
+ }
+
+ assert_se(idx == num);
+}
+
+static void test_iterated_cache(void) {
+ Hashmap *m;
+ IteratedCache *c;
+
+ assert_se(m = hashmap_new(NULL));
+ assert_se(c = hashmap_iterated_cache_new(m));
+ compare_cache(m, c);
+
+ for (int stage = 0; stage < 100; stage++) {
+
+ for (int i = 0; i < 100; i++) {
+ int foo = stage * 1000 + i;
+
+ assert_se(hashmap_put(m, INT_TO_PTR(foo), INT_TO_PTR(foo + 777)) == 1);
+ }
+
+ compare_cache(m, c);
+
+ if (!(stage % 10)) {
+ for (int i = 0; i < 100; i++) {
+ int foo = stage * 1000 + i;
+
+ assert_se(hashmap_remove(m, INT_TO_PTR(foo)) == INT_TO_PTR(foo + 777));
+ }
+
+ compare_cache(m, c);
+ }
+ }
+
+ hashmap_clear(m);
+ compare_cache(m, c);
+
+ assert_se(hashmap_free(m) == NULL);
+ assert_se(iterated_cache_free(c) == NULL);
+}
+
int main(int argc, const char *argv[]) {
test_hashmap_funcs();
test_ordered_hashmap_funcs();
@@ -89,4 +146,5 @@ int main(int argc, const char *argv[]) {
test_uint64_compare_func();
test_trivial_compare_func();
test_string_compare_func();
+ test_iterated_cache();
}