test: move all module tests to the test/ directory

This commit is contained in:
2025-11-01 10:12:18 +00:00
parent a68b9f7ba7
commit bd2fe50ec9
32 changed files with 5 additions and 5 deletions

203
test/core/core-units.c Normal file
View File

@@ -0,0 +1,203 @@
#include "blue/core/misc.h"
#include <CuTest.h>
#include <blue/core/btree.h>
#include <blue/core/queue.h>
#include <blue/core/stringstream.h>
#include <stdlib.h>
#include <time.h>
struct test_tree_node {
int value;
b_btree_node node;
};
struct test_queue_entry {
int value;
b_queue_entry entry;
};
B_BTREE_DEFINE_SIMPLE_INSERT(struct test_tree_node, node, value, test_tree_insert);
void test_btree_insert(CuTest *tc)
{
b_btree tree = {0};
struct test_tree_node nodes[3] = {0};
for (int i = 0; i < sizeof nodes / sizeof *nodes; i++) {
nodes[i].value = i;
}
test_tree_insert(&tree, &nodes[0]);
CuAssertPtrEquals(tc, NULL, nodes[0].node.b_left);
CuAssertPtrEquals(tc, NULL, nodes[0].node.b_right);
CuAssertIntEquals(tc, 1, nodes[0].node.b_height);
test_tree_insert(&tree, &nodes[1]);
CuAssertPtrEquals(tc, NULL, nodes[0].node.b_left);
CuAssertPtrEquals(tc, &nodes[1].node, nodes[0].node.b_right);
CuAssertIntEquals(tc, 2, nodes[0].node.b_height);
CuAssertPtrEquals(tc, NULL, nodes[1].node.b_left);
CuAssertPtrEquals(tc, NULL, nodes[1].node.b_right);
CuAssertIntEquals(tc, 1, nodes[1].node.b_height);
test_tree_insert(&tree, &nodes[2]);
CuAssertPtrEquals(tc, &nodes[0].node, nodes[1].node.b_left);
CuAssertPtrEquals(tc, &nodes[2].node, nodes[1].node.b_right);
CuAssertIntEquals(tc, 2, nodes[1].node.b_height);
CuAssertPtrEquals(tc, NULL, nodes[0].node.b_left);
CuAssertPtrEquals(tc, NULL, nodes[0].node.b_right);
CuAssertIntEquals(tc, 1, nodes[0].node.b_height);
CuAssertPtrEquals(tc, NULL, nodes[2].node.b_left);
CuAssertPtrEquals(tc, NULL, nodes[2].node.b_right);
CuAssertIntEquals(tc, 1, nodes[2].node.b_height);
}
void test_btree_iterate(CuTest *tc)
{
static const size_t nr_nodes = 256;
srand(time(NULL));
b_btree tree = {0};
struct test_tree_node *nodes = calloc(nr_nodes, sizeof *nodes);
CuAssertPtrNotNull(tc, nodes);
for (int i = 0; i < nr_nodes; i++) {
nodes[i].value = rand();
test_tree_insert(&tree, &nodes[i]);
}
int prev = -1;
b_btree_node *bnode = b_btree_first(&tree);
while (bnode) {
struct test_tree_node *node
= b_unbox(struct test_tree_node, bnode, node);
CuAssertPtrNotNull(tc, node);
if (prev == -1) {
prev = node->value;
bnode = b_btree_next(bnode);
continue;
}
CuAssertTrue(tc, prev <= node->value);
prev = node->value;
bnode = b_btree_next(bnode);
}
free(nodes);
}
void test_queue_insert(CuTest *tc)
{
struct test_queue_entry entries[5] = {0};
for (int i = 0; i < sizeof entries / sizeof *entries; i++) {
entries[i].value = i;
}
b_queue q = B_QUEUE_INIT;
b_queue_push_back(&q, &entries[0].entry);
b_queue_push_back(&q, &entries[2].entry);
b_queue_push_back(&q, &entries[4].entry);
b_queue_insert_after(&q, &entries[3].entry, &entries[2].entry);
b_queue_insert_before(&q, &entries[1].entry, &entries[2].entry);
CuAssertPtrEquals(tc, NULL, entries[0].entry.qe_prev);
CuAssertPtrEquals(tc, &entries[1].entry, entries[0].entry.qe_next);
CuAssertPtrEquals(tc, &entries[0].entry, entries[1].entry.qe_prev);
CuAssertPtrEquals(tc, &entries[2].entry, entries[1].entry.qe_next);
CuAssertPtrEquals(tc, &entries[1].entry, entries[2].entry.qe_prev);
CuAssertPtrEquals(tc, &entries[3].entry, entries[2].entry.qe_next);
CuAssertPtrEquals(tc, &entries[2].entry, entries[3].entry.qe_prev);
CuAssertPtrEquals(tc, &entries[4].entry, entries[3].entry.qe_next);
CuAssertPtrEquals(tc, &entries[3].entry, entries[4].entry.qe_prev);
CuAssertPtrEquals(tc, NULL, entries[4].entry.qe_next);
}
void test_queue_iterate(CuTest *tc)
{
b_queue q = B_QUEUE_INIT;
struct test_queue_entry entries[32] = {0};
for (int i = 0; i < sizeof entries / sizeof *entries; i++) {
entries[i].value = i;
b_queue_push_back(&q, &entries[i].entry);
}
int prev = -1;
struct b_queue_entry *entry = b_queue_first(&q);
while (entry) {
struct test_queue_entry *e
= b_unbox(struct test_queue_entry, entry, entry);
CuAssertPtrNotNull(tc, e);
if (prev == -1) {
prev = e->value;
goto skip;
}
CuAssertTrue(tc, prev < e->value);
prev = e->value;
skip:
entry = b_queue_next(entry);
}
}
void test_stringstream_1(CuTest *tc)
{
char buf[1024];
b_stringstream *s = b_stringstream_create_with_buffer(buf, sizeof buf);
b_stream_write_string(s, "hello", NULL);
b_stream_write_fmt(s, NULL, "(%d + %.1f)", 32, 2.3);
char *end = b_stringstream_steal(s);
b_stringstream_unref(s);
CuAssertStrEquals(tc, "hello(32 + 2.3)", end);
}
void test_stringstream_2(CuTest *tc)
{
char buf[1024];
b_stringstream *s = b_stringstream_create_with_buffer(buf, sizeof buf);
b_stream_write_string(s, "{\n", NULL);
b_stream_push_indent(s, 1);
b_stream_write_string(s, "a = 32,\n", NULL);
b_stream_write_string(s, "b = 64\n", NULL);
b_stream_pop_indent(s);
b_stream_write_string(s, "}", NULL);
char *str = b_stringstream_steal(s);
b_stringstream_unref(s);
CuAssertStrEquals(tc, "{\n a = 32,\n b = 64\n}", str);
}
CuSuite *get_all_tests(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_btree_insert);
SUITE_ADD_TEST(suite, test_btree_iterate);
SUITE_ADD_TEST(suite, test_queue_insert);
SUITE_ADD_TEST(suite, test_queue_iterate);
SUITE_ADD_TEST(suite, test_stringstream_1);
SUITE_ADD_TEST(suite, test_stringstream_2);
return suite;
}

6
test/core/errors.c Normal file
View File

@@ -0,0 +1,6 @@
#include <blue/core/error.h>
int main(void)
{
return 0;
}

74
test/core/hash.c Normal file
View File

@@ -0,0 +1,74 @@
#include <blue/core/hash.h>
#include <stdio.h>
#include <string.h>
static void print_digest(
const char *func_name, b_hash_function func, const char *msg,
size_t msg_len, size_t digest_len)
{
unsigned char digest[128];
b_hash_ctx ctx;
if (!B_OK(b_hash_ctx_init(&ctx, func))) {
printf("b_hash_ctx_init failed\n");
return;
}
if (!B_OK(b_hash_ctx_update(&ctx, msg, msg_len))) {
printf("b_hash_ctx_update failed\n");
return;
}
if (!B_OK(b_hash_ctx_finish(&ctx, digest, sizeof digest))) {
printf("b_hash_ctx_finish failed\n");
return;
}
char digest_str[256];
for (size_t i = 0; i < digest_len; i++) {
snprintf(
digest_str + (i * 2), sizeof digest_str - (i * 2),
"%02x", digest[i]);
}
printf("%s(%s) = %s\n", func_name, msg, digest_str);
}
int main(void)
{
const char *msg = "Hello, world!";
size_t msg_len = strlen(msg);
print_digest("MD4", B_HASH_MD4, msg, msg_len, B_DIGEST_LENGTH_MD4);
print_digest("MD5", B_HASH_MD5, msg, msg_len, B_DIGEST_LENGTH_MD5);
print_digest("SHA1", B_HASH_SHA1, msg, msg_len, B_DIGEST_LENGTH_SHA1);
print_digest(
"SHA224", B_HASH_SHA2_224, msg, msg_len, B_DIGEST_LENGTH_SHA2_224);
print_digest(
"SHA256", B_HASH_SHA2_256, msg, msg_len, B_DIGEST_LENGTH_SHA2_256);
print_digest(
"SHA384", B_HASH_SHA2_384, msg, msg_len, B_DIGEST_LENGTH_SHA2_384);
print_digest(
"SHA512", B_HASH_SHA2_512, msg, msg_len, B_DIGEST_LENGTH_SHA2_512);
print_digest(
"SHA3-224", B_HASH_SHA3_224, msg, msg_len,
B_DIGEST_LENGTH_SHA3_224);
print_digest(
"SHA3-256", B_HASH_SHA3_256, msg, msg_len,
B_DIGEST_LENGTH_SHA3_256);
print_digest(
"SHA3-384", B_HASH_SHA3_384, msg, msg_len,
B_DIGEST_LENGTH_SHA3_384);
print_digest(
"SHA3-512", B_HASH_SHA3_512, msg, msg_len,
B_DIGEST_LENGTH_SHA3_512);
print_digest(
"SHAKE128", B_HASH_SHAKE128, msg, msg_len,
B_DIGEST_LENGTH_SHAKE128);
print_digest(
"SHAKE256", B_HASH_SHAKE256, msg, msg_len,
B_DIGEST_LENGTH_SHAKE256);
return 0;
}

39
test/core/randomise.c Normal file
View File

@@ -0,0 +1,39 @@
#include <blue/core/random.h>
#include <stdio.h>
#define NRAND_NUMBERS 12
#define NRAND_BYTES 128
#define NRAND_DOUBLES 8
int main(void)
{
b_random_ctx random;
b_random_init(&random, B_RANDOM_SECURE | B_RANDOM_MT19937);
printf("generating %d random numbers:\n", NRAND_NUMBERS);
for (int i = 0; i < NRAND_NUMBERS; i++) {
unsigned long long v = b_random_next_int64(&random);
printf(" %llu\n", v);
}
printf("\ngenerating %d random bytes:", NRAND_BYTES);
unsigned char bytes[16];
for (int i = 0; i < NRAND_BYTES; i++) {
if (i == 0 || (i % 16) == 0) {
printf("\n ");
b_random_next_bytes(&random, bytes, sizeof bytes);
} else if ((i % 4) == 0) {
printf(" ");
}
printf("%02x", bytes[i % 16]);
}
printf("\n\ngenerating %d random doubles:\n", NRAND_DOUBLES);
for (int i = 0; i < NRAND_DOUBLES; i++) {
double v = b_random_next_double(&random);
printf(" %lf\n", v);
}
return 0;
}

67
test/core/ringbuffers.c Normal file
View File

@@ -0,0 +1,67 @@
#include <assert.h>
#include <blue/core/ringbuffer.h>
#include <stdio.h>
#include <string.h>
#define BUF_SIZE 32
int main(void)
{
b_ringbuffer *buf = b_ringbuffer_create(BUF_SIZE);
size_t read_available = b_ringbuffer_available_data_remaining(buf);
size_t write_available = b_ringbuffer_write_capacity_remaining(buf);
printf("read available: %zu\n", read_available);
printf("write available: %zu\n", write_available);
assert(read_available == 0);
assert(write_available == BUF_SIZE - 1);
const char ch = 'X';
printf("putc(%c)\n", ch);
b_ringbuffer_putc(buf, ch);
read_available = b_ringbuffer_available_data_remaining(buf);
write_available = b_ringbuffer_write_capacity_remaining(buf);
printf("read available: %zu\n", read_available);
printf("write available: %zu\n", write_available);
assert(read_available == 1);
assert(write_available == BUF_SIZE - 2);
int c = b_ringbuffer_getc(buf);
printf("getc() = %c\n", c);
assert(c == ch);
const char s[]
= "A very long string that is designed to overflow the "
"ringbuffer";
size_t s_len = strlen(s);
size_t nr_written = 0;
printf("write(%s)\n", s);
b_ringbuffer_write(buf, s, s_len, &nr_written);
read_available = b_ringbuffer_available_data_remaining(buf);
write_available = b_ringbuffer_write_capacity_remaining(buf);
printf("nr written: %zu\n", nr_written);
printf("read available: %zu\n", read_available);
printf("write available: %zu\n", write_available);
assert(read_available == BUF_SIZE - 1);
assert(write_available == 0);
char data[BUF_SIZE + 32] = {0};
size_t nr_read = 0;
b_ringbuffer_read(buf, data, sizeof data, &nr_read);
printf("read(%u) = %zu bytes\n", BUF_SIZE + 32, nr_read);
printf(" = %s\n", data);
read_available = b_ringbuffer_available_data_remaining(buf);
write_available = b_ringbuffer_write_capacity_remaining(buf);
printf("read available: %zu\n", read_available);
printf("write available: %zu\n", write_available);
assert(read_available == 0);
assert(write_available == BUF_SIZE - 1);
b_ringbuffer_unref(buf);
return 0;
}

28
test/core/ropes.c Normal file
View File

@@ -0,0 +1,28 @@
#include <blue/core/rope.h>
#include <stdio.h>
int main(void)
{
b_rope a = B_ROPE_CHAR('a');
b_rope b = B_ROPE_CSTR_STATIC("Hello, world!");
b_rope c = B_ROPE_INT(-4096);
b_rope d = B_ROPE_UINT(4096);
b_rope str;
const b_rope *ropes[] = {
&a,
&b,
&c,
&d,
};
b_rope_join(&str, ropes, sizeof ropes / sizeof ropes[0]);
char cstr[1024];
b_rope_to_cstr(&str, cstr, sizeof cstr);
b_rope_destroy(&str);
printf("%s\n", cstr);
return 0;
}

10
test/core/streams.c Normal file
View File

@@ -0,0 +1,10 @@
#include <blue/core/stream.h>
#include <stdio.h>
int main(void)
{
b_stream_read_line_s(b_stdin, b_stdout);
b_stream_write_char(b_stdout, '\n');
return 0;
}