-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest.c
262 lines (228 loc) · 7.87 KB
/
test.c
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <stdio.h>
#include <ctype.h>
#include <inttypes.h>
#define SV_IMPLEMENTATION
#include "./sv.h"
void sv_assert_eq_String_View(const char *file, size_t line,
const char *expected_expr, String_View expected,
const char *actual_expr, String_View actual)
{
if (!sv_eq(expected, actual)) {
fprintf(stderr, "%s:%zu: FAILED: %s == %s\n",
file, line, expected_expr, actual_expr);
fprintf(stderr, " EXPECTED: " SV_Fmt "\n", SV_Arg(expected));
fprintf(stderr, " ACTUAL: " SV_Fmt "\n", SV_Arg(actual));
exit(1);
}
}
void sv_assert_eq_size_t(const char *file, size_t line,
const char *expected_expr, size_t expected,
const char *actual_expr, size_t actual)
{
if (expected != actual) {
fprintf(stderr, "%s:%zu: FAILED: %s == %s\n",
file, line, expected_expr, actual_expr);
fprintf(stderr, " EXPECTED: %zu\n", expected);
fprintf(stderr, " ACTUAL: %zu\n", actual);
exit(1);
}
}
void sv_assert_eq_uint64_t(const char *file, size_t line,
const char *expected_expr, uint64_t expected,
const char *actual_expr, uint64_t actual)
{
if (expected != actual) {
fprintf(stderr, "%s:%zu: FAILED: %s == %s\n",
file, line, expected_expr, actual_expr);
fprintf(stderr, " EXPECTED: %" PRIu64 "\n", expected);
fprintf(stderr, " ACTUAL: %" PRIu64 "\n", actual);
exit(1);
}
}
#define ASSERT_EQ(type, expected, actual) sv_assert_eq_##type(__FILE__, __LINE__, #expected, expected, #actual, actual)
void sv_assert_true(const char *file, size_t line,
const char *expression_cstr,
bool expression)
{
if (!expression) {
fprintf(stderr, "%s:%zu: FAILED: %s\n", file, line, expression_cstr);
exit(1);
}
}
#define ASSERT_TRUE(expression) sv_assert_true(__FILE__, __LINE__, #expression, expression);
bool is_alpha(char x)
{
return isalpha(x);
}
int main(void)
{
// Construct
{
ASSERT_EQ(String_View, SV("Foo"), sv_from_cstr("Foo"));
}
// Trimming Whitespace
{
ASSERT_EQ(String_View, SV("hello "), sv_trim_left(SV(" hello ")));
ASSERT_EQ(String_View, SV(" hello"), sv_trim_right(SV(" hello ")));
ASSERT_EQ(String_View, SV("hello"), sv_trim(SV(" hello ")));
}
// Chop by delimiter
{
// Existing
{
String_View input = SV_STATIC("hello\nworld");
String_View line = sv_chop_by_delim(&input, '\n');
ASSERT_EQ(String_View, SV("hello"), line);
ASSERT_EQ(String_View, SV("world"), input);
}
// Non-Existing
{
String_View input = SV_STATIC("hello\nworld");
String_View line = sv_chop_by_delim(&input, ' ');
ASSERT_EQ(String_View, SV("hello\nworld"), line);
ASSERT_EQ(String_View, SV(""), input);
}
}
// Chop by String_View (thicc delimiter)
{
// Existing
{
String_View input = SV_STATIC("hello\nworld\ngoodbye");
String_View line = sv_chop_by_sv(&input, SV("\nwor"));
ASSERT_EQ(String_View, SV("hello"), line);
ASSERT_EQ(String_View, SV("ld\ngoodbye"), input);
}
// Non-Existing
{
String_View input = SV_STATIC("hello\nworld");
String_View line = sv_chop_by_sv(&input, SV("goodbye"));
ASSERT_EQ(String_View, SV("hello\nworld"), line);
ASSERT_EQ(String_View, SV(""), input);
}
}
// Try to chop by delimiter
{
// Existing
{
String_View input = SV_STATIC("hello\nworld");
String_View line = SV_NULL;
bool result = sv_try_chop_by_delim(&input, '\n', &line);
ASSERT_TRUE(result);
ASSERT_EQ(String_View, SV("hello"), line);
ASSERT_EQ(String_View, SV("world"), input);
}
// Non-Existing
{
String_View input = SV_STATIC("hello\nworld");
String_View line = SV_NULL;
bool result = sv_try_chop_by_delim(&input, ' ', &line);
ASSERT_TRUE(!result);
ASSERT_EQ(String_View, SV(""), line);
ASSERT_EQ(String_View, SV("hello\nworld"), input);
}
}
// Chop N characters
{
// Chop left
{
String_View input = SV_STATIC("hello");
String_View hell = sv_chop_left(&input, 4);
ASSERT_EQ(String_View, SV("o"), input);
ASSERT_EQ(String_View, SV("hell"), hell);
}
// Overchop left
{
String_View input = SV_STATIC("hello");
String_View hell = sv_chop_left(&input, 10);
ASSERT_EQ(String_View, SV(""), input);
ASSERT_EQ(String_View, SV("hello"), hell);
}
// Chop right
{
String_View input = SV_STATIC("hello");
String_View hell = sv_chop_right(&input, 4);
ASSERT_EQ(String_View, SV("h"), input);
ASSERT_EQ(String_View, SV("ello"), hell);
}
// Overchop right
{
String_View input = SV_STATIC("hello");
String_View hell = sv_chop_right(&input, 10);
ASSERT_EQ(String_View, SV(""), input);
ASSERT_EQ(String_View, SV("hello"), hell);
}
}
// Take while
{
// Take while is_alpha
{
String_View input = SV_STATIC("hello1234");
String_View hello = sv_take_left_while(input, is_alpha);
ASSERT_EQ(String_View, SV("hello1234"), input);
ASSERT_EQ(String_View, SV("hello"), hello);
}
// Overtake while
{
String_View input = SV_STATIC("helloworld");
String_View hello = sv_take_left_while(input, is_alpha);
ASSERT_EQ(String_View, SV("helloworld"), input);
ASSERT_EQ(String_View, SV("helloworld"), hello);
}
}
// Chop while
{
// Chop while is_alpha
{
String_View input = SV_STATIC("hello1234");
String_View hello = sv_chop_left_while(&input, is_alpha);
ASSERT_EQ(String_View, SV("1234"), input);
ASSERT_EQ(String_View, SV("hello"), hello);
}
// Overchop while
{
String_View input = SV_STATIC("helloworld");
String_View hello = sv_chop_left_while(&input, is_alpha);
ASSERT_EQ(String_View, SV(""), input);
ASSERT_EQ(String_View, SV("helloworld"), hello);
}
}
// Equals, ignoring case
{
// exactly equal
{
String_View input = SV_STATIC("hello, world");
ASSERT_TRUE(sv_eq_ignorecase(input, SV("hello, world")));
}
// equal ignoring case
{
String_View input = SV_STATIC("Hello, World");
ASSERT_TRUE(sv_eq_ignorecase(input, SV("hello, world")));
}
// unequal
{
String_View input = SV_STATIC("Goodbye, World");
ASSERT_TRUE(!(sv_eq_ignorecase(input, SV("Hello, World"))));
}
}
// Index of
{
size_t index = 0;
ASSERT_TRUE(sv_index_of(SV("hello world"), ' ', &index));
ASSERT_EQ(size_t, 5, index);
}
// Prefix/suffix check
{
ASSERT_TRUE(sv_starts_with(SV("Hello, World"), SV("Hello")));
ASSERT_TRUE(sv_ends_with(SV("Hello, World"), SV("World")));
}
// To Integer
{
String_View input = SV_STATIC("1234567890");
ASSERT_EQ(uint64_t, 1234567890, sv_to_u64(input));
ASSERT_EQ(String_View, input, SV("1234567890"));
ASSERT_EQ(uint64_t, 1234567890, sv_chop_u64(&input));
ASSERT_TRUE(input.count == 0);
}
printf("OK\n");
return 0;
}