From cc1ed4e33e21db14239ec7a85223aff91b176c63 Mon Sep 17 00:00:00 2001 From: John Kerl Date: Fri, 11 Sep 2015 17:07:30 -0400 Subject: [PATCH] move manual tests to unit tests: checkpoint --- c/containers/lhmslv.c | 8 ++++--- c/containers/lhmslv.h | 3 +++ c/containers/test_maps_and_sets.c | 37 +++++++++++++++++++++---------- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/c/containers/lhmslv.c b/c/containers/lhmslv.c index 77d43c3aa..301b6b6d2 100644 --- a/c/containers/lhmslv.c +++ b/c/containers/lhmslv.c @@ -246,6 +246,7 @@ void* lhmslv_remove(lhmslv_t* pmap, slls_t* key) { void lhmslv_clear(lhmslv_t* pmap) { for (int i = 0; i < pmap->array_length; i++) { lhmslve_clear(&pmap->entries[i]); + pmap->states[i] = EMPTY; } pmap->num_occupied = 0; pmap->num_freed = 0; @@ -271,7 +272,7 @@ static void lhmslv_enlarge(lhmslv_t* pmap) { } // ---------------------------------------------------------------- -void lhmslv_check_counts(lhmslv_t* pmap) { +int lhmslv_check_counts(lhmslv_t* pmap) { int nocc = 0; int ndel = 0; for (int index = 0; index < pmap->array_length; index++) { @@ -284,14 +285,15 @@ void lhmslv_check_counts(lhmslv_t* pmap) { fprintf(stderr, "occupancy-count mismatch: actual %d != cached %d\n", nocc, pmap->num_occupied); - exit(1); + return FALSE; } if (ndel != pmap->num_freed) { fprintf(stderr, "freed-count mismatch: actual %d != cached %d\n", ndel, pmap->num_freed); - exit(1); + return FALSE; } + return TRUE; } // ---------------------------------------------------------------- diff --git a/c/containers/lhmslv.h b/c/containers/lhmslv.h index 91a3a6a09..eb7d43c26 100644 --- a/c/containers/lhmslv.h +++ b/c/containers/lhmslv.h @@ -49,4 +49,7 @@ void* lhmslv_remove(lhmslv_t* pmap, slls_t* key); void lhmslv_clear(lhmslv_t* pmap); int lhmslv_size(lhmslv_t* pmap); +// Unit-test hook +int lhmslv_check_counts(lhmslv_t* pmap); + #endif // LHMSLV_H diff --git a/c/containers/test_maps_and_sets.c b/c/containers/test_maps_and_sets.c index 6fb357d1f..ca656c794 100644 --- a/c/containers/test_maps_and_sets.c +++ b/c/containers/test_maps_and_sets.c @@ -263,25 +263,38 @@ static char* test_lhms2v() { static char* test_lhmslv() { mu_assert_lf(0 == 0); - slls_t* ax = slls_alloc(); - slls_add_no_free(ax, "a"); - slls_add_no_free(ax, "x"); - // xxx assertions here - - slls_t* ay = slls_alloc(); - slls_add_no_free(ay, "a"); - slls_add_no_free(ay, "y"); - - slls_t* bz = slls_alloc(); - slls_add_no_free(bz, "b"); - slls_add_no_free(bz, "z"); + slls_t* ax = slls_alloc(); slls_add_no_free(ax, "a"); slls_add_no_free(ax, "x"); + slls_t* ay = slls_alloc(); slls_add_no_free(ay, "a"); slls_add_no_free(ay, "y"); + slls_t* bz = slls_alloc(); slls_add_no_free(bz, "b"); slls_add_no_free(bz, "z"); + // xxx more assertions here lhmslv_t *pmap = lhmslv_alloc(); + mu_assert_lf(pmap->num_occupied == 0); + mu_assert_lf(lhmslv_check_counts(pmap)); + lhmslv_put(pmap, ax, "3"); + mu_assert_lf(pmap->num_occupied == 1); + mu_assert_lf(lhmslv_check_counts(pmap)); + lhmslv_put(pmap, ay, "5"); + mu_assert_lf(pmap->num_occupied == 2); + mu_assert_lf(lhmslv_check_counts(pmap)); + lhmslv_put(pmap, ax, "4"); + mu_assert_lf(pmap->num_occupied == 2); + mu_assert_lf(lhmslv_check_counts(pmap)); + lhmslv_put(pmap, bz, "7"); + mu_assert_lf(pmap->num_occupied == 3); + mu_assert_lf(lhmslv_check_counts(pmap)); + lhmslv_remove(pmap, ay); + mu_assert_lf(pmap->num_occupied == 2); + mu_assert_lf(lhmslv_check_counts(pmap)); + + lhmslv_clear(pmap); + mu_assert_lf(pmap->num_occupied == 0); + mu_assert_lf(lhmslv_check_counts(pmap)); lhmslv_free(pmap);