implement unittests for array util

This commit is contained in:
falsycat 2023-11-27 08:35:44 +09:00
parent cdc22d158f
commit 8369e96a37
6 changed files with 262 additions and 22 deletions

View File

@ -51,8 +51,9 @@ static bool run_trigger_setup_(struct nf7_core_test* mod) {
.malloc = mod->malloc,
.uv = mod->uv,
.test = {
.nf7 = mod->nf7,
.data = this,
.nf7 = mod->nf7,
.malloc = mod->malloc,
.data = this,
.run = run_single_test_,
.expect = run_expect_,
.finalize = run_finalize_,
@ -101,6 +102,8 @@ static void run_single_test_(
struct nf7_core_test_run* this = test->data;
nf7_util_log_info("running test: %s", name);
this->running_test_name = name;
const bool result = func(&this->test);
this->running_test_name = nullptr;

View File

@ -4,16 +4,18 @@
#include <stdint.h>
#define NF7_TEST(name) bool name([[maybe_unused]] struct nf7_test*)
#define NF7_TEST(name) bool name([[maybe_unused]] struct nf7_test* test_)
struct nf7;
struct nf7_test;
struct nf7_util_malloc;
typedef bool (*nf7_test_func)(struct nf7_test*);
struct nf7_test {
struct nf7* nf7;
void* data;
struct nf7* nf7;
struct nf7_util_malloc* malloc;
void* data;
uint64_t refcnt;
@ -32,7 +34,7 @@ static inline void nf7_test_unref(struct nf7_test* test) {
}
}
#define nf7_test_expect(test, expr) nf7_test_expect_(test, (expr), #expr)
#define nf7_test_expect(expr) nf7_test_expect_(test_, (expr), #expr)
static inline bool nf7_test_expect_(
struct nf7_test* test, bool val, const char* expr) {
test->expect(test, val, expr);

View File

@ -26,7 +26,7 @@
static inline void PREFIX##_deinit(struct PREFIX* this) { \
assert(nullptr != this); \
nf7_util_malloc_del(this->malloc, this->ptr); \
*this = (struct PREFIX) {}; \
*this = (struct PREFIX) {0}; \
} \
\
static inline bool PREFIX##_resize(struct PREFIX* this, uint64_t n) { \
@ -46,7 +46,7 @@
} \
\
if (extend) { \
memset(this->ptr[this->n], 0, (n - this->n) * sizeof(T)); \
memset(&this->ptr[this->n], 0, (n - this->n) * sizeof(T)); \
} \
this->n = n; \
return true; \
@ -63,7 +63,7 @@
} \
const uint64_t tails = this->n - idx - 1; \
memmove(&this->ptr[idx+1], &this->ptr[idx], tails*sizeof(T)); \
this->ptr[this->n] = item; \
this->ptr[idx] = item; \
return true; \
} \
\
@ -78,6 +78,7 @@
} \
const uint64_t tails = this->n - idx - 1; \
memmove(&this->ptr[idx], &this->ptr[idx+1], tails*sizeof(T)); \
PREFIX##_resize(this, this->n - 1); \
} \
\
static inline bool PREFIX##_find(struct PREFIX* this, uint64_t* idx, const T needle) { \
@ -103,3 +104,12 @@
return true; \
} \
static_assert(true)
NF7_UTIL_ARRAY(nf7_util_array_u8 , uint8_t);
NF7_UTIL_ARRAY(nf7_util_array_u16, uint16_t);
NF7_UTIL_ARRAY(nf7_util_array_u32, uint32_t);
NF7_UTIL_ARRAY(nf7_util_array_u64, uint64_t);
NF7_UTIL_ARRAY(nf7_util_array_s8 , int8_t);
NF7_UTIL_ARRAY(nf7_util_array_s16, int16_t);
NF7_UTIL_ARRAY(nf7_util_array_s32, int32_t);
NF7_UTIL_ARRAY(nf7_util_array_s64, int64_t);

View File

@ -3,10 +3,229 @@
#include "test/common.h"
#include "util/log.h"
NF7_TEST(nf7_util_array_test) {
return true;
}
NF7_TEST(nf7_util_array_test2) {
return true;
}
#define TEST_(T) do { \
struct nf7_util_array_##T sut; \
nf7_util_array_##T##_init(&sut, test_->malloc); \
const bool ret = \
nf7_test_expect(nf7_util_array_##T##_resize(&sut, 32)) && \
nf7_test_expect(32 == sut.n); \
nf7_util_array_##T##_deinit(&sut); \
return ret; \
} while (0)
NF7_TEST(nf7_util_array_u8_test_resize) { TEST_(u8); }
NF7_TEST(nf7_util_array_u16_test_resize) { TEST_(u16); }
NF7_TEST(nf7_util_array_u32_test_resize) { TEST_(u32); }
NF7_TEST(nf7_util_array_u64_test_resize) { TEST_(u64); }
NF7_TEST(nf7_util_array_s8_test_resize) { TEST_(s8); }
NF7_TEST(nf7_util_array_s16_test_resize) { TEST_(s16); }
NF7_TEST(nf7_util_array_s32_test_resize) { TEST_(s32); }
NF7_TEST(nf7_util_array_s64_test_resize) { TEST_(s64); }
#undef TEST_
#define TEST_(T) do { \
struct nf7_util_array_##T sut; \
nf7_util_array_##T##_init(&sut, test_->malloc); \
const bool ret = \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, 0, 66)) && \
nf7_test_expect(1 == sut.n); \
nf7_test_expect(66 == sut.ptr[0]); \
nf7_util_array_##T##_deinit(&sut); \
return ret; \
} while (0)
NF7_TEST(nf7_util_array_u8_test_insert_first) { TEST_(u8); }
NF7_TEST(nf7_util_array_u16_test_insert_first) { TEST_(u16); }
NF7_TEST(nf7_util_array_u32_test_insert_first) { TEST_(u32); }
NF7_TEST(nf7_util_array_u64_test_insert_first) { TEST_(u64); }
NF7_TEST(nf7_util_array_s8_test_insert_first) { TEST_(s8); }
NF7_TEST(nf7_util_array_s16_test_insert_first) { TEST_(s16); }
NF7_TEST(nf7_util_array_s32_test_insert_first) { TEST_(s32); }
NF7_TEST(nf7_util_array_s64_test_insert_first) { TEST_(s64); }
#undef TEST_
#define TEST_(T) do { \
struct nf7_util_array_##T sut; \
nf7_util_array_##T##_init(&sut, test_->malloc); \
const bool ret = \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, 0, 66)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, 0, 77)) && \
nf7_test_expect(2 == sut.n) && \
nf7_test_expect(77 == sut.ptr[0]) && \
nf7_test_expect(66 == sut.ptr[1]); \
nf7_util_array_##T##_deinit(&sut); \
return ret; \
} while (0)
NF7_TEST(nf7_util_array_u8_test_insert_head) { TEST_(u8); }
NF7_TEST(nf7_util_array_u16_test_insert_head) { TEST_(u16); }
NF7_TEST(nf7_util_array_u32_test_insert_head) { TEST_(u32); }
NF7_TEST(nf7_util_array_u64_test_insert_head) { TEST_(u64); }
NF7_TEST(nf7_util_array_s8_test_insert_head) { TEST_(s8); }
NF7_TEST(nf7_util_array_s16_test_insert_head) { TEST_(s16); }
NF7_TEST(nf7_util_array_s32_test_insert_head) { TEST_(s32); }
NF7_TEST(nf7_util_array_s64_test_insert_head) { TEST_(s64); }
#undef TEST_
#define TEST_(T) do { \
struct nf7_util_array_##T sut; \
nf7_util_array_##T##_init(&sut, test_->malloc); \
const bool ret = \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 66)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 77)) && \
nf7_test_expect(2 == sut.n) && \
nf7_test_expect(66 == sut.ptr[0]) && \
nf7_test_expect(77 == sut.ptr[1]); \
nf7_util_array_##T##_deinit(&sut); \
return ret; \
} while (0)
NF7_TEST(nf7_util_array_u8_test_insert_tail) { TEST_(u8); }
NF7_TEST(nf7_util_array_u16_test_insert_tail) { TEST_(u16); }
NF7_TEST(nf7_util_array_u32_test_insert_tail) { TEST_(u32); }
NF7_TEST(nf7_util_array_u64_test_insert_tail) { TEST_(u64); }
NF7_TEST(nf7_util_array_s8_test_insert_tail) { TEST_(s8); }
NF7_TEST(nf7_util_array_s16_test_insert_tail) { TEST_(s16); }
NF7_TEST(nf7_util_array_s32_test_insert_tail) { TEST_(s32); }
NF7_TEST(nf7_util_array_s64_test_insert_tail) { TEST_(s64); }
#undef TEST_
#define TEST_(T) do { \
struct nf7_util_array_##T sut; \
nf7_util_array_##T##_init(&sut, test_->malloc); \
const bool ret = \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 66)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 88)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, 1, 77)) && \
nf7_test_expect(3 == sut.n) && \
nf7_test_expect(66 == sut.ptr[0]) && \
nf7_test_expect(77 == sut.ptr[1]) && \
nf7_test_expect(88 == sut.ptr[2]); \
nf7_util_array_##T##_deinit(&sut); \
return ret; \
} while (0)
NF7_TEST(nf7_util_array_u8_test_insert_mid) { TEST_(u8); }
NF7_TEST(nf7_util_array_u16_test_insert_mid) { TEST_(u16); }
NF7_TEST(nf7_util_array_u32_test_insert_mid) { TEST_(u32); }
NF7_TEST(nf7_util_array_u64_test_insert_mid) { TEST_(u64); }
NF7_TEST(nf7_util_array_s8_test_insert_mid) { TEST_(s8); }
NF7_TEST(nf7_util_array_s16_test_insert_mid) { TEST_(s16); }
NF7_TEST(nf7_util_array_s32_test_insert_mid) { TEST_(s32); }
NF7_TEST(nf7_util_array_s64_test_insert_mid) { TEST_(s64); }
#undef TEST_
#define TEST_(T) do { \
struct nf7_util_array_##T sut; \
nf7_util_array_##T##_init(&sut, test_->malloc); \
const bool ret = \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 66)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 77)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 88)) && \
(nf7_util_array_##T##_remove(&sut, 0), true) && \
nf7_test_expect(2 == sut.n) && \
nf7_test_expect(77 == sut.ptr[0]) && \
nf7_test_expect(88 == sut.ptr[1]); \
nf7_util_array_##T##_deinit(&sut); \
return ret; \
} while (0)
NF7_TEST(nf7_util_array_u8_test_remove_head) { TEST_(u8); }
NF7_TEST(nf7_util_array_u16_test_remove_head) { TEST_(u16); }
NF7_TEST(nf7_util_array_u32_test_remove_head) { TEST_(u32); }
NF7_TEST(nf7_util_array_u64_test_remove_head) { TEST_(u64); }
NF7_TEST(nf7_util_array_s8_test_remove_head) { TEST_(s8); }
NF7_TEST(nf7_util_array_s16_test_remove_head) { TEST_(s16); }
NF7_TEST(nf7_util_array_s32_test_remove_head) { TEST_(s32); }
NF7_TEST(nf7_util_array_s64_test_remove_head) { TEST_(s64); }
#undef TEST_
#define TEST_(T) do { \
struct nf7_util_array_##T sut; \
nf7_util_array_##T##_init(&sut, test_->malloc); \
const bool ret = \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 66)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 77)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 88)) && \
(nf7_util_array_##T##_remove(&sut, UINT64_MAX), true) && \
nf7_test_expect(2 == sut.n) && \
nf7_test_expect(66 == sut.ptr[0]) && \
nf7_test_expect(77 == sut.ptr[1]); \
nf7_util_array_##T##_deinit(&sut); \
return ret; \
} while (0)
NF7_TEST(nf7_util_array_u8_test_remove_tail) { TEST_(u8); }
NF7_TEST(nf7_util_array_u16_test_remove_tail) { TEST_(u16); }
NF7_TEST(nf7_util_array_u32_test_remove_tail) { TEST_(u32); }
NF7_TEST(nf7_util_array_u64_test_remove_tail) { TEST_(u64); }
NF7_TEST(nf7_util_array_s8_test_remove_tail) { TEST_(s8); }
NF7_TEST(nf7_util_array_s16_test_remove_tail) { TEST_(s16); }
NF7_TEST(nf7_util_array_s32_test_remove_tail) { TEST_(s32); }
NF7_TEST(nf7_util_array_s64_test_remove_tail) { TEST_(s64); }
#undef TEST_
#define TEST_(T) do { \
struct nf7_util_array_##T sut; \
nf7_util_array_##T##_init(&sut, test_->malloc); \
const bool ret = \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 66)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 77)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 88)) && \
(nf7_util_array_##T##_remove(&sut, 1), true) && \
nf7_test_expect(2 == sut.n) && \
nf7_test_expect(66 == sut.ptr[0]) && \
nf7_test_expect(88 == sut.ptr[1]); \
nf7_util_array_##T##_deinit(&sut); \
return ret; \
} while (0)
NF7_TEST(nf7_util_array_u8_test_remove_mid) { TEST_(u8); }
NF7_TEST(nf7_util_array_u16_test_remove_mid) { TEST_(u16); }
NF7_TEST(nf7_util_array_u32_test_remove_mid) { TEST_(u32); }
NF7_TEST(nf7_util_array_u64_test_remove_mid) { TEST_(u64); }
NF7_TEST(nf7_util_array_s8_test_remove_mid) { TEST_(s8); }
NF7_TEST(nf7_util_array_s16_test_remove_mid) { TEST_(s16); }
NF7_TEST(nf7_util_array_s32_test_remove_mid) { TEST_(s32); }
NF7_TEST(nf7_util_array_s64_test_remove_mid) { TEST_(s64); }
#undef TEST_
#define TEST_(T) do { \
struct nf7_util_array_##T sut; \
nf7_util_array_##T##_init(&sut, test_->malloc); \
uint64_t idx; \
const bool ret = \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 66)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 77)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 88)) && \
nf7_test_expect(nf7_util_array_##T##_find(&sut, &idx, 77)) && \
nf7_test_expect(1 == idx); \
nf7_util_array_##T##_deinit(&sut); \
return ret; \
} while (0)
NF7_TEST(nf7_util_array_u8_test_find_found) { TEST_(u8); }
NF7_TEST(nf7_util_array_u16_test_find_found) { TEST_(u16); }
NF7_TEST(nf7_util_array_u32_test_find_found) { TEST_(u32); }
NF7_TEST(nf7_util_array_u64_test_find_found) { TEST_(u64); }
NF7_TEST(nf7_util_array_s8_test_find_found) { TEST_(s8); }
NF7_TEST(nf7_util_array_s16_test_find_found) { TEST_(s16); }
NF7_TEST(nf7_util_array_s32_test_find_found) { TEST_(s32); }
NF7_TEST(nf7_util_array_s64_test_find_found) { TEST_(s64); }
#undef TEST_
#define TEST_(T) do { \
struct nf7_util_array_##T sut; \
nf7_util_array_##T##_init(&sut, test_->malloc); \
uint64_t idx; \
const bool ret = \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 66)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 77)) && \
nf7_test_expect(nf7_util_array_##T##_insert(&sut, UINT64_MAX, 88)) && \
nf7_test_expect(!nf7_util_array_##T##_find(&sut, &idx, 99)); \
nf7_util_array_##T##_deinit(&sut); \
return ret; \
} while (0)
NF7_TEST(nf7_util_array_u8_test_find_notfound) { TEST_(u8); }
NF7_TEST(nf7_util_array_u16_test_find_notfound) { TEST_(u16); }
NF7_TEST(nf7_util_array_u32_test_find_notfound) { TEST_(u32); }
NF7_TEST(nf7_util_array_u64_test_find_notfound) { TEST_(u64); }
NF7_TEST(nf7_util_array_s8_test_find_notfound) { TEST_(s8); }
NF7_TEST(nf7_util_array_s16_test_find_notfound) { TEST_(s16); }
NF7_TEST(nf7_util_array_s32_test_find_notfound) { TEST_(s32); }
NF7_TEST(nf7_util_array_s64_test_find_notfound) { TEST_(s64); }
#undef TEST_

View File

@ -6,6 +6,8 @@
#include <stdint.h>
#include <stdio.h>
#include <uv.h>
#define NF7_UTIL_LOG_CURRENT_FILE \
((const char*) __FILE__ + NF7_PROJECT_DIR_LEN)

View File

@ -14,14 +14,18 @@ struct nf7_util_malloc { atomic_uint_least64_t count; };
static inline void* nf7_util_malloc_new(struct nf7_util_malloc* this, uint64_t n) {
assert(nullptr != this);
if (0 < n) {
const uint64_t prev_count = atomic_fetch_add(&this->count, 1);
assert(UINT64_MAX > prev_count);
return calloc(n, 1);
} else {
if (0 == n) {
return nullptr;
}
void* ret = calloc(n, 1);
if (nullptr == ret) {
return nullptr;
}
const uint64_t prev_count = atomic_fetch_add(&this->count, 1);
assert(UINT64_MAX > prev_count);
return ret;
}
static inline void nf7_util_malloc_del(struct nf7_util_malloc* this, void* ptr) {
assert(nullptr != this);
@ -37,7 +41,7 @@ static inline void* nf7_util_malloc_renew(struct nf7_util_malloc* this, void* pt
assert(nullptr != this);
if (n > 0) {
return nullptr != ptr? realloc(ptr, n): malloc(n);
return nullptr != ptr? realloc(ptr, n): nf7_util_malloc_new(this, n);
} else {
nf7_util_malloc_del(this, ptr);
return nullptr;