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
|
#include "first.h"
#undef NDEBUG
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "buffer.c"
static void run_buffer_path_simplify(buffer *psrc, buffer *pdest, const char *in, size_t in_len, const char *out, size_t out_len) {
buffer_copy_string_len(psrc, in, in_len);
pdest = psrc; /*(buffer_path_simplify() now takes only one arg)*/
buffer_path_simplify(pdest);
if (!buffer_eq_slen(pdest, out, out_len)) {
fprintf(stderr,
"%s.%d: buffer_path_simplify('%s') failed: expected '%s', got '%s'\n",
__FILE__,
__LINE__,
in,
out,
pdest->ptr);
fflush(stderr);
abort();
} else {
buffer_path_simplify(pdest);
if (!buffer_eq_slen(pdest, out, out_len)) {
fprintf(stderr,
"%s.%d: buffer_path_simplify('%s') failed - not idempotent: expected '%s', got '%s'\n",
__FILE__,
__LINE__,
in,
out,
pdest->ptr);
fflush(stderr);
abort();
}
}
}
static void test_buffer_path_simplify_with(buffer *psrc, buffer *pdest) {
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN(""), CONST_STR_LEN(""));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/"), CONST_STR_LEN("/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("//"), CONST_STR_LEN("/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc"), CONST_STR_LEN("abc"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc//"), CONST_STR_LEN("abc/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc/./xyz"), CONST_STR_LEN("abc/xyz"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc/.//xyz"), CONST_STR_LEN("abc/xyz"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc/../xyz"), CONST_STR_LEN("/xyz"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/abc/./xyz"), CONST_STR_LEN("/abc/xyz"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/abc//./xyz"), CONST_STR_LEN("/abc/xyz"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/abc/../xyz"), CONST_STR_LEN("/xyz"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc/../xyz/."), CONST_STR_LEN("/xyz/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/abc/../xyz/."), CONST_STR_LEN("/xyz/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("abc/./xyz/.."), CONST_STR_LEN("abc/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/abc/./xyz/.."), CONST_STR_LEN("/abc/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("."), CONST_STR_LEN(""));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN(".."), CONST_STR_LEN(""));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("..."), CONST_STR_LEN("..."));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("...."), CONST_STR_LEN("...."));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN(".../"), CONST_STR_LEN(".../"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("./xyz/.."), CONST_STR_LEN("/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN(".//xyz/.."), CONST_STR_LEN("/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/./xyz/.."), CONST_STR_LEN("/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN(".././xyz/.."), CONST_STR_LEN("/"));
run_buffer_path_simplify(psrc, pdest, CONST_STR_LEN("/.././xyz/.."), CONST_STR_LEN("/"));
}
static void test_buffer_path_simplify(void) {
buffer *psrc = buffer_init();
/* test with using the same buffer and with using different buffers */
test_buffer_path_simplify_with(psrc, psrc);
buffer_free(psrc);
}
static void test_buffer_to_lower_upper(void) {
buffer *psrc = buffer_init();
buffer_copy_string_len(psrc, CONST_STR_LEN("0123456789abcdefghijklmnopqrstuvwxyz"));
buffer_to_lower(psrc);
assert(buffer_eq_slen(psrc, CONST_STR_LEN("0123456789abcdefghijklmnopqrstuvwxyz")));
buffer_to_upper(psrc);
assert(buffer_eq_slen(psrc, CONST_STR_LEN("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")));
buffer_to_upper(psrc);
assert(buffer_eq_slen(psrc, CONST_STR_LEN("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")));
buffer_to_lower(psrc);
assert(buffer_eq_slen(psrc, CONST_STR_LEN("0123456789abcdefghijklmnopqrstuvwxyz")));
buffer_free(psrc);
}
static void test_buffer_string_space(void) {
buffer *b = buffer_init();
size_t space;
space = buffer_string_space(b);
assert(0 == space);
buffer_copy_string_len(b, CONST_STR_LEN(""));
space = buffer_string_space(b);
assert(space > 0);
assert(space + buffer_string_length(b) == b->size - 1);
buffer_commit(b, b->size - 1);
assert(b->used == b->size);
space = buffer_string_space(b);
assert(0 == space);
buffer_free(b);
}
static void test_buffer_append_path_len(void) {
buffer *b = buffer_init();
buffer_append_path_len(b, CONST_STR_LEN("a"));
assert(buffer_eq_slen(b, CONST_STR_LEN("/a")));
buffer_clear(b);
buffer_append_path_len(b, CONST_STR_LEN("a"));
assert(buffer_eq_slen(b, CONST_STR_LEN("/a")));
buffer_clear(b);
buffer_append_path_len(b, CONST_STR_LEN("/a"));
assert(buffer_eq_slen(b, CONST_STR_LEN("/a")));
buffer_copy_string_len(b, CONST_STR_LEN("/"));
buffer_append_path_len(b, CONST_STR_LEN("a"));
assert(buffer_eq_slen(b, CONST_STR_LEN("/a")));
buffer_copy_string_len(b, CONST_STR_LEN("/"));
buffer_append_path_len(b, CONST_STR_LEN("/a"));
assert(buffer_eq_slen(b, CONST_STR_LEN("/a")));
buffer_copy_string_len(b, CONST_STR_LEN("a"));
buffer_append_path_len(b, CONST_STR_LEN("a"));
assert(buffer_eq_slen(b, CONST_STR_LEN("a/a")));
buffer_copy_string_len(b, CONST_STR_LEN("a/"));
buffer_append_path_len(b, CONST_STR_LEN("a"));
assert(buffer_eq_slen(b, CONST_STR_LEN("a/a")));
buffer_copy_string_len(b, CONST_STR_LEN("a/"));
buffer_append_path_len(b, CONST_STR_LEN("/a"));
assert(buffer_eq_slen(b, CONST_STR_LEN("a/a")));
buffer_copy_string_len(b, CONST_STR_LEN("/a/"));
buffer_append_path_len(b, CONST_STR_LEN("/a"));
assert(buffer_eq_slen(b, CONST_STR_LEN("/a/a")));
buffer_free(b);
}
static void test_buffer_append_bs_escaped(void) {
buffer *b = buffer_init();
buffer_append_bs_escaped_json(b, CONST_STR_LEN(" "));
assert(buffer_eq_slen(b, CONST_STR_LEN(" ")));
buffer_clear(b);
buffer_append_bs_escaped_json(b, CONST_STR_LEN("\0"));
assert(buffer_eq_slen(b, CONST_STR_LEN("\\u0000")));
buffer_clear(b);
buffer_append_bs_escaped_json(b, CONST_STR_LEN("\1"));
assert(buffer_eq_slen(b, CONST_STR_LEN("\\u0001")));
buffer_clear(b);
buffer_append_bs_escaped_json(b, CONST_STR_LEN("\n"));
assert(buffer_eq_slen(b, CONST_STR_LEN("\\n")));
buffer_clear(b);
buffer_append_bs_escaped_json(b, CONST_STR_LEN("é"));
assert(buffer_eq_slen(b, CONST_STR_LEN("é")));
buffer_clear(b);
buffer_append_bs_escaped_json(b, CONST_STR_LEN("ö"));
assert(buffer_eq_slen(b, CONST_STR_LEN("ö")));
#if 0
buffer_clear(b);
magnet_buffer_append_bsdec(b, CONST_STR_LEN("\\u00E9"));
assert(buffer_eq_slen(b, CONST_STR_LEN("é")));
buffer_clear(b);
magnet_buffer_append_bsdec(b, CONST_STR_LEN("\\u00F6"));
assert(buffer_eq_slen(b, CONST_STR_LEN("ö")));
#endif
/* TODO: more */
buffer_free(b);
}
void test_buffer (void);
void test_buffer (void)
{
test_buffer_path_simplify();
test_buffer_to_lower_upper();
test_buffer_string_space();
test_buffer_append_path_len();
test_buffer_append_bs_escaped();
}
|