Reintroduce C++ support and add usage tests for C++ (#4)
Reviewed-on: #4 Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com> Co-committed-by: Abdelrahman <said.abdelrahman89@gmail.com>
This commit is contained in:
18
tests/allocator/test_allocator.cc
Normal file
18
tests/allocator/test_allocator.cc
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "test_allocator.h"
|
||||
#include "wapp.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
}
|
112
tests/arena/test_arena.cc
Normal file
112
tests/arena/test_arena.cc
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "test_arena.h"
|
||||
#include "wapp.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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);
|
||||
}
|
274
tests/array/test_i32_array.cc
Normal file
274
tests/array/test_i32_array.cc
Normal file
@@ -0,0 +1,274 @@
|
||||
#include "test_i32_array.h"
|
||||
#include "wapp.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
TestFuncResult test_i32_array(void) {
|
||||
bool result;
|
||||
|
||||
I32Array array = wapp_i32_array(1, 2, 3, 4, 5, 6, 7);
|
||||
result = array.count == 7 && array.capacity == 16;
|
||||
|
||||
i32 *item;
|
||||
u64 count = array.count;
|
||||
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);
|
||||
}
|
205
tests/cpath/test_cpath.cc
Normal file
205
tests/cpath/test_cpath.cc
Normal file
@@ -0,0 +1,205 @@
|
||||
#include "test_cpath.h"
|
||||
#include "wapp.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#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);
|
||||
}
|
70
tests/shell_commander/test_shell_commander.cc
Normal file
70
tests/shell_commander/test_shell_commander.cc
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "test_shell_commander.h"
|
||||
#include "wapp.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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);
|
||||
}
|
614
tests/str8/test_str8.cc
Normal file
614
tests/str8/test_str8.cc
Normal file
@@ -0,0 +1,614 @@
|
||||
#include "test_str8.h"
|
||||
#include "wapp.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#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);
|
||||
}
|
271
tests/str8/test_str8_list.cc
Normal file
271
tests/str8/test_str8_list.cc
Normal file
@@ -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);
|
||||
}
|
124
tests/wapptest.c
124
tests/wapptest.c
@@ -9,67 +9,69 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
77
tests/wapptest.cc
Normal file
77
tests/wapptest.cc
Normal file
@@ -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 <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
Reference in New Issue
Block a user