From c0f920c5a731419ee06d14cf2e0efdca6aa56940 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 10 Aug 2025 14:33:07 +0100 Subject: [PATCH 1/7] Start implementing C++ support --- codegen/datatypes.py | 7 ++++- src/common/aliases/aliases.h | 6 ++++ src/common/assert/assert.h | 14 ++++++++++ src/common/misc/misc_utils.h | 8 ++++++ src/common/platform/platform.h | 25 ++++++++++++++++- src/core/mem/arena/mem_arena.h | 8 ++++++ src/core/mem/arena/mem_arena_allocator.h | 8 ++++++ src/core/mem/utils/mem_utils.h | 8 ++++++ src/core/os/cpath/cpath.h | 8 ++++++ src/core/os/mem/mem_os.h | 8 ++++++ src/core/os/mem/mem_os_ops.h | 8 ++++++ src/core/os/mem/posix/mem_os_posix.h | 8 ++++++ src/core/os/mem/win/mem_os_win.h | 8 ++++++ src/primitives/array/array.h | 8 ++++++ src/primitives/dbl_list/dbl_list.h | 8 ++++++ src/primitives/mem_allocator/mem_allocator.h | 8 ++++++ src/primitives/strings/str8/str8.h | 29 ++++++++++++++++++++ src/prng/xorshift/xorshift.h | 8 ++++++ src/testing/tester/tester.h | 8 ++++++ src/uuid/uuid.h | 8 ++++++ 20 files changed, 199 insertions(+), 2 deletions(-) diff --git a/codegen/datatypes.py b/codegen/datatypes.py index 8d69ce4..64e6e13 100644 --- a/codegen/datatypes.py +++ b/codegen/datatypes.py @@ -246,7 +246,10 @@ class CHeader(CFile): name_upper = self.name.upper() header_guard_name = f"{name_upper}_H" header_guard_open = f"#ifndef {header_guard_name}\n#define {header_guard_name}\n\n" - header_guard_close = f"\n#endif // !{header_guard_name}\n" + header_guard_close = f"#endif // !{header_guard_name}\n" + + c_linkage_open = "#ifdef WAPP_PLATFORM_CPP\nBEGIN_C_LINKAGE\n#endif // !WAPP_PLATFORM_CPP\n\n" + c_linkage_close = "\n#ifdef WAPP_PLATFORM_CPP\nEND_C_LINKAGE\n#endif // !WAPP_PLATFORM_CPP\n\n" includes = _get_includes_string(self.includes) @@ -274,10 +277,12 @@ class CHeader(CFile): super().__str__() + header_guard_open + includes + + c_linkage_open + macros + forward_declarations + types + funcs + + c_linkage_close + header_guard_close ) diff --git a/src/common/aliases/aliases.h b/src/common/aliases/aliases.h index 6759a0f..9792f80 100644 --- a/src/common/aliases/aliases.h +++ b/src/common/aliases/aliases.h @@ -54,4 +54,10 @@ #define internal static #define persistent static +#ifdef WAPP_PLATFORM_CPP +#define class_mem static +#define BEGIN_C_LINKAGE extern "C" { +#define END_C_LINKAGE } +#endif // WAPP_PLATFORM_CPP + #endif // !ALIASES_H diff --git a/src/common/assert/assert.h b/src/common/assert/assert.h index 92d80dc..ae98975 100644 --- a/src/common/assert/assert.h +++ b/src/common/assert/assert.h @@ -1,10 +1,18 @@ // vim:fileencoding=utf-8:foldmethod=marker +#ifndef WAPP_ASSERT_H +#define WAPP_ASSERT_H + #include "../aliases/aliases.h" +#include "../platform/platform.h" #include #include #include +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #define wapp_static_assert(EXPR, MSG) extern char ASSERTION_FAILED[EXPR ? 1 : -1] #define wapp_runtime_assert(EXPR, MSG) __wapp_runtime_assert(EXPR, MSG) @@ -25,3 +33,9 @@ abort(); \ } \ } while(false) + +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + +#endif // !WAPP_ASSERT_H diff --git a/src/common/misc/misc_utils.h b/src/common/misc/misc_utils.h index 07fcd88..e416d2a 100644 --- a/src/common/misc/misc_utils.h +++ b/src/common/misc/misc_utils.h @@ -5,6 +5,10 @@ #include "../aliases/aliases.h" +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #define KB(SIZE) (SIZE * 1024ull) #define MB(SIZE) (KB(SIZE) * 1024) #define GB(SIZE) (MB(SIZE) * 1024) @@ -36,4 +40,8 @@ #define wapp_misc_utils_va_args_count(T, ...) (sizeof((T[]){__VA_ARGS__})/sizeof(T)) +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !MISC_UTILS_H diff --git a/src/common/platform/platform.h b/src/common/platform/platform.h index c6796a7..9891f86 100644 --- a/src/common/platform/platform.h +++ b/src/common/platform/platform.h @@ -61,7 +61,30 @@ #endif #ifdef __cplusplus - #error "WAPP is a C only library" + #define WAPP_PLATFORM_CPP + #define WAPP_PLATFORM_CPP_VERSION __cplusplus + #define WAPP_PLATFORM_CPP98_VERSION 199711L + #define WAPP_PLATFORM_CPP11_VERSION 201103L + #define WAPP_PLATFORM_CPP14_VERSION 201402L + #define WAPP_PLATFORM_CPP17_VERSION 201703L + #define WAPP_PLATFORM_CPP20_VERSION 202002L + #define WAPP_PLATFORM_CPP23_VERSION 202302L + + #if WAPP_PLATFORM_CPP_VERSION == WAPP_PLATFORM_CPP98_VERSION + #define WAPP_PLATFORM_CPP98 + #elif WAPP_PLATFORM_CPP_VERSION == WAPP_PLATFORM_CPP11_VERSION + #define WAPP_PLATFORM_CPP11 + #elif WAPP_PLATFORM_CPP_VERSION == WAPP_PLATFORM_CPP14_VERSION + #define WAPP_PLATFORM_CPP14 + #elif WAPP_PLATFORM_CPP_VERSION == WAPP_PLATFORM_CPP17_VERSION + #define WAPP_PLATFORM_CPP17 + #elif WAPP_PLATFORM_CPP_VERSION == WAPP_PLATFORM_CPP20_VERSION + #define WAPP_PLATFORM_CPP20 + #elif WAPP_PLATFORM_CPP_VERSION == WAPP_PLATFORM_CPP23_VERSION + #define WAPP_PLATFORM_CPP23 + #else + #error "Unrecognised C++ version" + #endif #else #define WAPP_PLATFORM_C diff --git a/src/core/mem/arena/mem_arena.h b/src/core/mem/arena/mem_arena.h index e72a676..ec05f32 100644 --- a/src/core/mem/arena/mem_arena.h +++ b/src/core/mem/arena/mem_arena.h @@ -8,6 +8,10 @@ #include "../../os/mem/mem_os.h" #include +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + typedef struct arena Arena; #define wapp_mem_arena_init(arena_dptr, base_capacity) \ @@ -32,4 +36,8 @@ void *wapp_mem_arena_realloc_aligned(Arena *arena, void *ptr, u64 old_size, u64 void wapp_mem_arena_clear(Arena *arena); void wapp_mem_arena_destroy(Arena **arena); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !MEM_ARENA_H diff --git a/src/core/mem/arena/mem_arena_allocator.h b/src/core/mem/arena/mem_arena_allocator.h index 7fb84c7..f0bd4c3 100644 --- a/src/core/mem/arena/mem_arena_allocator.h +++ b/src/core/mem/arena/mem_arena_allocator.h @@ -9,6 +9,10 @@ #include "../../os/mem/mem_os.h" #include +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #define wapp_mem_arena_allocator_init(base_capacity) \ (wapp_mem_arena_allocator_init_custom(base_capacity, WAPP_MEM_ALLOC_RESERVE, false)) #define wapp_mem_arena_allocator_init_commit(base_capacity) \ @@ -33,4 +37,8 @@ Allocator wapp_mem_arena_allocator_init_custom(u64 base_capacity, MemAllocFlags void wapp_mem_arena_allocator_clear(Allocator *allocator); void wapp_mem_arena_allocator_destroy(Allocator *allocator); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !MEM_ARENA_ALLOCATOR_H diff --git a/src/core/mem/utils/mem_utils.h b/src/core/mem/utils/mem_utils.h index 8bb1e2f..ca7f781 100644 --- a/src/core/mem/utils/mem_utils.h +++ b/src/core/mem/utils/mem_utils.h @@ -6,6 +6,14 @@ #include "../../../common/aliases/aliases.h" #include "../../../common/platform/platform.h" +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + void *wapp_mem_util_align_forward(void *ptr, u64 alignment); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !MEM_UTILS_H diff --git a/src/core/os/cpath/cpath.h b/src/core/os/cpath/cpath.h index 68a24ce..f8363d5 100644 --- a/src/core/os/cpath/cpath.h +++ b/src/core/os/cpath/cpath.h @@ -8,6 +8,10 @@ #include "../../../primitives/mem_allocator/mem_allocator.h" #include "../../../primitives/strings/str8/str8.h" +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #ifdef WAPP_PLATFORM_POSIX #define PATH_SEP '/' #elif defined(WAPP_PLATFORM_WINDOWS) @@ -29,4 +33,8 @@ enum { u32 wapp_cpath_join_path(Str8 *dst, const Str8List *parts); Str8 *dirup(const Allocator *allocator, Str8RO *path, u64 levels); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !CPATH_H diff --git a/src/core/os/mem/mem_os.h b/src/core/os/mem/mem_os.h index 77535bc..451485c 100644 --- a/src/core/os/mem/mem_os.h +++ b/src/core/os/mem/mem_os.h @@ -8,6 +8,10 @@ #include "mem_os_ops.h" +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #if defined(WAPP_PLATFORM_WINDOWS) #include "win/mem_os_win.h" #elif defined(WAPP_PLATFORM_POSIX) @@ -22,4 +26,8 @@ void wapp_mem_util_free(void *ptr, u64 size); external void *mem_util_allocate(void *addr, u64 size, MemAccess access, MemAllocFlags flags, MemInitType type); external void mem_util_free(void *ptr, u64 size); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !MEM_OS_H diff --git a/src/core/os/mem/mem_os_ops.h b/src/core/os/mem/mem_os_ops.h index 04d8e2c..460c9a8 100644 --- a/src/core/os/mem/mem_os_ops.h +++ b/src/core/os/mem/mem_os_ops.h @@ -5,6 +5,10 @@ #include "../../../common/platform/platform.h" +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + typedef enum mem_access { WAPP_MEM_ACCESS_NONE, WAPP_MEM_ACCESS_READ_ONLY, @@ -19,4 +23,8 @@ typedef enum mem_init_type { WAPP_MEM_INIT_INITIALISED, } MemInitType; +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !MEM_OS_OPS_H diff --git a/src/core/os/mem/posix/mem_os_posix.h b/src/core/os/mem/posix/mem_os_posix.h index f93c3e2..1d3c013 100644 --- a/src/core/os/mem/posix/mem_os_posix.h +++ b/src/core/os/mem/posix/mem_os_posix.h @@ -5,6 +5,10 @@ #include "../../../../common/platform/platform.h" +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #ifdef WAPP_PLATFORM_POSIX #include @@ -24,4 +28,8 @@ typedef enum mem_alloc_flags { #endif // !WAPP_PLATFORM_POSIX +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !MEM_OS_POSIX_H diff --git a/src/core/os/mem/win/mem_os_win.h b/src/core/os/mem/win/mem_os_win.h index 1af2557..31f826e 100644 --- a/src/core/os/mem/win/mem_os_win.h +++ b/src/core/os/mem/win/mem_os_win.h @@ -5,6 +5,10 @@ #include "../../../../common/platform/platform.h" +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #ifdef WAPP_PLATFORM_WINDOWS #define WIN32_LEAN_AND_MEAN @@ -18,4 +22,8 @@ typedef enum mem_alloc_flags { #endif // !WAPP_PLATFORM_WINDOWS +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !MEM_OS_WIN_H diff --git a/src/primitives/array/array.h b/src/primitives/array/array.h index da31ac8..884e3a4 100644 --- a/src/primitives/array/array.h +++ b/src/primitives/array/array.h @@ -11,6 +11,10 @@ #include "../../common/aliases/aliases.h" #include "../../common/platform/platform.h" +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #define wapp_str8_array(...) ((Str8Array){ \ .items = (Str8[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ .count = wapp_misc_utils_va_args_count(Str8, __VA_ARGS__), \ @@ -616,4 +620,8 @@ UptrArray *wapp_uptr_array_copy_alloc(const Allocator *allocator, const UptrArra uptr *_uptr_array_pop(UptrArray *array); VoidPArray *_array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !ARRAY_H diff --git a/src/primitives/dbl_list/dbl_list.h b/src/primitives/dbl_list/dbl_list.h index 58fb6b7..a08a3e2 100644 --- a/src/primitives/dbl_list/dbl_list.h +++ b/src/primitives/dbl_list/dbl_list.h @@ -9,6 +9,10 @@ #include "../../common/aliases/aliases.h" #include "../../common/platform/platform.h" +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #define wapp_str8_list_node(ITEM_PTR) ((Str8Node){.item = ITEM_PTR}) #define wapp_void_ptr_list_node(ITEM_PTR) ((VoidPNode){.item = ITEM_PTR}) #define wapp_b32_list_node(ITEM_PTR) ((B32Node){.item = ITEM_PTR}) @@ -473,4 +477,8 @@ UptrNode *wapp_uptr_list_pop_back(UptrList *list); UptrNode *wapp_uptr_list_remove(UptrList *list, u64 index); void wapp_uptr_list_empty(UptrList *list); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !DBL_LIST_H diff --git a/src/primitives/mem_allocator/mem_allocator.h b/src/primitives/mem_allocator/mem_allocator.h index 6d2828d..41877e3 100644 --- a/src/primitives/mem_allocator/mem_allocator.h +++ b/src/primitives/mem_allocator/mem_allocator.h @@ -7,6 +7,10 @@ #include "../../common/platform/platform.h" #include +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + typedef void *(MemAllocFunc)(u64 size, void *alloc_obj); typedef void *(MemAllocAlignedFunc)(u64 size, u64 alignment, void *alloc_obj); typedef void *(MemReallocFunc)(void *ptr, u64 old_size, u64 new_size, void *alloc_obj); @@ -32,4 +36,8 @@ void *wapp_mem_allocator_realloc_aligned(const Allocator *allocator, void *ptr, u64 new_size, u64 alignment); void wapp_mem_allocator_free(const Allocator *allocator, void **ptr, u64 size); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !MEM_ALLOCATOR_H diff --git a/src/primitives/strings/str8/str8.h b/src/primitives/strings/str8/str8.h index 26a06dd..d66c337 100644 --- a/src/primitives/strings/str8/str8.h +++ b/src/primitives/strings/str8/str8.h @@ -4,12 +4,17 @@ #define STR8_H #include "../../../common/aliases/aliases.h" +#include "../../../common/assert/assert.h" #include "../../../common/platform/platform.h" #include "../../../primitives/dbl_list/dbl_list.h" #include "../../mem_allocator/mem_allocator.h" #include #include +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + typedef struct str8 Str8; struct str8 { u64 capacity; @@ -28,6 +33,25 @@ typedef const Str8 Str8RO; /** * Str8 stack buffers */ + +#ifdef WAPP_PLATFORM_CPP +// Uses a lambda to achieve the same behaviour achieved by the C macro +#define wapp_str8_buf(CAPACITY) ([](){ \ + persistent c8 buf[CAPACITY] = {}; \ + memset(buf, 0, CAPACITY); \ + return Str8{CAPACITY, 0, buf}; \ +}()) + +// Uses a lambda to achieve the same behaviour achieved by the C macro +#define wapp_str8_lit(STRING) ([]() { \ + persistent c8 buf[sizeof(STRING) * 2] = {}; \ + memcpy(buf, STRING, sizeof(STRING)); \ + return Str8{(sizeof(STRING) - 1) * 2, sizeof(STRING) - 1, buf}; \ +}()) + +#define wapp_str8_lit_ro(STRING) Str8RO{sizeof(STRING) - 1, sizeof(STRING) - 1, (c8 *)STRING} +#define wapp_str8_lit_ro_initialiser_list(STRING) {sizeof(STRING) - 1, sizeof(STRING) - 1, (c8 *)STRING} +#else #define wapp_str8_buf(CAPACITY) ((Str8){.capacity = CAPACITY, .size = 0, .buf = (c8[CAPACITY]){0}}) // Utilises the fact that memcpy returns pointer to dest buffer and that getting @@ -44,6 +68,7 @@ typedef const Str8 Str8RO; #define wapp_str8_lit_ro_initialiser_list(STRING) {.capacity = sizeof(STRING) - 1, \ .size = sizeof(STRING) - 1, \ .buf = (c8 *)STRING} +#endif // !WAPP_PLATFORM_CPP /** * Str8 allocated buffers @@ -94,4 +119,8 @@ Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8R #define wapp_str8_node_from_str8(STRING) wapp_str8_list_node(&(STRING)) u64 wapp_str8_list_total_size(const Str8List *list); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !STR8_H diff --git a/src/prng/xorshift/xorshift.h b/src/prng/xorshift/xorshift.h index 768d925..20d1aea 100644 --- a/src/prng/xorshift/xorshift.h +++ b/src/prng/xorshift/xorshift.h @@ -6,6 +6,10 @@ #include "../../common/aliases/aliases.h" #include "../../common/platform/platform.h" +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + typedef struct xor_256_state XOR256State; struct xor_256_state { u64 x; @@ -19,4 +23,8 @@ u64 wapp_prng_xorshift_256(XOR256State *state); u64 wapp_prng_xorshift_256ss(XOR256State *state); u64 wapp_prng_xorshift_256p(XOR256State *state); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !XORSHIFT_H diff --git a/src/testing/tester/tester.h b/src/testing/tester/tester.h index 82bf8f8..60d41f3 100644 --- a/src/testing/tester/tester.h +++ b/src/testing/tester/tester.h @@ -8,6 +8,10 @@ #include "../../primitives/strings/str8/str8.h" #include +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #define wapp_tester_result(PASSED) ((TestFuncResult){.name = wapp_str8_lit_ro(__func__), .passed = PASSED}) #define wapp_tester_run_tests(...) run_tests(__VA_ARGS__, NULL) @@ -25,4 +29,8 @@ typedef TestFuncResult(TestFunc)(void); void run_tests(TestFunc *func1, ...); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !TESTER_H diff --git a/src/uuid/uuid.h b/src/uuid/uuid.h index ca970a9..290a462 100644 --- a/src/uuid/uuid.h +++ b/src/uuid/uuid.h @@ -7,6 +7,10 @@ #include "../common/platform/platform.h" #include "../primitives/strings/str8/str8.h" +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #define UUID_BUF_LENGTH 48 #define WAPP_UUID_SPEC WAPP_STR8_SPEC #define wapp_uuid_varg(UUID) wapp_str8_varg((UUID).uuid) @@ -26,4 +30,8 @@ struct uuid { // Fails when passed a NULL pointer. UUID *wapp_uuid_init_uuid4(UUID *uuid); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !UUID_H -- 2.39.5 From eee4387201d142524532dd776ebedfe949fae76b Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 10 Aug 2025 15:06:21 +0100 Subject: [PATCH 2/7] Update dbl_list to add C++ specific macros --- codegen/datatypes.py | 11 +++++++++++ codegen/dbl_list/make_dbl_list.py | 17 +++++++++++++++-- codegen/dbl_list/snippets/list_node_cpp | 1 + src/primitives/dbl_list/dbl_list.h | 23 +++++++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 codegen/dbl_list/snippets/list_node_cpp diff --git a/codegen/datatypes.py b/codegen/datatypes.py index 64e6e13..2641022 100644 --- a/codegen/datatypes.py +++ b/codegen/datatypes.py @@ -211,6 +211,8 @@ class CFile: funcs: List[CFunc] = field(default_factory=list) decl_types: List[CStruct] = field(default_factory=list) macros: List[CMacro] = field(default_factory=list) + c_macros: List[CMacro] = field(default_factory=list) + cpp_macros: List[CMacro] = field(default_factory=list) def save(self, output_dir: Path): self.includes.extend( @@ -259,6 +261,15 @@ class CHeader(CFile): if len(macros) > 0: macros += "\n" + if len(self.cpp_macros) > 0: + macros += "#ifdef WAPP_PLATFORM_CPP\n" + for macro in self.cpp_macros: + macros += str(macro) + macros += "#else\n" + for macro in self.c_macros: + macros += str(macro) + macros += "#endif // !WAPP_PLATFORM_CPP\n\n" + forward_declarations = "" for _type in self.decl_types: forward_declarations += _type.declare() diff --git a/codegen/dbl_list/make_dbl_list.py b/codegen/dbl_list/make_dbl_list.py index 2f169d5..8eaa96f 100644 --- a/codegen/dbl_list/make_dbl_list.py +++ b/codegen/dbl_list/make_dbl_list.py @@ -139,7 +139,7 @@ def make_dbl_list(user_datatypes: Dict[CDataType, DblListData] = {}): ], ) - node_macro = CMacro( + node_cmacro = CMacro( name=f"wapp_{type_string_lower}_list_node(ITEM_PTR)", value=__format_func_body( filename=snippets_dir / "list_node", @@ -151,6 +151,18 @@ def make_dbl_list(user_datatypes: Dict[CDataType, DblListData] = {}): ), ) + node_cppmacro = CMacro( + name=f"wapp_{type_string_lower}_list_node(ITEM_PTR)", + value=__format_func_body( + filename=snippets_dir / "list_node_cpp", + type_string=type_string, + type_string_upper=type_string_upper, + type_string_lower=type_string_lower, + node_typename=dbl_list_data.node_typename, + list_typename=dbl_list_data.list_typename + ), + ) + get_func = CFunc( name=f"wapp_{type_string_lower}_list_get", ret_type=node, @@ -307,7 +319,8 @@ def make_dbl_list(user_datatypes: Dict[CDataType, DblListData] = {}): ) header.decl_types.extend(dbl_list_data.hdr_decl_types) - header.macros.append(node_macro) + header.c_macros.append(node_cmacro) + header.cpp_macros.append(node_cppmacro) header.types.extend([node, dl_list]) header.funcs.extend([ get_func, diff --git a/codegen/dbl_list/snippets/list_node_cpp b/codegen/dbl_list/snippets/list_node_cpp new file mode 100644 index 0000000..c753803 --- /dev/null +++ b/codegen/dbl_list/snippets/list_node_cpp @@ -0,0 +1 @@ +{NodeType}{{ITEM_PTR, nullptr, nullptr}} diff --git a/src/primitives/dbl_list/dbl_list.h b/src/primitives/dbl_list/dbl_list.h index a08a3e2..80f3eae 100644 --- a/src/primitives/dbl_list/dbl_list.h +++ b/src/primitives/dbl_list/dbl_list.h @@ -13,6 +13,28 @@ BEGIN_C_LINKAGE #endif // !WAPP_PLATFORM_CPP +#ifdef WAPP_PLATFORM_CPP +#define wapp_str8_list_node(ITEM_PTR) Str8Node{ITEM_PTR, nullptr, nullptr} +#define wapp_void_ptr_list_node(ITEM_PTR) VoidPNode{ITEM_PTR, nullptr, nullptr} +#define wapp_b32_list_node(ITEM_PTR) B32Node{ITEM_PTR, nullptr, nullptr} +#define wapp_char_list_node(ITEM_PTR) CharNode{ITEM_PTR, nullptr, nullptr} +#define wapp_c8_list_node(ITEM_PTR) C8Node{ITEM_PTR, nullptr, nullptr} +#define wapp_c16_list_node(ITEM_PTR) C16Node{ITEM_PTR, nullptr, nullptr} +#define wapp_c32_list_node(ITEM_PTR) C32Node{ITEM_PTR, nullptr, nullptr} +#define wapp_i8_list_node(ITEM_PTR) I8Node{ITEM_PTR, nullptr, nullptr} +#define wapp_i16_list_node(ITEM_PTR) I16Node{ITEM_PTR, nullptr, nullptr} +#define wapp_i32_list_node(ITEM_PTR) I32Node{ITEM_PTR, nullptr, nullptr} +#define wapp_i64_list_node(ITEM_PTR) I64Node{ITEM_PTR, nullptr, nullptr} +#define wapp_u8_list_node(ITEM_PTR) U8Node{ITEM_PTR, nullptr, nullptr} +#define wapp_u16_list_node(ITEM_PTR) U16Node{ITEM_PTR, nullptr, nullptr} +#define wapp_u32_list_node(ITEM_PTR) U32Node{ITEM_PTR, nullptr, nullptr} +#define wapp_u64_list_node(ITEM_PTR) U64Node{ITEM_PTR, nullptr, nullptr} +#define wapp_f32_list_node(ITEM_PTR) F32Node{ITEM_PTR, nullptr, nullptr} +#define wapp_f64_list_node(ITEM_PTR) F64Node{ITEM_PTR, nullptr, nullptr} +#define wapp_f128_list_node(ITEM_PTR) F128Node{ITEM_PTR, nullptr, nullptr} +#define wapp_iptr_list_node(ITEM_PTR) IptrNode{ITEM_PTR, nullptr, nullptr} +#define wapp_uptr_list_node(ITEM_PTR) UptrNode{ITEM_PTR, nullptr, nullptr} +#else #define wapp_str8_list_node(ITEM_PTR) ((Str8Node){.item = ITEM_PTR}) #define wapp_void_ptr_list_node(ITEM_PTR) ((VoidPNode){.item = ITEM_PTR}) #define wapp_b32_list_node(ITEM_PTR) ((B32Node){.item = ITEM_PTR}) @@ -33,6 +55,7 @@ BEGIN_C_LINKAGE #define wapp_f128_list_node(ITEM_PTR) ((F128Node){.item = ITEM_PTR}) #define wapp_iptr_list_node(ITEM_PTR) ((IptrNode){.item = ITEM_PTR}) #define wapp_uptr_list_node(ITEM_PTR) ((UptrNode){.item = ITEM_PTR}) +#endif // !WAPP_PLATFORM_CPP typedef struct str8 Str8; -- 2.39.5 From df29d851c274c018a7cfa5000da47f35e33c1550 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 10 Aug 2025 16:18:21 +0100 Subject: [PATCH 3/7] Update array to support C++ --- codegen/array/make_array.py | 49 ++- codegen/array/snippets/array_pop_macro_cpp | 4 + codegen/array/snippets/stack_array_cpp | 9 + .../array/snippets/stack_capacity_array_cpp | 4 + src/common/misc/misc_utils.h | 9 + src/primitives/array/array.h | 384 +++++++++++++++++- 6 files changed, 433 insertions(+), 26 deletions(-) create mode 100644 codegen/array/snippets/array_pop_macro_cpp create mode 100644 codegen/array/snippets/stack_array_cpp create mode 100644 codegen/array/snippets/stack_capacity_array_cpp diff --git a/codegen/array/make_array.py b/codegen/array/make_array.py index af3922c..e395e34 100644 --- a/codegen/array/make_array.py +++ b/codegen/array/make_array.py @@ -153,7 +153,7 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): ) generic_funcs.append(alloc_capacity_func) - stack_array_macro = CMacro( + stack_array_cmacro = CMacro( name=f"wapp_{type_string_lower}_array(...)", value=__format_func_body( filename=snippets_dir / "stack_array", @@ -163,8 +163,18 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): array_typename=array_data.array_typename, ), ) + stack_array_cppmacro = CMacro( + name=f"wapp_{type_string_lower}_array(...)", + value=__format_func_body( + filename=snippets_dir / "stack_array_cpp", + type_string=type_string, + type_string_upper=type_string_upper, + type_string_lower=type_string_lower, + array_typename=array_data.array_typename, + ), + ) - stack_capacity_array_macro = CMacro( + stack_capacity_array_cmacro = CMacro( name=f"wapp_{type_string_lower}_array_with_capacity(CAPACITY)", value=__format_func_body( filename=snippets_dir / "stack_capacity_array", @@ -174,6 +184,16 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): array_typename=array_data.array_typename, ), ) + stack_capacity_array_cppmacro = CMacro( + name=f"wapp_{type_string_lower}_array_with_capacity(CAPACITY)", + value=__format_func_body( + filename=snippets_dir / "stack_capacity_array_cpp", + type_string=type_string, + type_string_upper=type_string_upper, + type_string_lower=type_string_lower, + array_typename=array_data.array_typename, + ), + ) alloc_capacity_array_macro = CMacro( name=f"wapp_{type_string_lower}_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY)", @@ -186,7 +206,7 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): ), ) - array_pop_macro = CMacro( + array_pop_cmacro = CMacro( name=f"wapp_{type_string_lower}_array_pop(ARRAY_PTR)", value=__format_func_body( filename=snippets_dir / "array_pop_macro", @@ -196,6 +216,16 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): array_typename=array_data.array_typename, ), ) + array_pop_cppmacro = CMacro( + name=f"wapp_{type_string_lower}_array_pop(ARRAY_PTR)", + value=__format_func_body( + filename=snippets_dir / "array_pop_macro_cpp", + type_string=type_string, + type_string_upper=type_string_upper, + type_string_lower=type_string_lower, + array_typename=array_data.array_typename, + ), + ) get_func = CFunc( name=f"wapp_{type_string_lower}_array_get", @@ -366,10 +396,17 @@ def make_array(user_datatypes: Dict[CDataType, ArrayData] = {}): header.decl_types.extend(array_data.hdr_decl_types) header.macros.extend([ - stack_array_macro, - stack_capacity_array_macro, alloc_capacity_array_macro, - array_pop_macro, + ]) + header.c_macros.extend([ + stack_array_cmacro, + stack_capacity_array_cmacro, + array_pop_cmacro, + ]) + header.cpp_macros.extend([ + stack_array_cppmacro, + stack_capacity_array_cppmacro, + array_pop_cppmacro, ]) header.types.extend([array]) header.funcs.extend([ diff --git a/codegen/array/snippets/array_pop_macro_cpp b/codegen/array/snippets/array_pop_macro_cpp new file mode 100644 index 0000000..41784b6 --- /dev/null +++ b/codegen/array/snippets/array_pop_macro_cpp @@ -0,0 +1,4 @@ +(ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_{Tlower}_array_pop(ARRAY_PTR) : \ + {T}{{}} \ +) diff --git a/codegen/array/snippets/stack_array_cpp b/codegen/array/snippets/stack_array_cpp new file mode 100644 index 0000000..938bccc --- /dev/null +++ b/codegen/array/snippets/stack_array_cpp @@ -0,0 +1,9 @@ +([]() {{ \ + persistent {T} buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count({T}, __VA_ARGS__) * 2)] = {{__VA_ARGS__}}; \ + return {ArrayType}{{ \ + buf, \ + wapp_misc_utils_va_args_count({T}, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count({T}, __VA_ARGS__) * 2), \ + sizeof({T}) \ + }}; \ +}}()) diff --git a/codegen/array/snippets/stack_capacity_array_cpp b/codegen/array/snippets/stack_capacity_array_cpp new file mode 100644 index 0000000..7ef0cb8 --- /dev/null +++ b/codegen/array/snippets/stack_capacity_array_cpp @@ -0,0 +1,4 @@ +([]() {{ \ + persistent {T} buf[CAPACITY] = {{}}; \ + return {ArrayType}{{buf, 0, CAPACITY, sizeof({T})}}; \ +}}()) diff --git a/src/common/misc/misc_utils.h b/src/common/misc/misc_utils.h index e416d2a..27c0074 100644 --- a/src/common/misc/misc_utils.h +++ b/src/common/misc/misc_utils.h @@ -38,10 +38,19 @@ BEGIN_C_LINKAGE ) + 1 \ ) +#ifdef WAPP_PLATFORM_CPP +#define wapp_misc_utils_va_args_count(T, ...) va_args_count(__VA_ARGS__) +#else #define wapp_misc_utils_va_args_count(T, ...) (sizeof((T[]){__VA_ARGS__})/sizeof(T)) +#endif // !WAPP_PLATFORM_CPP #ifdef WAPP_PLATFORM_CPP END_C_LINKAGE + +template +constexpr u64 va_args_count(Args&&...) { + return sizeof...(Args); +} #endif // !WAPP_PLATFORM_CPP #endif // !MISC_UTILS_H diff --git a/src/primitives/array/array.h b/src/primitives/array/array.h index 884e3a4..e4a1414 100644 --- a/src/primitives/array/array.h +++ b/src/primitives/array/array.h @@ -15,6 +15,369 @@ BEGIN_C_LINKAGE #endif // !WAPP_PLATFORM_CPP +#define wapp_str8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((Str8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(Str8))) +#define wapp_void_ptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((VoidPArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(void *))) +#define wapp_b32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((B32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(b32))) +#define wapp_char_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((CharArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(char))) +#define wapp_c8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c8))) +#define wapp_c16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c16))) +#define wapp_c32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c32))) +#define wapp_i8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i8))) +#define wapp_i16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i16))) +#define wapp_i32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i32))) +#define wapp_i64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i64))) +#define wapp_u8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u8))) +#define wapp_u16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u16))) +#define wapp_u32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u32))) +#define wapp_u64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u64))) +#define wapp_f32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f32))) +#define wapp_f64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f64))) +#define wapp_f128_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F128Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f128))) +#define wapp_iptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((IptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(iptr))) +#define wapp_uptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((UptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(uptr))) + +#ifdef WAPP_PLATFORM_CPP +#define wapp_str8_array(...) ([]() { \ + persistent Str8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return Str8Array{ \ + buf, \ + wapp_misc_utils_va_args_count(Str8, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2), \ + sizeof(Str8) \ + }; \ +}()) +#define wapp_str8_array_with_capacity(CAPACITY) ([]() { \ + persistent Str8 buf[CAPACITY] = {}; \ + return Str8Array{buf, 0, CAPACITY, sizeof(Str8)}; \ +}()) +#define wapp_str8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_str8_array_pop(ARRAY_PTR) : \ + Str8{} \ +) +#define wapp_void_ptr_array(...) ([]() { \ + persistent void * buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(void *, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return VoidPArray{ \ + buf, \ + wapp_misc_utils_va_args_count(void *, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(void *, __VA_ARGS__) * 2), \ + sizeof(void *) \ + }; \ +}()) +#define wapp_void_ptr_array_with_capacity(CAPACITY) ([]() { \ + persistent void * buf[CAPACITY] = {}; \ + return VoidPArray{buf, 0, CAPACITY, sizeof(void *)}; \ +}()) +#define wapp_void_ptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_void_ptr_array_pop(ARRAY_PTR) : \ + void *{} \ +) +#define wapp_b32_array(...) ([]() { \ + persistent b32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return B32Array{ \ + buf, \ + wapp_misc_utils_va_args_count(b32, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b32, __VA_ARGS__) * 2), \ + sizeof(b32) \ + }; \ +}()) +#define wapp_b32_array_with_capacity(CAPACITY) ([]() { \ + persistent b32 buf[CAPACITY] = {}; \ + return B32Array{buf, 0, CAPACITY, sizeof(b32)}; \ +}()) +#define wapp_b32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_b32_array_pop(ARRAY_PTR) : \ + b32{} \ +) +#define wapp_char_array(...) ([]() { \ + persistent char buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(char, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return CharArray{ \ + buf, \ + wapp_misc_utils_va_args_count(char, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(char, __VA_ARGS__) * 2), \ + sizeof(char) \ + }; \ +}()) +#define wapp_char_array_with_capacity(CAPACITY) ([]() { \ + persistent char buf[CAPACITY] = {}; \ + return CharArray{buf, 0, CAPACITY, sizeof(char)}; \ +}()) +#define wapp_char_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_char_array_pop(ARRAY_PTR) : \ + char{} \ +) +#define wapp_c8_array(...) ([]() { \ + persistent c8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return C8Array{ \ + buf, \ + wapp_misc_utils_va_args_count(c8, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c8, __VA_ARGS__) * 2), \ + sizeof(c8) \ + }; \ +}()) +#define wapp_c8_array_with_capacity(CAPACITY) ([]() { \ + persistent c8 buf[CAPACITY] = {}; \ + return C8Array{buf, 0, CAPACITY, sizeof(c8)}; \ +}()) +#define wapp_c8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_c8_array_pop(ARRAY_PTR) : \ + c8{} \ +) +#define wapp_c16_array(...) ([]() { \ + persistent c16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return C16Array{ \ + buf, \ + wapp_misc_utils_va_args_count(c16, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c16, __VA_ARGS__) * 2), \ + sizeof(c16) \ + }; \ +}()) +#define wapp_c16_array_with_capacity(CAPACITY) ([]() { \ + persistent c16 buf[CAPACITY] = {}; \ + return C16Array{buf, 0, CAPACITY, sizeof(c16)}; \ +}()) +#define wapp_c16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_c16_array_pop(ARRAY_PTR) : \ + c16{} \ +) +#define wapp_c32_array(...) ([]() { \ + persistent c32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return C32Array{ \ + buf, \ + wapp_misc_utils_va_args_count(c32, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c32, __VA_ARGS__) * 2), \ + sizeof(c32) \ + }; \ +}()) +#define wapp_c32_array_with_capacity(CAPACITY) ([]() { \ + persistent c32 buf[CAPACITY] = {}; \ + return C32Array{buf, 0, CAPACITY, sizeof(c32)}; \ +}()) +#define wapp_c32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_c32_array_pop(ARRAY_PTR) : \ + c32{} \ +) +#define wapp_i8_array(...) ([]() { \ + persistent i8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return I8Array{ \ + buf, \ + wapp_misc_utils_va_args_count(i8, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i8, __VA_ARGS__) * 2), \ + sizeof(i8) \ + }; \ +}()) +#define wapp_i8_array_with_capacity(CAPACITY) ([]() { \ + persistent i8 buf[CAPACITY] = {}; \ + return I8Array{buf, 0, CAPACITY, sizeof(i8)}; \ +}()) +#define wapp_i8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_i8_array_pop(ARRAY_PTR) : \ + i8{} \ +) +#define wapp_i16_array(...) ([]() { \ + persistent i16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return I16Array{ \ + buf, \ + wapp_misc_utils_va_args_count(i16, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i16, __VA_ARGS__) * 2), \ + sizeof(i16) \ + }; \ +}()) +#define wapp_i16_array_with_capacity(CAPACITY) ([]() { \ + persistent i16 buf[CAPACITY] = {}; \ + return I16Array{buf, 0, CAPACITY, sizeof(i16)}; \ +}()) +#define wapp_i16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_i16_array_pop(ARRAY_PTR) : \ + i16{} \ +) +#define wapp_i32_array(...) ([]() { \ + persistent i32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return I32Array{ \ + buf, \ + wapp_misc_utils_va_args_count(i32, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2), \ + sizeof(i32) \ + }; \ +}()) +#define wapp_i32_array_with_capacity(CAPACITY) ([]() { \ + persistent i32 buf[CAPACITY] = {}; \ + return I32Array{buf, 0, CAPACITY, sizeof(i32)}; \ +}()) +#define wapp_i32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_i32_array_pop(ARRAY_PTR) : \ + i32{} \ +) +#define wapp_i64_array(...) ([]() { \ + persistent i64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return I64Array{ \ + buf, \ + wapp_misc_utils_va_args_count(i64, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i64, __VA_ARGS__) * 2), \ + sizeof(i64) \ + }; \ +}()) +#define wapp_i64_array_with_capacity(CAPACITY) ([]() { \ + persistent i64 buf[CAPACITY] = {}; \ + return I64Array{buf, 0, CAPACITY, sizeof(i64)}; \ +}()) +#define wapp_i64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_i64_array_pop(ARRAY_PTR) : \ + i64{} \ +) +#define wapp_u8_array(...) ([]() { \ + persistent u8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return U8Array{ \ + buf, \ + wapp_misc_utils_va_args_count(u8, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u8, __VA_ARGS__) * 2), \ + sizeof(u8) \ + }; \ +}()) +#define wapp_u8_array_with_capacity(CAPACITY) ([]() { \ + persistent u8 buf[CAPACITY] = {}; \ + return U8Array{buf, 0, CAPACITY, sizeof(u8)}; \ +}()) +#define wapp_u8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_u8_array_pop(ARRAY_PTR) : \ + u8{} \ +) +#define wapp_u16_array(...) ([]() { \ + persistent u16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return U16Array{ \ + buf, \ + wapp_misc_utils_va_args_count(u16, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u16, __VA_ARGS__) * 2), \ + sizeof(u16) \ + }; \ +}()) +#define wapp_u16_array_with_capacity(CAPACITY) ([]() { \ + persistent u16 buf[CAPACITY] = {}; \ + return U16Array{buf, 0, CAPACITY, sizeof(u16)}; \ +}()) +#define wapp_u16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_u16_array_pop(ARRAY_PTR) : \ + u16{} \ +) +#define wapp_u32_array(...) ([]() { \ + persistent u32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return U32Array{ \ + buf, \ + wapp_misc_utils_va_args_count(u32, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u32, __VA_ARGS__) * 2), \ + sizeof(u32) \ + }; \ +}()) +#define wapp_u32_array_with_capacity(CAPACITY) ([]() { \ + persistent u32 buf[CAPACITY] = {}; \ + return U32Array{buf, 0, CAPACITY, sizeof(u32)}; \ +}()) +#define wapp_u32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_u32_array_pop(ARRAY_PTR) : \ + u32{} \ +) +#define wapp_u64_array(...) ([]() { \ + persistent u64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return U64Array{ \ + buf, \ + wapp_misc_utils_va_args_count(u64, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u64, __VA_ARGS__) * 2), \ + sizeof(u64) \ + }; \ +}()) +#define wapp_u64_array_with_capacity(CAPACITY) ([]() { \ + persistent u64 buf[CAPACITY] = {}; \ + return U64Array{buf, 0, CAPACITY, sizeof(u64)}; \ +}()) +#define wapp_u64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_u64_array_pop(ARRAY_PTR) : \ + u64{} \ +) +#define wapp_f32_array(...) ([]() { \ + persistent f32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return F32Array{ \ + buf, \ + wapp_misc_utils_va_args_count(f32, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f32, __VA_ARGS__) * 2), \ + sizeof(f32) \ + }; \ +}()) +#define wapp_f32_array_with_capacity(CAPACITY) ([]() { \ + persistent f32 buf[CAPACITY] = {}; \ + return F32Array{buf, 0, CAPACITY, sizeof(f32)}; \ +}()) +#define wapp_f32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_f32_array_pop(ARRAY_PTR) : \ + f32{} \ +) +#define wapp_f64_array(...) ([]() { \ + persistent f64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return F64Array{ \ + buf, \ + wapp_misc_utils_va_args_count(f64, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f64, __VA_ARGS__) * 2), \ + sizeof(f64) \ + }; \ +}()) +#define wapp_f64_array_with_capacity(CAPACITY) ([]() { \ + persistent f64 buf[CAPACITY] = {}; \ + return F64Array{buf, 0, CAPACITY, sizeof(f64)}; \ +}()) +#define wapp_f64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_f64_array_pop(ARRAY_PTR) : \ + f64{} \ +) +#define wapp_f128_array(...) ([]() { \ + persistent f128 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f128, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return F128Array{ \ + buf, \ + wapp_misc_utils_va_args_count(f128, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f128, __VA_ARGS__) * 2), \ + sizeof(f128) \ + }; \ +}()) +#define wapp_f128_array_with_capacity(CAPACITY) ([]() { \ + persistent f128 buf[CAPACITY] = {}; \ + return F128Array{buf, 0, CAPACITY, sizeof(f128)}; \ +}()) +#define wapp_f128_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_f128_array_pop(ARRAY_PTR) : \ + f128{} \ +) +#define wapp_iptr_array(...) ([]() { \ + persistent iptr buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(iptr, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return IptrArray{ \ + buf, \ + wapp_misc_utils_va_args_count(iptr, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(iptr, __VA_ARGS__) * 2), \ + sizeof(iptr) \ + }; \ +}()) +#define wapp_iptr_array_with_capacity(CAPACITY) ([]() { \ + persistent iptr buf[CAPACITY] = {}; \ + return IptrArray{buf, 0, CAPACITY, sizeof(iptr)}; \ +}()) +#define wapp_iptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_iptr_array_pop(ARRAY_PTR) : \ + iptr{} \ +) +#define wapp_uptr_array(...) ([]() { \ + persistent uptr buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(uptr, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ + return UptrArray{ \ + buf, \ + wapp_misc_utils_va_args_count(uptr, __VA_ARGS__), \ + wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(uptr, __VA_ARGS__) * 2), \ + sizeof(uptr) \ + }; \ +}()) +#define wapp_uptr_array_with_capacity(CAPACITY) ([]() { \ + persistent uptr buf[CAPACITY] = {}; \ + return UptrArray{buf, 0, CAPACITY, sizeof(uptr)}; \ +}()) +#define wapp_uptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ + *_uptr_array_pop(ARRAY_PTR) : \ + uptr{} \ +) +#else #define wapp_str8_array(...) ((Str8Array){ \ .items = (Str8[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2)]){__VA_ARGS__}, \ .count = wapp_misc_utils_va_args_count(Str8, __VA_ARGS__), \ @@ -22,7 +385,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(Str8) \ }) #define wapp_str8_array_with_capacity(CAPACITY) ((Str8Array){.items = (Str8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(Str8)}) -#define wapp_str8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((Str8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(Str8))) #define wapp_str8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_str8_array_pop(ARRAY_PTR) : \ (Str8){0} \ @@ -34,7 +396,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(void *) \ }) #define wapp_void_ptr_array_with_capacity(CAPACITY) ((VoidPArray){.items = (void *[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(void *)}) -#define wapp_void_ptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((VoidPArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(void *))) #define wapp_void_ptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_void_ptr_array_pop(ARRAY_PTR) : \ (void *){0} \ @@ -46,7 +407,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(b32) \ }) #define wapp_b32_array_with_capacity(CAPACITY) ((B32Array){.items = (b32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(b32)}) -#define wapp_b32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((B32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(b32))) #define wapp_b32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_b32_array_pop(ARRAY_PTR) : \ (b32){0} \ @@ -58,7 +418,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(char) \ }) #define wapp_char_array_with_capacity(CAPACITY) ((CharArray){.items = (char[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(char)}) -#define wapp_char_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((CharArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(char))) #define wapp_char_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_char_array_pop(ARRAY_PTR) : \ (char){0} \ @@ -70,7 +429,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(c8) \ }) #define wapp_c8_array_with_capacity(CAPACITY) ((C8Array){.items = (c8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(c8)}) -#define wapp_c8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c8))) #define wapp_c8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_c8_array_pop(ARRAY_PTR) : \ (c8){0} \ @@ -82,7 +440,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(c16) \ }) #define wapp_c16_array_with_capacity(CAPACITY) ((C16Array){.items = (c16[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(c16)}) -#define wapp_c16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c16))) #define wapp_c16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_c16_array_pop(ARRAY_PTR) : \ (c16){0} \ @@ -94,7 +451,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(c32) \ }) #define wapp_c32_array_with_capacity(CAPACITY) ((C32Array){.items = (c32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(c32)}) -#define wapp_c32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((C32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(c32))) #define wapp_c32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_c32_array_pop(ARRAY_PTR) : \ (c32){0} \ @@ -106,7 +462,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(i8) \ }) #define wapp_i8_array_with_capacity(CAPACITY) ((I8Array){.items = (i8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i8)}) -#define wapp_i8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i8))) #define wapp_i8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_i8_array_pop(ARRAY_PTR) : \ (i8){0} \ @@ -118,7 +473,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(i16) \ }) #define wapp_i16_array_with_capacity(CAPACITY) ((I16Array){.items = (i16[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i16)}) -#define wapp_i16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i16))) #define wapp_i16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_i16_array_pop(ARRAY_PTR) : \ (i16){0} \ @@ -130,7 +484,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(i32) \ }) #define wapp_i32_array_with_capacity(CAPACITY) ((I32Array){.items = (i32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i32)}) -#define wapp_i32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i32))) #define wapp_i32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_i32_array_pop(ARRAY_PTR) : \ (i32){0} \ @@ -142,7 +495,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(i64) \ }) #define wapp_i64_array_with_capacity(CAPACITY) ((I64Array){.items = (i64[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(i64)}) -#define wapp_i64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((I64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(i64))) #define wapp_i64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_i64_array_pop(ARRAY_PTR) : \ (i64){0} \ @@ -154,7 +506,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(u8) \ }) #define wapp_u8_array_with_capacity(CAPACITY) ((U8Array){.items = (u8[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u8)}) -#define wapp_u8_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U8Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u8))) #define wapp_u8_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_u8_array_pop(ARRAY_PTR) : \ (u8){0} \ @@ -166,7 +517,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(u16) \ }) #define wapp_u16_array_with_capacity(CAPACITY) ((U16Array){.items = (u16[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u16)}) -#define wapp_u16_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U16Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u16))) #define wapp_u16_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_u16_array_pop(ARRAY_PTR) : \ (u16){0} \ @@ -178,7 +528,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(u32) \ }) #define wapp_u32_array_with_capacity(CAPACITY) ((U32Array){.items = (u32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u32)}) -#define wapp_u32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u32))) #define wapp_u32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_u32_array_pop(ARRAY_PTR) : \ (u32){0} \ @@ -190,7 +539,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(u64) \ }) #define wapp_u64_array_with_capacity(CAPACITY) ((U64Array){.items = (u64[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(u64)}) -#define wapp_u64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((U64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(u64))) #define wapp_u64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_u64_array_pop(ARRAY_PTR) : \ (u64){0} \ @@ -202,7 +550,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(f32) \ }) #define wapp_f32_array_with_capacity(CAPACITY) ((F32Array){.items = (f32[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(f32)}) -#define wapp_f32_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F32Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f32))) #define wapp_f32_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_f32_array_pop(ARRAY_PTR) : \ (f32){0} \ @@ -214,7 +561,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(f64) \ }) #define wapp_f64_array_with_capacity(CAPACITY) ((F64Array){.items = (f64[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(f64)}) -#define wapp_f64_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F64Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f64))) #define wapp_f64_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_f64_array_pop(ARRAY_PTR) : \ (f64){0} \ @@ -226,7 +572,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(f128) \ }) #define wapp_f128_array_with_capacity(CAPACITY) ((F128Array){.items = (f128[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(f128)}) -#define wapp_f128_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((F128Array *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(f128))) #define wapp_f128_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_f128_array_pop(ARRAY_PTR) : \ (f128){0} \ @@ -238,7 +583,6 @@ BEGIN_C_LINKAGE .item_size = sizeof(iptr) \ }) #define wapp_iptr_array_with_capacity(CAPACITY) ((IptrArray){.items = (iptr[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(iptr)}) -#define wapp_iptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((IptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(iptr))) #define wapp_iptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_iptr_array_pop(ARRAY_PTR) : \ (iptr){0} \ @@ -250,11 +594,11 @@ BEGIN_C_LINKAGE .item_size = sizeof(uptr) \ }) #define wapp_uptr_array_with_capacity(CAPACITY) ((UptrArray){.items = (uptr[CAPACITY]){0}, .count = 0, .capacity = CAPACITY, .item_size = sizeof(uptr)}) -#define wapp_uptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((UptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(uptr))) #define wapp_uptr_array_pop(ARRAY_PTR) (ARRAY_PTR != NULL && (ARRAY_PTR)->count > 0 ? \ *_uptr_array_pop(ARRAY_PTR) : \ (uptr){0} \ ) +#endif // !WAPP_PLATFORM_CPP typedef struct str8 Str8; -- 2.39.5 From 92a231d3ce9555e6799945b5efe075c7d977b889 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 10 Aug 2025 18:32:55 +0100 Subject: [PATCH 4/7] Add missing c linkage --- src/core/os/shell/commander/commander.h | 8 ++++++++ src/core/os/shell/commander/commander_output.h | 8 ++++++++ src/core/os/shell/termcolour/termcolour.h | 8 ++++++++ src/core/os/shell/termcolour/terminal_colours.h | 8 ++++++++ src/core/os/shell/utils/shell_utils.h | 8 ++++++++ 5 files changed, 40 insertions(+) diff --git a/src/core/os/shell/commander/commander.h b/src/core/os/shell/commander/commander.h index f7e6e0c..3a54657 100644 --- a/src/core/os/shell/commander/commander.h +++ b/src/core/os/shell/commander/commander.h @@ -11,10 +11,18 @@ #include #include +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #define CMD_NO_EXIT(ERR) ((CMDResult){.exited = false, .exit_code = EXIT_FAILURE, .error = ERR}) CMDResult wapp_shell_commander_execute(CMDOutHandling out_handling, Str8 *out_buf, const Str8List *cmd); external CMDError get_output_status(FILE *fp, i32 *status_out); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !COMMANDER_H diff --git a/src/core/os/shell/commander/commander_output.h b/src/core/os/shell/commander/commander_output.h index d24577a..32b2b12 100644 --- a/src/core/os/shell/commander/commander_output.h +++ b/src/core/os/shell/commander/commander_output.h @@ -7,6 +7,10 @@ #include "../../../../common/platform/platform.h" #include +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + typedef enum { SHELL_OUTPUT_DISCARD, SHELL_OUTPUT_PRINT, @@ -34,4 +38,8 @@ struct commander_result { #endif // !WAPP_PLATFORM_WINDOWS }; +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !COMMANDER_OUTPUT_H diff --git a/src/core/os/shell/termcolour/termcolour.h b/src/core/os/shell/termcolour/termcolour.h index e443532..e720092 100644 --- a/src/core/os/shell/termcolour/termcolour.h +++ b/src/core/os/shell/termcolour/termcolour.h @@ -8,9 +8,17 @@ #include "../../../../common/platform/platform.h" #include "../../../../primitives/strings/str8/str8.h" +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + void wapp_shell_termcolour_print_text(Str8RO *text, TerminalColour colour); void wapp_shell_termcolour_clear_colour(void); external void print_coloured_text(Str8RO *text, TerminalColour colour); +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !TERM_COLOUR_H diff --git a/src/core/os/shell/termcolour/terminal_colours.h b/src/core/os/shell/termcolour/terminal_colours.h index 92f468b..197b234 100644 --- a/src/core/os/shell/termcolour/terminal_colours.h +++ b/src/core/os/shell/termcolour/terminal_colours.h @@ -5,6 +5,10 @@ #include "../../../../common/platform/platform.h" +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + typedef enum { WAPP_TERM_COLOUR_FG_BLACK, WAPP_TERM_COLOUR_FG_RED, @@ -27,4 +31,8 @@ typedef enum { COUNT_TERM_COLOUR, } TerminalColour; +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !TERMINAL_COLOURS_H diff --git a/src/core/os/shell/utils/shell_utils.h b/src/core/os/shell/utils/shell_utils.h index 315d21d..b5899d7 100644 --- a/src/core/os/shell/utils/shell_utils.h +++ b/src/core/os/shell/utils/shell_utils.h @@ -6,6 +6,10 @@ #include "../../../../common/platform/platform.h" #include +#ifdef WAPP_PLATFORM_CPP +BEGIN_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #ifdef WAPP_PLATFORM_WINDOWS #define wapp_shell_utils_popen _popen #define wapp_shell_utils_pclose _pclose @@ -14,4 +18,8 @@ #define wapp_shell_utils_pclose pclose #endif /* ifdef WAPP_PLATFORM_WINDOWS */ +#ifdef WAPP_PLATFORM_CPP +END_C_LINKAGE +#endif // !WAPP_PLATFORM_CPP + #endif // !SHELL_UTILS_H -- 2.39.5 From 2a085370ecf8e0103a8360958b6a432946ff11ea Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 10 Aug 2025 18:52:22 +0100 Subject: [PATCH 5/7] Set C++ tests --- Makefile | 43 +- codegen/array/snippets/stack_array_cpp | 2 +- .../array/snippets/stack_capacity_array_cpp | 2 +- src/common/aliases/aliases.h | 8 +- .../os/shell/termcolour/terminal_colours.h | 1 + src/core/os/shell/utils/shell_utils.h | 1 + src/primitives/array/array.h | 80 +-- src/primitives/mem_allocator/mem_allocator.h | 7 + src/primitives/strings/str8/str8.h | 21 +- src/testing/tester/tester.h | 5 + tests/allocator/test_allocator.cc | 18 + tests/arena/test_arena.cc | 112 ++++ tests/array/test_i32_array.cc | 274 ++++++++ tests/cpath/test_cpath.cc | 205 ++++++ tests/shell_commander/test_shell_commander.cc | 70 ++ tests/str8/test_str8.cc | 614 ++++++++++++++++++ tests/str8/test_str8_list.cc | 271 ++++++++ tests/wapptest.cc | 77 +++ 18 files changed, 1751 insertions(+), 60 deletions(-) create mode 100644 tests/allocator/test_allocator.cc create mode 100644 tests/arena/test_arena.cc create mode 100644 tests/array/test_i32_array.cc create mode 100644 tests/cpath/test_cpath.cc create mode 100644 tests/shell_commander/test_shell_commander.cc create mode 100644 tests/str8/test_str8.cc create mode 100644 tests/str8/test_str8_list.cc create mode 100644 tests/wapptest.cc diff --git a/Makefile b/Makefile index a9855d2..53085e7 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,30 @@ CC = clang +CXX = clang++ +AR = ar BUILD_TYPE = debug CFLAGS = -Wall -Wextra -Werror -pedantic -std=gnu11 -Isrc -LIBFLAGS = -fPIC -shared +CXXFLAGS = -Wall -Wextra -Werror -pedantic -std=gnu++11 -Isrc +LIBFLAGS = -fPIC KERNEL = $(shell uname -s) MACHINE = $(shell uname -m) PLATFORM = $(KERNEL)_$(MACHINE) TEST_INCLUDE = -Isrc $(shell find tests -type d | xargs -I{} echo -n "-I{} ") -TEST_SRC = src/wapp.c $(shell find tests -type f -name "*.c" | xargs -I{} echo -n "{} ") +TEST_SRC = $(shell find tests -type f -name "*.c" | xargs -I{} echo -n "{} ") +TEST_C_SRC = src/wapp.c $(TEST_SRC) +TEST_CXX_SRC = $(shell find tests -type f -name "*.cc" | xargs -I{} echo -n "{} ") BUILD_DIR = libwapp-build/$(PLATFORM)-$(BUILD_TYPE) -LIB_OUT = $(BUILD_DIR)/libwapp.so -TEST_OUT = $(BUILD_DIR)/wapptest +LIB_NAME = wapp +OBJ_OUT = $(BUILD_DIR)/$(LIB_NAME).o +LIB_OUT = $(BUILD_DIR)/lib$(LIB_NAME).a +TEST_C_OUT = $(BUILD_DIR)/wapptest +TEST_CXX_OUT = $(BUILD_DIR)/wapptestcc ifeq ($(BUILD_TYPE),debug) CFLAGS += -g -fsanitize=address,undefined -DWAPP_DEBUG_ASSERT + CXXFLAGS += -g -fsanitize=address,undefined -DWAPP_DEBUG_ASSERT else ifeq ($(BUILD_TYPE),release) CFLAGS += -O3 + CXXFLAGS += -O3 else $(error Invalid BUILD type '$(BUILD_TYPE)'. Use 'debug' or 'release') endif @@ -26,7 +36,7 @@ endif .PHONY: all clean builddir build-test run-test codegen build-lib full prng testing uuid core primitives -all: clean builddir codegen run-test full +all: clean builddir codegen run-c-test full run-cc-test clean: @rm -rf $(BUILD_DIR) @@ -34,18 +44,29 @@ clean: builddir: @mkdir -p $(BUILD_DIR) -build-test: - $(CC) $(CFLAGS) $(TEST_INCLUDE) $(TEST_SRC) -o $(TEST_OUT) +build-c-test: + $(CC) $(CFLAGS) $(TEST_INCLUDE) $(TEST_C_SRC) -o $(TEST_C_OUT) -run-test: build-test - @$(TEST_OUT) - @rm $(TEST_OUT) +run-c-test: build-c-test + @echo -e "\n\033[34;1mRUNNING C TESTS\033[0m" + @$(TEST_C_OUT) + @rm $(TEST_C_OUT) + +build-cc-test: + $(CXX) $(CXXFLAGS) $(TEST_INCLUDE) $(TEST_CXX_SRC) $(LIB_OUT) -o $(TEST_CXX_OUT) + +run-cc-test: build-cc-test + @echo -e "\n\033[34;1mRUNNING C++ TESTS\033[0m" + @export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(BUILD_DIR) && $(TEST_CXX_OUT) + @rm $(TEST_CXX_OUT) codegen: python3 -m codegen build-lib: - $(CC) $(CFLAGS) $(LIBFLAGS) $(LIB_SRC) -o $(LIB_OUT) + $(CC) -c $(CFLAGS) $(LIBFLAGS) $(LIB_SRC) -o $(OBJ_OUT) + $(AR) r $(LIB_OUT) $(OBJ_OUT) + @rm $(OBJ_OUT) full: LIB_SRC = src/wapp.c full: build-lib diff --git a/codegen/array/snippets/stack_array_cpp b/codegen/array/snippets/stack_array_cpp index 938bccc..fb9272d 100644 --- a/codegen/array/snippets/stack_array_cpp +++ b/codegen/array/snippets/stack_array_cpp @@ -1,4 +1,4 @@ -([]() {{ \ +([&]() {{ \ persistent {T} buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count({T}, __VA_ARGS__) * 2)] = {{__VA_ARGS__}}; \ return {ArrayType}{{ \ buf, \ diff --git a/codegen/array/snippets/stack_capacity_array_cpp b/codegen/array/snippets/stack_capacity_array_cpp index 7ef0cb8..8d76812 100644 --- a/codegen/array/snippets/stack_capacity_array_cpp +++ b/codegen/array/snippets/stack_capacity_array_cpp @@ -1,4 +1,4 @@ -([]() {{ \ +([&]() {{ \ persistent {T} buf[CAPACITY] = {{}}; \ return {ArrayType}{{buf, 0, CAPACITY, sizeof({T})}}; \ }}()) diff --git a/src/common/aliases/aliases.h b/src/common/aliases/aliases.h index 9792f80..0534834 100644 --- a/src/common/aliases/aliases.h +++ b/src/common/aliases/aliases.h @@ -30,13 +30,17 @@ #define b32 uint32_t +#ifndef WAPP_PLATFORM_CPP + #ifndef false #define false (b32)0 -#endif +#endif // !false #ifndef true #define true (b32)1 -#endif +#endif // !true + +#endif // !WAPP_PLATFORM_CPP #define i8 int8_t #define i16 int16_t diff --git a/src/core/os/shell/termcolour/terminal_colours.h b/src/core/os/shell/termcolour/terminal_colours.h index 197b234..c2c7ff2 100644 --- a/src/core/os/shell/termcolour/terminal_colours.h +++ b/src/core/os/shell/termcolour/terminal_colours.h @@ -3,6 +3,7 @@ #ifndef TERMINAL_COLOURS_H #define TERMINAL_COLOURS_H +#include "../../../../common/aliases/aliases.h" #include "../../../../common/platform/platform.h" #ifdef WAPP_PLATFORM_CPP diff --git a/src/core/os/shell/utils/shell_utils.h b/src/core/os/shell/utils/shell_utils.h index b5899d7..7148ca7 100644 --- a/src/core/os/shell/utils/shell_utils.h +++ b/src/core/os/shell/utils/shell_utils.h @@ -3,6 +3,7 @@ #ifndef SHELL_UTILS_H #define SHELL_UTILS_H +#include "../../../../common/aliases/aliases.h" #include "../../../../common/platform/platform.h" #include diff --git a/src/primitives/array/array.h b/src/primitives/array/array.h index e4a1414..a786faa 100644 --- a/src/primitives/array/array.h +++ b/src/primitives/array/array.h @@ -37,7 +37,7 @@ BEGIN_C_LINKAGE #define wapp_uptr_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY) ((UptrArray *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(uptr))) #ifdef WAPP_PLATFORM_CPP -#define wapp_str8_array(...) ([]() { \ +#define wapp_str8_array(...) ([&]() { \ persistent Str8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(Str8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return Str8Array{ \ buf, \ @@ -46,7 +46,7 @@ BEGIN_C_LINKAGE sizeof(Str8) \ }; \ }()) -#define wapp_str8_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_str8_array_with_capacity(CAPACITY) ([&]() { \ persistent Str8 buf[CAPACITY] = {}; \ return Str8Array{buf, 0, CAPACITY, sizeof(Str8)}; \ }()) @@ -54,7 +54,7 @@ BEGIN_C_LINKAGE *_str8_array_pop(ARRAY_PTR) : \ Str8{} \ ) -#define wapp_void_ptr_array(...) ([]() { \ +#define wapp_void_ptr_array(...) ([&]() { \ persistent void * buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(void *, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return VoidPArray{ \ buf, \ @@ -63,7 +63,7 @@ BEGIN_C_LINKAGE sizeof(void *) \ }; \ }()) -#define wapp_void_ptr_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_void_ptr_array_with_capacity(CAPACITY) ([&]() { \ persistent void * buf[CAPACITY] = {}; \ return VoidPArray{buf, 0, CAPACITY, sizeof(void *)}; \ }()) @@ -71,7 +71,7 @@ BEGIN_C_LINKAGE *_void_ptr_array_pop(ARRAY_PTR) : \ void *{} \ ) -#define wapp_b32_array(...) ([]() { \ +#define wapp_b32_array(...) ([&]() { \ persistent b32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(b32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return B32Array{ \ buf, \ @@ -80,7 +80,7 @@ BEGIN_C_LINKAGE sizeof(b32) \ }; \ }()) -#define wapp_b32_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_b32_array_with_capacity(CAPACITY) ([&]() { \ persistent b32 buf[CAPACITY] = {}; \ return B32Array{buf, 0, CAPACITY, sizeof(b32)}; \ }()) @@ -88,7 +88,7 @@ BEGIN_C_LINKAGE *_b32_array_pop(ARRAY_PTR) : \ b32{} \ ) -#define wapp_char_array(...) ([]() { \ +#define wapp_char_array(...) ([&]() { \ persistent char buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(char, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return CharArray{ \ buf, \ @@ -97,7 +97,7 @@ BEGIN_C_LINKAGE sizeof(char) \ }; \ }()) -#define wapp_char_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_char_array_with_capacity(CAPACITY) ([&]() { \ persistent char buf[CAPACITY] = {}; \ return CharArray{buf, 0, CAPACITY, sizeof(char)}; \ }()) @@ -105,7 +105,7 @@ BEGIN_C_LINKAGE *_char_array_pop(ARRAY_PTR) : \ char{} \ ) -#define wapp_c8_array(...) ([]() { \ +#define wapp_c8_array(...) ([&]() { \ persistent c8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return C8Array{ \ buf, \ @@ -114,7 +114,7 @@ BEGIN_C_LINKAGE sizeof(c8) \ }; \ }()) -#define wapp_c8_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_c8_array_with_capacity(CAPACITY) ([&]() { \ persistent c8 buf[CAPACITY] = {}; \ return C8Array{buf, 0, CAPACITY, sizeof(c8)}; \ }()) @@ -122,7 +122,7 @@ BEGIN_C_LINKAGE *_c8_array_pop(ARRAY_PTR) : \ c8{} \ ) -#define wapp_c16_array(...) ([]() { \ +#define wapp_c16_array(...) ([&]() { \ persistent c16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return C16Array{ \ buf, \ @@ -131,7 +131,7 @@ BEGIN_C_LINKAGE sizeof(c16) \ }; \ }()) -#define wapp_c16_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_c16_array_with_capacity(CAPACITY) ([&]() { \ persistent c16 buf[CAPACITY] = {}; \ return C16Array{buf, 0, CAPACITY, sizeof(c16)}; \ }()) @@ -139,7 +139,7 @@ BEGIN_C_LINKAGE *_c16_array_pop(ARRAY_PTR) : \ c16{} \ ) -#define wapp_c32_array(...) ([]() { \ +#define wapp_c32_array(...) ([&]() { \ persistent c32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(c32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return C32Array{ \ buf, \ @@ -148,7 +148,7 @@ BEGIN_C_LINKAGE sizeof(c32) \ }; \ }()) -#define wapp_c32_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_c32_array_with_capacity(CAPACITY) ([&]() { \ persistent c32 buf[CAPACITY] = {}; \ return C32Array{buf, 0, CAPACITY, sizeof(c32)}; \ }()) @@ -156,7 +156,7 @@ BEGIN_C_LINKAGE *_c32_array_pop(ARRAY_PTR) : \ c32{} \ ) -#define wapp_i8_array(...) ([]() { \ +#define wapp_i8_array(...) ([&]() { \ persistent i8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return I8Array{ \ buf, \ @@ -165,7 +165,7 @@ BEGIN_C_LINKAGE sizeof(i8) \ }; \ }()) -#define wapp_i8_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_i8_array_with_capacity(CAPACITY) ([&]() { \ persistent i8 buf[CAPACITY] = {}; \ return I8Array{buf, 0, CAPACITY, sizeof(i8)}; \ }()) @@ -173,7 +173,7 @@ BEGIN_C_LINKAGE *_i8_array_pop(ARRAY_PTR) : \ i8{} \ ) -#define wapp_i16_array(...) ([]() { \ +#define wapp_i16_array(...) ([&]() { \ persistent i16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return I16Array{ \ buf, \ @@ -182,7 +182,7 @@ BEGIN_C_LINKAGE sizeof(i16) \ }; \ }()) -#define wapp_i16_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_i16_array_with_capacity(CAPACITY) ([&]() { \ persistent i16 buf[CAPACITY] = {}; \ return I16Array{buf, 0, CAPACITY, sizeof(i16)}; \ }()) @@ -190,7 +190,7 @@ BEGIN_C_LINKAGE *_i16_array_pop(ARRAY_PTR) : \ i16{} \ ) -#define wapp_i32_array(...) ([]() { \ +#define wapp_i32_array(...) ([&]() { \ persistent i32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return I32Array{ \ buf, \ @@ -199,7 +199,7 @@ BEGIN_C_LINKAGE sizeof(i32) \ }; \ }()) -#define wapp_i32_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_i32_array_with_capacity(CAPACITY) ([&]() { \ persistent i32 buf[CAPACITY] = {}; \ return I32Array{buf, 0, CAPACITY, sizeof(i32)}; \ }()) @@ -207,7 +207,7 @@ BEGIN_C_LINKAGE *_i32_array_pop(ARRAY_PTR) : \ i32{} \ ) -#define wapp_i64_array(...) ([]() { \ +#define wapp_i64_array(...) ([&]() { \ persistent i64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(i64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return I64Array{ \ buf, \ @@ -216,7 +216,7 @@ BEGIN_C_LINKAGE sizeof(i64) \ }; \ }()) -#define wapp_i64_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_i64_array_with_capacity(CAPACITY) ([&]() { \ persistent i64 buf[CAPACITY] = {}; \ return I64Array{buf, 0, CAPACITY, sizeof(i64)}; \ }()) @@ -224,7 +224,7 @@ BEGIN_C_LINKAGE *_i64_array_pop(ARRAY_PTR) : \ i64{} \ ) -#define wapp_u8_array(...) ([]() { \ +#define wapp_u8_array(...) ([&]() { \ persistent u8 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u8, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return U8Array{ \ buf, \ @@ -233,7 +233,7 @@ BEGIN_C_LINKAGE sizeof(u8) \ }; \ }()) -#define wapp_u8_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_u8_array_with_capacity(CAPACITY) ([&]() { \ persistent u8 buf[CAPACITY] = {}; \ return U8Array{buf, 0, CAPACITY, sizeof(u8)}; \ }()) @@ -241,7 +241,7 @@ BEGIN_C_LINKAGE *_u8_array_pop(ARRAY_PTR) : \ u8{} \ ) -#define wapp_u16_array(...) ([]() { \ +#define wapp_u16_array(...) ([&]() { \ persistent u16 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u16, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return U16Array{ \ buf, \ @@ -250,7 +250,7 @@ BEGIN_C_LINKAGE sizeof(u16) \ }; \ }()) -#define wapp_u16_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_u16_array_with_capacity(CAPACITY) ([&]() { \ persistent u16 buf[CAPACITY] = {}; \ return U16Array{buf, 0, CAPACITY, sizeof(u16)}; \ }()) @@ -258,7 +258,7 @@ BEGIN_C_LINKAGE *_u16_array_pop(ARRAY_PTR) : \ u16{} \ ) -#define wapp_u32_array(...) ([]() { \ +#define wapp_u32_array(...) ([&]() { \ persistent u32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return U32Array{ \ buf, \ @@ -267,7 +267,7 @@ BEGIN_C_LINKAGE sizeof(u32) \ }; \ }()) -#define wapp_u32_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_u32_array_with_capacity(CAPACITY) ([&]() { \ persistent u32 buf[CAPACITY] = {}; \ return U32Array{buf, 0, CAPACITY, sizeof(u32)}; \ }()) @@ -275,7 +275,7 @@ BEGIN_C_LINKAGE *_u32_array_pop(ARRAY_PTR) : \ u32{} \ ) -#define wapp_u64_array(...) ([]() { \ +#define wapp_u64_array(...) ([&]() { \ persistent u64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(u64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return U64Array{ \ buf, \ @@ -284,7 +284,7 @@ BEGIN_C_LINKAGE sizeof(u64) \ }; \ }()) -#define wapp_u64_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_u64_array_with_capacity(CAPACITY) ([&]() { \ persistent u64 buf[CAPACITY] = {}; \ return U64Array{buf, 0, CAPACITY, sizeof(u64)}; \ }()) @@ -292,7 +292,7 @@ BEGIN_C_LINKAGE *_u64_array_pop(ARRAY_PTR) : \ u64{} \ ) -#define wapp_f32_array(...) ([]() { \ +#define wapp_f32_array(...) ([&]() { \ persistent f32 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f32, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return F32Array{ \ buf, \ @@ -301,7 +301,7 @@ BEGIN_C_LINKAGE sizeof(f32) \ }; \ }()) -#define wapp_f32_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_f32_array_with_capacity(CAPACITY) ([&]() { \ persistent f32 buf[CAPACITY] = {}; \ return F32Array{buf, 0, CAPACITY, sizeof(f32)}; \ }()) @@ -309,7 +309,7 @@ BEGIN_C_LINKAGE *_f32_array_pop(ARRAY_PTR) : \ f32{} \ ) -#define wapp_f64_array(...) ([]() { \ +#define wapp_f64_array(...) ([&]() { \ persistent f64 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f64, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return F64Array{ \ buf, \ @@ -318,7 +318,7 @@ BEGIN_C_LINKAGE sizeof(f64) \ }; \ }()) -#define wapp_f64_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_f64_array_with_capacity(CAPACITY) ([&]() { \ persistent f64 buf[CAPACITY] = {}; \ return F64Array{buf, 0, CAPACITY, sizeof(f64)}; \ }()) @@ -326,7 +326,7 @@ BEGIN_C_LINKAGE *_f64_array_pop(ARRAY_PTR) : \ f64{} \ ) -#define wapp_f128_array(...) ([]() { \ +#define wapp_f128_array(...) ([&]() { \ persistent f128 buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(f128, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return F128Array{ \ buf, \ @@ -335,7 +335,7 @@ BEGIN_C_LINKAGE sizeof(f128) \ }; \ }()) -#define wapp_f128_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_f128_array_with_capacity(CAPACITY) ([&]() { \ persistent f128 buf[CAPACITY] = {}; \ return F128Array{buf, 0, CAPACITY, sizeof(f128)}; \ }()) @@ -343,7 +343,7 @@ BEGIN_C_LINKAGE *_f128_array_pop(ARRAY_PTR) : \ f128{} \ ) -#define wapp_iptr_array(...) ([]() { \ +#define wapp_iptr_array(...) ([&]() { \ persistent iptr buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(iptr, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return IptrArray{ \ buf, \ @@ -352,7 +352,7 @@ BEGIN_C_LINKAGE sizeof(iptr) \ }; \ }()) -#define wapp_iptr_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_iptr_array_with_capacity(CAPACITY) ([&]() { \ persistent iptr buf[CAPACITY] = {}; \ return IptrArray{buf, 0, CAPACITY, sizeof(iptr)}; \ }()) @@ -360,7 +360,7 @@ BEGIN_C_LINKAGE *_iptr_array_pop(ARRAY_PTR) : \ iptr{} \ ) -#define wapp_uptr_array(...) ([]() { \ +#define wapp_uptr_array(...) ([&]() { \ persistent uptr buf[wapp_misc_utils_u64_round_up_pow2(wapp_misc_utils_va_args_count(uptr, __VA_ARGS__) * 2)] = {__VA_ARGS__}; \ return UptrArray{ \ buf, \ @@ -369,7 +369,7 @@ BEGIN_C_LINKAGE sizeof(uptr) \ }; \ }()) -#define wapp_uptr_array_with_capacity(CAPACITY) ([]() { \ +#define wapp_uptr_array_with_capacity(CAPACITY) ([&]() { \ persistent uptr buf[CAPACITY] = {}; \ return UptrArray{buf, 0, CAPACITY, sizeof(uptr)}; \ }()) diff --git a/src/primitives/mem_allocator/mem_allocator.h b/src/primitives/mem_allocator/mem_allocator.h index 41877e3..389e158 100644 --- a/src/primitives/mem_allocator/mem_allocator.h +++ b/src/primitives/mem_allocator/mem_allocator.h @@ -27,7 +27,14 @@ struct allocator { MemFreeFunc *free; }; +#ifdef WAPP_PLATFORM_CPP +#define wapp_mem_allocator_invalid(ALLOCATOR) ([&]() { \ + Allocator alloc{}; \ + return memcmp(ALLOCATOR, &alloc, sizeof(Allocator)) == 0; \ +}()) +#else #define wapp_mem_allocator_invalid(ALLOCATOR) (memcmp(ALLOCATOR, &((Allocator){0}), sizeof(Allocator)) == 0) +#endif // !WAPP_PLATFORM_CPP void *wapp_mem_allocator_alloc(const Allocator *allocator, u64 size); void *wapp_mem_allocator_alloc_aligned(const Allocator *allocator, u64 size, u64 alignment); diff --git a/src/primitives/strings/str8/str8.h b/src/primitives/strings/str8/str8.h index d66c337..ea07f82 100644 --- a/src/primitives/strings/str8/str8.h +++ b/src/primitives/strings/str8/str8.h @@ -36,14 +36,14 @@ typedef const Str8 Str8RO; #ifdef WAPP_PLATFORM_CPP // Uses a lambda to achieve the same behaviour achieved by the C macro -#define wapp_str8_buf(CAPACITY) ([](){ \ - persistent c8 buf[CAPACITY] = {}; \ - memset(buf, 0, CAPACITY); \ - return Str8{CAPACITY, 0, buf}; \ +#define wapp_str8_buf(CAPACITY) ([&](){ \ + persistent c8 buf[CAPACITY] = {}; \ + memset(buf, 0, CAPACITY); \ + return Str8{CAPACITY, 0, buf}; \ }()) // Uses a lambda to achieve the same behaviour achieved by the C macro -#define wapp_str8_lit(STRING) ([]() { \ +#define wapp_str8_lit(STRING) ([&]() { \ persistent c8 buf[sizeof(STRING) * 2] = {}; \ memcpy(buf, STRING, sizeof(STRING)); \ return Str8{(sizeof(STRING) - 1) * 2, sizeof(STRING) - 1, buf}; \ @@ -115,8 +115,19 @@ Str8 *wapp_str8_join(const Allocator *allocator, const Str8List *list, Str8R /** * Str8 list utilities */ +#ifdef WAPP_PLATFORM_CPP +#define wapp_str8_node_from_cstr(STRING) wapp_str8_list_node([&]() { \ + persistent Str8 str = wapp_str8_lit(STRING); \ + return &str; \ +}()) +#define wapp_str8_node_from_str8(STRING) wapp_str8_list_node([&]() { \ + persistent Str8 str = STRING; \ + return &str; \ +}()) +#else #define wapp_str8_node_from_cstr(STRING) wapp_str8_list_node(&wapp_str8_lit(STRING)) #define wapp_str8_node_from_str8(STRING) wapp_str8_list_node(&(STRING)) +#endif // !WAPP_PLATFORM_CPP u64 wapp_str8_list_total_size(const Str8List *list); #ifdef WAPP_PLATFORM_CPP diff --git a/src/testing/tester/tester.h b/src/testing/tester/tester.h index 60d41f3..c8dace5 100644 --- a/src/testing/tester/tester.h +++ b/src/testing/tester/tester.h @@ -12,7 +12,12 @@ BEGIN_C_LINKAGE #endif // !WAPP_PLATFORM_CPP +#ifdef WAPP_PLATFORM_CPP +#define wapp_tester_result(PASSED) (TestFuncResult{wapp_str8_lit_ro(__func__), PASSED}) +#else #define wapp_tester_result(PASSED) ((TestFuncResult){.name = wapp_str8_lit_ro(__func__), .passed = PASSED}) +#endif // !WAPP_PLATFORM_CPP + #define wapp_tester_run_tests(...) run_tests(__VA_ARGS__, NULL) typedef struct test_func_result TestFuncResult; diff --git a/tests/allocator/test_allocator.cc b/tests/allocator/test_allocator.cc new file mode 100644 index 0000000..ffce876 --- /dev/null +++ b/tests/allocator/test_allocator.cc @@ -0,0 +1,18 @@ +#include "test_allocator.h" +#include "wapp.h" +#include +#include + +TestFuncResult test_arena_allocator(void) { + Allocator allocator = wapp_mem_arena_allocator_init(4096); + bool result = allocator.obj != nullptr && allocator.alloc != nullptr && + allocator.alloc_aligned != nullptr && + allocator.realloc != nullptr && allocator.realloc_aligned != nullptr && + allocator.free == nullptr; + void *ptr = wapp_mem_allocator_alloc(&allocator, 20); + result = result && (ptr != nullptr); + + wapp_mem_arena_allocator_destroy(&allocator); + + return wapp_tester_result(result); +} diff --git a/tests/arena/test_arena.cc b/tests/arena/test_arena.cc new file mode 100644 index 0000000..b934f98 --- /dev/null +++ b/tests/arena/test_arena.cc @@ -0,0 +1,112 @@ +#include "test_arena.h" +#include "wapp.h" +#include +#include + +#define ARENA_CAPACITY KB(16) + +internal Arena *arena = nullptr; +internal i32 count = 20; +internal i32 *array = nullptr; + +TestFuncResult test_arena_init(void) { + bool result = wapp_mem_arena_init(&arena, ARENA_CAPACITY); + + return wapp_tester_result(result); +} + +TestFuncResult test_arena_init_succeeds_when_reserving_very_large_size(void) { + Arena *large_arena = nullptr; + u64 capacity = GB(512); + bool result = wapp_mem_arena_init(&large_arena, capacity); + if (result) { + wapp_mem_arena_destroy(&large_arena); + } + + return wapp_tester_result(result); +} + +TestFuncResult test_arena_alloc_succeeds_when_within_capacity(void) { + array = (i32 *)wapp_mem_arena_alloc(arena, count * sizeof(i32)); + bool result = array != nullptr; + + for (i32 i = 0; i < count; ++i) { + array[i] = i * 10; + } + + return wapp_tester_result(result); +} + +TestFuncResult test_arena_alloc_fails_when_over_capacity(void) { + u8 *bytes = (u8 *)wapp_mem_arena_alloc(arena, ARENA_CAPACITY * 2); + bool result = bytes == nullptr; + + return wapp_tester_result(result); +} + +TestFuncResult test_arena_realloc_bigger_size(void) { + u64 old_count = 10; + u64 new_count = 20; + i32 *bytes = (i32 *)wapp_mem_arena_alloc(arena, old_count * sizeof(i32)); + + for (u64 i = 0; i < old_count; ++i) { + bytes[i] = (i32)i; + } + + i32 *new_bytes = (i32 *)wapp_mem_arena_realloc(arena, bytes, old_count * sizeof(i32), new_count * sizeof(i32)); + if (!new_bytes) { + return wapp_tester_result(false); + } + + for (u64 i = 0; i < new_count; ++i) { + if (i < old_count && new_bytes[i] != bytes[i]) { + return wapp_tester_result(false); + } + } + + return wapp_tester_result(true); +} + +TestFuncResult test_arena_realloc_smaller_size(void) { + u64 old_count = 10; + u64 new_count = 5; + i32 *bytes = (i32 *)wapp_mem_arena_alloc(arena, old_count * sizeof(i32)); + + for (u64 i = 0; i < old_count; ++i) { + bytes[i] = (i32)i; + } + + i32 *new_bytes = (i32 *)wapp_mem_arena_realloc(arena, bytes, old_count * sizeof(i32), new_count * sizeof(i32)); + if (!new_bytes) { + return wapp_tester_result(false); + } + + for (u64 i = 0; i < new_count; ++i) { + if (i < new_count && new_bytes[i] != bytes[i]) { + return wapp_tester_result(false); + } + } + + return wapp_tester_result(true); +} + +TestFuncResult test_arena_clear(void) { + wapp_mem_arena_clear(arena); + bool result = true; + + for (i32 i = 0; i < count; ++i) { + if (array[i] != 0) { + result = false; + break; + } + } + + return wapp_tester_result(result); +} + +TestFuncResult test_arena_destroy(void) { + wapp_mem_arena_destroy(&arena); + bool result = arena == nullptr; + + return wapp_tester_result(result); +} diff --git a/tests/array/test_i32_array.cc b/tests/array/test_i32_array.cc new file mode 100644 index 0000000..2009984 --- /dev/null +++ b/tests/array/test_i32_array.cc @@ -0,0 +1,274 @@ +#include "test_i32_array.h" +#include "wapp.h" +#include + +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; + u64 index = 0; + bool running = true; + while (running) { + item = wapp_i32_array_get(&array, index); + result = result && item && (*item == (i32)(index + 1)); + + ++index; + running = index < count; + } + + 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; + u64 index = 0; + bool running = true; + while (running) { + item = wapp_i32_array_get(&array, index); + result = result && item && (*item == (i32)index); + + ++index; + running = index < count; + } + + 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; + u64 index = 0; + bool running = true; + while (running) { + i32 num = (i32)(index * 2); + wapp_i32_array_set(&array, index, &num); + item = wapp_i32_array_get(&array, index); + result = result && item && (*item == (i32)(index * 2)); + + ++index; + running = index < count; + } + + return wapp_tester_result(result); +} + +TestFuncResult test_i32_array_append_capped(void) { + bool result; + + I32Array array = wapp_i32_array_with_capacity(64); + i32 item1 = 10; + wapp_i32_array_append_capped(&array, &item1); + + result = array.count == 1; + i32 *item = wapp_i32_array_get(&array, 0); + result = result && item && *item == 10; + + array = wapp_i32_array(1); + i32 item2 = 10; + wapp_i32_array_append_capped(&array, &item2); + + 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; + + 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; + + u64 index = 0; + bool running = true; + while (running) { + result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(&dst1, index)); + + ++index; + running = index < expected_count; + } + + expected_count = 4; + wapp_i32_array_copy_capped(&src, &dst2); + result = result && dst2.count == expected_count; + + index = 0; + running = true; + while (running) { + result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(&dst2, index)); + + ++index; + running = index < expected_count; + } + + 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); + + i32 num = 10; + I32Array *arr_ptr = wapp_i32_array_append_alloc(&allocator, &array1, &num); + result = arr_ptr == &array1; + + u64 count = 4; + u64 index = 0; + bool running = true; + while (running) { + i32 num = (i32)index; + arr_ptr = wapp_i32_array_append_alloc(&allocator, &array2, &num); + + ++index; + running = index < count; + } + 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 = nullptr; + + u64 expected_count = 5; + array_ptr = wapp_i32_array_copy_alloc(&allocator, &src, &dst1); + result = array_ptr->count == expected_count && array_ptr == &dst1; + + u64 index = 0; + bool running = true; + while (running) { + result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(array_ptr, index)); + + ++index; + running = index < expected_count; + } + + expected_count = 5; + array_ptr = wapp_i32_array_copy_alloc(&allocator, &src, &dst2); + result = result && array_ptr->count == expected_count && array_ptr != &dst2; + + index = 0; + running = true; + while (running) { + result = result && (*wapp_i32_array_get(&src, index) == *wapp_i32_array_get(array_ptr, index)); + + ++index; + running = index < expected_count; + } + + wapp_mem_arena_allocator_destroy(&allocator); + + return wapp_tester_result(result); +} diff --git a/tests/cpath/test_cpath.cc b/tests/cpath/test_cpath.cc new file mode 100644 index 0000000..92cae3a --- /dev/null +++ b/tests/cpath/test_cpath.cc @@ -0,0 +1,205 @@ +#include "test_cpath.h" +#include "wapp.h" +#include +#include +#include + +#define MAIN_BUF_SIZE 4096 +#define TMP_BUF_SIZE 1024 + +TestFuncResult test_cpath_join_path(void) { + bool result; + + Str8 expected = wapp_str8_buf(MAIN_BUF_SIZE); + Str8 out = wapp_str8_buf(MAIN_BUF_SIZE); + Str8 tmp = wapp_str8_buf(TMP_BUF_SIZE); + + wapp_str8_format(&expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); + wapp_str8_format(&tmp, "%c", PATH_SEP); + + Str8List parts = {}; + + Str8Node tmp_node = wapp_str8_node_from_str8(tmp); + wapp_str8_list_push_back(&parts, &tmp_node); + + Str8Node home_node = wapp_str8_node_from_cstr("home"); + wapp_str8_list_push_back(&parts, &home_node); + + Str8Node user_node = wapp_str8_node_from_cstr("abdelrahman"); + wapp_str8_list_push_back(&parts, &user_node); + + Str8Node docs_node = wapp_str8_node_from_cstr("Documents"); + wapp_str8_list_push_back(&parts, &docs_node); + + wapp_cpath_join_path(&out, &parts); + result = wapp_str8_equal(&out, &expected); + + wapp_str8_list_pop_front(&parts); + + wapp_str8_format(&expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); + + wapp_cpath_join_path(&out, &parts); + result = result && wapp_str8_equal(&out, &expected); + + Str8RO str = wapp_str8_lit_ro("home"); + wapp_str8_concat_capped(&tmp, &str); + wapp_str8_list_pop_front(&parts); + + Str8Node tmp_node_2 = wapp_str8_node_from_str8(tmp); + wapp_str8_list_push_front(&parts, &tmp_node_2); + + wapp_str8_format(&expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); + + wapp_cpath_join_path(&out, &parts); + result = result && wapp_str8_equal(&out, &expected); + + wapp_str8_format(&tmp, "home%c", PATH_SEP); + wapp_str8_list_pop_front(&parts); + + Str8Node home_node_2 = wapp_str8_node_from_cstr("home"); + wapp_str8_list_push_front(&parts, &home_node_2); + + wapp_str8_format(&expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); + + wapp_cpath_join_path(&out, &parts); + result = result && wapp_str8_equal(&out, &expected); + + wapp_str8_list_empty(&parts); + + wapp_str8_format(&tmp, "%chome", PATH_SEP); + + Str8Node tmp_node_3 = wapp_str8_node_from_str8(tmp); + wapp_str8_list_push_back(&parts, &tmp_node_3); + + Str8Node empty_node = wapp_str8_node_from_cstr(""); + wapp_str8_list_push_back(&parts, &empty_node); + + wapp_str8_format(&expected, "%chome", PATH_SEP); + + wapp_cpath_join_path(&out, &parts); + result = result && wapp_str8_equal(&out, &expected); + + wapp_str8_list_pop_front(&parts); + + Str8Node empty_node_2 = wapp_str8_node_from_cstr(""); + wapp_str8_list_push_back(&parts, &empty_node_2); + + wapp_str8_format(&expected, "%s", ""); + + wapp_cpath_join_path(&out, &parts); + result = result && wapp_str8_equal(&out, &expected); + + wapp_str8_list_pop_back(&parts); + + Str8Node home_node_3 = wapp_str8_node_from_cstr("home"); + wapp_str8_list_push_back(&parts, &home_node_3); + + wapp_str8_copy_cstr_capped(&expected, "home"); + + wapp_cpath_join_path(&out, &parts); + result = result && wapp_str8_equal(&out, &expected); + + return wapp_tester_result(result); +} + +TestFuncResult test_cpath_dirname(void) { + Allocator arena = wapp_mem_arena_allocator_init(MB(8)); + if (wapp_mem_allocator_invalid(&arena)) { + return wapp_tester_result(false); + } + + bool result; + Str8 *output = nullptr; + + Str8 expected = wapp_str8_buf(MAIN_BUF_SIZE); + Str8 tmp = wapp_str8_buf(TMP_BUF_SIZE); + + // CASE 1 + wapp_str8_format(&tmp, "%c", PATH_SEP); + wapp_str8_format(&expected, "%c", PATH_SEP); + + output = wapp_cpath_dirname(&arena, &tmp); + result = output != nullptr && wapp_str8_equal(output, &expected); + + // CASE 2 + wapp_str8_format(&expected, "%s", "."); + + Str8 path = wapp_str8_lit("home"); + output = wapp_cpath_dirname(&arena, &path); + result = result && output != nullptr && wapp_str8_equal(output, &expected); + + // CASE 3 + path = wapp_str8_lit(""); + output = wapp_cpath_dirname(&arena, &path); + result = result && output != nullptr && wapp_str8_equal(output, &expected); + + // CASE 4 + wapp_str8_format(&tmp, "%chome%ctest", PATH_SEP, PATH_SEP); + wapp_str8_format(&expected, "%chome", PATH_SEP); + + output = wapp_cpath_dirname(&arena, &tmp); + result = result && output != nullptr && wapp_str8_equal(output, &expected); + + // CASE 5 + wapp_str8_format(&tmp, "%chome%ctest%c", PATH_SEP, PATH_SEP, PATH_SEP); + wapp_str8_format(&expected, "%chome", PATH_SEP); + + output = wapp_cpath_dirname(&arena, &tmp); + result = result && output != nullptr && wapp_str8_equal(output, &expected); + + wapp_mem_arena_allocator_destroy(&arena); + + return wapp_tester_result(result); +} + +TestFuncResult test_cpath_dirup(void) { + Allocator arena = wapp_mem_arena_allocator_init(MB(8)); + if (wapp_mem_allocator_invalid(&arena)) { + return wapp_tester_result(false); + } + + bool result; + Str8 *output = nullptr; + + Str8 expected = wapp_str8_buf(MAIN_BUF_SIZE); + Str8 tmp = wapp_str8_buf(TMP_BUF_SIZE); + + // CASE 1 + wapp_str8_format(&tmp, "%c", PATH_SEP); + wapp_str8_format(&expected, "%c", PATH_SEP); + + output = wapp_cpath_dirup(&arena, &tmp, 3); + result = output != nullptr && wapp_str8_equal(output, &expected); + + // CASE 2 + wapp_str8_format(&tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); + wapp_str8_format(&expected, "%c", PATH_SEP); + + output = wapp_cpath_dirup(&arena, &tmp, 3); + result = result && output != nullptr && wapp_str8_equal(output, &expected); + + // CASE 3 + wapp_str8_format(&tmp, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); + wapp_str8_copy_cstr_capped(&expected, "."); + + output = wapp_cpath_dirup(&arena, &tmp, 3); + result = result && output != nullptr && wapp_str8_equal(output, &expected); + + // CASE 4 + wapp_str8_format(&tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); + wapp_str8_format(&expected, "%chome", PATH_SEP); + + output = wapp_cpath_dirup(&arena, &tmp, 2); + result = result && output != nullptr && wapp_str8_equal(output, &expected); + + // CASE 5 + wapp_str8_format(&tmp, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); + wapp_str8_copy_cstr_capped(&expected, "home"); + + output = wapp_cpath_dirup(&arena, &tmp, 2); + result = result && output != nullptr && wapp_str8_equal(output, &expected); + + wapp_mem_arena_allocator_destroy(&arena); + + return wapp_tester_result(result); +} diff --git a/tests/shell_commander/test_shell_commander.cc b/tests/shell_commander/test_shell_commander.cc new file mode 100644 index 0000000..2594055 --- /dev/null +++ b/tests/shell_commander/test_shell_commander.cc @@ -0,0 +1,70 @@ +#include "test_shell_commander.h" +#include "wapp.h" +#include +#include +#include +#include + +TestFuncResult test_commander_cmd_success(void) { + Str8List cmd = {}; + Str8Node echo = wapp_str8_node_from_cstr("echo"); + Str8Node msg = wapp_str8_node_from_cstr("hello world"); + wapp_str8_list_push_back(&cmd, &echo); + wapp_str8_list_push_back(&cmd, &msg); + + CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, nullptr, &cmd); + bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS && + result.error == SHELL_ERR_NO_ERROR; + + return wapp_tester_result(succeeded); +} + +TestFuncResult test_commander_cmd_failure(void) { + Str8List cmd = {}; + Str8Node grep = wapp_str8_node_from_cstr("grep"); + wapp_str8_list_push_back(&cmd, &grep); + + CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, nullptr, &cmd); + bool failed = result.exited && result.exit_code != EXIT_SUCCESS && + result.error == SHELL_ERR_NO_ERROR; + + return wapp_tester_result(failed); +} + +TestFuncResult test_commander_cmd_out_buf_success(void) { + Str8 buf = wapp_str8_buf(64); + Str8 expected = wapp_str8_buf(64); + char msg[] = "hello world"; + wapp_str8_copy_cstr_capped(&expected, msg); + + Str8List cmd = {}; + Str8Node echo = wapp_str8_node_from_cstr("echo"); + Str8Node arg = wapp_str8_node_from_cstr(msg); + wapp_str8_list_push_back(&cmd, &echo); + wapp_str8_list_push_back(&cmd, &arg); + + CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, &buf, &cmd); + bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS && + result.error == SHELL_ERR_NO_ERROR && wapp_str8_equal_to_count(&buf, &expected, strlen(msg)); + + return wapp_tester_result(succeeded); +} + +TestFuncResult test_commander_cmd_out_buf_failure(void) { + Str8 buf = wapp_str8_buf(4); + Str8 expected = wapp_str8_buf(64); + char msg[] = "hello world"; + wapp_str8_copy_cstr_capped(&expected, msg); + + Str8List cmd = {}; + Str8Node echo = wapp_str8_node_from_cstr("echo"); + Str8Node arg = wapp_str8_node_from_cstr(msg); + wapp_str8_list_push_back(&cmd, &echo); + wapp_str8_list_push_back(&cmd, &arg); + + CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, &buf, &cmd); + bool failed = !result.exited && result.exit_code != EXIT_SUCCESS && + result.error == SHELL_ERR_OUT_BUF_FULL && !wapp_str8_equal(&buf, &expected); + + return wapp_tester_result(failed); +} diff --git a/tests/str8/test_str8.cc b/tests/str8/test_str8.cc new file mode 100644 index 0000000..fc2a42f --- /dev/null +++ b/tests/str8/test_str8.cc @@ -0,0 +1,614 @@ +#include "test_str8.h" +#include "wapp.h" +#include + +#define ARRLEN(ARR) (sizeof(ARR) / sizeof(ARR[0])) + +TestFuncResult test_str8_lit(void) { + bool result; + + Str8 s1 = wapp_str8_lit("Hello world"); + result = s1.capacity == 22 && s1.capacity != s1.size; + + Str8 s2 = wapp_str8_lit("Different strokes for different folks"); + result = result && s2.capacity == 74 && s2.capacity != s2.size; + + Str8 s3 = wapp_str8_lit("Discretion is the better part of valour"); + result = result && s3.capacity == 78 && s3.capacity != s3.size; + + Str8 s4 = wapp_str8_lit("Distance lends enchantment to the view"); + result = result && s4.capacity == 76 && s4.capacity != s4.size; + + Str8 s5 = wapp_str8_lit("Do as I say, not as I do"); + result = result && s5.capacity == 48 && s5.capacity != s5.size; + + Str8 s6 = wapp_str8_lit("Do as you would be done by"); + result = result && s6.capacity == 52 && s6.capacity != s6.size; + + Str8 s7 = wapp_str8_lit("Do unto others as you would have them do to you"); + result = result && s7.capacity == 94 && s7.capacity != s7.size; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_lit_ro(void) { + bool result; + + Str8RO s1 = wapp_str8_lit_ro("Hello world"); + result = s1.capacity == 11 && s1.capacity == s1.size; + + Str8RO s2 = wapp_str8_lit_ro("Different strokes for different folks"); + result = result && s2.capacity == 37 && s2.capacity == s2.size; + + Str8RO s3 = wapp_str8_lit_ro("Discretion is the better part of valour"); + result = result && s3.capacity == 39 && s3.capacity == s3.size; + + Str8RO s4 = wapp_str8_lit_ro("Distance lends enchantment to the view"); + result = result && s4.capacity == 38 && s4.capacity == s4.size; + + Str8RO s5 = wapp_str8_lit_ro("Do as I say, not as I do"); + result = result && s5.capacity == 24 && s5.capacity == s5.size; + + Str8RO s6 = wapp_str8_lit_ro("Do as you would be done by"); + result = result && s6.capacity == 26 && s6.capacity == s6.size; + + Str8RO s7 = wapp_str8_lit_ro("Do unto others as you would have them do to you"); + result = result && s7.capacity == 47 && s7.capacity == s7.size; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_buf(void) { + bool result; + + Str8 s1 = wapp_str8_buf(1024); + result = s1.capacity == 1024 && s1.size == 0; + + Str8 s2 = wapp_str8_buf(2048); + result = result && s2.capacity == 2048 && s2.size == 0; + + Str8 s3 = wapp_str8_buf(4096); + result = result && s3.capacity == 4096 && s3.size == 0; + + Str8 s4 = wapp_str8_buf(8192); + result = result && s4.capacity == 8192 && s4.size == 0; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_alloc_buf(void) { + bool result; + Allocator allocator = wapp_mem_arena_allocator_init(KB(100)); + if (wapp_mem_allocator_invalid(&allocator)) { + return wapp_tester_result(false); + } + + u64 capacity = 4096; + + Str8 *s = wapp_str8_alloc_buf(&allocator, capacity); + if (!s) { + result = false; + wapp_mem_arena_allocator_destroy(&allocator); + return wapp_tester_result(result); + } + + result = s->capacity == capacity; + + const char *cstr = "My name is Abdelrahman"; + wapp_str8_copy_cstr_capped(s, cstr); + + result = result && s->capacity == capacity && s->size == strlen(cstr) && memcmp(s->buf, cstr, s->size) == 0; + + wapp_mem_arena_allocator_destroy(&allocator); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_alloc_cstr(void) { + bool result; + Allocator allocator = wapp_mem_arena_allocator_init(KB(100)); + if (wapp_mem_allocator_invalid(&allocator)) { + return wapp_tester_result(false); + } + + const char *str = "Abdelrahman"; + u64 length = strlen(str); + Str8 *s = wapp_str8_alloc_cstr(&allocator, str); + if (!s) { + return wapp_tester_result(false); + } + + result = s->size == length && memcmp(s->buf, str, length) == 0; + + wapp_mem_arena_allocator_destroy(&allocator); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_alloc_str8(void) { + bool result; + Allocator allocator = wapp_mem_arena_allocator_init(KB(100)); + if (wapp_mem_allocator_invalid(&allocator)) { + return wapp_tester_result(false); + } + + Str8 str = wapp_str8_lit("Abdelrahman"); + Str8 *s = wapp_str8_alloc_str8(&allocator, &str); + if (!s) { + return wapp_tester_result(false); + } + + result = wapp_str8_equal(s, &str); + + wapp_mem_arena_allocator_destroy(&allocator); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_alloc_substr(void) { + bool result; + Allocator allocator = wapp_mem_arena_allocator_init(KB(100)); + if (wapp_mem_allocator_invalid(&allocator)) { + return wapp_tester_result(false); + } + + Str8 str = wapp_str8_lit("Abdelrahman"); + Str8 *s = wapp_str8_alloc_substr(&allocator, &str, 3, 8); + if (!s) { + return wapp_tester_result(false); + } + + result = s->size == 5 && memcmp(s->buf, "elrah", s->size) == 0; + + wapp_mem_arena_allocator_destroy(&allocator); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_get_index_within_bounds(void) { + bool result; + + Str8RO s1 = wapp_str8_lit_ro("Hello world"); + result = wapp_str8_get(&s1, 4) == 'o'; + + Str8RO s2 = wapp_str8_lit_ro("Different strokes for different folks"); + result = result && wapp_str8_get(&s2, 0) == 'D'; + + Str8RO s3 = wapp_str8_lit_ro("Discretion is the better part of valour"); + result = result && wapp_str8_get(&s3, 13) == ' '; + + Str8RO s4 = wapp_str8_lit_ro("Distance lends enchantment to the view"); + result = result && wapp_str8_get(&s4, 20) == 'n'; + + Str8RO s5 = wapp_str8_lit_ro("Do as I say, not as I do"); + result = result && wapp_str8_get(&s5, 11) == ','; + + Str8RO s6 = wapp_str8_lit_ro("Do as you would be done by"); + result = result && wapp_str8_get(&s6, 25) == 'y'; + + Str8RO s7 = wapp_str8_lit_ro("Do unto others as you would have them do to you"); + result = result && wapp_str8_get(&s7, 16) == 's'; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_get_index_out_of_bounds(void) { + Str8 s1 = wapp_str8_lit("Hello world"); + bool result = wapp_str8_get(&s1, 20) == '\0'; + return wapp_tester_result(result); +} + +TestFuncResult test_str8_set(void) { + bool result; + + Str8 s1 = wapp_str8_lit("Hello world"); + wapp_str8_set(&s1, 4, 'f'); + result = wapp_str8_get(&s1, 4) == 'f'; + + Str8 s2 = wapp_str8_lit("Different strokes for different folks"); + wapp_str8_set(&s2, 0, 'A'); + result = result && wapp_str8_get(&s2, 0) == 'A'; + + Str8 s3 = wapp_str8_lit("Discretion is the better part of valour"); + wapp_str8_set(&s3, 13, 'u'); + result = result && wapp_str8_get(&s3, 13) == 'u'; + + Str8 s4 = wapp_str8_lit("Distance lends enchantment to the view"); + wapp_str8_set(&s4, 20, 'R'); + result = result && wapp_str8_get(&s4, 20) == 'R'; + + Str8 s5 = wapp_str8_lit("Do as I say, not as I do"); + wapp_str8_set(&s5, 11, '.'); + result = result && wapp_str8_get(&s5, 11) == '.'; + + Str8 s6 = wapp_str8_lit("Do as you would be done by"); + wapp_str8_set(&s6, 25, 'w'); + result = result && wapp_str8_get(&s6, 25) == 'w'; + + Str8 s7 = wapp_str8_lit("Do unto others as you would have them do to you"); + wapp_str8_set(&s7, 16, 'i'); + result = result && wapp_str8_get(&s7, 16) == 'i'; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_push_back(void) { + bool result; + + Str8 expected = wapp_str8_lit("Abdelrahman"); + Str8 buf = wapp_str8_buf(64); + wapp_str8_push_back(&buf, 'A'); + wapp_str8_push_back(&buf, 'b'); + wapp_str8_push_back(&buf, 'd'); + wapp_str8_push_back(&buf, 'e'); + wapp_str8_push_back(&buf, 'l'); + wapp_str8_push_back(&buf, 'r'); + wapp_str8_push_back(&buf, 'a'); + wapp_str8_push_back(&buf, 'h'); + wapp_str8_push_back(&buf, 'm'); + wapp_str8_push_back(&buf, 'a'); + wapp_str8_push_back(&buf, 'n'); + + result = wapp_str8_equal(&buf, &expected); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_equal(void) { + bool result; + + Str8RO s1 = wapp_str8_lit_ro("hello"); + Str8RO s2 = wapp_str8_lit_ro("hell"); + Str8RO s3 = wapp_str8_lit_ro("hello"); + Str8RO s4 = wapp_str8_lit_ro("goodbye"); + + result = wapp_str8_equal(&s1, &s2) == false; + result = result && wapp_str8_equal(&s1, &s3) == true; + result = result && wapp_str8_equal(&s1, &s4) == false; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_slice(void) { + bool result; + Str8 s = wapp_str8_lit("Different strokes for different folks"); + + Str8RO sub1 = wapp_str8_slice(&s, 3, 9); + result = sub1.size == 6 && sub1.capacity == 6; + + Str8RO sub2 = wapp_str8_slice(&s, 18, 21); + result = result && sub2.size == 3 && sub2.capacity == 3; + + Str8RO sub3 = wapp_str8_slice(&s, 5, 1); + result = result && sub3.size == 0 && sub3.capacity == 0; + + Str8RO sub4 = wapp_str8_slice(&s, 70, 80); + result = result && sub4.size == 0 && sub4.capacity == 0; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_alloc_concat(void) { + bool result; + Allocator arena = wapp_mem_arena_allocator_init(KB(100)); + + Str8 str = wapp_str8_lit("Hello world"); + Str8 suffix1 = wapp_str8_lit(" from me."); + Str8 suffix2 = wapp_str8_lit(" This is my code."); + Str8 concat1 = wapp_str8_lit("Hello world from me."); + Str8 concat2 = wapp_str8_lit("Hello world from me. This is my code."); + + Str8 *output; + + output = wapp_str8_alloc_concat(&arena, &str, &suffix1); + result = output->size == concat1.size && wapp_str8_equal(output, &concat1); + + output = wapp_str8_alloc_concat(&arena, output, &suffix2); + result = result && output->size == concat2.size && wapp_str8_equal(output, &concat2); + + wapp_mem_arena_allocator_destroy(&arena); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_concat_capped(void) { + bool result; + + Str8 str = wapp_str8_lit("Hello world"); + Str8 suffix1 = wapp_str8_lit(" from me."); + Str8 suffix2 = wapp_str8_lit(" This is my code."); + Str8 concat1 = wapp_str8_lit("Hello world from me."); + Str8 concat2 = wapp_str8_lit("Hello world from me. T"); + + wapp_str8_concat_capped(&str, &suffix1); + result = str.size == concat1.size && wapp_str8_equal(&str, &concat1); + + wapp_str8_concat_capped(&str, &suffix2); + result = result && str.size == concat2.size && wapp_str8_equal(&str, &concat2); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_copy_cstr_capped(void) { + bool result; + + Str8 buf = wapp_str8_buf(32); + const char *src1 = "Hello world"; + const char *src2 = "Hello world from the Wizard Apprentice standard library"; + Str8RO src1_cp = wapp_str8_lit_ro("Hello world"); + Str8RO src2_cp = wapp_str8_lit_ro("Hello world from the Wizard Appr"); + + wapp_str8_copy_cstr_capped(&buf, src1); + result = buf.size == src1_cp.size && wapp_str8_equal(&buf, &src1_cp); + + wapp_str8_copy_cstr_capped(&buf, src2); + result = result && buf.size == src2_cp.size && buf.size == buf.capacity && wapp_str8_equal(&buf, &src2_cp); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_copy_str8_capped(void) { + bool result; + + Str8 buf = wapp_str8_buf(32); + Str8RO src1 = wapp_str8_lit_ro("Hello world"); + Str8RO src2 = wapp_str8_lit_ro("Hello world from the Wizard Apprentice standard library"); + Str8RO src2_cp = wapp_str8_lit_ro("Hello world from the Wizard Appr"); + + wapp_str8_copy_str8_capped(&buf, &src1); + result = buf.size == src1.size && wapp_str8_equal(&buf, &src1); + + wapp_str8_copy_str8_capped(&buf, &src2); + result = result && buf.size < src2.size && buf.size == buf.capacity && wapp_str8_equal(&buf, &src2_cp); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_format(void) { + bool result; + + Str8 buf = wapp_str8_buf(128); + Str8 expected = wapp_str8_lit("My name is Abdelrahman and I am 35 years old"); + + wapp_str8_format(&buf, "My name is %s and I am %u years old", "Abdelrahman", 35); + + result = wapp_str8_equal(&buf, &expected); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_find(void) { + bool result; + Str8RO s = wapp_str8_lit("Do as I say, not as I do"); + + result = wapp_str8_find(&s, wapp_str8_lit_ro("d")) != -1; + result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("not")) != -1); + result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("as I say")) != -1); + + result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("f")) == -1); + result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("hello")) == -1); + result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("not sa I")) == -1); + result = result && (wapp_str8_find(&s, wapp_str8_lit_ro("Do unto others as you would have them do to you")) == -1); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_rfind(void) { + bool result; + Str8RO s = wapp_str8_lit("Do as I say, not as I do"); + + result = wapp_str8_rfind(&s, wapp_str8_lit_ro("d")) != -1; + result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("not")) != -1); + result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("as I say")) != -1); + + result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("f")) == -1); + result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("hello")) == -1); + result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("not sa I")) == -1); + result = result && (wapp_str8_rfind(&s, wapp_str8_lit_ro("Do unto others as you would have them do to you")) == -1); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_split(void) { + bool result; + Allocator arena = wapp_mem_arena_allocator_init(KB(100)); + + Str8 str = wapp_str8_lit("hello world from me"); + Str8 delim1 = wapp_str8_lit(" "); + Str8 delim2 = wapp_str8_lit("from"); + Str8List *list1 = wapp_str8_split(&arena, &str, &delim1); + Str8List *list2 = wapp_str8_split(&arena, &str, &delim2); + + Str8RO splits1[] = { + wapp_str8_slice(&str, 0, 5), + wapp_str8_slice(&str, 6, 11), + wapp_str8_slice(&str, 12, 16), + wapp_str8_slice(&str, 17, 19), + }; + Str8RO splits2[] = { + wapp_str8_slice(&str, 0, 12), + wapp_str8_slice(&str, 16, 19), + }; + + u64 index1 = 0; + u64 count1 = ARRLEN(splits1); + bool running1 = true; + + u64 index2 = 0; + u64 count2 = ARRLEN(splits2); + bool running2 = true; + + result = list1->node_count == count1 && wapp_str8_list_total_size(list1) == str.size - 3; + result = result && list2->node_count == count2 && wapp_str8_list_total_size(list2) == str.size - 4; + + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + while (running1) { + Str8Node *node = wapp_str8_list_get(list1, index1); + result = result && wapp_str8_equal(node->item, &(splits1[index1])); + + ++index1; + running1 = index1 < count1; + } + + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + while (running2) { + Str8Node *node = wapp_str8_list_get(list2, index2); + result = result && wapp_str8_equal(node->item, &(splits2[index2])); + + ++index2; + running2 = index2 < count2; + } + + wapp_mem_arena_allocator_destroy(&arena); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_split_with_max(void) { + bool result; + Allocator arena = wapp_mem_arena_allocator_init(KB(100)); + + Str8 str = wapp_str8_lit("hello world from me"); + Str8 delim = wapp_str8_lit(" "); + Str8List *list = wapp_str8_split_with_max(&arena, &str, &delim, 2); + + Str8RO splits[] = { + wapp_str8_slice(&str, 0, 5), + wapp_str8_slice(&str, 6, 11), + wapp_str8_slice(&str, 12, 19), + }; + + u64 index = 0; + u64 count = ARRLEN(splits); + bool running = true; + + result = list->node_count == count && wapp_str8_list_total_size(list) == str.size - 2; + + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + while (running) { + Str8Node *node = wapp_str8_list_get(list, index); + result = result && wapp_str8_equal(node->item, &(splits[index])); + + ++index; + running = index < count; + } + + wapp_mem_arena_allocator_destroy(&arena); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_rsplit(void) { + bool result; + Allocator arena = wapp_mem_arena_allocator_init(KB(100)); + + Str8 str = wapp_str8_lit("hello world from me"); + Str8 delim1 = wapp_str8_lit(" "); + Str8 delim2 = wapp_str8_lit("from"); + Str8List *list1 = wapp_str8_rsplit(&arena, &str, &delim1); + Str8List *list2 = wapp_str8_rsplit(&arena, &str, &delim2); + + Str8RO splits1[] = { + wapp_str8_slice(&str, 0, 5), + wapp_str8_slice(&str, 6, 11), + wapp_str8_slice(&str, 12, 16), + wapp_str8_slice(&str, 17, 19), + }; + Str8RO splits2[] = { + wapp_str8_slice(&str, 0, 12), + wapp_str8_slice(&str, 16, 19), + }; + + u64 index1 = 0; + u64 count1 = ARRLEN(splits1); + bool running1 = true; + + u64 index2 = 0; + u64 count2 = ARRLEN(splits2); + bool running2 = true; + + result = list1->node_count == count1 && wapp_str8_list_total_size(list1) == str.size - 3; + result = result && list2->node_count == count2 && wapp_str8_list_total_size(list2) == str.size - 4; + + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + while (running1) { + Str8Node *node = wapp_str8_list_get(list1, index1); + result = result && wapp_str8_equal(node->item, &(splits1[index1])); + + ++index1; + running1 = index1 < count1; + } + + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + while (running2) { + Str8Node *node = wapp_str8_list_get(list2, index2); + result = result && wapp_str8_equal(node->item, &(splits2[index2])); + + ++index2; + running2 = index2 < count2; + } + + wapp_mem_arena_allocator_destroy(&arena); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_rsplit_with_max(void) { + bool result; + Allocator arena = wapp_mem_arena_allocator_init(KB(100)); + + Str8 str = wapp_str8_lit("hello world from me"); + Str8 delim = wapp_str8_lit(" "); + Str8List *list = wapp_str8_rsplit_with_max(&arena, &str, &delim, 2); + + Str8RO splits[] = { + wapp_str8_slice(&str, 0, 11), + wapp_str8_slice(&str, 12, 16), + wapp_str8_slice(&str, 17, 19), + }; + + u64 index = 0; + u64 count = ARRLEN(splits); + bool running = true; + + result = list->node_count == count && wapp_str8_list_total_size(list) == str.size - 2; + + // NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of + // MSVC Spectre mitigation warnings + while (running) { + Str8Node *node = wapp_str8_list_get(list, index); + result = result && wapp_str8_equal(node->item, &(splits[index])); + + ++index; + running = index < count; + } + + wapp_mem_arena_allocator_destroy(&arena); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_join(void) { + bool result; + Allocator arena = wapp_mem_arena_allocator_init(KB(100)); + + Str8 str = wapp_str8_lit("hello world from me"); + Str8 delim1 = wapp_str8_lit(" "); + Str8 delim2 = wapp_str8_lit("from"); + Str8List *list1 = wapp_str8_rsplit(&arena, &str, &delim1); + Str8List *list2 = wapp_str8_rsplit(&arena, &str, &delim2); + Str8 *join1 = wapp_str8_join(&arena, list1, &delim1); + Str8 *join2 = wapp_str8_join(&arena, list2, &delim2); + + result = join1->size == str.size && wapp_str8_equal(join1, &str); + result = result && join2->size == str.size && wapp_str8_equal(join2, &str); + + wapp_mem_arena_allocator_destroy(&arena); + + return wapp_tester_result(result); +} diff --git a/tests/str8/test_str8_list.cc b/tests/str8/test_str8_list.cc new file mode 100644 index 0000000..4359445 --- /dev/null +++ b/tests/str8/test_str8_list.cc @@ -0,0 +1,271 @@ +#include "test_str8_list.h" +#include "wapp.h" + +TestFuncResult test_str8_list_get(void) { + bool result; + + Str8 s1 = wapp_str8_lit("1"); + Str8 s2 = wapp_str8_lit("2"); + Str8 s3 = wapp_str8_lit("3"); + Str8 s4 = wapp_str8_lit("4"); + Str8 s5 = wapp_str8_lit("5"); + + Str8List list = {}; + Str8Node n1 = { &s1, nullptr, nullptr }; + Str8Node n2 = { &s2, nullptr, nullptr }; + Str8Node n3 = { &s3, nullptr, nullptr }; + Str8Node n4 = { &s4, nullptr, nullptr }; + Str8Node n5 = { &s5, nullptr, nullptr }; + + wapp_str8_list_push_back(&list, &n1); + wapp_str8_list_push_back(&list, &n2); + wapp_str8_list_push_back(&list, &n3); + wapp_str8_list_push_back(&list, &n4); + wapp_str8_list_push_back(&list, &n5); + + Str8Node *node = wapp_str8_list_get(&list, 0); + result = node->item == &s1 && wapp_str8_equal(node->item, &s1); + + node = wapp_str8_list_get(&list, 1); + result = result && node->item == &s2 && wapp_str8_equal(node->item, &s2); + + node = wapp_str8_list_get(&list, 2); + result = result && node->item == &s3 && wapp_str8_equal(node->item, &s3); + + node = wapp_str8_list_get(&list, 3); + result = result && node->item == &s4 && wapp_str8_equal(node->item, &s4); + + node = wapp_str8_list_get(&list, 4); + result = result && node->item == &s5 && wapp_str8_equal(node->item, &s5); + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_list_push_front(void) { + bool result; + + Str8 s1 = wapp_str8_lit("1"); + Str8 s2 = wapp_str8_lit("2"); + Str8 s3 = wapp_str8_lit("3"); + + Str8List list = {}; + Str8Node n1 = { &s1, nullptr, nullptr }; + Str8Node n2 = { &s2, nullptr, nullptr }; + Str8Node n3 = { &s3, nullptr, nullptr }; + + wapp_str8_list_push_front(&list, &n1); + result = list.first == list.last && list.first == &n1 && list.first->item == &s1 && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1; + + wapp_str8_list_push_front(&list, &n2); + result = result && list.first == &n2 && list.first->item == &s2 && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2; + + wapp_str8_list_push_front(&list, &n3); + result = result && list.first == &n3 && list.first->item == &s3 && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_list_push_back(void) { + bool result; + + Str8 s1 = wapp_str8_lit("1"); + Str8 s2 = wapp_str8_lit("2"); + Str8 s3 = wapp_str8_lit("3"); + + Str8List list = {}; + Str8Node n1 = { &s1, nullptr, nullptr }; + Str8Node n2 = { &s2, nullptr, nullptr }; + Str8Node n3 = { &s3, nullptr, nullptr }; + + wapp_str8_list_push_back(&list, &n1); + result = list.first == list.last && list.last == &n1 && list.last->item == &s1 && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1; + + wapp_str8_list_push_back(&list, &n2); + result = result && list.last == &n2 && list.last->item == &s2 && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2; + + wapp_str8_list_push_back(&list, &n3); + result = result && list.last == &n3 && list.last->item == &s3 && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_list_insert(void) { + bool result; + + Str8 s1 = wapp_str8_lit("1"); + Str8 s2 = wapp_str8_lit("2"); + Str8 s3 = wapp_str8_lit("3"); + Str8 s4 = wapp_str8_lit("4"); + Str8 s5 = wapp_str8_lit("5"); + Str8 s6 = wapp_str8_lit("6"); + Str8 s7 = wapp_str8_lit("7"); + + Str8List list = {}; + Str8Node n1 = { &s1, nullptr, nullptr }; + Str8Node n2 = { &s2, nullptr, nullptr }; + Str8Node n3 = { &s3, nullptr, nullptr }; + Str8Node n4 = { &s4, nullptr, nullptr }; + Str8Node n5 = { &s5, nullptr, nullptr }; + Str8Node n6 = { &s6, nullptr, nullptr }; + Str8Node n7 = { &s7, nullptr, nullptr }; + + wapp_str8_list_push_back(&list, &n1); + wapp_str8_list_push_back(&list, &n2); + wapp_str8_list_push_back(&list, &n3); + wapp_str8_list_push_back(&list, &n4); + wapp_str8_list_push_back(&list, &n5); + + Str8Node *node; + wapp_str8_list_insert(&list, &n6, 2); + node = wapp_str8_list_get(&list, 2); + result = node != NULL && node->item == &s6 && wapp_str8_list_total_size(&list) == 6 && list.node_count == 6; + wapp_str8_list_insert(&list, &n7, 5); + node = wapp_str8_list_get(&list, 5); + result = result && node != NULL && node->item == &s7 && wapp_str8_list_total_size(&list) == 7 && list.node_count == 7; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_list_pop_front(void) { + bool result; + + Str8 s1 = wapp_str8_lit("1"); + Str8 s2 = wapp_str8_lit("2"); + Str8 s3 = wapp_str8_lit("3"); + Str8 s4 = wapp_str8_lit("4"); + Str8 s5 = wapp_str8_lit("5"); + + Str8List list = {}; + Str8Node n1 = { &s1, nullptr, nullptr }; + Str8Node n2 = { &s2, nullptr, nullptr }; + Str8Node n3 = { &s3, nullptr, nullptr }; + Str8Node n4 = { &s4, nullptr, nullptr }; + Str8Node n5 = { &s5, nullptr, nullptr }; + + wapp_str8_list_push_back(&list, &n1); + wapp_str8_list_push_back(&list, &n2); + wapp_str8_list_push_back(&list, &n3); + wapp_str8_list_push_back(&list, &n4); + wapp_str8_list_push_back(&list, &n5); + + Str8Node *node = wapp_str8_list_pop_front(&list); + result = node == &n1 && node->item == &s1 && wapp_str8_equal(node->item, &s1) && wapp_str8_list_total_size(&list) == 4 && list.node_count == 4; + + node = wapp_str8_list_pop_front(&list); + result = result && node == &n2 && node->item == &s2 && wapp_str8_equal(node->item, &s2) && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3; + + node = wapp_str8_list_pop_front(&list); + result = result && node == &n3 && node->item == &s3 && wapp_str8_equal(node->item, &s3) && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2; + + node = wapp_str8_list_pop_front(&list); + result = result && node == &n4 && node->item == &s4 && wapp_str8_equal(node->item, &s4) && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1; + + node = wapp_str8_list_pop_front(&list); + result = result && node == &n5 && node->item == &s5 && wapp_str8_equal(node->item, &s5) && wapp_str8_list_total_size(&list) == 0 && list.node_count == 0; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_list_pop_back(void) { + bool result; + + Str8 s1 = wapp_str8_lit("1"); + Str8 s2 = wapp_str8_lit("2"); + Str8 s3 = wapp_str8_lit("3"); + Str8 s4 = wapp_str8_lit("4"); + Str8 s5 = wapp_str8_lit("5"); + + Str8List list = {}; + Str8Node n1 = { &s1, nullptr, nullptr }; + Str8Node n2 = { &s2, nullptr, nullptr }; + Str8Node n3 = { &s3, nullptr, nullptr }; + Str8Node n4 = { &s4, nullptr, nullptr }; + Str8Node n5 = { &s5, nullptr, nullptr }; + + wapp_str8_list_push_front(&list, &n1); + wapp_str8_list_push_front(&list, &n2); + wapp_str8_list_push_front(&list, &n3); + wapp_str8_list_push_front(&list, &n4); + wapp_str8_list_push_front(&list, &n5); + + Str8Node *node = wapp_str8_list_pop_back(&list); + result = node == &n1 && node->item == &s1 && wapp_str8_equal(node->item, &s1) && wapp_str8_list_total_size(&list) == 4 && list.node_count == 4; + + node = wapp_str8_list_pop_back(&list); + result = result && node == &n2 && node->item == &s2 && wapp_str8_equal(node->item, &s2) && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3; + + node = wapp_str8_list_pop_back(&list); + result = result && node == &n3 && node->item == &s3 && wapp_str8_equal(node->item, &s3) && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2; + + node = wapp_str8_list_pop_back(&list); + result = result && node == &n4 && node->item == &s4 && wapp_str8_equal(node->item, &s4) && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1; + + node = wapp_str8_list_pop_back(&list); + result = result && node == &n5 && node->item == &s5 && wapp_str8_equal(node->item, &s5) && wapp_str8_list_total_size(&list) == 0 && list.node_count == 0; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_list_remove(void) { + bool result; + + Str8 s1 = wapp_str8_lit("1"); + Str8 s2 = wapp_str8_lit("2"); + Str8 s3 = wapp_str8_lit("3"); + Str8 s4 = wapp_str8_lit("4"); + Str8 s5 = wapp_str8_lit("5"); + + Str8List list = {}; + Str8Node n1 = { &s1, nullptr, nullptr }; + Str8Node n2 = { &s2, nullptr, nullptr }; + Str8Node n3 = { &s3, nullptr, nullptr }; + Str8Node n4 = { &s4, nullptr, nullptr }; + Str8Node n5 = { &s5, nullptr, nullptr }; + + wapp_str8_list_push_back(&list, &n1); + wapp_str8_list_push_back(&list, &n2); + wapp_str8_list_push_back(&list, &n3); + wapp_str8_list_push_back(&list, &n4); + wapp_str8_list_push_back(&list, &n5); + + Str8Node *node = wapp_str8_list_remove(&list, 0); + result = node == &n1 && node->item == &s1 && wapp_str8_equal(node->item, &s1) && wapp_str8_list_total_size(&list) == 4 && list.node_count == 4; + + node = wapp_str8_list_remove(&list, 0); + result = result && node == &n2 && node->item == &s2 && wapp_str8_equal(node->item, &s2) && wapp_str8_list_total_size(&list) == 3 && list.node_count == 3; + + node = wapp_str8_list_remove(&list, 0); + result = result && node == &n3 && node->item == &s3 && wapp_str8_equal(node->item, &s3) && wapp_str8_list_total_size(&list) == 2 && list.node_count == 2; + + node = wapp_str8_list_remove(&list, 0); + result = result && node == &n4 && node->item == &s4 && wapp_str8_equal(node->item, &s4) && wapp_str8_list_total_size(&list) == 1 && list.node_count == 1; + + node = wapp_str8_list_remove(&list, 0); + result = result && node == &n5 && node->item == &s5 && wapp_str8_equal(node->item, &s5) && wapp_str8_list_total_size(&list) == 0 && list.node_count == 0; + + return wapp_tester_result(result); +} + +TestFuncResult test_str8_list_empty(void) { + bool result; + + Str8List list = {}; + + Str8Node hello = wapp_str8_node_from_cstr("Hello"); + wapp_str8_list_push_back(&list, &hello); + + Str8Node from = wapp_str8_node_from_cstr("from"); + wapp_str8_list_push_back(&list, &from); + + Str8Node wizapp = wapp_str8_node_from_cstr("wizapp"); + wapp_str8_list_push_back(&list, &wizapp); + + Str8Node stdlib = wapp_str8_node_from_cstr("stdlib"); + wapp_str8_list_push_back(&list, &stdlib); + + wapp_str8_list_empty(&list); + + result = list.first == NULL && list.last == NULL && list.node_count == 0 && wapp_str8_list_total_size(&list) == 0; + + return wapp_tester_result(result); +} diff --git a/tests/wapptest.cc b/tests/wapptest.cc new file mode 100644 index 0000000..d75dc79 --- /dev/null +++ b/tests/wapptest.cc @@ -0,0 +1,77 @@ +#include "test_str8.h" +#include "test_str8_list.h" +#include "test_allocator.h" +#include "test_arena.h" +#include "test_i32_array.h" +#include "test_cpath.h" +#include "test_shell_commander.h" +#include "wapp.h" +#include + +int main(void) { + wapp_tester_run_tests( + test_arena_allocator, + test_arena_init, + test_arena_init_succeeds_when_reserving_very_large_size, + test_arena_alloc_succeeds_when_within_capacity, + test_arena_alloc_fails_when_over_capacity, + test_arena_realloc_bigger_size, + test_arena_realloc_smaller_size, + test_arena_clear, + test_arena_destroy, + test_i32_array, + test_i32_array_with_capacity, + test_i32_array_get, + test_i32_array_set, + test_i32_array_append_capped, + test_i32_array_extend_capped, + test_i32_array_clear, + 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_ro, + test_str8_buf, + test_str8_alloc_buf, + test_str8_alloc_cstr, + test_str8_alloc_str8, + test_str8_alloc_substr, + test_str8_alloc_concat, + test_str8_get_index_within_bounds, + test_str8_get_index_out_of_bounds, + test_str8_set, + test_str8_equal, + test_str8_slice, + test_str8_concat_capped, + test_str8_copy_cstr_capped, + test_str8_copy_str8_capped, + test_str8_format, + test_str8_find, + test_str8_rfind, + test_str8_split, + test_str8_split_with_max, + test_str8_rsplit, + test_str8_rsplit_with_max, + test_str8_join, + test_str8_list_get, + test_str8_list_push_front, + test_str8_list_push_back, + test_str8_list_insert, + test_str8_list_pop_front, + test_str8_list_pop_back, + test_str8_list_remove, + test_str8_list_empty, + test_cpath_join_path, + test_cpath_dirname, + test_cpath_dirup, + test_commander_cmd_success, + test_commander_cmd_failure, + test_commander_cmd_out_buf_success, + test_commander_cmd_out_buf_failure + ); + + return EXIT_SUCCESS; +} -- 2.39.5 From a221e7f540bbccf1bc245a477f1cea8ff8e26e6b Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 10 Aug 2025 18:56:51 +0100 Subject: [PATCH 6/7] Reformat C tests --- tests/wapptest.c | 124 ++++++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 61 deletions(-) diff --git a/tests/wapptest.c b/tests/wapptest.c index 2a06bbc..d75dc79 100644 --- a/tests/wapptest.c +++ b/tests/wapptest.c @@ -9,67 +9,69 @@ #include int main(void) { - wapp_tester_run_tests(test_arena_allocator, - test_arena_init, - test_arena_init_succeeds_when_reserving_very_large_size, - test_arena_alloc_succeeds_when_within_capacity, - test_arena_alloc_fails_when_over_capacity, - test_arena_realloc_bigger_size, - test_arena_realloc_smaller_size, - test_arena_clear, - test_arena_destroy, - test_i32_array, - test_i32_array_with_capacity, - test_i32_array_get, - test_i32_array_set, - test_i32_array_append_capped, - test_i32_array_extend_capped, - test_i32_array_clear, - 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_ro, - test_str8_buf, - test_str8_alloc_buf, - test_str8_alloc_cstr, - test_str8_alloc_str8, - test_str8_alloc_substr, - test_str8_alloc_concat, - test_str8_get_index_within_bounds, - test_str8_get_index_out_of_bounds, - test_str8_set, - test_str8_equal, - test_str8_slice, - test_str8_concat_capped, - test_str8_copy_cstr_capped, - test_str8_copy_str8_capped, - test_str8_format, - test_str8_find, - test_str8_rfind, - test_str8_split, - test_str8_split_with_max, - test_str8_rsplit, - test_str8_rsplit_with_max, - test_str8_join, - test_str8_list_get, - test_str8_list_push_front, - test_str8_list_push_back, - test_str8_list_insert, - test_str8_list_pop_front, - test_str8_list_pop_back, - test_str8_list_remove, - test_str8_list_empty, - test_cpath_join_path, - test_cpath_dirname, - test_cpath_dirup, - test_commander_cmd_success, - test_commander_cmd_failure, - test_commander_cmd_out_buf_success, - test_commander_cmd_out_buf_failure); + wapp_tester_run_tests( + test_arena_allocator, + test_arena_init, + test_arena_init_succeeds_when_reserving_very_large_size, + test_arena_alloc_succeeds_when_within_capacity, + test_arena_alloc_fails_when_over_capacity, + test_arena_realloc_bigger_size, + test_arena_realloc_smaller_size, + test_arena_clear, + test_arena_destroy, + test_i32_array, + test_i32_array_with_capacity, + test_i32_array_get, + test_i32_array_set, + test_i32_array_append_capped, + test_i32_array_extend_capped, + test_i32_array_clear, + 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_ro, + test_str8_buf, + test_str8_alloc_buf, + test_str8_alloc_cstr, + test_str8_alloc_str8, + test_str8_alloc_substr, + test_str8_alloc_concat, + test_str8_get_index_within_bounds, + test_str8_get_index_out_of_bounds, + test_str8_set, + test_str8_equal, + test_str8_slice, + test_str8_concat_capped, + test_str8_copy_cstr_capped, + test_str8_copy_str8_capped, + test_str8_format, + test_str8_find, + test_str8_rfind, + test_str8_split, + test_str8_split_with_max, + test_str8_rsplit, + test_str8_rsplit_with_max, + test_str8_join, + test_str8_list_get, + test_str8_list_push_front, + test_str8_list_push_back, + test_str8_list_insert, + test_str8_list_pop_front, + test_str8_list_pop_back, + test_str8_list_remove, + test_str8_list_empty, + test_cpath_join_path, + test_cpath_dirname, + test_cpath_dirup, + test_commander_cmd_success, + test_commander_cmd_failure, + test_commander_cmd_out_buf_success, + test_commander_cmd_out_buf_failure + ); return EXIT_SUCCESS; } -- 2.39.5 From 1c560ebf0b4800e6595817da76a74524741a635e Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 10 Aug 2025 23:28:06 +0100 Subject: [PATCH 7/7] Add function to fill str buffer with zeros --- src/primitives/strings/str8/str8.c | 9 +++++++++ src/primitives/strings/str8/str8.h | 1 + 2 files changed, 10 insertions(+) diff --git a/src/primitives/strings/str8/str8.c b/src/primitives/strings/str8/str8.c index 4bbdae8..7a507db 100644 --- a/src/primitives/strings/str8/str8.c +++ b/src/primitives/strings/str8/str8.c @@ -28,6 +28,15 @@ RETURN_STR8: return str; } +Str8 *wapp_str8_alloc_and_fill_buf(const Allocator *allocator, u64 capacity) { + Str8 *out = wapp_str8_alloc_buf(allocator, capacity); + if (out) { + memset(out->buf, 0, capacity); + out->size = capacity; + } + return out; +} + Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str) { wapp_debug_assert(allocator != NULL && str != NULL, "`allocator` and `str` should not be NULL"); diff --git a/src/primitives/strings/str8/str8.h b/src/primitives/strings/str8/str8.h index ea07f82..b74ffc8 100644 --- a/src/primitives/strings/str8/str8.h +++ b/src/primitives/strings/str8/str8.h @@ -74,6 +74,7 @@ typedef const Str8 Str8RO; * Str8 allocated buffers */ Str8 *wapp_str8_alloc_buf(const Allocator *allocator, u64 capacity); +Str8 *wapp_str8_alloc_and_fill_buf(const Allocator *allocator, u64 capacity); Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str); Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str); Str8 *wapp_str8_alloc_substr(const Allocator *allocator, Str8RO *str, u64 start, u64 end); -- 2.39.5