aboutsummaryrefslogtreecommitdiff
path: root/tests/test6.c
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2020-12-01 13:33:34 +0100
committerToni Uhlig <matzeton@googlemail.com>2020-12-01 13:33:34 +0100
commitc8bf38e5fb717d40635a2a89b22ed71b0de4266b (patch)
tree63751b2f5497c6f99e1c6a78f23a8e6e5c49833f /tests/test6.c
Squashed 'dependencies/uthash/' content from commit 8e67ced
git-subtree-dir: dependencies/uthash git-subtree-split: 8e67ced1d1c5bd8141c542a22630e6de78aa6b90
Diffstat (limited to 'tests/test6.c')
-rw-r--r--tests/test6.c121
1 files changed, 121 insertions, 0 deletions
diff --git a/tests/test6.c b/tests/test6.c
new file mode 100644
index 000000000..55ce36bfb
--- /dev/null
+++ b/tests/test6.c
@@ -0,0 +1,121 @@
+#include "uthash.h"
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* Set up macros for alternative malloc/free functions */
+#undef uthash_malloc
+#undef uthash_free
+#undef uthash_memcmp
+#undef uthash_strlen
+#undef uthash_bzero
+#define uthash_malloc(sz) alt_malloc(sz)
+#define uthash_free(ptr,sz) alt_free(ptr,sz)
+#define uthash_memcmp(a,b,n) alt_memcmp(a,b,n)
+#define uthash_strlen(s) ..fail_to_compile..
+#define uthash_bzero(a,n) alt_bzero(a,n)
+
+typedef struct example_user_t {
+ int id;
+ int cookie;
+ UT_hash_handle hh;
+} example_user_t;
+
+static size_t alt_malloc_sizes[10];
+static int alt_malloc_balance = 0;
+static void *alt_malloc(size_t sz)
+{
+ alt_malloc_sizes[alt_malloc_balance++] = sz;
+ if (alt_malloc_balance == 1) {
+ assert(sz == sizeof(UT_hash_table));
+ }
+ return malloc(sz);
+}
+static void alt_free(void *ptr, size_t sz)
+{
+ size_t expected = alt_malloc_sizes[--alt_malloc_balance];
+ if (sz != expected) {
+ printf("expected free of size %d, got %d\n", (int)expected, (int)sz);
+ }
+ free(ptr);
+}
+
+static int alt_memcmp_count = 0;
+static int alt_memcmp(const void *a, const void *b, size_t n)
+{
+ ++alt_memcmp_count;
+ return memcmp(a,b,n);
+}
+
+static int alt_bzero_count = 0;
+static void alt_bzero(void *a, size_t n)
+{
+ ++alt_bzero_count;
+ memset(a,0,n);
+}
+
+static void *real_malloc(size_t n)
+{
+ return malloc(n);
+}
+
+static void real_free(void *p)
+{
+ free(p);
+}
+
+#undef malloc
+#undef realloc
+#undef free
+#undef memset
+#undef memcmp
+#undef strlen
+#define malloc ..fail_to_compile..
+#define realloc ..fail_to_compile..
+#define free ..fail_to_compile..
+#define memset ..fail_to_compile..
+#define memcmp ..fail_to_compile..
+#define strlen ..fail_to_compile..
+
+int main()
+{
+ int i;
+ example_user_t *user, *tmp, *users=NULL;
+
+ /* create elements */
+ for(i=0; i<10; i++) {
+ user = (example_user_t*)real_malloc(sizeof(example_user_t));
+ if (user == NULL) {
+ exit(-1);
+ }
+ user->id = i;
+ user->cookie = i*i;
+ HASH_ADD_INT(users,id,user);
+ }
+
+ /* delete each ID */
+ for(i=0; i<10; i++) {
+ HASH_FIND_INT(users,&i,tmp);
+ if (tmp != NULL) {
+ HASH_DEL(users,tmp);
+ real_free(tmp);
+ } else {
+ printf("user id %d not found\n", i);
+ }
+ }
+
+ /* show the hash */
+ for(user=users; user != NULL; user=(example_user_t*)(user->hh.next)) {
+ printf("user %d, cookie %d\n", user->id, user->cookie);
+ }
+
+#ifdef HASH_BLOOM
+ assert(alt_bzero_count == 3);
+#else
+ assert(alt_bzero_count == 2);
+#endif
+ assert(alt_memcmp_count == 10);
+ assert(alt_malloc_balance == 0);
+ return 0;
+}