Main: include/kernaux/memmap.h: Test assertions

This commit is contained in:
Alex Kotov 2022-06-16 14:15:19 +03:00
parent 82dcb74824
commit 9cab961d46
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
2 changed files with 31 additions and 2 deletions

View File

@ -29,7 +29,7 @@ typedef struct KernAux_MemMap {
struct KernAux_MemMap KernAux_MemMap_create(size_t memory_size);
void KernAux_MemMap_init(KernAux_MemMap memmap, size_t memory_size);
/// @warning Must only be called with unfinished memmap, otherwise panics.
/// @warning Must only be called with NOT finished memmap, otherwise panics.
bool KernAux_MemMap_add_entry(
KernAux_MemMap memmap,
bool is_available,
@ -38,7 +38,7 @@ bool KernAux_MemMap_add_entry(
size_t size
);
/// @warning Must only be called with unfinished memmap, otherwise panics.
/// @warning Must only be called with NOT finished memmap, otherwise panics.
bool KernAux_MemMap_finish(KernAux_MemMap memmap);
/// @warning Must only be called with finished memmap, otherwise panics.

View File

@ -2,6 +2,7 @@
#include "config.h"
#endif
#include <kernaux/assert.h>
#include <kernaux/memmap.h>
#include <assert.h>
@ -11,11 +12,27 @@
static KernAux_MemMap memmap;
static unsigned int assert_count_exp = 0;
static unsigned int assert_count_ctr = 0;
static const char *assert_last_file = NULL;
static void assert_cb(
const char *const file,
__attribute__((unused)) const int line,
__attribute__((unused)) const char *const msg
) {
++assert_count_ctr;
assert_last_file = file;
}
#define MEMSET memset(memmap, 0xff, sizeof(memmap))
#define MEMMAP (*memmap)
int main()
{
kernaux_assert_cb = assert_cb;
{
MEMSET;
KernAux_MemMap_init(memmap, 0);
@ -31,6 +48,12 @@ int main()
assert(MEMMAP.entries_count == 0);
assert(KernAux_MemMap_entry_by_index(memmap, 0) == NULL);
assert(assert_count_ctr == assert_count_exp);
assert(!KernAux_MemMap_finish(memmap));
assert(assert_count_ctr == ++assert_count_exp);
assert(strstr(assert_last_file, "src/memmap.c") != NULL);
assert_last_file = NULL;
}
{
@ -140,7 +163,11 @@ int main()
assert(MEMMAP.entries[0].end == 1);
assert(MEMMAP.entries[0].limit == 2);
assert(assert_count_ctr == assert_count_exp);
assert(KernAux_MemMap_entry_by_index(memmap, 0) == NULL);
assert(assert_count_ctr == ++assert_count_exp);
assert(strstr(assert_last_file, "src/memmap.c") != NULL);
assert_last_file = NULL;
}
{
@ -185,5 +212,7 @@ int main()
assert(KernAux_MemMap_entry_by_index(memmap, 1) == &MEMMAP.entries[1]);
}
assert(assert_count_ctr == assert_count_exp);
return 0;
}