Small code clean-up.

This commit is contained in:
LoRd_MuldeR 2022-11-25 13:06:54 +01:00
parent e550528fe8
commit 4dfc5f5279
2 changed files with 39 additions and 32 deletions

View File

@ -276,43 +276,46 @@ static int test_function_3(hash_set_t *const hash_set)
random_t random; random_t random;
random_init(&random); random_init(&random);
for (;;) for (size_t r = 0U; r < 3U; ++r)
{ {
const uint64_t rnd = random_next(&random) & UINT64_C(0x3FFFFFFFFFFFFFF); for (;;)
const errno_t error = hash_set_insert(hash_set, rnd);
if (error)
{ {
if (error != EEXIST) const uint64_t rnd = random_next(&random) & UINT64_C(0x3FFFFFFFFFFFFFF);
const errno_t error = hash_set_insert(hash_set, rnd);
if (error)
{ {
printf("Insert operation has failed! (error: %d)\n", error); if (error != EEXIST)
return EXIT_FAILURE; {
printf("Insert operation has failed! (error: %d)\n", error);
return EXIT_FAILURE;
}
else
{
PRINT_SET_INFO(3);
printf("Collision detected! [%016llX]\n", rnd);
break;
}
} }
else if (!(++spinner & 0x7F))
{ {
PRINT_SET_INFO(3); const clock_t clock_now = clock();
printf("Collision detected! [%016llX]\n", rnd); if ((clock_now < last_update) || (clock_now >= last_update + CLOCKS_PER_SEC))
break; {
PRINT_SET_INFO(3);
last_update = clock_now;
}
} }
} }
if (!(++spinner & 0x7F))
PRINT_SET_INFO(3);
if (hash_set_clear(hash_set))
{ {
const clock_t clock_now = clock(); puts("Clear operation has failed!");
if ((clock_now < last_update) || (clock_now >= last_update + CLOCKS_PER_SEC)) return EXIT_FAILURE;
{
PRINT_SET_INFO(3);
last_update = clock_now;
}
} }
} }
PRINT_SET_INFO(3);
if (hash_set_clear(hash_set))
{
puts("Clear operation has failed!");
return EXIT_FAILURE;
}
PRINT_SET_INFO(3); PRINT_SET_INFO(3);
puts("---------"); puts("---------");
@ -323,13 +326,15 @@ static int test_function_3(hash_set_t *const hash_set)
/* TEST #4 */ /* TEST #4 */
/* ========================================================================= */ /* ========================================================================= */
#define LIMIT (UINT64_MAX >> 2)
static int test_function_4(hash_set_t *const hash_set) static int test_function_4(hash_set_t *const hash_set)
{ {
size_t capacity, valid, deleted, limit; size_t capacity, valid, deleted, limit;
uint8_t spinner = 0U; uint8_t spinner = 0U;
clock_t last_update = clock(); clock_t last_update = clock();
for (uint64_t value = 0U; value < ((uint64_t)INT32_MAX); ++value) for (uint64_t value = 0U; value < LIMIT; ++value)
{ {
const errno_t error = hash_set_insert(hash_set, value); const errno_t error = hash_set_insert(hash_set, value);
if (error) if (error)
@ -349,7 +354,7 @@ static int test_function_4(hash_set_t *const hash_set)
} }
} }
for (uint64_t value = 0U; value < ((uint64_t)INT32_MAX); ++value) for (uint64_t value = 0U; value < LIMIT; ++value)
{ {
const errno_t error = hash_set_remove(hash_set, value); const errno_t error = hash_set_remove(hash_set, value);
if (error) if (error)

View File

@ -68,7 +68,7 @@ static FORCE_INLINE size_t div_ceil(const size_t value, const size_t divisor)
static FORCE_INLINE size_t round_sz(const double d) static FORCE_INLINE size_t round_sz(const double d)
{ {
return ((!isnan(d)) && (d >= 0.0)) ? ((d + 0.5 >= ((double)SIZE_MAX)) ? SIZE_MAX : ((size_t)(d + 0.5))) : 0U; return ((!isnan(d)) && (d >= 0.0)) ? ((d + 0.5 >= ((double)SIZE_MAX)) ? SIZE_MAX : ((size_t)(d + 0.5))) : 0U;
} }
@ -136,7 +136,7 @@ static INLINE uint64_t hash_compute(const uint64_t i, const uint64_t value)
/* Allocation */ /* Allocation */
/* ------------------------------------------------- */ /* ------------------------------------------------- */
static INLINE void zero_memory(void* const addr, const size_t count, const size_t size) static INLINE void zero_memory(void *const addr, const size_t count, const size_t size)
{ {
memset(addr, 0, safe_mult(count, size)); memset(addr, 0, safe_mult(count, size));
} }
@ -170,7 +170,7 @@ static INLINE bool_t alloc_data(struct _hash_set_data *const data, const size_t
return TRUE; return TRUE;
} }
static INLINE void free_data(struct _hash_set_data* const data) static INLINE void free_data(struct _hash_set_data *const data)
{ {
if (data) if (data)
{ {
@ -195,7 +195,7 @@ static INLINE void set_flag(uint8_t *const flags, const size_t index)
flags[index / 8U] |= UINT8_C(1) << (index % 8U); flags[index / 8U] |= UINT8_C(1) << (index % 8U);
} }
static INLINE void clear_flag(uint8_t* const flags, const size_t index) static INLINE void clear_flag(uint8_t *const flags, const size_t index)
{ {
flags[index / 8U] &= ~(UINT8_C(1) << (index % 8U)); flags[index / 8U] &= ~(UINT8_C(1) << (index % 8U));
} }
@ -261,10 +261,12 @@ static INLINE void put_value(struct _hash_set_data *const data, const size_t ind
static size_t INLINE compute_limit(const size_t capacity, const double load_factor) static size_t INLINE compute_limit(const size_t capacity, const double load_factor)
{ {
size_t limit = round_sz(capacity * load_factor); size_t limit = round_sz(capacity * load_factor);
while (capacity && (limit >= capacity)) while (capacity && (limit >= capacity))
{ {
limit = safe_decr(limit); limit = safe_decr(limit);
} }
return limit; return limit;
} }