test-hashmap.c revision d5099efc47d4e6ac60816b5381a5f607ab03f06e
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/***
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani This file is part of systemd
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Copyright 2013 Daniel Buch
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani systemd is free software; you can redistribute it and/or modify it
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani under the terms of the GNU Lesser General Public License as published by
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani the Free Software Foundation; either version 2.1 of the License, or
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani (at your option) any later version.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani systemd is distributed in the hope that it will be useful, but
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani WITHOUT ANY WARRANTY; without even the implied warranty of
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Lesser General Public License for more details.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani You should have received a copy of the GNU Lesser General Public License
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani along with systemd; If not, see <http://www.gnu.org/licenses/>.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani***/
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include <inttypes.h>
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "strv.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "util.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "hashmap.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void test_hashmap_replace(void) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Hashmap *m;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani char *val1, *val2, *val3, *val4, *val5, *r;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani m = hashmap_new(&string_hash_ops);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val1 = strdup("val1");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val2 = strdup("val2");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val3 = strdup("val3");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val3);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val4 = strdup("val4");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val4);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val5 = strdup("val5");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val5);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 1", val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 2", val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 3", val3);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 4", val4);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_replace(m, "key 3", val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_get(m, "key 3");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(r, "val1"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_replace(m, "key 5", val5);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_get(m, "key 5");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(r, "val5"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani free(val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani free(val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani free(val3);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani free(val4);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani free(val5);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_free(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void test_hashmap_copy(void) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Hashmap *m, *copy;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani char *val1, *val2, *val3, *val4, *r;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val1 = strdup("val1");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val2 = strdup("val2");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val3 = strdup("val3");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val3);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val4 = strdup("val4");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val4);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani m = hashmap_new(&string_hash_ops);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 1", val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 2", val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 3", val3);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 4", val4);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani copy = hashmap_copy(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_get(copy, "key 1");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(r, "val1"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_get(copy, "key 2");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(r, "val2"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_get(copy, "key 3");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(r, "val3"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_get(copy, "key 4");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(r, "val4"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_free_free(copy);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_free(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void test_hashmap_get_strv(void) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Hashmap *m;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani char **strv;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani char *val1, *val2, *val3, *val4;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val1 = strdup("val1");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val2 = strdup("val2");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val3 = strdup("val3");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val3);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val4 = strdup("val4");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val4);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani m = hashmap_new(&string_hash_ops);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 1", val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 2", val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 3", val3);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 4", val4);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani strv = hashmap_get_strv(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(strv[0], "val1"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(strv[1], "val2"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(strv[2], "val3"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(strv[3], "val4"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani strv_free(strv);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_free(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void test_hashmap_move_one(void) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Hashmap *m, *n;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani char *val1, *val2, *val3, *val4, *r;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val1 = strdup("val1");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val2 = strdup("val2");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val3 = strdup("val3");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val3);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val4 = strdup("val4");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val4);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani m = hashmap_new(&string_hash_ops);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani n = hashmap_new(&string_hash_ops);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 1", val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 2", val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 3", val3);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 4", val4);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_move_one(n, m, "key 3");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_move_one(n, m, "key 4");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_get(n, "key 3");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(r && streq(r, "val3"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_get(n, "key 4");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(r && streq(r, "val4"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_get(m, "key 3");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(!r);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_free_free(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_free_free(n);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void test_hashmap_next(void) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Hashmap *m;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani char *val1, *val2, *val3, *val4, *r;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani m = hashmap_new(&string_hash_ops);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val1 = strdup("val1");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val2 = strdup("val2");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val3 = strdup("val3");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val3);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val4 = strdup("val4");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val4);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 1", val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 2", val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 3", val3);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 4", val4);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_next(m, "key 1");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(r, val2));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_next(m, "key 2");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(r, val3));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_next(m, "key 3");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(r, val4));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_next(m, "key 4");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(!r);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_free_free(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void test_hashmap_update(void) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Hashmap *m;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani char *val1, *val2, *r;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani m = hashmap_new(&string_hash_ops);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val1 = strdup("old_value");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val2 = strdup("new_value");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, "key 1", val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_get(m, "key 1");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(r, "old_value"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_update(m, "key 1", val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_get(m, "key 1");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(r, "new_value"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani free(val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani free(val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_free(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void test_hashmap_put(void) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Hashmap *m;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int valid_hashmap_put;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani m = hashmap_new(&string_hash_ops);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani valid_hashmap_put = hashmap_put(m, "key 1", (void*) (const char *) "val 1");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(valid_hashmap_put == 1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_free(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void test_hashmap_remove_and_put(void) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani _cleanup_hashmap_free_ Hashmap *m = NULL;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int valid;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani char *r;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani m = hashmap_new(&string_hash_ops);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani valid = hashmap_remove_and_put(m, "unvalid key", "new key", NULL);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(valid < 0);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani valid = hashmap_put(m, "key 1", (void*) (const char *) "val 1");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(valid == 1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani valid = hashmap_remove_and_put(m, "key 1", "key 2", (void*) (const char *) "val 2");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(valid == 0);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = hashmap_get(m, "key 2");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(streq(r, "val 2"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(!hashmap_get(m, "key 1"));
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani valid = hashmap_put(m, "key 3", (void*) (const char *) "val 3");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(valid == 1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani valid = hashmap_remove_and_put(m, "key 3", "key 2", (void*) (const char *) "val 2");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(valid < 0);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void test_hashmap_ensure_allocated(void) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Hashmap *m;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int valid_hashmap;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani m = hashmap_new(&string_hash_ops);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani valid_hashmap = hashmap_ensure_allocated(&m, &string_hash_ops);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(valid_hashmap == 0);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_free(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void test_hashmap_foreach_key(void) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Hashmap *m;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Iterator i;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani bool key_found[] = { false, false, false, false };
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani const char *s;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani const char *key;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani static const char key_table[] =
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani "key 1\0"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani "key 2\0"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani "key 3\0"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani "key 4\0";
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani m = hashmap_new(&string_hash_ops);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani NULSTR_FOREACH(key, key_table)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_put(m, key, (void*) (const char*) "my dummy val");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani HASHMAP_FOREACH_KEY(s, key, m, i) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (!key_found[0] && streq(key, "key 1"))
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani key_found[0] = true;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani else if (!key_found[1] && streq(key, "key 2"))
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani key_found[1] = true;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani else if (!key_found[2] && streq(key, "key 3"))
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani key_found[2] = true;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani else if (!key_found[3] && streq(key, "fail"))
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani key_found[3] = true;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(key_found[0] && key_found[1] && key_found[2] && !key_found[3]);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani hashmap_free(m);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanistatic void test_hashmap_foreach(void) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Hashmap *m;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Iterator i;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani bool value_found[] = { false, false, false, false };
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani char *val1, *val2, *val3, *val4, *s;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val1 = strdup("my val1");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val2 = strdup("my val2");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val2);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val3 = strdup("my val3");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val3);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani val4 = strdup("my val4");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_se(val4);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
m = hashmap_new(&string_hash_ops);
hashmap_put(m, "Key 1", val1);
hashmap_put(m, "Key 2", val2);
hashmap_put(m, "Key 3", val3);
hashmap_put(m, "Key 4", val4);
HASHMAP_FOREACH(s, m, i) {
if (!value_found[0] && streq(s, val1))
value_found[0] = true;
else if (!value_found[1] && streq(s, val2))
value_found[1] = true;
else if (!value_found[2] && streq(s, val3))
value_found[2] = true;
else if (!value_found[3] && streq(s, val4))
value_found[3] = true;
}
assert_se(m);
assert_se(value_found[0] && value_found[1] && value_found[2] && value_found[3]);
hashmap_free_free(m);
}
static void test_hashmap_foreach_backwards(void) {
Hashmap *m;
Iterator i;
char *val1, *val2, *val3, *val4, *s;
bool value_found[] = { false, false, false, false };
val1 = strdup("my val1");
assert_se(val1);
val2 = strdup("my val2");
assert_se(val2);
val3 = strdup("my val3");
assert_se(val3);
val4 = strdup("my val4");
assert_se(val4);
m = hashmap_new(&string_hash_ops);
hashmap_put(m, "Key 1", val1);
hashmap_put(m, "Key 2", val2);
hashmap_put(m, "Key 3", val3);
hashmap_put(m, "Key 4", val4);
HASHMAP_FOREACH_BACKWARDS(s, m, i) {
if (!value_found[0] && streq(s, val1))
value_found[0] = true;
else if (!value_found[1] && streq(s, val2))
value_found[1] = true;
else if (!value_found[2] && streq(s, val3))
value_found[2] = true;
else if (!value_found[3] && streq(s, val4))
value_found[3] = true;
}
assert_se(m);
assert_se(value_found[0] && value_found[1] && value_found[2] && value_found[3]);
hashmap_free_free(m);
}
static void test_hashmap_merge(void) {
Hashmap *m;
Hashmap *n;
char *val1, *val2, *val3, *val4, *r;
val1 = strdup("my val1");
assert_se(val1);
val2 = strdup("my val2");
assert_se(val2);
val3 = strdup("my val3");
assert_se(val3);
val4 = strdup("my val4");
assert_se(val4);
n = hashmap_new(&string_hash_ops);
m = hashmap_new(&string_hash_ops);
hashmap_put(m, "Key 1", val1);
hashmap_put(m, "Key 2", val2);
hashmap_put(n, "Key 3", val3);
hashmap_put(n, "Key 4", val4);
assert_se(hashmap_merge(m, n) == 0);
r = hashmap_get(m, "Key 3");
assert_se(r && streq(r, "my val3"));
r = hashmap_get(m, "Key 4");
assert_se(r && streq(r, "my val4"));
assert_se(n);
assert_se(m);
hashmap_free(n);
hashmap_free_free(m);
}
static void test_hashmap_contains(void) {
Hashmap *m;
char *val1;
val1 = strdup("my val");
assert_se(val1);
m = hashmap_new(&string_hash_ops);
assert_se(!hashmap_contains(m, "Key 1"));
hashmap_put(m, "Key 1", val1);
assert_se(hashmap_contains(m, "Key 1"));
assert_se(m);
hashmap_free_free(m);
}
static void test_hashmap_isempty(void) {
Hashmap *m;
char *val1;
val1 = strdup("my val");
assert_se(val1);
m = hashmap_new(&string_hash_ops);
assert_se(hashmap_isempty(m));
hashmap_put(m, "Key 1", val1);
assert_se(!hashmap_isempty(m));
assert_se(m);
hashmap_free_free(m);
}
static void test_hashmap_size(void) {
Hashmap *m;
char *val1, *val2, *val3, *val4;
val1 = strdup("my val");
assert_se(val1);
val2 = strdup("my val");
assert_se(val2);
val3 = strdup("my val");
assert_se(val3);
val4 = strdup("my val");
assert_se(val4);
m = hashmap_new(&string_hash_ops);
hashmap_put(m, "Key 1", val1);
hashmap_put(m, "Key 2", val2);
hashmap_put(m, "Key 3", val3);
hashmap_put(m, "Key 4", val4);
assert_se(m);
assert_se(hashmap_size(m) == 4);
hashmap_free_free(m);
}
static void test_hashmap_get(void) {
Hashmap *m;
char *r;
char *val;
val = strdup("my val");
assert_se(val);
m = hashmap_new(&string_hash_ops);
hashmap_put(m, "Key 1", val);
r = hashmap_get(m, "Key 1");
assert_se(streq(r, val));
assert_se(m);
hashmap_free_free(m);
}
static void test_hashmap_many(void) {
Hashmap *h;
unsigned i;
#define N_ENTRIES 100000
assert_se(h = hashmap_new(NULL));
for (i = 1; i < N_ENTRIES*3; i+=3) {
assert_se(hashmap_put(h, UINT_TO_PTR(i), UINT_TO_PTR(i)) >= 0);
assert_se(PTR_TO_UINT(hashmap_get(h, UINT_TO_PTR(i))) == i);
}
for (i = 1; i < N_ENTRIES*3; i++)
assert_se(hashmap_contains(h, UINT_TO_PTR(i)) == (i % 3 == 1));
log_info("%u <= %u * 0.75 = %g", hashmap_size(h), hashmap_buckets(h), hashmap_buckets(h) * 0.75);
assert_se(hashmap_size(h) <= hashmap_buckets(h) * 0.75);
assert_se(hashmap_size(h) == N_ENTRIES);
hashmap_free(h);
}
static void test_hashmap_first_key(void) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
m = hashmap_new(&string_hash_ops);
assert_se(m);
assert_se(!hashmap_first_key(m));
assert_se(hashmap_put(m, "key 1", NULL) == 1);
assert_se(streq(hashmap_first_key(m), "key 1"));
assert_se(hashmap_put(m, "key 2", NULL) == 1);
assert_se(streq(hashmap_first_key(m), "key 1"));
assert_se(hashmap_remove(m, "key 1") == NULL);
assert_se(streq(hashmap_first_key(m), "key 2"));
}
static void test_hashmap_last(void) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
m = hashmap_new(&string_hash_ops);
assert_se(m);
assert_se(!hashmap_last(m));
assert_se(hashmap_put(m, "key 1", (void *) (const char *) "val 1") == 1);
assert_se(streq(hashmap_last(m), "val 1"));
assert_se(hashmap_put(m, "key 2", (void *) (const char *) "bar") == 1);
assert_se(streq(hashmap_last(m), "bar"));
assert_se(hashmap_remove(m, "key 2"));
assert_se(streq(hashmap_last(m), "val 1"));
}
static void test_hashmap_steal_first_key(void) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
m = hashmap_new(&string_hash_ops);
assert_se(m);
assert_se(!hashmap_steal_first_key(m));
assert_se(hashmap_put(m, "key 1", NULL) == 1);
assert_se(streq(hashmap_steal_first_key(m), "key 1"));
assert_se(hashmap_isempty(m));
}
static void test_hashmap_clear_free_free(void) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
m = hashmap_new(&string_hash_ops);
assert_se(m);
assert_se(hashmap_put(m, strdup("key 1"), NULL) == 1);
assert_se(hashmap_put(m, strdup("key 2"), NULL) == 1);
assert_se(hashmap_put(m, strdup("key 3"), NULL) == 1);
hashmap_clear_free_free(m);
assert_se(hashmap_isempty(m));
}
static void test_uint64_compare_func(void) {
const uint64_t a = 0x100, b = 0x101;
assert_se(uint64_compare_func(&a, &a) == 0);
assert_se(uint64_compare_func(&a, &b) == -1);
assert_se(uint64_compare_func(&b, &a) == 1);
}
static void test_trivial_compare_func(void) {
assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
}
static void test_string_compare_func(void) {
assert_se(!string_compare_func("fred", "wilma") == 0);
assert_se(string_compare_func("fred", "fred") == 0);
}
int main(int argc, const char *argv[]) {
test_hashmap_copy();
test_hashmap_get_strv();
test_hashmap_move_one();
test_hashmap_next();
test_hashmap_replace();
test_hashmap_update();
test_hashmap_put();
test_hashmap_remove_and_put();
test_hashmap_ensure_allocated();
test_hashmap_foreach();
test_hashmap_foreach_backwards();
test_hashmap_foreach_key();
test_hashmap_contains();
test_hashmap_merge();
test_hashmap_isempty();
test_hashmap_get();
test_hashmap_size();
test_hashmap_many();
test_hashmap_first_key();
test_hashmap_last();
test_hashmap_steal_first_key();
test_hashmap_clear_free_free();
test_uint64_compare_func();
test_trivial_compare_func();
test_string_compare_func();
}