Add copy array with allocation and finish writing array tests

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

View File

@ -66,7 +66,7 @@ i32 wapp_i32_array_pop(I32Array *array) {
return array->items[--(array->count)]; return array->items[--(array->count)];
} }
void wapp_i32_array_copy(const I32Array *src, I32Array *dst) { void wapp_i32_array_copy_capped(const I32Array *src, I32Array *dst) {
if (!src || !dst) { if (!src || !dst) {
return; return;
} }
@ -90,19 +90,19 @@ I32Array *wapp_i32_array_alloc_capacity(const Allocator *allocator, u64 capacity
I32Array *array = NULL; I32Array *array = NULL;
if (!allocator) { if (!allocator) {
goto RETURN_INT_ARRAY_ALLOC; goto RETURN_I32_ARRAY_ALLOC;
} }
array = wapp_mem_allocator_alloc(allocator, allocation_size); array = wapp_mem_allocator_alloc(allocator, allocation_size);
if (!array) { if (!array) {
goto RETURN_INT_ARRAY_ALLOC; goto RETURN_I32_ARRAY_ALLOC;
} }
array->items = (i32 *)((u8 *)array + sizeof(I32Array)); array->items = (i32 *)((u8 *)array + sizeof(I32Array));
array->count = 0; array->count = 0;
array->capacity = capacity; array->capacity = capacity;
RETURN_INT_ARRAY_ALLOC: RETURN_I32_ARRAY_ALLOC:
return array; return array;
} }
@ -110,7 +110,7 @@ I32Array *wapp_i32_array_append_alloc(const Allocator *allocator, I32Array *arra
I32Array *output = array; I32Array *output = array;
if (!allocator || !array) { if (!allocator || !array) {
goto RETURN_INT_ARRAY_APPEND_ALLOC; goto RETURN_I32_ARRAY_APPEND_ALLOC;
} }
if (array->count >= array->capacity) { if (array->count >= array->capacity) {
@ -118,14 +118,14 @@ I32Array *wapp_i32_array_append_alloc(const Allocator *allocator, I32Array *arra
output = wapp_i32_array_alloc_capacity(allocator, new_capacity); output = wapp_i32_array_alloc_capacity(allocator, new_capacity);
if (!output) { if (!output) {
output = array; output = array;
goto RETURN_INT_ARRAY_APPEND_ALLOC; goto RETURN_I32_ARRAY_APPEND_ALLOC;
} }
wapp_i32_array_copy(array, output); wapp_i32_array_copy_capped(array, output);
} }
wapp_i32_array_append_capped(output, item); wapp_i32_array_append_capped(output, item);
RETURN_INT_ARRAY_APPEND_ALLOC: RETURN_I32_ARRAY_APPEND_ALLOC:
return output; return output;
} }
@ -133,7 +133,7 @@ I32Array *wapp_i32_array_extend_alloc(const Allocator *allocator, I32Array *arra
I32Array *output = array; I32Array *output = array;
if (!allocator || !array || !other) { if (!allocator || !array || !other) {
goto RETURN_INT_ARRAY_EXTEND_ALLOC; goto RETURN_I32_ARRAY_EXTEND_ALLOC;
} }
u64 remaining_capacity = array->capacity - array->count; u64 remaining_capacity = array->capacity - array->count;
@ -142,13 +142,36 @@ I32Array *wapp_i32_array_extend_alloc(const Allocator *allocator, I32Array *arra
output = wapp_i32_array_alloc_capacity(allocator, new_capacity); output = wapp_i32_array_alloc_capacity(allocator, new_capacity);
if (!output) { if (!output) {
output = array; output = array;
goto RETURN_INT_ARRAY_EXTEND_ALLOC; goto RETURN_I32_ARRAY_EXTEND_ALLOC;
} }
wapp_i32_array_copy(array, output); wapp_i32_array_copy_capped(array, output);
} }
wapp_i32_array_extend_capped(output, other); wapp_i32_array_extend_capped(output, other);
RETURN_INT_ARRAY_EXTEND_ALLOC: RETURN_I32_ARRAY_EXTEND_ALLOC:
return output;
}
I32Array *wapp_i32_array_copy_alloc(const Allocator *allocator, const I32Array *src, I32Array *dst) {
I32Array *output = dst;
if (!allocator || !src || !dst) {
goto RETURN_I32_ARRAY_COPY_ALLOC;
}
if (src->count >= dst->capacity) {
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst->capacity * 2);
output = wapp_i32_array_alloc_capacity(allocator, new_capacity);
if (!output) {
output = dst;
goto RETURN_I32_ARRAY_COPY_ALLOC;
}
}
wapp_i32_array_clear(output);
wapp_i32_array_copy_capped(src, output);
RETURN_I32_ARRAY_COPY_ALLOC:
return output; return output;
} }

View File

@ -30,11 +30,12 @@ void wapp_i32_array_append_capped(I32Array *array, i32 item);
void wapp_i32_array_extend_capped(I32Array *array, const I32Array *other); void wapp_i32_array_extend_capped(I32Array *array, const I32Array *other);
void wapp_i32_array_clear(I32Array *array); void wapp_i32_array_clear(I32Array *array);
i32 wapp_i32_array_pop(I32Array *array); i32 wapp_i32_array_pop(I32Array *array);
void wapp_i32_array_copy(const I32Array *src, I32Array *dst); void wapp_i32_array_copy_capped(const I32Array *src, I32Array *dst);
I32Array *wapp_i32_array_alloc_capacity(const Allocator *allocator, u64 capacity); I32Array *wapp_i32_array_alloc_capacity(const Allocator *allocator, u64 capacity);
I32Array *wapp_i32_array_append_alloc(const Allocator *allocator, I32Array *array, i32 item); I32Array *wapp_i32_array_append_alloc(const Allocator *allocator, I32Array *array, i32 item);
I32Array *wapp_i32_array_extend_alloc(const Allocator *allocator, I32Array *array, const I32Array *other); I32Array *wapp_i32_array_extend_alloc(const Allocator *allocator, I32Array *array, const I32Array *other);
I32Array *wapp_i32_array_copy_alloc(const Allocator *allocator, const I32Array *src, I32Array *dst);
#ifdef WAPP_PLATFORM_CPP #ifdef WAPP_PLATFORM_CPP
END_C_LINKAGE END_C_LINKAGE

View File

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

View File

@ -15,6 +15,11 @@ TestFuncResult test_i32_array_append_capped(void);
TestFuncResult test_i32_array_extend_capped(void); TestFuncResult test_i32_array_extend_capped(void);
TestFuncResult test_i32_array_clear(void); TestFuncResult test_i32_array_clear(void);
TestFuncResult test_i32_array_pop(void); TestFuncResult test_i32_array_pop(void);
TestFuncResult test_i32_array_copy_capped(void);
TestFuncResult test_i32_array_alloc_capacity(void);
TestFuncResult test_i32_array_append_alloc(void);
TestFuncResult test_i32_array_extend_alloc(void);
TestFuncResult test_i32_array_copy_alloc(void);
#ifdef WAPP_PLATFORM_CPP #ifdef WAPP_PLATFORM_CPP
END_C_LINKAGE END_C_LINKAGE

View File

@ -26,6 +26,11 @@ int main(void) {
test_i32_array_extend_capped, test_i32_array_extend_capped,
test_i32_array_clear, test_i32_array_clear,
test_i32_array_pop, test_i32_array_pop,
test_i32_array_copy_capped,
test_i32_array_alloc_capacity,
test_i32_array_append_alloc,
test_i32_array_extend_alloc,
test_i32_array_copy_alloc,
test_str8_lit, test_str8_lit,
test_str8_lit_ro, test_str8_lit_ro,
test_str8_buf, test_str8_buf,