From c8bf38e5fb717d40635a2a89b22ed71b0de4266b Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Tue, 1 Dec 2020 13:33:34 +0100 Subject: Squashed 'dependencies/uthash/' content from commit 8e67ced git-subtree-dir: dependencies/uthash git-subtree-split: 8e67ced1d1c5bd8141c542a22630e6de78aa6b90 --- tests/test83.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tests/test83.c (limited to 'tests/test83.c') diff --git a/tests/test83.c b/tests/test83.c new file mode 100644 index 000000000..b2140c54d --- /dev/null +++ b/tests/test83.c @@ -0,0 +1,61 @@ +#include "uthash.h" +#include +#include /* malloc */ + +typedef struct person_t { + char first_name[10]; + int id; + UT_hash_handle hh; +} person_t; + +int main() +{ + person_t *people=NULL, *person, *new_person, *tmp; + const char **name; + const char * names[] = { "bob", "jack", "gary", "ty", "bo", "phil", "art", + "gil", "buck", "ted", NULL + }; + int id=0; + + for(name=names; *name!=NULL; name++) { + person = (person_t*)malloc(sizeof(person_t)); + if (person == NULL) { + exit(-1); + } + strcpy(person->first_name, *name); + person->id = id++; + HASH_ADD_STR(people,first_name,person); + printf("added %s (id %d)\n", person->first_name, person->id); + } + + person=NULL; + person_t **p=&person; + + for(name=names; *name!=NULL; name++) { + HASH_FIND_STR(people,*name,*p); + if (person != NULL) { + printf("found %s (id %d)\n", person->first_name, person->id); + new_person = (person_t *)malloc(sizeof(person_t)); + if (new_person == NULL) { + exit(-1); + } + memcpy(new_person, person, sizeof(person_t)); + new_person->id = person->id*10; + HASH_REPLACE_STR(people,first_name,new_person,tmp); + printf("replaced (%c) with %s (id %d)\n", (tmp!=NULL)?'y':'n', new_person->first_name, new_person->id); + if (tmp != NULL) { + free(tmp); + } + } else { + printf("failed to find %s\n", *name); + } + } + + printf("traversing... \n"); + HASH_ITER(hh, people, person, tmp) { + printf("%s (id %d)\n", person->first_name, person->id); + HASH_DEL(people,person); + free(person); + } + return 0; +} -- cgit v1.2.3