From 14caa074a83fa2e659b3e490bf9bd31ff0b6b4b1 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Thu, 13 Jul 2017 18:17:02 -0700 Subject: Make idhash non-inlined (so we can add a mutex.) --- tests/idhash.c | 229 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 120 insertions(+), 109 deletions(-) (limited to 'tests') diff --git a/tests/idhash.c b/tests/idhash.c index a51db654..a30b0d0a 100644 --- a/tests/idhash.c +++ b/tests/idhash.c @@ -1,5 +1,6 @@ // -// Copyright 2016 Garrett D'Amore +// Copyright 2017 Garrett D'Amore +// Copyright 2017 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -7,139 +8,149 @@ // found online at https://opensource.org/licenses/MIT. // -#include "convey.h" #include "core/idhash.c" +#include "convey.h" Main({ - Test("General ID Hash", { - int rv; + Test("General ID Hash", + { + int rv; - Convey("Given an id hash", { - nni_idhash h; + Convey("Given an id hash", { + nni_idhash *h = NULL; - So(nni_idhash_init(&h) == 0); - So(nni_idhash_count(&h) == 0); + So(nni_idhash_init(&h) == 0); + So(h != NULL); + So(nni_idhash_count(h) == 0); - Reset({ - nni_idhash_fini(&h); - }) + Reset({ nni_idhash_fini(h); }); - Convey("We can insert an element", { - char *five = "five"; - char *four = "four"; - rv = nni_idhash_insert(&h, 5, five); - So(nni_idhash_count(&h) == 1); - So(rv == 0); + Convey("We can insert an element", { + char *five = "five"; + char *four = "four"; + rv = nni_idhash_insert(h, 5, five); + So(nni_idhash_count(h) == 1); + So(rv == 0); - Convey("And we can find it", { - void *ptr; - rv = nni_idhash_find(&h, 5, &ptr); - So(rv == 0); - So(ptr == five); - }) - Convey("We can delete it", { - void *ptr; - rv = nni_idhash_remove(&h, 5); - So(rv == 0); - rv = nni_idhash_find(&h, 5, &ptr); - So(rv == NNG_ENOENT); - }) - Convey("We can change the value", { - void *ptr; - So(nni_idhash_insert(&h, 5, four) == 0); - So(nni_idhash_count(&h) == 1); - So(nni_idhash_find(&h, 5, &ptr) == 0); - So(ptr == four); - }) - Convey("We can insert a hash collision", { - void *ptr; - So(nni_idhash_insert(&h, 13, four) == 0); - So(nni_idhash_count(&h) == 2); - So(nni_idhash_find(&h, 5, &ptr) == 0); - So(ptr == five); - So(nni_idhash_find(&h, 13, &ptr) == 0); - So(ptr == four); - Convey("And delete the intermediate", { - So(nni_idhash_remove(&h, 5) == 0); - ptr = NULL; - So(nni_idhash_find(&h, 13, &ptr) == 0); - So(ptr == four); - }) - }) + Convey("And we can find it", { + void *ptr; + rv = nni_idhash_find(h, 5, &ptr); + So(rv == 0); + So(ptr == five); + }); + Convey("We can delete it", { + void *ptr; + rv = nni_idhash_remove(h, 5); + So(rv == 0); + rv = nni_idhash_find(h, 5, &ptr); + So(rv == NNG_ENOENT); + }); + Convey("We can change the value", { + void *ptr; + So(nni_idhash_insert(h, 5, four) == + 0); + So(nni_idhash_count(h) == 1); + So(nni_idhash_find(h, 5, &ptr) == + 0); + So(ptr == four); + }); + Convey("We can insert a hash collision", { + void *ptr; + So(nni_idhash_insert( + h, 13, four) == 0); + So(nni_idhash_count(h) == 2); + So(nni_idhash_find(h, 5, &ptr) == + 0); + So(ptr == five); + So(nni_idhash_find(h, 13, &ptr) == + 0); + So(ptr == four); + Convey("And delete intermediate", { + So(nni_idhash_remove( + h, 5) == 0); + ptr = NULL; + So(nni_idhash_find( + h, 13, &ptr) == 0); + So(ptr == four); + }); + }); - }) - Convey("We cannot find bogus values", { - void *ptr = NULL; - rv = nni_idhash_find(&h, 42, &ptr); - So(rv == NNG_ENOENT); - So(ptr == NULL); - }) - }) - }) + }); + Convey("We cannot find bogus values", { + void *ptr; + ptr = NULL; + rv = nni_idhash_find(h, 42, &ptr); + So(rv == NNG_ENOENT); + So(ptr == NULL); + }); + }); + }) - Test("Resize ID Hash", { - int expect[1024]; - int i; + Test("Resize ID Hash", { + int expect[1024]; + int i; - for (i = 0; i < 1024; i++) { - expect[i] = i; - } - Convey("Given an id hash", { - nni_idhash h; + for (i = 0; i < 1024; i++) { + expect[i] = i; + } + Convey("Given an id hash", { + nni_idhash *h; - So(nni_idhash_init(&h) == 0); - So(nni_idhash_count(&h) == 0); + So(nni_idhash_init(&h) == 0); + So(nni_idhash_count(h) == 0); - Reset({ - nni_idhash_fini(&h); - }) + Reset({ nni_idhash_fini(h); }); - Convey("We can insert 1024 items", { - for (i = 0; i < 1024; i++) { - nni_idhash_insert(&h, i, &expect[i]); - } - So(nni_idhash_count(&h) == 1024); + Convey("We can insert 1024 items", { + for (i = 0; i < 1024; i++) { + nni_idhash_insert( + h, i, &expect[i]); + } + So(nni_idhash_count(h) == 1024); - Convey("We can remove them", { - for (i = 0; i < 1024; i++) { - nni_idhash_remove(&h, i); - } - So(nni_idhash_count(&h) == 0); - }) - }) - }) - }) + Convey("We can remove them", { + for (i = 0; i < 1024; i++) { + nni_idhash_remove(h, i); + } + So(nni_idhash_count(h) == 0); + }); + }); + }); + }); Test("Dynamic ID generation", { Convey("Given a small ID hash", { - nni_idhash h; - int expect[5]; - uint32_t id; - int i; + nni_idhash *h; + int expect[5]; + uint32_t id; + int i; So(nni_idhash_init(&h) == 0); - Reset({ - nni_idhash_fini(&h); - }) - nni_idhash_set_limits(&h, 10, 13, 10); + Reset({ nni_idhash_fini(h); }); + nni_idhash_set_limits(h, 10, 13, 10); So(1); Convey("We can fill the table", { for (i = 0; i < 4; i++) { - So(nni_idhash_alloc(&h, &id, &expect[i]) == 0); + So(nni_idhash_alloc( + h, &id, &expect[i]) == 0); So(id == (i + 10)); } Convey("Adding another fails", { - So(nni_idhash_alloc(&h, &id, &expect[5]) == NNG_ENOMEM); - }) + So(nni_idhash_alloc(h, &id, + &expect[5]) == NNG_ENOMEM); + }); Convey("Deleting one lets us reinsert", { - nni_idhash_remove(&h, 11); - So(nni_idhash_alloc(&h, &id, &expect[5]) == 0); + nni_idhash_remove(h, 11); + So(nni_idhash_alloc( + h, &id, &expect[5]) == 0); So(id == 11); - }) - }) + }); + }); Convey("We cannot insert bogus values", { - So(nni_idhash_insert(&h, 1, &expect[0]) == NNG_EINVAL); - So(nni_idhash_insert(&h, 100, &expect[0]) == NNG_EINVAL); - }) - }) - }) -}) + So(nni_idhash_insert(h, 1, &expect[0]) == + NNG_EINVAL); + So(nni_idhash_insert(h, 100, &expect[0]) == + NNG_EINVAL); + }); + }); + }); +}); -- cgit v1.2.3-70-g09d2