From eb3b131db73610274a41f04d8fd6b7cf879cc016 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Tue, 17 Jan 2017 16:55:25 -0800 Subject: Added dynamic ID generation & management for idhash tables. This will allow us to use idhash tables to manage id handles a bit more flexibly. For example, sockets, pipe IDs, etc. can all be generated, and we can use hash tables to ensure that values do not collide. --- tests/idhash.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/idhash.c b/tests/idhash.c index fb240078..b05b4b2f 100644 --- a/tests/idhash.c +++ b/tests/idhash.c @@ -7,9 +7,9 @@ // found online at https://opensource.org/licenses/MIT. // -#include "core/idhash.c" #include "convey.h" #include "stubs.h" +#include "core/idhash.c" Main({ Test("General ID Hash", { @@ -133,4 +133,37 @@ Main({ }) }) }) + + Test("Dynamic ID generation", { + Convey("Given a small ID hash", { + nni_idhash *h; + int expect[5]; + uint32_t id; + int i; + So(nni_idhash_create(&h) == 0); + Reset({ + nni_idhash_destroy(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(id == (i + 10)); + } + Convey("Adding another fails", { + 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); + 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); + }) + }) + }) }) -- cgit v1.2.3-70-g09d2