wizapp-stdlib/tests/array/test_i32_array.c

230 lines
6.1 KiB
C

#include "test_i32_array.h"
#include "wapp.h"
#include <stdbool.h>
TestFuncResult test_i32_array(void) {
bool result;
I32Array array = wapp_i32_array(1, 2, 3, 4, 5, 6, 7);
result = array.count == 7 && array.capacity == 16;
i32 *item;
u64 count = array.count;
for (u64 i = 0; i < count; ++i) {
item = wapp_i32_array_get(&array, i);
result = result && item && (*item == (i32)(i + 1));
}
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_with_capacity(void) {
bool result;
I32Array array = wapp_i32_array_with_capacity(64);
result = array.count == 0 && array.capacity == 64;
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_get(void) {
bool result = true;
I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *item;
u64 count = array.count;
for (u64 i = 0; i < count; ++i) {
item = wapp_i32_array_get(&array, i);
result = result && item && (*item == (i32)i);
}
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_set(void) {
bool result = true;
I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *item;
u64 count = array.count;
for (u64 i = 0; i < count; ++i) {
wapp_i32_array_set(&array, i, (i32)(i * 2));
item = wapp_i32_array_get(&array, i);
result = result && item && (*item == (i32)(i * 2));
}
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_append_capped(void) {
bool result;
I32Array array = wapp_i32_array_with_capacity(64);
wapp_i32_array_append_capped(&array, 10);
result = array.count == 1;
i32 *item = wapp_i32_array_get(&array, 0);
result = result && item && *item == 10;
array = wapp_i32_array(1);
wapp_i32_array_append_capped(&array, 10);
wapp_i32_array_append_capped(&array, 20);
result = result && array.count == 2;
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_extend_capped(void) {
bool result;
I32Array array1 = wapp_i32_array(1, 2, 3, 4);
I32Array array2 = wapp_i32_array(10, 20);
result = array1.count == 4 && array2.count == 2;
wapp_i32_array_extend_capped(&array1, &array2);
result = result && array1.count == 6;
wapp_i32_array_extend_capped(&array1, &array1);
result = result && array1.count == 6;
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_clear(void) {
bool result;
I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8);
result = array.count == 9;
wapp_i32_array_clear(&array);
result = result && array.count == 0;
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_pop(void) {
bool result;
I32Array array1 = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8);
I32Array array2 = wapp_i32_array_with_capacity(32);
i32 item1 = wapp_i32_array_pop(&array1);
i32 item2 = wapp_i32_array_pop(&array2);
result = item1 == 8 && item2 == 0;
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_copy_capped(void) {
bool result;
I32Array src = wapp_i32_array(1, 2, 3, 4, 5);
I32Array dst1 = wapp_i32_array(1, 2, 3, 4, 5, 6);
I32Array dst2 = wapp_i32_array(1, 2);
u64 expected_count = 5;
wapp_i32_array_copy_capped(&src, &dst1);
result = dst1.count == expected_count;
for (u64 i = 0; i < expected_count; ++i) {
result = result && (*wapp_i32_array_get(&src, i) == *wapp_i32_array_get(&dst1, i));
}
expected_count = 4;
wapp_i32_array_copy_capped(&src, &dst2);
result = result && dst2.count == expected_count;
for (u64 i = 0; i < expected_count; ++i) {
result = result && (*wapp_i32_array_get(&src, i) == *wapp_i32_array_get(&dst2, i));
}
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_alloc_capacity(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
u64 capacity = 32;
I32Array *array = wapp_i32_array_alloc_capacity(&allocator, capacity);
result = array && array->capacity == capacity;
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_append_alloc(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
I32Array array1 = wapp_i32_array(1, 2, 3, 4, 5, 6, 7, 8);
I32Array array2 = wapp_i32_array(1, 2);
I32Array *arr_ptr = wapp_i32_array_append_alloc(&allocator, &array1, 10);
result = arr_ptr == &array1;
for (u64 i = 0; i < 4; ++i) {
arr_ptr = wapp_i32_array_append_alloc(&allocator, &array2, (i32)i);
}
result = result && arr_ptr != &array2;
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_extend_alloc(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
I32Array array1 = wapp_i32_array(1, 2, 3, 4, 5, 6, 7, 8);
I32Array array2 = wapp_i32_array(1, 2);
I32Array array3 = wapp_i32_array(1, 2, 3, 4);
I32Array *arr_ptr = wapp_i32_array_extend_alloc(&allocator, &array1, &array3);
result = arr_ptr == &array1;
arr_ptr = wapp_i32_array_extend_alloc(&allocator, &array2, &array3);
result = result && arr_ptr != &array2;
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_copy_alloc(void) {
bool result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
I32Array src = wapp_i32_array(1, 2, 3, 4, 5);
I32Array dst1 = wapp_i32_array(1, 2, 3, 4, 5, 6);
I32Array dst2 = wapp_i32_array(1, 2);
I32Array *array_ptr = NULL;
u64 expected_count = 5;
array_ptr = wapp_i32_array_copy_alloc(&allocator, &src, &dst1);
result = array_ptr->count == expected_count && array_ptr == &dst1;
for (u64 i = 0; i < expected_count; ++i) {
result = result && (*wapp_i32_array_get(&src, i) == *wapp_i32_array_get(array_ptr, i));
}
expected_count = 5;
array_ptr = wapp_i32_array_copy_alloc(&allocator, &src, &dst2);
result = result && array_ptr->count == expected_count && array_ptr != &dst2;
for (u64 i = 0; i < expected_count; ++i) {
result = result && (*wapp_i32_array_get(&src, i) == *wapp_i32_array_get(array_ptr, i));
}
wapp_mem_arena_allocator_destroy(&allocator);
return wapp_tester_result(result);
}