test-hashmap-plain.c revision e1323fbfbe8a574f28b704f2df8ce7f99e3a28f5
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/***
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2013 Daniel Buch
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering***/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "strv.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "hashmap.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringvoid test_hashmap_funcs(void);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void test_hashmap_replace(void) {
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering Hashmap *m;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering char *val1, *val2, *val3, *val4, *val5, *r;
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering m = hashmap_new(&string_hash_ops);
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering val1 = strdup("val1");
25300b5a1fcf54674a69d0f4ab08925be00b0227Lennart Poettering assert_se(val1);
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering val2 = strdup("val2");
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering assert_se(val2);
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering val3 = strdup("val3");
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering assert_se(val3);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering val4 = strdup("val4");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(val4);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering val5 = strdup("val5");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(val5);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 1", val1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 2", val2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 3", val3);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 4", val4);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt hashmap_replace(m, "key 3", val1);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt r = hashmap_get(m, "key 3");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(streq(r, "val1"));
d3e84ddb885e9d5f0ae9930eb905910e3a81f157Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering hashmap_replace(m, "key 5", val5);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = hashmap_get(m, "key 5");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(streq(r, "val5"));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
afc6adb5ec7e73bc13156c43f52fb015cd80cc68Lennart Poettering free(val1);
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering free(val2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(val3);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(val4);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(val5);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_free(m);
cde93897cdefdd7c7f66c400a61e42ceee5f6a46Lennart Poettering}
cde93897cdefdd7c7f66c400a61e42ceee5f6a46Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void test_hashmap_copy(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Hashmap *m, *copy;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *val1, *val2, *val3, *val4, *r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering val1 = strdup("val1");
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering assert_se(val1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering val2 = strdup("val2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(val2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering val3 = strdup("val3");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(val3);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering val4 = strdup("val4");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(val4);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m = hashmap_new(&string_hash_ops);
d3e84ddb885e9d5f0ae9930eb905910e3a81f157Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 1", val1);
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering hashmap_put(m, "key 2", val2);
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering hashmap_put(m, "key 3", val3);
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering hashmap_put(m, "key 4", val4);
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering copy = hashmap_copy(m);
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering r = hashmap_get(copy, "key 1");
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering assert_se(streq(r, "val1"));
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering r = hashmap_get(copy, "key 2");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(streq(r, "val2"));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = hashmap_get(copy, "key 3");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(streq(r, "val3"));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = hashmap_get(copy, "key 4");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(streq(r, "val4"));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering hashmap_free_free(copy);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering hashmap_free(m);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering}
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poetteringstatic void test_hashmap_get_strv(void) {
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering Hashmap *m;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering char **strv;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering char *val1, *val2, *val3, *val4;
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering val1 = strdup("val1");
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering assert_se(val1);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering val2 = strdup("val2");
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering assert_se(val2);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering val3 = strdup("val3");
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering assert_se(val3);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering val4 = strdup("val4");
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering assert_se(val4);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering m = hashmap_new(&string_hash_ops);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering hashmap_put(m, "key 1", val1);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering hashmap_put(m, "key 2", val2);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering hashmap_put(m, "key 3", val3);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering hashmap_put(m, "key 4", val4);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering strv = hashmap_get_strv(m);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering#ifndef ORDERED
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering strv = strv_sort(strv);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering#endif
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering assert_se(streq(strv[0], "val1"));
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering assert_se(streq(strv[1], "val2"));
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering assert_se(streq(strv[2], "val3"));
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering assert_se(streq(strv[3], "val4"));
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering strv_free(strv);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering hashmap_free(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void test_hashmap_move_one(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Hashmap *m, *n;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *val1, *val2, *val3, *val4, *r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering val1 = strdup("val1");
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering assert_se(val1);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering val2 = strdup("val2");
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering assert_se(val2);
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering val3 = strdup("val3");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(val3);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering val4 = strdup("val4");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(val4);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m = hashmap_new(&string_hash_ops);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering n = hashmap_new(&string_hash_ops);
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 1", val1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 2", val2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 3", val3);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 4", val4);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(hashmap_move_one(n, NULL, "key 3") == -ENOENT);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(hashmap_move_one(n, m, "key 5") == -ENOENT);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(hashmap_move_one(n, m, "key 3") == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(hashmap_move_one(n, m, "key 4") == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering r = hashmap_get(n, "key 3");
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering assert_se(r && streq(r, "val3"));
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering r = hashmap_get(n, "key 4");
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering assert_se(r && streq(r, "val4"));
b9a8d250810d4803bc9bf6b36932b528cb991d1eLennart Poettering r = hashmap_get(m, "key 3");
b9a8d250810d4803bc9bf6b36932b528cb991d1eLennart Poettering assert_se(!r);
b9a8d250810d4803bc9bf6b36932b528cb991d1eLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(hashmap_move_one(n, m, "key 3") == -EEXIST);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
fbe550738d03b178bb004a1390e74115e904118aLennart Poettering hashmap_free_free(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_free_free(n);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void test_hashmap_move(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Hashmap *m, *n;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *val1, *val2, *val3, *val4, *r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering val1 = strdup("val1");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(val1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering val2 = strdup("val2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(val2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering val3 = strdup("val3");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(val3);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering val4 = strdup("val4");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(val4);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m = hashmap_new(&string_hash_ops);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering n = hashmap_new(&string_hash_ops);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(n, "key 1", strdup(val1));
76b543756ef69ce69784d571aefe8de65eaeb331Lennart Poettering hashmap_put(m, "key 1", val1);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt hashmap_put(m, "key 2", val2);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt hashmap_put(m, "key 3", val3);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 4", val4);
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt assert(hashmap_move(n, NULL) == 0);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt assert(hashmap_move(n, m) == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering assert_se(hashmap_size(m) == 1);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt r = hashmap_get(m, "key 1");
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt assert_se(r && streq(r, "val1"));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering r = hashmap_get(n, "key 1");
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt assert_se(r && streq(r, "val1"));
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt r = hashmap_get(n, "key 2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(r && streq(r, "val2"));
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering r = hashmap_get(n, "key 3");
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering assert_se(r && streq(r, "val3"));
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering r = hashmap_get(n, "key 4");
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering assert_se(r && streq(r, "val4"));
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering hashmap_free_free(m);
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering hashmap_free_free(n);
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poetteringstatic void test_hashmap_update(void) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering Hashmap *m;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering char *val1, *val2, *r;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering m = hashmap_new(&string_hash_ops);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering val1 = strdup("old_value");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(val1);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering val2 = strdup("new_value");
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt assert_se(val2);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 1", val1);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = hashmap_get(m, "key 1");
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering assert_se(streq(r, "old_value"));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(hashmap_update(m, "key 2", val2) == -ENOENT);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = hashmap_get(m, "key 1");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(streq(r, "old_value"));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(hashmap_update(m, "key 1", val2) == 0);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = hashmap_get(m, "key 1");
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt assert_se(streq(r, "new_value"));
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering free(val1);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering free(val2);
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering hashmap_free(m);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void test_hashmap_put(void) {
11b90e69e5620c2483b019340eff121d504db115Lennart Poettering Hashmap *m = NULL;
11b90e69e5620c2483b019340eff121d504db115Lennart Poettering int valid_hashmap_put;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering void *val1 = (void*) "val 1";
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt hashmap_ensure_allocated(&m, &string_hash_ops);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt assert_se(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering valid_hashmap_put = hashmap_put(m, "key 1", val1);
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering assert_se(valid_hashmap_put == 1);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(hashmap_put(m, "key 1", val1) == 0);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(hashmap_put(m, "key 1", (void *)"val 2") == -EEXIST);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering hashmap_free(m);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void test_hashmap_remove(void) {
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt _cleanup_hashmap_free_ Hashmap *m = NULL;
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt char *r;
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering r = hashmap_remove(NULL, "key 1");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(r == NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m = hashmap_new(&string_hash_ops);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_remove(m, "no such key");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(r == NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering hashmap_put(m, "key 1", (void*) "val 1");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering hashmap_put(m, "key 2", (void*) "val 2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_remove(m, "key 1");
5bb658a1784a0fd4f0f32adb4b1fb636ff503f7dKay Sievers assert_se(streq(r, "val 1"));
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt r = hashmap_get(m, "key 2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(streq(r, "val 2"));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(!hashmap_get(m, "key 1"));
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt}
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void test_hashmap_remove2(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_hashmap_free_free_free_ Hashmap *m = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char key1[] = "key 1";
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char key2[] = "key 2";
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char val1[] = "val 1";
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char val2[] = "val 2";
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering void *r, *r2;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_remove2(NULL, "key 1", &r2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(r == NULL);
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m = hashmap_new(&string_hash_ops);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(m);
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering r = hashmap_remove2(m, "no such key", &r2);
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering assert_se(r == NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering hashmap_put(m, strdup(key1), strdup(val1));
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering hashmap_put(m, strdup(key2), strdup(val2));
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering r = hashmap_remove2(m, key1, &r2);
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering assert_se(streq(r, val1));
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering assert_se(streq(r2, key1));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(r);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(r2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_get(m, key2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(streq(r, val2));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(!hashmap_get(m, key1));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void test_hashmap_remove_value(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_hashmap_free_ Hashmap *m = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_remove_value(NULL, "key 1", (void*) "val 1");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(r == NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m = hashmap_new(&string_hash_ops);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_remove_value(m, "key 1", (void*) "val 1");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(r == NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 1", (void*) "val 1");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 2", (void*) "val 2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_remove_value(m, "key 1", (void*) "val 1");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(streq(r, "val 1"));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_get(m, "key 2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(streq(r, "val 2"));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(!hashmap_get(m, "key 1"));
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering r = hashmap_remove_value(m, "key 2", (void*) "val 1");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(r == NULL);
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_get(m, "key 2");
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering assert_se(streq(r, "val 2"));
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering assert_se(!hashmap_get(m, "key 1"));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poetteringstatic void test_hashmap_remove_and_put(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_hashmap_free_ Hashmap *m = NULL;
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering int valid;
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering char *r;
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering m = hashmap_new(&string_hash_ops);
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering assert_se(m);
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_put(m, "invalid key", "new key", NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(valid == -ENOENT);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_put(m, "key 1", (void*) (const char *) "val 1");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(valid == 1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_put(NULL, "key 1", "key 2", (void*) (const char *) "val 2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(valid == -ENOENT);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_put(m, "key 1", "key 2", (void*) (const char *) "val 2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(valid == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_get(m, "key 2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(streq(r, "val 2"));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(!hashmap_get(m, "key 1"));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_put(m, "key 3", (void*) (const char *) "val 3");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(valid == 1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_put(m, "key 3", "key 2", (void*) (const char *) "val 2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(valid == -EEXIST);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void test_hashmap_remove_and_replace(void) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering _cleanup_hashmap_free_ Hashmap *m = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int valid;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering void *key1 = UINT_TO_PTR(1);
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poettering void *key2 = UINT_TO_PTR(2);
0370612e0522191f929e3feb7d4937fff3d421e2Lennart Poettering void *key3 = UINT_TO_PTR(3);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering void *r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int i, j;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering m = hashmap_new(&trivial_hash_ops);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(m);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_replace(m, key1, key2, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(valid == -ENOENT);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_put(m, key1, key1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(valid == 1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek valid = hashmap_remove_and_replace(NULL, key1, key2, key2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(valid == -ENOENT);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_replace(m, key1, key2, key2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(valid == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_get(m, key2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(r == key2);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek assert_se(!hashmap_get(m, key1));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_put(m, key3, key3);
3e044c492e3ebe64f4e3175c94f9db8a62557b82Markus Elfring assert_se(valid == 1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_replace(m, key3, key2, key2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(valid == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_get(m, key2);
assert_se(r == key2);
assert_se(!hashmap_get(m, key3));
/* Repeat this test several times to increase the chance of hitting
* the less likely case in hashmap_remove_and_replace where it
* compensates for the backward shift. */
for (i = 0; i < 20; i++) {
hashmap_clear(m);
for (j = 1; j < 7; j++)
hashmap_put(m, UINT_TO_PTR(10*i + j), UINT_TO_PTR(10*i + j));
valid = hashmap_remove_and_replace(m, UINT_TO_PTR(10*i + 1),
UINT_TO_PTR(10*i + 2),
UINT_TO_PTR(10*i + 2));
assert_se(valid == 0);
assert_se(!hashmap_get(m, UINT_TO_PTR(10*i + 1)));
for (j = 2; j < 7; j++) {
r = hashmap_get(m, UINT_TO_PTR(10*i + j));
assert_se(r == UINT_TO_PTR(10*i + j));
}
}
}
static void test_hashmap_ensure_allocated(void) {
Hashmap *m;
int valid_hashmap;
m = hashmap_new(&string_hash_ops);
valid_hashmap = hashmap_ensure_allocated(&m, &string_hash_ops);
assert_se(valid_hashmap == 0);
assert_se(m);
hashmap_free(m);
}
static void test_hashmap_foreach_key(void) {
Hashmap *m;
Iterator i;
bool key_found[] = { false, false, false, false };
const char *s;
const char *key;
static const char key_table[] =
"key 1\0"
"key 2\0"
"key 3\0"
"key 4\0";
m = hashmap_new(&string_hash_ops);
NULSTR_FOREACH(key, key_table)
hashmap_put(m, key, (void*) (const char*) "my dummy val");
HASHMAP_FOREACH_KEY(s, key, m, i) {
if (!key_found[0] && streq(key, "key 1"))
key_found[0] = true;
else if (!key_found[1] && streq(key, "key 2"))
key_found[1] = true;
else if (!key_found[2] && streq(key, "key 3"))
key_found[2] = true;
else if (!key_found[3] && streq(key, "fail"))
key_found[3] = true;
}
assert_se(m);
assert_se(key_found[0] && key_found[1] && key_found[2] && !key_found[3]);
hashmap_free(m);
}
static void test_hashmap_foreach(void) {
Hashmap *m;
Iterator i;
bool value_found[] = { false, false, false, false };
char *val1, *val2, *val3, *val4, *s;
unsigned count;
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 = NULL;
count = 0;
HASHMAP_FOREACH(s, m, i)
count++;
assert_se(count == 0);
m = hashmap_new(&string_hash_ops);
count = 0;
HASHMAP_FOREACH(s, m, i)
count++;
assert_se(count == 0);
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_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(!hashmap_contains(m, "Key 2"));
assert_se(!hashmap_contains(NULL, "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);
assert_se(hashmap_size(NULL) == 0);
assert_se(hashmap_buckets(NULL) == 0);
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);
assert_se(hashmap_buckets(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);
r = hashmap_get(NULL, "Key 1");
assert_se(r == NULL);
m = hashmap_new(&string_hash_ops);
hashmap_put(m, "Key 1", val);
r = hashmap_get(m, "Key 1");
assert_se(streq(r, val));
r = hashmap_get(m, "no such key");
assert_se(r == NULL);
assert_se(m);
hashmap_free_free(m);
}
static void test_hashmap_get2(void) {
Hashmap *m;
char *r;
char *val;
char key_orig[] = "Key 1";
void *key_copy;
val = strdup("my val");
assert_se(val);
key_copy = strdup(key_orig);
assert_se(key_copy);
r = hashmap_get2(NULL, key_orig, &key_copy);
assert_se(r == NULL);
m = hashmap_new(&string_hash_ops);
hashmap_put(m, key_copy, val);
key_copy = NULL;
r = hashmap_get2(m, key_orig, &key_copy);
assert_se(streq(r, val));
assert_se(key_orig != key_copy);
assert_se(streq(key_orig, key_orig));
r = hashmap_get2(m, "no such key", NULL);
assert_se(r == NULL);
assert_se(m);
hashmap_free_free_free(m);
}
static unsigned long crippled_hashmap_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) {
return trivial_hash_func(p, hash_key) & 0xff;
}
static const struct hash_ops crippled_hashmap_ops = {
.hash = crippled_hashmap_func,
.compare = trivial_compare_func,
};
static void test_hashmap_many(void) {
Hashmap *h;
unsigned i, j;
void *v, *k;
static const struct {
const struct hash_ops *ops;
unsigned n_entries;
} tests[] = {
{ .ops = NULL, .n_entries = 1 << 20 },
{ .ops = &crippled_hashmap_ops, .n_entries = 1 << 11 },
};
for (j = 0; j < ELEMENTSOF(tests); j++) {
assert_se(h = hashmap_new(tests[j].ops));
for (i = 1; i < tests[j].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 < tests[j].n_entries*3; i++)
assert_se(hashmap_contains(h, UINT_TO_PTR(i)) == (i % 3 == 1));
log_info("%u <= %u * 0.8 = %g", hashmap_size(h), hashmap_buckets(h), hashmap_buckets(h) * 0.8);
assert_se(hashmap_size(h) <= hashmap_buckets(h) * 0.8);
assert_se(hashmap_size(h) == tests[j].n_entries);
while (!hashmap_isempty(h)) {
k = hashmap_first_key(h);
v = hashmap_remove(h, k);
assert_se(v == k);
}
hashmap_free(h);
}
}
static void test_hashmap_first(void) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
m = hashmap_new(&string_hash_ops);
assert_se(m);
assert_se(!hashmap_first(m));
assert_se(hashmap_put(m, "key 1", (void*) "val 1") == 1);
assert_se(streq(hashmap_first(m), "val 1"));
assert_se(hashmap_put(m, "key 2", (void*) "val 2") == 1);
#ifdef ORDERED
assert_se(streq(hashmap_first(m), "val 1"));
assert_se(hashmap_remove(m, "key 1"));
assert_se(streq(hashmap_first(m), "val 2"));
#endif
}
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);
#ifdef ORDERED
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"));
#endif
}
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_steal_first(void) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
int seen[3] = {};
char *val;
m = hashmap_new(&string_hash_ops);
assert_se(m);
assert_se(hashmap_put(m, "key 1", (void*) "1") == 1);
assert_se(hashmap_put(m, "key 2", (void*) "22") == 1);
assert_se(hashmap_put(m, "key 3", (void*) "333") == 1);
while ((val = hashmap_steal_first(m)))
seen[strlen(val) - 1]++;
assert_se(seen[0] == 1 && seen[1] == 1 && seen[2] == 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_hashmap_reserve(void) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
m = hashmap_new(&string_hash_ops);
assert_se(hashmap_reserve(m, 1) == 0);
assert_se(hashmap_buckets(m) < 1000);
assert_se(hashmap_reserve(m, 1000) == 0);
assert_se(hashmap_buckets(m) >= 1000);
assert_se(hashmap_isempty(m));
assert_se(hashmap_put(m, "key 1", (void*) "val 1") == 1);
assert_se(hashmap_reserve(m, UINT_MAX) == -ENOMEM);
assert_se(hashmap_reserve(m, UINT_MAX - 1) == -ENOMEM);
}
void test_hashmap_funcs(void) {
test_hashmap_copy();
test_hashmap_get_strv();
test_hashmap_move_one();
test_hashmap_move();
test_hashmap_replace();
test_hashmap_update();
test_hashmap_put();
test_hashmap_remove();
test_hashmap_remove2();
test_hashmap_remove_value();
test_hashmap_remove_and_put();
test_hashmap_remove_and_replace();
test_hashmap_ensure_allocated();
test_hashmap_foreach();
test_hashmap_foreach_key();
test_hashmap_contains();
test_hashmap_merge();
test_hashmap_isempty();
test_hashmap_get();
test_hashmap_get2();
test_hashmap_size();
test_hashmap_many();
test_hashmap_first();
test_hashmap_first_key();
test_hashmap_steal_first_key();
test_hashmap_steal_first();
test_hashmap_clear_free_free();
test_hashmap_reserve();
}