aboutsummaryrefslogtreecommitdiff
path: root/tests/test97.c
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2024-10-17 12:16:20 +0200
committerToni Uhlig <matzeton@googlemail.com>2024-10-17 12:16:20 +0200
commit9a14454d3c5589373253571cee7428c593adefd9 (patch)
tree2ebd3c81ca4594ed2c9b1267a7af31d8cd1646e9 /tests/test97.c
parent1fa53c5bf8d0717f784c79abaa5111f88ab00221 (diff)
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
Diffstat (limited to 'tests/test97.c')
-rw-r--r--tests/test97.c57
1 files changed, 57 insertions, 0 deletions
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 <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#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;
+}