From ebcb4d64f9ed7854bc52729c5e40069560cef64f Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Thu, 24 Nov 2022 17:00:19 +0100 Subject: [PATCH] Improved test functions. --- README.md | 2 +- hashset/src/main.c | 86 +++++++++++++++++++++++++++++++++-- libhashset/include/hash_set.h | 2 +- libhashset/src/hash_set.c | 2 +- 4 files changed, 86 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 240393f..9a6fc00 100644 --- a/README.md +++ b/README.md @@ -229,7 +229,7 @@ On success, this function returns *zero*. On error, the appropriate error code i ### hash_set_contains() -Tests whether the hash set contains a specific value. The operation fails, if the set does *not* contain the given value. +Tests whether the hash set contains a value. The operation fails, if the set does *not* contain the given value. ```C errno_t hash_set_contains( diff --git a/hashset/src/main.c b/hashset/src/main.c index 57d6728..1bee90f 100644 --- a/hashset/src/main.c +++ b/hashset/src/main.c @@ -190,7 +190,82 @@ static int test_function_1(hash_set_t *const hash_set) /* TEST #2 */ /* ========================================================================= */ +#define ARRSIZE 14867U + static int test_function_2(hash_set_t *const hash_set) +{ + size_t capacity, valid, deleted, limit; + uint64_t value; + uint8_t test[ARRSIZE]; + + random_t random; + random_init(&random); + + for (size_t i = 0U, offset = 0U; i < 5U; ++i, offset = 0U) + { + memset(test, 0, sizeof(test)); + for (size_t j = 0U; j < ARRSIZE / 3U; ++j) + { + size_t rnd; + do + { + rnd = random_next(&random) % ARRSIZE; + } + while (test[rnd]); + test[rnd] = 1U; + } + for (size_t j = 0U; j < ARRSIZE; ++j) + { + if (test[j]) + { + const errno_t error = hash_set_insert(hash_set, j); + if (error) + { + printf("Insert operation has failed! (error: %d)\n", error); + return EXIT_FAILURE; + } + PRINT_SET_INFO(); + } + } + while (hash_set_iterate(hash_set, &offset, &value) == 0) + { + if (!test[value]) + { + puts("Error has been detected!"); + return EXIT_FAILURE; + } + } + for (size_t j = 0U; j < ARRSIZE; ++j) + { + if (test[j]) + { + const errno_t error = hash_set_remove(hash_set, j); + if (error) + { + printf("Remove operation has failed! (error: %d)\n", error); + return EXIT_FAILURE; + } + PRINT_SET_INFO(); + } + } + if (hash_set_size(hash_set) != 0U) + { + puts("Invalid size!"); + return EXIT_FAILURE; + } + } + + PRINT_SET_INFO(); + puts("--------"); + + return EXIT_SUCCESS; +} + +/* ========================================================================= */ +/* TEST #3 */ +/* ========================================================================= */ + +static int test_function_3(hash_set_t *const hash_set) { size_t capacity, valid, deleted, limit; uint8_t spinner = 0U; @@ -243,10 +318,10 @@ static int test_function_2(hash_set_t *const hash_set) } /* ========================================================================= */ -/* TEST #3 */ +/* TEST #4 */ /* ========================================================================= */ -static int test_function_3(hash_set_t *const hash_set) +static int test_function_4(hash_set_t *const hash_set) { size_t capacity, valid, deleted, limit; uint8_t spinner = 0U; @@ -327,12 +402,17 @@ int main(void) { goto failure; } - + if (test_function_3(hash_set) != EXIT_SUCCESS) { goto failure; } + if (test_function_4(hash_set) != EXIT_SUCCESS) + { + goto failure; + } + hash_set_destroy(hash_set); puts("Test completed successfully."); return EXIT_SUCCESS; diff --git a/libhashset/include/hash_set.h b/libhashset/include/hash_set.h index f66ad04..93e265a 100644 --- a/libhashset/include/hash_set.h +++ b/libhashset/include/hash_set.h @@ -22,7 +22,7 @@ struct _hash_set; typedef struct _hash_set hash_set_t; hash_set_t *hash_set_create(const size_t initial_capacity, const double load_factor); -void hash_set_destroy(hash_set_t *instance); +void hash_set_destroy(hash_set_t *const instance); errno_t hash_set_insert(hash_set_t *const instance, const uint64_t value); errno_t hash_set_remove(hash_set_t *const instance, const uint64_t value); diff --git a/libhashset/src/hash_set.c b/libhashset/src/hash_set.c index dc824d2..a8ccc43 100644 --- a/libhashset/src/hash_set.c +++ b/libhashset/src/hash_set.c @@ -43,7 +43,7 @@ struct _hash_set static const size_t MINIMUM_CAPACITY = 128U; static const size_t DEFAULT_CAPACITY = 16384U; -static const double DEFAULT_LOADFCTR = 0.8125; +static const double DEFAULT_LOADFCTR = 0.8; /* ========================================================================= */ /* PRIVATE FUNCTIONS */