From 9a14454d3c5589373253571cee7428c593adefd9 Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Thu, 17 Oct 2024 12:16:20 +0200 Subject: Squashed 'dependencies/uthash/' changes from bf152630..f69112c0 f69112c0 utarray: Fix typo in docs 619fe95c Fix MSVC warning C4127 in HASH_BLOOM_TEST (#261) eeba1961 uthash: Improve the docs for HASH_ADD_INORDER ca98384c HASH_DEL should be able to delete a const-qualified node 095425f7 utlist: Add one more assertion in DL_DELETE2 399bf74b utarray: Stop making `oom` a synonym for `utarray_oom` 85bf75ab utarray_str_cpy: Remove strdup; utarray_oom() if strdup fails. 1a53f304 GitHub CI: Also test building the docs (#248) 4d01591e The MCST Elbrus C Compiler supports __typeof. (#247) 1e0baf06 CI: Add GitHub Actions CI 8844b529 Update test57.c per a suggestion by @mark-summerfield 44a66fe8 Update http:// URLs to https://, and copyright dates to 2022. NFC. git-subtree-dir: dependencies/uthash git-subtree-split: f69112c04f1b6e059b8071cb391a1fcc83791a00 --- tests/test97.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/test97.c (limited to 'tests/test97.c') diff --git a/tests/test97.c b/tests/test97.c new file mode 100644 index 000000000..d59008bac --- /dev/null +++ b/tests/test97.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include "uthash.h" + +struct item { + int payload; + UT_hash_handle hh; +}; + +void delete_without_modifying(struct item *head, const struct item *p) +{ + struct item old; + memcpy(&old, p, sizeof(struct item)); // also copy the padding bits + assert(memcmp(&old, p, sizeof(struct item)) == 0); + assert(p->hh.tbl == head->hh.tbl); // class invariant + HASH_DEL(head, p); + assert(memcmp(&old, p, sizeof(struct item)) == 0); // unmodified by HASH_DEL +} + +int main() +{ + struct item *items = NULL; + struct item *found = NULL; + int fortytwo = 42; + int i; + + for (i=0; i < 100; i++) { + struct item *p = (struct item *)malloc(sizeof *p); + p->payload = i; + HASH_ADD_INT(items, payload, p); + } + assert(HASH_COUNT(items) == 100); + + // Delete item "42" from the hash, wherever it is. + HASH_FIND_INT(items, &fortytwo, found); + assert(found != NULL); + assert(found->payload == 42); + delete_without_modifying(items, found); + + assert(HASH_COUNT(items) == 99); + HASH_FIND_INT(items, &fortytwo, found); + assert(found == NULL); + + // Delete the very first item in the hash. + assert(items != NULL); + i = items->payload; + delete_without_modifying(items, items); + + assert(HASH_COUNT(items) == 98); + HASH_FIND_INT(items, &i, found); + assert(found == NULL); + + // leak the items, we don't care + + return 0; +} -- cgit v1.2.3