diff options
| author | Garrett D'Amore <garrett@damore.org> | 2020-11-21 22:11:21 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2020-11-23 22:20:12 -0800 |
| commit | d1218d7309475193b53911667911c4f59a1a7752 (patch) | |
| tree | 6ea796998fb60d2cb8afa704faa77fe7fddd644c /src/core/id_test.c | |
| parent | b826bfc171d90f8bde7bd672c0ac14201b8b2742 (diff) | |
| download | nng-d1218d7309475193b53911667911c4f59a1a7752.tar.gz nng-d1218d7309475193b53911667911c4f59a1a7752.tar.bz2 nng-d1218d7309475193b53911667911c4f59a1a7752.zip | |
New NUTS test framework (NNG Unit Test Support).
This is based on testutil/acutest, but is cleaner and fixes some
short-comings. We will be adding more support for additional
common paradigms to better facilitate transport tests.
While here we added some more test cases, and fixed a possible
symbol collision in the the stats framework (due to Linux use
of a macro definition of "si_value" in a standard OS header).
Test coverage may regress slightly as we are no longer using
some of the legacy APIs.
Diffstat (limited to 'src/core/id_test.c')
| -rw-r--r-- | src/core/id_test.c | 103 |
1 files changed, 51 insertions, 52 deletions
diff --git a/src/core/id_test.c b/src/core/id_test.c index 8312b5cc..51872e69 100644 --- a/src/core/id_test.c +++ b/src/core/id_test.c @@ -7,8 +7,7 @@ // found online at https://opensource.org/licenses/MIT. // -#include "acutest.h" -#include "testutil.h" +#include <nuts.h> #include "idhash.h" @@ -22,16 +21,16 @@ test_basic(void) nni_id_map_init(&m, 0, 0, false); // insert it - TEST_NNG_PASS(nni_id_set(&m, 5, five)); + NUTS_PASS(nni_id_set(&m, 5, five)); // retrieve it - TEST_CHECK(nni_id_get(&m, 5) == five); + NUTS_TRUE(nni_id_get(&m, 5) == five); // change it - TEST_NNG_PASS(nni_id_set(&m, 5, four)); - TEST_CHECK(nni_id_get(&m, 5) == four); + NUTS_PASS(nni_id_set(&m, 5, four)); + NUTS_TRUE(nni_id_get(&m, 5) == four); // delete - TEST_NNG_PASS(nni_id_remove(&m, 5)); + NUTS_PASS(nni_id_remove(&m, 5)); nni_id_map_fini(&m); } @@ -44,17 +43,17 @@ test_random(void) for (i = 0; i < 2; i++) { nni_id_map m; nni_id_map_init(&m, 0, 0, true); - TEST_NNG_PASS(nni_id_alloc(&m, &id, &id)); + NUTS_PASS(nni_id_alloc(&m, &id, &id)); nni_id_map_fini(&m); - TEST_CHECK(id != 0); + NUTS_TRUE(id != 0); if (id != 1) { break; } // one chance in 4 billion, but try again } - TEST_CHECK(id != 1); - TEST_CHECK(i < 2); + NUTS_TRUE(id != 1); + NUTS_TRUE(i < 2); } void @@ -67,14 +66,14 @@ test_collision(void) nni_id_map_init(&m, 0, 0, false); // Carefully crafted -- 13 % 8 == 5. - TEST_NNG_PASS(nni_id_set(&m, 5, five)); - TEST_NNG_PASS(nni_id_set(&m, 13, four)); - TEST_CHECK(nni_id_get(&m, 5) == five); - TEST_CHECK(nni_id_get(&m, 13) == four); + NUTS_PASS(nni_id_set(&m, 5, five)); + NUTS_PASS(nni_id_set(&m, 13, four)); + NUTS_TRUE(nni_id_get(&m, 5) == five); + NUTS_TRUE(nni_id_get(&m, 13) == four); // Delete the intermediate - TEST_NNG_PASS(nni_id_remove(&m, 5)); - TEST_CHECK(nni_id_get(&m, 13) == four); + NUTS_PASS(nni_id_remove(&m, 5)); + NUTS_TRUE(nni_id_get(&m, 13) == four); nni_id_map_fini(&m); } @@ -85,9 +84,9 @@ test_empty(void) nni_id_map m; nni_id_map_init(&m, 0, 0, false); - TEST_CHECK(nni_id_get(&m, 42) == NULL); - TEST_NNG_FAIL(nni_id_remove(&m, 42), NNG_ENOENT); - TEST_NNG_FAIL(nni_id_remove(&m, 1), NNG_ENOENT); + NUTS_TRUE(nni_id_get(&m, 42) == NULL); + NUTS_FAIL(nni_id_remove(&m, 42), NNG_ENOENT); + NUTS_FAIL(nni_id_remove(&m, 1), NNG_ENOENT); nni_id_map_fini(&m); } @@ -98,10 +97,10 @@ test_not_found(void) uint32_t id; nni_id_map_init(&m, 0, 0, false); - TEST_NNG_PASS(nni_id_alloc(&m, &id, &id)); - TEST_NNG_FAIL(nni_id_remove(&m, 42), NNG_ENOENT); - TEST_NNG_FAIL(nni_id_remove(&m, 2), NNG_ENOENT); - TEST_NNG_PASS(nni_id_remove(&m, id)); + NUTS_PASS(nni_id_alloc(&m, &id, &id)); + NUTS_FAIL(nni_id_remove(&m, 42), NNG_ENOENT); + NUTS_FAIL(nni_id_remove(&m, 2), NNG_ENOENT); + NUTS_PASS(nni_id_remove(&m, id)); nni_id_map_fini(&m); } @@ -121,13 +120,13 @@ test_resize(void) for (i = 0; i < 1024; i++) { if ((rv = nni_id_set(&m, i, &expect[i])) != 0) { - TEST_NNG_PASS(rv); + NUTS_PASS(rv); } } for (i = 0; i < 1024; i++) { if ((rv = nni_id_remove(&m, i)) != 0) { - TEST_NNG_PASS(rv); + NUTS_PASS(rv); } } nni_id_map_fini(&m); @@ -143,24 +142,24 @@ test_dynamic(void) nni_id_map_init(&m, 10, 13, false); // We can fill the table. - TEST_NNG_PASS(nni_id_alloc(&m, &id, &expect[0])); - TEST_CHECK(id == 10); - TEST_NNG_PASS(nni_id_alloc(&m, &id, &expect[1])); - TEST_CHECK(id == 11); - TEST_NNG_PASS(nni_id_alloc(&m, &id, &expect[2])); - TEST_CHECK(id == 12); - TEST_NNG_PASS(nni_id_alloc(&m, &id, &expect[3])); - TEST_CHECK(id == 13); + NUTS_PASS(nni_id_alloc(&m, &id, &expect[0])); + NUTS_TRUE(id == 10); + NUTS_PASS(nni_id_alloc(&m, &id, &expect[1])); + NUTS_TRUE(id == 11); + NUTS_PASS(nni_id_alloc(&m, &id, &expect[2])); + NUTS_TRUE(id == 12); + NUTS_PASS(nni_id_alloc(&m, &id, &expect[3])); + NUTS_TRUE(id == 13); // Adding another fails. - TEST_NNG_FAIL(nni_id_alloc(&m, &id, &expect[4]), NNG_ENOMEM); + NUTS_FAIL(nni_id_alloc(&m, &id, &expect[4]), NNG_ENOMEM); // Delete one. - TEST_NNG_PASS(nni_id_remove(&m, 11)); + NUTS_PASS(nni_id_remove(&m, 11)); // And now we can allocate one. - TEST_NNG_PASS(nni_id_alloc(&m, &id, &expect[4])); - TEST_CHECK(id == 11); + NUTS_PASS(nni_id_alloc(&m, &id, &expect[4])); + NUTS_TRUE(id == 11); nni_id_map_fini(&m); } @@ -174,10 +173,10 @@ test_set_out_of_range(void) nni_id_map_init(&m, 10, 13, false); // We can insert outside the range forcibly. - TEST_NNG_PASS(nni_id_set(&m, 1, &x)); - TEST_NNG_PASS(nni_id_set(&m, 100, &x)); - TEST_NNG_PASS(nni_id_alloc(&m, &id, &x)); - TEST_CHECK(id == 10); + NUTS_PASS(nni_id_set(&m, 1, &x)); + NUTS_PASS(nni_id_set(&m, 100, &x)); + NUTS_PASS(nni_id_alloc(&m, &id, &x)); + NUTS_TRUE(id == 10); nni_id_map_fini(&m); } @@ -207,7 +206,7 @@ test_stress(void) x = &values[rand() % NUM_VALUES]; values[v] = x; if ((rv = nni_id_set(&m, v, x)) != 0) { - TEST_NNG_PASS(rv); + NUTS_PASS(rv); goto out; } break; @@ -216,13 +215,13 @@ test_stress(void) rv = nni_id_remove(&m, v); if (values[v] == NULL) { if (rv != NNG_ENOENT) { - TEST_NNG_FAIL(rv, NNG_ENOENT); + NUTS_FAIL(rv, NNG_ENOENT); goto out; } } else { values[v] = NULL; if (rv != 0) { - TEST_NNG_PASS(rv); + NUTS_PASS(rv); goto out; } } @@ -230,20 +229,20 @@ test_stress(void) case 2: x = nni_id_get(&m, v); if (x != values[v]) { - TEST_CHECK(x == values[v]); + NUTS_TRUE(x == values[v]); goto out; } break; } } out: - TEST_CHECK(i == STRESS_LOAD); + NUTS_TRUE(i == STRESS_LOAD); // Post stress check. for (i = 0; i < NUM_VALUES; i++) { x = nni_id_get(&m, i); if (x != values[i]) { - TEST_CHECK(x == values[i]); + NUTS_TRUE(x == values[i]); break; } @@ -251,17 +250,17 @@ out: // to fail. Otherwise there will be too many errors reported. rv = nni_id_remove(&m, i); if ((x == NULL) && (rv != NNG_ENOENT)) { - TEST_NNG_FAIL(rv, NNG_ENOENT); + NUTS_FAIL(rv, NNG_ENOENT); } else if ((x != NULL) && (rv != 0)) { - TEST_NNG_PASS(rv); + NUTS_PASS(rv); } } - TEST_CHECK(i == NUM_VALUES); + NUTS_TRUE(i == NUM_VALUES); nni_id_map_fini(&m); } -TEST_LIST = { +NUTS_TESTS = { { "basic", test_basic }, { "random", test_random }, { "collision", test_collision }, |
