summaryrefslogtreecommitdiff
path: root/strings/test.cpp
blob: 49f81c9bb943464fa0440b9d5ac6b76187bb783f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include "strings.h"
#include "strings.cpp"

void TestCstrSize()
{
  u64 size = CstrSize("hello sailor\n");
  assert(size == 13 && "test_cstr_size() failed"
      "the cstr size was wrong\n");
}

void TestEmptyCstrSize()
{
  u64 size = CstrSize("");
  assert(size == 0 && "test_empty_cstr_size() failed"
      "the cstr size was wrong\n");
}

void TestStr8InitCstr(u8* buffer, u64 buffer_size)
{
  struct arena a = {};
  arena_init(&a, buffer, buffer_size);
  struct res_str8 qres = Str8InitCstr(&a, "hello sailor");
  assert((qres.bytes_count == 12) && "Error TestStr8InitCstr:\n"
      "Reason: Invalid size of allocated string\n"
      "Hint: check what is happening to the bytes_count and how it is set first\n");
}

void TestStr8InitCstrEmpty(u8* buffer, u64 buffer_size)
{
  struct arena a = {};
  arena_init(&a, buffer, buffer_size);
  struct res_str8 qres = Str8InitCstr(&a, "");
  assert((qres.string.size == 0) && "Error TestStr8InitCstrEmpty:\n"
      "Reason: Invalid size of allocated string\n"
      "Hint: check what is happening to the bytes_count and how it is set first\n");
}

void TestStr8InitCstrNoSpace(u8* buffer, u8 buffer_size)
{
  struct arena a = {};
  arena_init(&a, buffer, buffer_size);
  struct res_str8 qres = Str8InitCstr(&a, "hello sailor, looks like you've got no space here on this boat!");
  assert(qres.string.size == 0 && "Error TestStr8InitStrNoSpace:\n"
      "Reason: the bytes count is not 0, even though the string is larger than the supposedly available\n"
      "arena space. This is an error and should not occur.\n"
      "Hint: Look at what is going on in the allocation since that arena helpers handle allocations\n.");
}

void test_strings()
{
  printf("\n======= Testing Strings Library =======\n");
  printf("===== Testing Cstring Functions =====\n");
  TestCstrSize();
  TestEmptyCstrSize();

  u64 buffer_size = KB(64);
  u8* buffer = (u8*)malloc(sizeof(u8)*buffer_size);

  TestStr8InitCstr(buffer, buffer_size);
  TestStr8InitCstrEmpty(buffer, buffer_size);
  TestStr8InitCstrNoSpace(buffer, 8);
  printf("Cstring tests passed successfully\n");
  free(buffer);
}