88 lines
1.9 KiB
C
88 lines
1.9 KiB
C
/* test/storage/test_page.c
|
|
*
|
|
* TDD-Zyklus: test_page_create
|
|
* RED -> diese Datei schreiben, noch kein page.c -> Linker-Fehler
|
|
* GREEN -> page_create() + page_free() in page.c implementieren
|
|
*/
|
|
#include "pauldb/storage.h"
|
|
|
|
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
static void
|
|
test_create_returns_non_null(void)
|
|
{
|
|
Page* p = page_create(42);
|
|
assert(p != NULL);
|
|
page_free(p);
|
|
printf(" OK: page_create(42) returns non-NULL\n");
|
|
}
|
|
|
|
static void
|
|
test_insert_returns_slot_id(void)
|
|
{
|
|
Page* p = page_create(4);
|
|
int slot_id = page_insert(p, "Hallo", 5);
|
|
assert(slot_id == 0);
|
|
page_free(p);
|
|
printf(" OK: Insert\n");
|
|
}
|
|
|
|
static void
|
|
test_insert_and_get(void)
|
|
{
|
|
Page* p = page_create(1);
|
|
|
|
int slot = page_insert(p, "ABAP", 4);
|
|
assert(slot == 0);
|
|
|
|
size_t len = 0;
|
|
void* result = page_get(p, 0, &len);
|
|
assert(result != NULL);
|
|
assert(len == 4);
|
|
assert(memcmp(result, "ABAP", 4) == 0);
|
|
|
|
page_free(p);
|
|
printf(" OK: insert + get roundtrip\n");
|
|
}
|
|
|
|
static void
|
|
test_multiple_inserts(void)
|
|
{
|
|
Page* p = page_create(2);
|
|
|
|
assert(page_insert(p, "ABAP", 4) == 0);
|
|
assert(page_insert(p, "HANA", 4) == 1);
|
|
assert(page_insert(p, "CDS", 3) == 2);
|
|
|
|
size_t len = 0;
|
|
assert(memcmp(page_get(p, 0, &len), "ABAP", 4) == 0);
|
|
assert(memcmp(page_get(p, 1, &len), "HANA", 4) == 0);
|
|
assert(memcmp(page_get(p, 2, &len), "CDS", 3) == 0);
|
|
|
|
page_free(p);
|
|
printf(" OK: multiple inserts\n");
|
|
}
|
|
|
|
static void
|
|
test_invalid_slot_returns_null(void)
|
|
{
|
|
Page* p = page_create(3);
|
|
assert(page_get(p, 99, NULL) == NULL);
|
|
page_free(p);
|
|
printf(" OK: invalid slot returns NULL\n");
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
printf("=== test_page ===\n");
|
|
test_create_returns_non_null();
|
|
test_insert_returns_slot_id();
|
|
test_insert_and_get();
|
|
test_multiple_inserts();
|
|
test_invalid_slot_returns_null();
|
|
printf("All tests passed.\n");
|
|
return 0;
|
|
} |