Improved test functions.

This commit is contained in:
LoRd_MuldeR 2022-11-24 17:00:19 +01:00
parent b3e1c4e3a9
commit ebcb4d64f9
4 changed files with 86 additions and 6 deletions

View File

@ -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(

View File

@ -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;

View File

@ -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);

View File

@ -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 */