Update wapp array C API

This commit is contained in:
2025-12-07 21:56:16 +00:00
committed by Abdelrahman Said
parent 551836d4c8
commit 2b49bfcdbf
10 changed files with 365 additions and 3991 deletions

View File

@@ -66,8 +66,8 @@ u64 wapp_file_read(GenericArray *dst, File *file, u64 item_count) {
"`dst`, `dst->items` and `file` should not be NULL."); "`dst`, `dst->items` and `file` should not be NULL.");
u64 file_length = wapp_file_get_length(file); u64 file_length = wapp_file_get_length(file);
u64 dst_byte_capacity = dst->item_size * dst->capacity; u64 dst_byte_capacity = dst->header.item_size * dst->header.capacity;
u64 req_byte_count = item_count * dst->item_size; u64 req_byte_count = item_count * dst->header.item_size;
u64 copy_byte_count = 0; u64 copy_byte_count = 0;
if (req_byte_count <= file_length && req_byte_count <= dst_byte_capacity) { if (req_byte_count <= file_length && req_byte_count <= dst_byte_capacity) {
@@ -76,17 +76,17 @@ u64 wapp_file_read(GenericArray *dst, File *file, u64 item_count) {
copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity; copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity;
} }
dst->count = fread(dst->items, sizeof(u8), copy_byte_count, file) / dst->item_size; dst->header.count = fread(dst->items, sizeof(u8), copy_byte_count, file) / dst->header.item_size;
return dst->count; return dst->header.count;
} }
u64 wapp_file_write(const GenericArray *src, File *file, u64 item_count) { u64 wapp_file_write(const GenericArray *src, File *file, u64 item_count) {
wapp_debug_assert(src != NULL && src->items != NULL && file != NULL, wapp_debug_assert(src != NULL && src->items != NULL && file != NULL,
"`src`, `src->items` and `file` should not be NULL."); "`src`, `src->items` and `file` should not be NULL.");
u64 src_byte_count = src->count * src->item_size; u64 src_byte_count = src->header.count * src->header.item_size;
u64 req_byte_count = item_count * src->item_size; u64 req_byte_count = item_count * src->header.item_size;
u64 to_copy = req_byte_count <= src_byte_count ? req_byte_count : src_byte_count; u64 to_copy = req_byte_count <= src_byte_count ? req_byte_count : src_byte_count;
return fwrite(src->items, sizeof(u8), to_copy, file); return fwrite(src->items, sizeof(u8), to_copy, file);

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,7 @@
// vim:fileencoding=utf-8:foldmethod=marker // vim:fileencoding=utf-8:foldmethod=marker
#include "str8.h" #include "str8.h"
#include "../../array/array.h"
#include "../../../common/aliases/aliases.h" #include "../../../common/aliases/aliases.h"
#include "../../../common/assert/assert.h" #include "../../../common/assert/assert.h"
#include "../../mem_allocator/mem_allocator.h" #include "../../mem_allocator/mem_allocator.h"
@@ -260,14 +261,15 @@ void wapp_str8_to_upper(Str8 *dst, Str8RO *src) {
} }
} }
void wapp_str8_from_bytes(Str8 *dst, const U8Array *src) { void wapp_str8_from_bytes(Str8 *dst, const u8 *src_byte_array) {
u64 size = src->count * src->item_size; wapp_debug_assert(src_byte_array != NULL && dst != NULL, "`dst` and `src` should not be NULL");
u64 size = wapp_array_count(u8, src_byte_array) * wapp_array_item_size(u8, src_byte_array);
wapp_debug_assert(src != NULL && dst != NULL, "`dst` and `src` should not be NULL");
wapp_debug_assert(dst->capacity >= size, "`dst` does not have enough capacity"); wapp_debug_assert(dst->capacity >= size, "`dst` does not have enough capacity");
dst->size = size; dst->size = size;
memcpy(dst->buf, src->items, size); memcpy(dst->buf, src_byte_array, size);
} }
i64 wapp_str8_find(Str8RO *str, Str8RO substr) { i64 wapp_str8_find(Str8RO *str, Str8RO substr) {

View File

@@ -99,7 +99,7 @@ void wapp_str8_copy_to_cstr(char *dst, Str8RO *src, u64 dst_capacity);
void wapp_str8_format(Str8 *dst, const char *format, ...); void wapp_str8_format(Str8 *dst, const char *format, ...);
void wapp_str8_to_lower(Str8 *dst, Str8RO *src); void wapp_str8_to_lower(Str8 *dst, Str8RO *src);
void wapp_str8_to_upper(Str8 *dst, Str8RO *src); void wapp_str8_to_upper(Str8 *dst, Str8RO *src);
void wapp_str8_from_bytes(Str8 *dst, const U8Array *src); void wapp_str8_from_bytes(Str8 *dst, const u8 *src);
/** /**
* Str8 find functions * Str8 find functions

View File

@@ -4,16 +4,16 @@
TestFuncResult test_i32_array(void) { TestFuncResult test_i32_array(void) {
b8 result; b8 result;
I32Array array = wapp_i32_array(1, 2, 3, 4, 5, 6, 7); i32 *array = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7);
result = array.count == 7 && array.capacity == 16; result = wapp_array_count(i32, array) == 7 && wapp_array_capacity(i32, array) == 16;
i32 *item; i32 item;
u64 count = array.count; u64 count = wapp_array_count(i32, array);
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
item = wapp_i32_array_get(&array, index); item = wapp_array_get(i32, array, index);
result = result && item && (*item == (i32)(index + 1)); result = result && item && item == (i32)(index + 1);
++index; ++index;
running = index < count; running = index < count;
@@ -25,8 +25,8 @@ TestFuncResult test_i32_array(void) {
TestFuncResult test_i32_array_with_capacity(void) { TestFuncResult test_i32_array_with_capacity(void) {
b8 result; b8 result;
I32Array array = wapp_i32_array_with_capacity(64); i32 *array = wapp_array_with_capacity(i32, 64);
result = array.count == 0 && array.capacity == 64; result = wapp_array_count(i32, array) == 0 && wapp_array_capacity(i32, array) == 64;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
@@ -34,15 +34,15 @@ TestFuncResult test_i32_array_with_capacity(void) {
TestFuncResult test_i32_array_get(void) { TestFuncResult test_i32_array_get(void) {
b8 result = true; b8 result = true;
I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *item; i32 item;
u64 count = array.count; u64 count = wapp_array_count(i32, array);
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
item = wapp_i32_array_get(&array, index); item = wapp_array_get(i32, array, index);
result = result && item && (*item == (i32)index); result = result && item == (i32)index;
++index; ++index;
running = index < count; running = index < count;
@@ -54,17 +54,17 @@ TestFuncResult test_i32_array_get(void) {
TestFuncResult test_i32_array_set(void) { TestFuncResult test_i32_array_set(void) {
b8 result = true; b8 result = true;
I32Array array = wapp_i32_array(0, 1, 2, 3, 4, 5, 6, 7, 8); i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *item; i32 item;
u64 count = array.count; u64 count = wapp_array_count(i32, array);
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
i32 num = (i32)(index * 2); i32 num = (i32)(index * 2);
wapp_i32_array_set(&array, index, &num); wapp_array_set(i32, array, index, &num);
item = wapp_i32_array_get(&array, index); item = wapp_array_get(i32, array, index);
result = result && item && (*item == (i32)(index * 2)); result = result && item == (i32)(index * 2);
++index; ++index;
running = index < count; running = index < count;
@@ -76,17 +76,17 @@ TestFuncResult test_i32_array_set(void) {
TestFuncResult test_i32_array_append_capped(void) { TestFuncResult test_i32_array_append_capped(void) {
b8 result; b8 result;
I32Array array = wapp_i32_array_with_capacity(64); i32 *array = wapp_array_with_capacity(i32, 64);
wapp_i32_array_append_capped(&array, &((i32){10})); wapp_array_append_capped(i32, array, &((i32){10}));
result = array.count == 1; result = wapp_array_count(i32, array) == 1;
i32 *item = wapp_i32_array_get(&array, 0); i32 item = wapp_array_get(i32, array, 0);
result = result && item && *item == 10; result = result && item == 10;
array = wapp_i32_array(1); array = wapp_array(i32, 1);
wapp_i32_array_append_capped(&array, &((i32){10})); wapp_array_append_capped(i32, array, &((i32){10}));
result = result && array.count == 2; result = result && wapp_array_count(i32, array) == 2;
return wapp_tester_result(result); return wapp_tester_result(result);
} }
@@ -94,41 +94,14 @@ TestFuncResult test_i32_array_append_capped(void) {
TestFuncResult test_i32_array_extend_capped(void) { TestFuncResult test_i32_array_extend_capped(void) {
b8 result; b8 result;
I32Array array1 = wapp_i32_array(1, 2, 3, 4); i32 *array1 = wapp_array(i32, 1, 2, 3, 4);
I32Array array2 = wapp_i32_array(10, 20); i32 *array2 = wapp_array(i32, 10, 20);
result = array1.count == 4 && array2.count == 2; result = wapp_array_count(i32, array1) == 4 && wapp_array_count(i32, array2) == 2;
wapp_i32_array_extend_capped(&array1, &array2); wapp_array_extend_capped(i32, array1, array2);
result = result && array1.count == 6; result = result && wapp_array_count(i32, array1) == 6;
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_clear(void) {
b8 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) {
b8 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); return wapp_tester_result(result);
} }
@@ -136,31 +109,31 @@ TestFuncResult test_i32_array_pop(void) {
TestFuncResult test_i32_array_copy_capped(void) { TestFuncResult test_i32_array_copy_capped(void) {
b8 result; b8 result;
I32Array src = wapp_i32_array(1, 2, 3, 4, 5); i32 *src = wapp_array(i32, 1, 2, 3, 4, 5);
I32Array dst1 = wapp_i32_array(1, 2, 3, 4, 5, 6); i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6);
I32Array dst2 = wapp_i32_array(1, 2); i32 *dst2 = wapp_array(i32, 1, 2);
u64 expected_count = 5; u64 expected_count = 5;
wapp_i32_array_copy_capped(&src, &dst1); wapp_array_copy_capped(i32, dst1, src);
result = dst1.count == expected_count; result = wapp_array_count(i32, dst1) == expected_count;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(&dst1, index)); result = result && wapp_array_get(i32, src, index) == wapp_array_get(i32, dst1, index);
++index; ++index;
running = index < expected_count; running = index < expected_count;
} }
expected_count = 4; expected_count = 4;
wapp_i32_array_copy_capped(&src, &dst2); wapp_array_copy_capped(i32, dst2, src);
result = result && dst2.count == expected_count; result = result && wapp_array_count(i32, dst2) == expected_count;
index = 0; index = 0;
running = true; running = true;
while (running) { while (running) {
result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(&dst2, index)); result = result && wapp_array_get(i32, src, index) == wapp_array_get(i32, dst2, index);
++index; ++index;
running = index < expected_count; running = index < expected_count;
@@ -174,9 +147,9 @@ TestFuncResult test_i32_array_alloc_capacity(void) {
Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
u64 capacity = 32; u64 capacity = 32;
I32Array *array = wapp_i32_array_alloc_capacity(&allocator, capacity); i32 *array = wapp_array_alloc_capacity(i32, &allocator, capacity);
result = array && array->capacity == capacity; result = array && wapp_array_capacity(i32, array) == capacity;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -187,23 +160,23 @@ TestFuncResult test_i32_array_append_alloc(void) {
b8 result; b8 result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
I32Array array1 = wapp_i32_array(1, 2, 3, 4, 5, 6, 7, 8); i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8);
I32Array array2 = wapp_i32_array(1, 2); i32 *array2 = wapp_array(i32, 1, 2);
I32Array *arr_ptr = wapp_i32_array_append_alloc(&allocator, &array1, &((i32){10})); i32 *arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &((i32){10}));
result = arr_ptr == &array1; result = arr_ptr == array1;
u64 count = 4; u64 count = 4;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
i32 num = (i32)index; i32 num = (i32)index;
arr_ptr = wapp_i32_array_append_alloc(&allocator, &array2, &num); arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num);
++index; ++index;
running = index < count; running = index < count;
} }
result = result && arr_ptr != &array2; result = result && arr_ptr != array2;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -214,15 +187,15 @@ TestFuncResult test_i32_array_extend_alloc(void) {
b8 result; b8 result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
I32Array array1 = wapp_i32_array(1, 2, 3, 4, 5, 6, 7, 8); i32 *array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8);
I32Array array2 = wapp_i32_array(1, 2); i32 *array2 = wapp_array(i32, 1, 2);
I32Array array3 = wapp_i32_array(1, 2, 3, 4); i32 *array3 = wapp_array(i32, 1, 2, 3, 4);
I32Array *arr_ptr = wapp_i32_array_extend_alloc(&allocator, &array1, &array3); i32 *arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3);
result = arr_ptr == &array1; result = arr_ptr == array1;
arr_ptr = wapp_i32_array_extend_alloc(&allocator, &array2, &array3); arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3);
result = result && arr_ptr != &array2; result = result && arr_ptr != array2;
wapp_mem_arena_allocator_destroy(&allocator); wapp_mem_arena_allocator_destroy(&allocator);
@@ -233,32 +206,32 @@ TestFuncResult test_i32_array_copy_alloc(void) {
b8 result; b8 result;
Allocator allocator = wapp_mem_arena_allocator_init(MB(4)); Allocator allocator = wapp_mem_arena_allocator_init(MB(4));
I32Array src = wapp_i32_array(1, 2, 3, 4, 5); i32 *src = wapp_array(i32, 1, 2, 3, 4, 5);
I32Array dst1 = wapp_i32_array(1, 2, 3, 4, 5, 6); i32 *dst1 = wapp_array(i32, 1, 2, 3, 4, 5, 6);
I32Array dst2 = wapp_i32_array(1, 2); i32 *dst2 = wapp_array(i32, 1, 2);
I32Array *array_ptr = NULL; i32 *array_ptr = NULL;
u64 expected_count = 5; u64 expected_count = 5;
array_ptr = wapp_i32_array_copy_alloc(&allocator, &src, &dst1); array_ptr = wapp_array_copy_alloc(i32, &allocator, dst1, src);
result = array_ptr->count == expected_count && array_ptr == &dst1; result = wapp_array_count(i32, array_ptr) == expected_count && array_ptr == dst1;
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(array_ptr, index)); result = result && wapp_array_get(i32, src, index) == wapp_array_get(i32, array_ptr, index);
++index; ++index;
running = index < expected_count; running = index < expected_count;
} }
expected_count = 5; expected_count = 5;
array_ptr = wapp_i32_array_copy_alloc(&allocator, &src, &dst2); array_ptr = wapp_array_copy_alloc(i32, &allocator, dst2, src);
result = result && array_ptr->count == expected_count && array_ptr != &dst2; result = result && wapp_array_count(i32, array_ptr) == expected_count && array_ptr != dst2;
index = 0; index = 0;
running = true; running = true;
while (running) { while (running) {
result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(array_ptr, index)); result = result && wapp_array_get(i32, src, index) == wapp_array_get(i32, array_ptr, index);
++index; ++index;
running = index < expected_count; running = index < expected_count;
@@ -268,3 +241,30 @@ TestFuncResult test_i32_array_copy_alloc(void) {
return wapp_tester_result(result); return wapp_tester_result(result);
} }
TestFuncResult test_i32_array_pop(void) {
b8 result;
i32 *array1 = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
i32 *array2 = wapp_array_with_capacity(i32, 32);
i32 item1 = wapp_array_pop(i32, array1);
i32 item2 = wapp_array_pop(i32, array2);
result = item1 == 8 && item2 == 0;
return wapp_tester_result(result);
}
TestFuncResult test_i32_array_clear(void) {
b8 result;
i32 *array = wapp_array(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
result = wapp_array_count(i32, array) == 9;
wapp_array_clear(i32, array);
result = result && wapp_array_count(i32, array) == 0;
return wapp_tester_result(result);
}

View File

@@ -9,12 +9,12 @@ TestFuncResult test_i32_array_get(void);
TestFuncResult test_i32_array_set(void); TestFuncResult test_i32_array_set(void);
TestFuncResult test_i32_array_append_capped(void); 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_pop(void);
TestFuncResult test_i32_array_copy_capped(void); TestFuncResult test_i32_array_copy_capped(void);
TestFuncResult test_i32_array_alloc_capacity(void); TestFuncResult test_i32_array_alloc_capacity(void);
TestFuncResult test_i32_array_append_alloc(void); TestFuncResult test_i32_array_append_alloc(void);
TestFuncResult test_i32_array_extend_alloc(void); TestFuncResult test_i32_array_extend_alloc(void);
TestFuncResult test_i32_array_copy_alloc(void); TestFuncResult test_i32_array_copy_alloc(void);
TestFuncResult test_i32_array_pop(void);
TestFuncResult test_i32_array_clear(void);
#endif // !TEST_INT_ARRAY_H #endif // !TEST_INT_ARRAY_H

View File

@@ -6,16 +6,16 @@ TestFuncResult test_str8_array(void) {
b8 result; b8 result;
Str8 expected[] = {wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")}; Str8 expected[] = {wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")};
Str8Array array = wapp_str8_array(wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye")); Str8 *array = wapp_array(Str8, wapp_str8_lit("Hello"), wapp_str8_lit("Hi"), wapp_str8_lit("Bye"));
result = array.count == 3 && array.capacity == 8; result = wapp_array_count(Str8, array) == 3 && wapp_array_capacity(Str8, array) == 8;
Str8 *item; Str8 item;
u64 count = array.count; u64 count = wapp_array_count(Str8, array);
u64 index = 0; u64 index = 0;
b8 running = true; b8 running = true;
while (running) { while (running) {
item = wapp_str8_array_get(&array, index); item = wapp_array_get(Str8, array, index);
result = result && item && (wapp_str8_equal(item, &expected[index])); result = result && (wapp_str8_equal(&item, &expected[index]));
++index; ++index;
running = index < count; running = index < count;

View File

@@ -615,11 +615,11 @@ TestFuncResult test_str8_join(void) {
TestFuncResult test_str8_from_bytes(void) { TestFuncResult test_str8_from_bytes(void) {
b8 result; b8 result;
Str8 str = wapp_str8_buf(1024); Str8 str = wapp_str8_buf(1024);
U8Array bytes = wapp_u8_array('W', 'A', 'P', 'P'); u8 *bytes = wapp_array(u8, 'W', 'A', 'P', 'P');
wapp_str8_from_bytes(&str, &bytes); wapp_str8_from_bytes(&str, bytes);
result = str.size == bytes.count * bytes.item_size; result = str.size == wapp_array_count(u8, bytes) * wapp_array_item_size(u8, bytes);
result = result && wapp_str8_equal(&str, &wapp_str8_lit_ro("WAPP")); result = result && wapp_str8_equal(&str, &wapp_str8_lit_ro("WAPP"));
return wapp_tester_result(result); return wapp_tester_result(result);

View File

@@ -27,13 +27,13 @@ int main(void) {
test_i32_array_set, test_i32_array_set,
test_i32_array_append_capped, test_i32_array_append_capped,
test_i32_array_extend_capped, test_i32_array_extend_capped,
test_i32_array_clear,
test_i32_array_pop,
test_i32_array_copy_capped, test_i32_array_copy_capped,
test_i32_array_alloc_capacity, test_i32_array_alloc_capacity,
test_i32_array_append_alloc, test_i32_array_append_alloc,
test_i32_array_extend_alloc, test_i32_array_extend_alloc,
test_i32_array_copy_alloc, test_i32_array_copy_alloc,
test_i32_array_pop,
test_i32_array_clear,
test_str8_lit, test_str8_lit,
test_str8_lit_ro, test_str8_lit_ro,
test_str8_buf, test_str8_buf,