Skip to content

Commit

Permalink
Update test57.c per a suggestion by @mark-summerfield
Browse files Browse the repository at this point in the history
Use a non-null `someaddr`, and use more thorough asserts instead
of a single printf so that we get a bit more test coverage here.
  • Loading branch information
Quuxplusone committed Jun 21, 2022
1 parent 44a66fe commit 8844b52
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
15 changes: 6 additions & 9 deletions doc/userguide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ Here is a simple example where a structure has a pointer member, called `key`.
.A pointer key
----------------------------------------------------------------------
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "uthash.h"

Expand All @@ -816,17 +816,16 @@ typedef struct {
} el_t;

el_t *hash = NULL;
char *someaddr = NULL;
void *someaddr = &hash;

int main() {
el_t *d;
el_t *e = (el_t *)malloc(sizeof *e);
if (!e) return -1;
e->key = (void*)someaddr;
e->key = someaddr;
e->i = 1;
HASH_ADD_PTR(hash, key, e);
HASH_FIND_PTR(hash, &someaddr, d);
if (d) printf("found\n");
assert(d == e);

/* release memory */
HASH_DEL(hash, e);
Expand All @@ -835,9 +834,7 @@ int main() {
}
----------------------------------------------------------------------
This example is included in `tests/test57.c`. Note that the end of the program
deletes the element out of the hash, (and since no more elements remain in the
hash), uthash releases its internal memory.
This example is included in `tests/test57.c`.
Structure keys
~~~~~~~~~~~~~~
Expand Down Expand Up @@ -893,7 +890,7 @@ int main(int argc, char *argv[]) {

----------------------------------------------------------------------
This usage is nearly the same as use of a compound key explained below.
This usage is nearly the same as the usage of a compound key explained below.
Note that the general macros require the name of the `UT_hash_handle` to be
passed as the first argument (here, this is `hh`). The general macros are
Expand Down
1 change: 0 additions & 1 deletion tests/test57.ans
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
found
59 changes: 40 additions & 19 deletions tests/test57.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
#include "uthash.h"

typedef struct {
Expand All @@ -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;
}

0 comments on commit 8844b52

Please sign in to comment.