vessel/tests/array.c
2025-06-13 17:58:13 +03:00

124 lines
3.8 KiB
C

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <vessel/array.h>
ARRAY_DECLARE(UInt8, uint8_t);
ARRAY_DEFINE(UInt8, uint8_t);
#define Err(...) \
{ \
fprintf(stderr, __VA_ARGS__); \
return 1; \
}
#define Test(cond, ...) \
{ \
puts("Test false: " #cond); \
if (cond) \
Err(__VA_ARGS__); \
}
int main(void) {
uint8_t idx, out8 = 0;
uint64_t out = 0;
UInt8Array a = { 0 };
Test(!UInt8Array_init(&a), "Unable to initialise the array.\n");
puts("Testing element insertion...");
for (idx = 0; idx < 132; ++idx) {
if (!UInt8Array_append(&a, idx)) {
fputs("Failed to insert element.\n", stderr);
UInt8Array_destroy(&a);
return 1;
}
if (!UInt8Array_index(&a, idx, &out) || out != idx || a.items[out] != idx) {
fprintf(stderr, "Element at %u is not %u or not at index %lu.\n", idx, idx, out);
UInt8Array_destroy(&a);
return 1;
}
}
puts("Passed.");
Test(a.size != 132 || a.cap != 256, "Reallocation of the array seems to have failed.\n");
Test(!UInt8Array_grow(&a), "Growing the array failed.\n");
Test(a.size != 132 || a.cap != 512, "Reallocation of the array seems to have failed.\n");
Test(!UInt8Array_grow(&a), "Growing the array failed.\n");
Test(a.size != 132 || a.cap != 1024, "Reallocation of the array seems to have failed.\n");
Test(!UInt8Array_grow(&a), "Growing the array failed.\n");
Test(a.size != 132 || a.cap != 2048, "Reallocation of the array seems to have failed.\n");
puts("Testing indexing...");
for (idx = 0; idx < 132; ++idx) {
if (!UInt8Array_index(&a, idx, &out) || out != idx || a.items[out] != idx) {
fprintf(stderr, "Element at %u is not %u or not at index %lu.\n", idx, idx, out);
UInt8Array_destroy(&a);
return 1;
}
}
puts("Passed.");
puts("Testing clearing...");
Test(!UInt8Array_clear(&a), "Clearing the array failed.\n");
Test(a.size != 0, "Clearing of the array seems to have failed.\n");
puts("Filling the array...");
for (idx = 0; idx < 132; ++idx) {
if (!UInt8Array_append(&a, idx % 3)) {
fputs("Failed to insert element.\n", stderr);
UInt8Array_destroy(&a);
return 1;
}
}
puts("Testing counting...");
Test(!UInt8Array_count(&a, 1, &out), "Failed to count 1s in the array.\n");
Test(out != 44, "Invalid count of 1s in the array.\n");
puts("Testing popping...");
Test(!UInt8Array_pop(&a, &out8), "Failed to pop from the array.\n");
Test(out8 != 2, "Invalid element popped from the array.\n");
puts("Testing copying...");
UInt8Array b = { 0 };
Test(!UInt8Array_init(&b), "Unable to initialise the intermediate array.\n");
Test(!UInt8Array_copy(&a, &b), "Unable to copy the array.\n");
Test(a.size != b.size || a.cap != b.cap || a.items == b.items,
"Unable to copy the array (invalid copy).\n");
Test(!UInt8Array_destroy(&a), "Unable to destroy the `a` array.\n");
puts("Testing integrity...");
for (idx = 0; idx < 132; ++idx) {
if ((idx % 3) != b.items[idx]) {
fprintf(
stderr, "Condition failed on element %u: %u != %u\n", idx, idx % 3, b.items[idx]);
UInt8Array_destroy(&b);
return 1;
}
}
Test(!UInt8Array_destroy(&b), "Unable to destroy the `b` array.\n");
puts("Passed.");
puts("All array array successfully passed.");
UInt8Array_destroy(&a);
UInt8Array_destroy(&b);
return 0;
}