test-hashmap-plain.c revision e1323fbfbe8a574f28b704f2df8ce7f99e3a28f5
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2013 Daniel Buch
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 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 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/>.
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void test_hashmap_replace(void) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering char *val1, *val2, *val3, *val4, *val5, *r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void test_hashmap_copy(void) {
fbe550738d03b178bb004a1390e74115e904118aLennart Poetteringstatic void test_hashmap_get_strv(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void test_hashmap_move_one(void) {
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 assert_se(hashmap_move_one(n, m, "key 3") == -EEXIST);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void test_hashmap_move(void) {
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poetteringstatic void test_hashmap_update(void) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(hashmap_update(m, "key 2", val2) == -ENOENT);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(hashmap_update(m, "key 1", val2) == 0);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void test_hashmap_put(void) {
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt hashmap_ensure_allocated(&m, &string_hash_ops);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering valid_hashmap_put = hashmap_put(m, "key 1", val1);
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 Poetteringstatic void test_hashmap_remove(void) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering hashmap_put(m, "key 1", (void*) "val 1");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering hashmap_put(m, "key 2", (void*) "val 2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void test_hashmap_remove2(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_hashmap_free_free_free_ Hashmap *m = NULL;
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering r = hashmap_remove2(m, "no such key", &r2);
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering hashmap_put(m, strdup(key1), strdup(val1));
49f3fffd94591bdf2bd6c2233a9300daeab79566Lennart Poettering hashmap_put(m, strdup(key2), strdup(val2));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void test_hashmap_remove_value(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_hashmap_free_ Hashmap *m = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_remove_value(NULL, "key 1", (void*) "val 1");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_remove_value(m, "key 1", (void*) "val 1");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 1", (void*) "val 1");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering hashmap_put(m, "key 2", (void*) "val 2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = hashmap_remove_value(m, "key 1", (void*) "val 1");
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering r = hashmap_remove_value(m, "key 2", (void*) "val 1");
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poetteringstatic void test_hashmap_remove_and_put(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_hashmap_free_ Hashmap *m = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_put(m, "invalid key", "new key", NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_put(m, "key 1", (void*) (const char *) "val 1");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_put(NULL, "key 1", "key 2", (void*) (const char *) "val 2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_put(m, "key 1", "key 2", (void*) (const char *) "val 2");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_put(m, "key 3", (void*) (const char *) "val 3");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_put(m, "key 3", "key 2", (void*) (const char *) "val 2");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void test_hashmap_remove_and_replace(void) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering _cleanup_hashmap_free_ Hashmap *m = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_replace(m, key1, key2, NULL);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek valid = hashmap_remove_and_replace(NULL, key1, key2, key2);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_replace(m, key1, key2, key2);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek assert_se(!hashmap_get(m, key1));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering valid = hashmap_remove_and_replace(m, key3, key2, key2);
hashmap_clear(m);
static void test_hashmap_ensure_allocated(void) {
Hashmap *m;
int valid_hashmap;
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 *key;
static const char key_table[] =
key_found[0] = true;
assert_se(m);
hashmap_free(m);
static void test_hashmap_foreach(void) {
Hashmap *m;
Iterator i;
bool value_found[] = { false, false, false, false };
unsigned count;
m = NULL;
count = 0;
HASHMAP_FOREACH(s, m, i)
count++;
count = 0;
HASHMAP_FOREACH(s, m, i)
count++;
HASHMAP_FOREACH(s, m, i) {
value_found[0] = true;
assert_se(m);
static void test_hashmap_merge(void) {
Hashmap *m;
Hashmap *n;
assert_se(n);
assert_se(m);
hashmap_free(n);
static void test_hashmap_contains(void) {
Hashmap *m;
char *val1;
assert_se(m);
static void test_hashmap_isempty(void) {
Hashmap *m;
char *val1;
assert_se(m);
static void test_hashmap_size(void) {
Hashmap *m;
assert_se(m);
static void test_hashmap_get(void) {
Hashmap *m;
char *val;
assert_se(m);
static void test_hashmap_get2(void) {
Hashmap *m;
char *val;
void *key_copy;
assert_se(m);
static void test_hashmap_many(void) {
Hashmap *h;
unsigned n_entries;
} tests[] = {
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) {
assert_se(m);
#ifdef ORDERED
static void test_hashmap_first_key(void) {
assert_se(m);
#ifdef ORDERED
static void test_hashmap_steal_first_key(void) {
assert_se(m);
static void test_hashmap_steal_first(void) {
char *val;
assert_se(m);
static void test_hashmap_clear_free_free(void) {
assert_se(m);
static void test_hashmap_reserve(void) {
void test_hashmap_funcs(void) {