From ff4d3c9e99802ba98a465a7b903f92944e61d0ee Mon Sep 17 00:00:00 2001
From: Abdelrahman <said.abdelrahman89@gmail.com>
Date: Sun, 9 Feb 2025 18:47:10 +0000
Subject: [PATCH] Add tests for string allocation functions

---
 tests/str8/test_str8.c | 78 ++++++++++++++++++++++++++++++++++++++++++
 tests/str8/test_str8.h |  4 +++
 tests/wapptest.c       |  4 +++
 3 files changed, 86 insertions(+)

diff --git a/tests/str8/test_str8.c b/tests/str8/test_str8.c
index 08caa7f..4e6cbdb 100644
--- a/tests/str8/test_str8.c
+++ b/tests/str8/test_str8.c
@@ -1,4 +1,7 @@
 #include "test_str8.h"
+#include "mem_allocator.h"
+#include "mem_arena_allocator.h"
+#include "misc_utils.h"
 #include "str8.h"
 #include "tester.h"
 #include <stdbool.h>
@@ -75,6 +78,62 @@ TestFuncResult test_str8_buf(void) {
   return wapp_tester_result(result);
 }
 
+TestFuncResult test_str8_buf_alloc(void) {
+  bool result;
+  Allocator allocator = wapp_mem_arena_allocator_init(MB(10));
+  if (wapp_mem_allocator_invalid(&allocator)) {
+    return wapp_tester_result(false);
+  }
+
+  Str8 *s = wapp_str8_buf_alloc(&allocator, 4096);
+  result  = s != NULL && s->capacity == 4096;
+
+  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(MB(10));
+  if (wapp_mem_allocator_invalid(&allocator)) {
+    return wapp_tester_result(false);
+  }
+
+  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(MB(10));
+  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 = s->size == str.size && memcmp(s->buf, str.buf, str.size) == 0;
+
+  wapp_mem_arena_allocator_destroy(&allocator);
+
+  return wapp_tester_result(result);
+}
+
 TestFuncResult test_str8_get_index_within_bounds(void) {
   bool result;
 
@@ -142,6 +201,25 @@ TestFuncResult test_str8_set(void) {
   return wapp_tester_result(result);
 }
 
+TestFuncResult test_str8_substr(void) {
+  bool result;
+  Str8 s = wapp_str8_lit("Different strokes for different folks");
+
+  Str8RO sub1 = wapp_str8_substr(&s, 3, 9);
+  result = sub1.size == 6 && sub1.capacity == 6;
+
+  Str8RO sub2 = wapp_str8_substr(&s, 18, 21);
+  result = result && sub2.size == 3 && sub2.capacity == 3;
+
+  Str8RO sub3 = wapp_str8_substr(&s, 5, 1);
+  result = result && sub3.size == 0 && sub3.capacity == 0;
+
+  Str8RO sub4 = wapp_str8_substr(&s, 70, 80);
+  result = result && sub4.size == 0 && sub4.capacity == 0;
+
+  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");
diff --git a/tests/str8/test_str8.h b/tests/str8/test_str8.h
index 8ddec36..392c40f 100644
--- a/tests/str8/test_str8.h
+++ b/tests/str8/test_str8.h
@@ -10,9 +10,13 @@ BEGIN_C_LINKAGE
 TestFuncResult test_str8_lit(void);
 TestFuncResult test_str8_lit_ro(void);
 TestFuncResult test_str8_buf(void);
+TestFuncResult test_str8_buf_alloc(void);
+TestFuncResult test_str8_alloc_cstr(void);
+TestFuncResult test_str8_alloc_str8(void);
 TestFuncResult test_str8_get_index_within_bounds(void);
 TestFuncResult test_str8_get_index_out_of_bounds(void);
 TestFuncResult test_str8_set(void);
+TestFuncResult test_str8_substr(void);
 TestFuncResult test_str8_find(void);
 TestFuncResult test_str8_rfind(void);
 
diff --git a/tests/wapptest.c b/tests/wapptest.c
index 1266e78..399acaf 100644
--- a/tests/wapptest.c
+++ b/tests/wapptest.c
@@ -15,9 +15,13 @@ int main(void) {
                         test_str8_lit,
                         test_str8_lit_ro,
                         test_str8_buf,
+                        test_str8_buf_alloc,
+                        test_str8_alloc_cstr,
+                        test_str8_alloc_str8,
                         test_str8_get_index_within_bounds,
                         test_str8_get_index_out_of_bounds,
                         test_str8_set,
+                        test_str8_substr,
                         test_str8_find,
                         test_str8_rfind,
                         test_cpath_join_path,