From 85903d04bd8a99b5a2c749c2c2bc103f87b35281 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 30 Jun 2021 01:55:23 -0400 Subject: Fix hash size after removing elements --- src/hash.c | 2 +- test/hash_test.c | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/hash.c b/src/hash.c index 8183082..d9556ed 100644 --- a/src/hash.c +++ b/src/hash.c @@ -200,6 +200,7 @@ zix_hash_remove(ZixHash* hash, const void* value) if (h_nomod == e->hash && hash->equal_func(zix_hash_value(e), value)) { *next_ptr = e->next; free(e); + --hash->count; return ZIX_STATUS_SUCCESS; } next_ptr = &e->next; @@ -214,7 +215,6 @@ zix_hash_remove(ZixHash* hash, const void* value) } } - --hash->count; return ZIX_STATUS_NOT_FOUND; } diff --git a/test/hash_test.c b/test/hash_test.c index 2b9bb51..00f1bce 100644 --- a/test/hash_test.c +++ b/test/hash_test.c @@ -158,12 +158,19 @@ stress(void) // Remove strings for (size_t i = 0; i < n_strings; ++i) { + const size_t initial_size = zix_hash_size(hash); + // Remove string ZixStatus st = zix_hash_remove(hash, &strings[i]); if (st) { return test_fail("Failed to remove `%s'\n", strings[i]); } + // Ensure size is updated + if (zix_hash_size(hash) != initial_size - 1) { + return test_fail("Removing node did not decrease hash size\n"); + } + // Ensure second removal fails st = zix_hash_remove(hash, &strings[i]); if (st != ZIX_STATUS_NOT_FOUND) { -- cgit v1.2.1