summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-06-30 01:55:23 -0400
committerDavid Robillard <d@drobilla.net>2021-06-30 01:55:23 -0400
commit85903d04bd8a99b5a2c749c2c2bc103f87b35281 (patch)
tree05876ec7266b66159783f2891b5a57cf9b907abe
parent10a26115c115fa1bd88b1f431f965b348c9b5cdc (diff)
downloadzix-85903d04bd8a99b5a2c749c2c2bc103f87b35281.tar.gz
zix-85903d04bd8a99b5a2c749c2c2bc103f87b35281.tar.bz2
zix-85903d04bd8a99b5a2c749c2c2bc103f87b35281.zip
Fix hash size after removing elements
-rw-r--r--src/hash.c2
-rw-r--r--test/hash_test.c7
2 files changed, 8 insertions, 1 deletions
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) {