Add copy array with allocation and finish writing array tests

This commit is contained in:
2025-04-27 19:30:14 +01:00
parent d3f1686d58
commit 77f3c40ebd
5 changed files with 152 additions and 13 deletions

View File

@@ -122,3 +122,108 @@ TestFuncResult test_i32_array_pop(void) {
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);
}