summaryrefslogtreecommitdiff
path: root/src/test/test-hashmap.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2018-01-27 13:10:39 -0800
committerSven Eden <yamakuzure@gmx.net>2018-05-30 07:58:46 +0200
commit8fdf8da964052a2623d3b1dd72ac7aed233f42ce (patch)
tree25a6db43bb810d8aa9d8bd9eae584db5365a8251 /src/test/test-hashmap.c
parent1032d657a9b6865dc82d415960ea6871e553d42d (diff)
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.c85
1 files changed, 56 insertions, 29 deletions
diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c
index f43834a84..16ca27cd5 100644
--- a/src/test/test-hashmap.c
+++ b/src/test/test-hashmap.c
@@ -80,32 +80,61 @@ static void test_string_compare_func(void) {
assert_se(string_compare_func("fred", "fred") == 0);
}
-static void test_path_hashmap(void) {
- _cleanup_(hashmap_freep) Hashmap *h = NULL;
-
- assert_se(h = hashmap_new(&path_hash_ops));
-
- assert_se(hashmap_put(h, "foo", INT_TO_PTR(1)) >= 0);
- assert_se(hashmap_put(h, "/foo", INT_TO_PTR(2)) >= 0);
- assert_se(hashmap_put(h, "//foo", INT_TO_PTR(3)) == -EEXIST);
- assert_se(hashmap_put(h, "//foox/", INT_TO_PTR(4)) >= 0);
- assert_se(hashmap_put(h, "/foox////", INT_TO_PTR(5)) == -EEXIST);
- assert_se(hashmap_put(h, "foo//////bar/quux//", INT_TO_PTR(6)) >= 0);
- assert_se(hashmap_put(h, "foo/bar//quux/", INT_TO_PTR(8)) == -EEXIST);
-
- assert_se(hashmap_get(h, "foo") == INT_TO_PTR(1));
- assert_se(hashmap_get(h, "foo/") == INT_TO_PTR(1));
- assert_se(hashmap_get(h, "foo////") == INT_TO_PTR(1));
- assert_se(hashmap_get(h, "/foo") == INT_TO_PTR(2));
- assert_se(hashmap_get(h, "//foo") == INT_TO_PTR(2));
- assert_se(hashmap_get(h, "/////foo////") == INT_TO_PTR(2));
- assert_se(hashmap_get(h, "/////foox////") == INT_TO_PTR(4));
- assert_se(hashmap_get(h, "/foox/") == INT_TO_PTR(4));
- assert_se(hashmap_get(h, "/foox") == INT_TO_PTR(4));
- assert_se(!hashmap_get(h, "foox"));
- assert_se(hashmap_get(h, "foo/bar/quux") == INT_TO_PTR(6));
- assert_se(hashmap_get(h, "foo////bar////quux/////") == INT_TO_PTR(6));
- assert_se(!hashmap_get(h, "/foo////bar////quux/////"));
+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[]) {
@@ -117,7 +146,5 @@ int main(int argc, const char *argv[]) {
test_uint64_compare_func();
test_trivial_compare_func();
test_string_compare_func();
- test_path_hashmap();
-
- return 0;
+ test_iterated_cache();
}