summaryrefslogtreecommitdiff
path: root/tests/string.c
blob: d10638012804d168f7b9320fd1769dcdd80d6583 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "../src/string.h"

#include <stddef.h>
#include <stdio.h>
#include <string.h>

#include <criterion/criterion.h>
#include <criterion/internal/assert.h>
#include <criterion/internal/test.h>

Test(string, create_string) {
    smrt_arena_t *arena = smrt_arena_create(KiB(4), KiB(4), false);
    strng_t *s = strng_new(arena, 50);

    cr_expect(s->alloc_size == 50);
    cr_expect(s->len == 0);

    smrt_arena_destroy(arena);
}

Test(string, from_cstring) {
    smrt_arena_t *arena = smrt_arena_create(KiB(4), KiB(4), false);
    strng_t *s = strng_from(arena, "Hello, world.");

    cr_expect(s->alloc_size == 13);
    cr_expect(s->len == 13);
    cr_expect(*((u8*)s+STRNG_BASE_POS) == 'H');
    cr_expect(*((u8*)s+STRNG_BASE_POS+s->len-1) == '.');

    smrt_arena_destroy(arena);
}

Test(string, to_cstring) {
    smrt_arena_t *arena = smrt_arena_create(KiB(4), KiB(4), false);
    strng_t *s = strng_from(arena, "Heavy is the head");

    char *c = strng_str(arena, s);

    cr_expect(strlen(c) == 17);
    cr_expect(strcmp(c, "Heavy is the head") == 0);

    smrt_arena_destroy(arena);
}

Test(string, set_string) {
    smrt_arena_t *arena = smrt_arena_create(KiB(4), KiB(4), false);
    strng_t *s = strng_from(arena, "Hello, world.");

    strng_set(s, "Look ma!");

    cr_expect(s->alloc_size == 13);
    cr_expect(s->len == 8);
    cr_expect(*((u8*)s+STRNG_BASE_POS) == 'L');
    cr_expect(*((u8*)s+STRNG_BASE_POS+s->len-1) == '!');

    smrt_arena_destroy(arena);
}

Test(string, string_duplicate) {
    smrt_arena_t *arena = smrt_arena_create(KiB(4), KiB(4), false);

    strng_t *orig = strng_new(arena, 50);
    strng_set(orig,  "that falls with the weight");


    strng_t *dup = strng_dup(arena, orig);

    cr_expect(dup->alloc_size == 26);
    cr_expect(dup->len == 26);
    cr_expect(*((u8*)dup+STRNG_BASE_POS) == 't');
    cr_expect(*((u8*)dup+STRNG_BASE_POS+dup->len-1) == 't');

    smrt_arena_destroy(arena);
}

Test(string, format_and_copy_string) {
    smrt_arena_t *arena = smrt_arena_create(KiB(4), KiB(4), false);

    strng_t *s = strng_from(arena, "Hello, world.");
    char *buffer = SMRTA_ALLOC_ARRAY(arena, char, s->len+1);

    u64 copied = snprintf(buffer, s->len+1, "%.*s", STRNG_FMT(s));

    cr_expect(copied == 13);
    cr_expect(strcmp(buffer, "Hello, world.") == 0);

    smrt_arena_destroy(arena);
}

Test(string, clear_string) {
    smrt_arena_t *arena = smrt_arena_create(KiB(4), KiB(4), false);
    strng_t *s = strng_from(arena, "Hello, world.");

    strng_clear(s);

    cr_expect_eq(s->len, 0);

    smrt_arena_destroy(arena);
}

Test(string, set_string_too_large_fails) {
    smrt_arena_t *arena = smrt_arena_create(KiB(4), KiB(4), false);
    strng_t *s = strng_new(arena, 4);

    b32 ok = strng_set(s, "way too long for a 4 byte buffer");

    cr_expect_eq(ok, false);
    cr_expect_eq(s->len, 0);

    smrt_arena_destroy(arena);
}

Test(string, to_cstring_of_empty_string) {
    smrt_arena_t *arena = smrt_arena_create(KiB(4), KiB(4), false);
    strng_t *s = strng_new(arena, 10);

    char *c = strng_str(arena, s);

    cr_assert_not_null(c);
    cr_expect(strlen(c) == 0);

    smrt_arena_destroy(arena);
}

Test(string, new_string_returns_null_when_arena_exhausted) {
    smrt_arena_t *arena = smrt_arena_create(1, 1, false);

    strng_t *s = strng_new(arena, MiB(1));

    cr_expect_eq(s, NULL);

    smrt_arena_destroy(arena);
}

Test(string, from_cstring_returns_null_when_arena_exhausted) {
    smrt_arena_t *arena = smrt_arena_create(1, 1, false);

    char big[KiB(8)+1];
    memset(big, 'a', sizeof(big)-1);
    big[sizeof(big)-1] = '\0';

    strng_t *s = strng_from(arena, big);

    cr_expect_eq(s, NULL);

    smrt_arena_destroy(arena);
}

Test(string, duplicate_returns_null_when_arena_exhausted) {
    smrt_arena_t *arena = smrt_arena_create(1, 1, false);

    strng_t fake_src = { .alloc_size = KiB(8), .len = KiB(8) };

    strng_t *s = strng_dup(arena, &fake_src);

    cr_expect_eq(s, NULL);

    smrt_arena_destroy(arena);
}

Test(string, to_cstring_returns_null_when_arena_exhausted) {
    smrt_arena_t *arena = smrt_arena_create(1, 1, false);

    strng_t fake_str = { .alloc_size = KiB(8), .len = KiB(8) };

    char *c = strng_str(arena, &fake_str);

    cr_expect_eq(c, NULL);

    smrt_arena_destroy(arena);
}