aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile2
-rw-r--r--tests/README1
-rw-r--r--tests/hashscan.c2
-rw-r--r--tests/test57.ans1
-rw-r--r--tests/test57.c59
-rw-r--r--tests/test65.c2
-rw-r--r--tests/test97.ans0
-rw-r--r--tests/test97.c57
8 files changed, 101 insertions, 23 deletions
diff --git a/tests/Makefile b/tests/Makefile
index 31f0cc240..ff3504ead 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -12,7 +12,7 @@ PROGS = test1 test2 test3 test4 test5 test6 test7 test8 test9 \
test66 test67 test68 test69 test70 test71 test72 test73 \
test74 test75 test76 test77 test78 test79 test80 test81 \
test82 test83 test84 test85 test86 test87 test88 test89 \
- test90 test91 test92 test93 test94 test95 test96
+ test90 test91 test92 test93 test94 test95 test96 test97
CFLAGS += -I$(HASHDIR)
#CFLAGS += -DHASH_BLOOM=16
#CFLAGS += -O2
diff --git a/tests/README b/tests/README
index a287de670..747661dd0 100644
--- a/tests/README
+++ b/tests/README
@@ -98,6 +98,7 @@ test93: alt_fatal
test94: utlist with fields named other than 'next' and 'prev'
test95: utstack
test96: HASH_FUNCTION + HASH_KEYCMP
+test97: deleting a const-qualified node from a hash
Other Make targets
================================================================================
diff --git a/tests/hashscan.c b/tests/hashscan.c
index d18b364ef..dc581f2dd 100644
--- a/tests/hashscan.c
+++ b/tests/hashscan.c
@@ -1,5 +1,5 @@
/*
-Copyright (c) 2005-2021, Troy D. Hanson http://troydhanson.github.io/uthash/
+Copyright (c) 2005-2022, Troy D. Hanson https://troydhanson.github.io/uthash/
All rights reserved.
Redistribution and use in source and binary forms, with or without
diff --git a/tests/test57.ans b/tests/test57.ans
index 4d3bb1d0b..e69de29bb 100644
--- a/tests/test57.ans
+++ b/tests/test57.ans
@@ -1 +0,0 @@
-found
diff --git a/tests/test57.c b/tests/test57.c
index 3b89fb52d..7ba3b81ae 100644
--- a/tests/test57.c
+++ b/tests/test57.c
@@ -1,5 +1,5 @@
-#include <stdio.h>
-#include <stdlib.h>
+#include <assert.h>
+#include <stddef.h>
#include "uthash.h"
typedef struct {
@@ -8,25 +8,46 @@ typedef struct {
UT_hash_handle hh;
} el_t;
+el_t *findit(el_t *hash, void *keytofind)
+{
+ el_t *found;
+ HASH_FIND_PTR(hash, &keytofind, found);
+ return found;
+}
+
int main()
{
- el_t *d;
el_t *hash = NULL;
- char *someaddr = NULL;
- el_t *e = (el_t*)malloc(sizeof(el_t));
- if (!e) {
- return -1;
- }
- e->key = (void*)someaddr;
- e->i = 1;
- HASH_ADD_PTR(hash,key,e);
- HASH_FIND_PTR(hash, &someaddr, d);
- if (d != NULL) {
- printf("found\n");
- }
-
- /* release memory */
- HASH_DEL(hash,e);
- free(e);
+ el_t e1;
+ el_t e2;
+
+ e1.key = NULL;
+ e1.i = 1;
+
+ e2.key = &e2;
+ e2.i = 2;
+
+ assert(findit(hash, NULL) == NULL);
+ assert(findit(hash, &e1) == NULL);
+ assert(findit(hash, &e2) == NULL);
+
+ HASH_ADD_PTR(hash, key, &e1);
+ assert(findit(hash, NULL) == &e1);
+ assert(findit(hash, &e1) == NULL);
+ assert(findit(hash, &e2) == NULL);
+
+ HASH_ADD_PTR(hash, key, &e2);
+ assert(findit(hash, NULL) == &e1);
+ assert(findit(hash, &e1) == NULL);
+ assert(findit(hash, &e2) == &e2);
+
+ HASH_DEL(hash, &e1);
+ assert(findit(hash, NULL) == NULL);
+ assert(findit(hash, &e1) == NULL);
+ assert(findit(hash, &e2) == &e2);
+
+ HASH_CLEAR(hh, hash);
+ assert(hash == NULL);
+
return 0;
}
diff --git a/tests/test65.c b/tests/test65.c
index 0584fb1a0..5820a4552 100644
--- a/tests/test65.c
+++ b/tests/test65.c
@@ -3,7 +3,7 @@
#include "uthash.h"
// this is an example of how to do a LRU cache in C using uthash
-// http://troydhanson.github.io/uthash/
+// https://troydhanson.github.io/uthash/
// by Jehiah Czebotar 2011 - jehiah@gmail.com
// this code is in the public domain http://unlicense.org/
diff --git a/tests/test97.ans b/tests/test97.ans
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/tests/test97.ans
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;
+}