Repository navigation
Expand file tree
/
Copy pathtest_spstring.c
More file actions
343 lines (308 loc) · 12.4 KB
/
Copy pathtest_spstring.c
File metadata and controls
343 lines (308 loc) · 12.4 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifndef SPSTRING_NO_THREADS
#include <pthread.h>
#endif
#include "spstring.h"
static int failures;
#define CHECK(condition) do { \
if (!(condition)) { \
fprintf(stderr, "%s:%d: check failed: %s\n", \
__FILE__, __LINE__, #condition); \
++failures; \
} \
} while (0)
static void check_string(const String *string, const char *expected)
{
CHECK(string != NULL);
if (string != NULL)
{
CHECK(string->str != NULL);
if (string->str != NULL)
{
CHECK(strcmp(string->str, expected) == 0);
CHECK(string->len == strlen(expected));
CHECK(string->sz > string->len);
}
}
}
static void test_dynamic_strings(void)
{
String *source = newString("alpha");
String *destination = newString("x");
CHECK(source != NULL);
CHECK(destination != NULL);
if (source == NULL || destination == NULL)
return;
CHECK(source->len == 5 && source->sz == 6);
CHECK(stringlen(source) == 5);
CHECK(stringcpy(destination, source) == 5);
check_string(destination, "alpha");
CHECK(destination->sz >= 6);
CHECK(stringcpy(destination, destination) == 5);
CHECK(stringchcpy(destination, "a longer C string") == 17);
check_string(destination, "a longer C string");
CHECK(stringcat(destination, source) == 5);
check_string(destination, "a longer C stringalpha");
CHECK(stringchcat(destination, "!") == 1);
check_string(destination, "a longer C stringalpha!");
CHECK(stringcat(destination, destination) == destination->len / 2);
check_string(destination, "a longer C stringalpha!a longer C stringalpha!");
String *duplicate = stringdup(source);
check_string(duplicate, "alpha");
if (duplicate != NULL)
{
CHECK(duplicate != source);
CHECK(duplicate->str != source->str);
delString(duplicate);
}
CHECK(stringcmp(source, destination) > 0);
String *z = newString("z");
String *aa = newString("aa");
CHECK(z != NULL && aa != NULL);
if (z != NULL && aa != NULL)
CHECK(stringcmp(z, aa) > 0);
CHECK(stringchcmp(source, "alpha") == 0);
CHECK(stringchcmp(source, "alph") > 0);
delString(z);
delString(aa);
CHECK(stringtrunc(destination, 5) == destination);
check_string(destination, "a lon");
CHECK(stringtrunc(destination, 0) == destination);
check_string(destination, "");
CHECK(stringtrunc(destination, 100) == destination);
check_string(destination, "");
stringprintf(destination, "%s %d", "value", 42);
check_string(destination, "value 42");
CHECK(destination->sz >= destination->len + 1);
delString(source);
delString(destination);
}
static void test_local_strings(void)
{
char clipped[12] = "longvalue";
LString initialized = localString(clipped, 5);
CHECK(initialized.sz == 5);
CHECK(initialized.len == 4);
CHECK(strcmp(initialized.str, "long") == 0);
char empty_buffer[8] = "";
char source_buffer[8] = "source";
char destination_buffer[9] = "x";
LString empty = localString(empty_buffer, sizeof empty_buffer);
LString source = localString(source_buffer, sizeof source_buffer);
LString destination = localString(destination_buffer, sizeof destination_buffer);
CHECK(empty.len == 0);
CHECK(lstringlen(&source) == 6);
CHECK(lstringcmp(&source, &destination) < 0);
CHECK(lstringchcmp(&source, "source") == 0);
CHECK(lstringchcmp(&source, "source!") < 0);
CHECK(lstringcpy(&destination, &source) == 6);
CHECK(strcmp(destination.str, "source") == 0);
CHECK(destination.len == 6);
CHECK(lstringchcpy(&destination, "xy") == 2);
CHECK(strcmp(destination.str, "xy") == 0);
CHECK(lstringcat(&destination, &source) == 6);
CHECK(strcmp(destination.str, "xysource") == 0);
CHECK(destination.len == 8);
CHECK(lstringchcat(&destination, "!") == (size_t) ST_OVERWRITE_ERR);
CHECK(strcmp(destination.str, "xysource") == 0);
CHECK(destination.len == 8);
char small_buffer[4] = "a";
char too_long_buffer[8] = "long";
LString small = localString(small_buffer, sizeof small_buffer);
LString too_long = localString(too_long_buffer, sizeof too_long_buffer);
CHECK(lstringcpy(&small, &too_long) == (size_t) ST_OVERWRITE_ERR);
CHECK(strcmp(small.str, "a") == 0);
CHECK(lstringchcpy(&small, "long") == (size_t) ST_OVERWRITE_ERR);
CHECK(strcmp(small.str, "a") == 0);
CHECK(lstringcat(&small, &too_long) == (size_t) ST_OVERWRITE_ERR);
CHECK(strcmp(small.str, "a") == 0);
CHECK(lstringchcat(&small, "long") == (size_t) ST_OVERWRITE_ERR);
CHECK(strcmp(small.str, "a") == 0);
CHECK(lstringtrunc(&destination, 3) == &destination);
CHECK(strcmp(destination.str, "xys") == 0);
CHECK(destination.len == 3);
CHECK(lstringtrunc(&destination, 0) == &destination);
CHECK(strcmp(destination.str, "") == 0);
LString invalid = localString(empty_buffer, 0);
CHECK(invalid.str == NULL);
}
static int compare_strings(const void *a, const void *b)
{
return stringcmp(*(String *const *) a, *(String *const *) b);
}
static void test_comparison_ordering(void)
{
String *b = newString("b");
String *a = newString("a");
String nobuf = { 0, 0, NULL };
CHECK(stringcmp(NULL, a) < 0);
CHECK(stringcmp(a, NULL) > 0);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(stringcmp(&nobuf, a) < 0);
CHECK(get_stringerr() == ST_NULLSTR_ERR);
CHECK(stringchcmp(a, NULL) > 0);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
String *array[] = { b, NULL, a };
qsort(array, 3, sizeof array[0], compare_strings);
CHECK(array[0] == NULL && array[1] == a && array[2] == b);
get_stringerr();
delString(a);
delString(b);
}
#ifndef SPSTRING_NO_THREADS
static void *thread_error(void *arg)
{
(void) arg;
CHECK(get_stringerr() == ST_NO_ERR); /* not the main thread's error */
stringlen(NULL);
return (void *) (long) get_stringerr();
}
#endif
static void test_thread_local_error(void)
{
#ifndef SPSTRING_NO_THREADS
pthread_t thread;
void *result = NULL;
CHECK(newString(NULL) == NULL); /* error in main thread */
CHECK(pthread_create(&thread, NULL, thread_error, NULL) == 0);
CHECK(pthread_join(thread, &result) == 0);
CHECK((long) result == ST_NULLPARAM_ERR);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
#endif
}
static void test_errors_and_validation(void)
{
CHECK(newString(NULL) == NULL);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(get_stringerr() == ST_NO_ERR);
delString(NULL);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(stringlen(NULL) == (size_t) ST_NULLPARAM_ERR);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(stringcpy(NULL, NULL) == (size_t) ST_NULLPARAM_ERR);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(stringchcpy(NULL, "x") == (size_t) ST_NULLPARAM_ERR);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(stringcat(NULL, NULL) == (size_t) ST_NULLPARAM_ERR);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(stringchcat(NULL, "x") == (size_t) ST_NULLPARAM_ERR);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(stringdup(NULL) == NULL);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(stringcmp(NULL, NULL) == 0);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(stringchcmp(NULL, "x") < 0);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(stringtrunc(NULL, 0) == NULL);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(lstringlen(NULL) == (size_t) ST_NULLPARAM_ERR);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(lstringcmp(NULL, NULL) == 0);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(lstringchcmp(NULL, "x") < 0);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(lstringtrunc(NULL, 0) == NULL);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
stringprintf(NULL, "%s", "x");
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
String *format_target = newString("");
stringprintf(format_target, NULL);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
delString(format_target);
char buffer[] = "ok";
String valid = { sizeof buffer, 2, buffer };
CHECK(STRINGCHECK(&valid) == 0);
LString lvalid = { sizeof buffer, 2, buffer };
CHECK(LSTRINGCHECK(&lvalid) == 0);
CHECK(lstringcheck(NULL, NULL, 0) == ST_NULLPARAM_ERR);
LString lbad_length = { sizeof buffer, 1, buffer };
CHECK(lstringcheck(&lbad_length, NULL, 0) == ST_INCONSISTENTLEN_ERR);
char unterminated[3] = { 'a', 'b', 'c' };
String no_nul = { sizeof unterminated, 2, unterminated };
CHECK(stringcheck(&no_nul, NULL, 0) == ST_INCONSISTENTLEN_ERR);
CHECK(stringcheck(NULL, NULL, 0) == ST_NULLPARAM_ERR);
String no_buffer = { 0, 0, NULL };
CHECK(stringcheck(&no_buffer, NULL, 0) == ST_NULLSTR_ERR);
String bad_size = { 2, 2, buffer };
CHECK(stringcheck(&bad_size, NULL, 0) == ST_INCONSISTENTSZ_ERR);
String bad_length = { sizeof buffer, 1, buffer };
CHECK(stringcheck(&bad_length, NULL, 0) == ST_INCONSISTENTLEN_ERR);
CHECK(stringtrunc(&bad_size, 0) == NULL);
CHECK(get_stringerr() == ST_INCONSISTENTSZ_ERR);
LString bad_local_size = { 1, 1, buffer };
CHECK(lstringtrunc(&bad_local_size, 0) == NULL);
CHECK(get_stringerr() == ST_INCONSISTENTSZ_ERR);
LString null_local = localString(NULL, 3);
CHECK(null_local.str == NULL);
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
}
static void test_aliasing(void)
{
/* Source pointing inside the destination buffer, with reallocation */
String *s = newString("abcdef");
CHECK(s != NULL);
if (s == NULL)
return;
CHECK(stringchcat(s, s->str) == 6);
check_string(s, "abcdefabcdef");
CHECK(stringchcat(s, s->str + 6) == 6);
check_string(s, "abcdefabcdefabcdef");
CHECK(stringchcpy(s, s->str + 12) == 6);
check_string(s, "abcdef");
stringprintf(s, "%s-%s-%s", s->str, s->str, s->str);
check_string(s, "abcdef-abcdef-abcdef");
stringprintf(s, "%.3s", s->str + 7);
check_string(s, "abc");
delString(s);
s = newString("abc");
CHECK(s != NULL);
if (s == NULL)
return;
String view = *s;
CHECK(stringcat(s, &view) == 3);
check_string(s, "abcabc");
delString(s);
s = newString("abcd");
CHECK(s != NULL);
if (s == NULL)
return;
String partial_view = { 4, 3, s->str + 1 };
CHECK(stringcat(s, &partial_view) == 3);
check_string(s, "abcdbcd");
delString(s);
}
static void test_error_preserved_by_growth(void)
{
CHECK(newString(NULL) == NULL);
String *s = newString("a");
CHECK(s != NULL);
if (s != NULL)
{
CHECK(stringchcat(s, "bcdefgh") == 7);
check_string(s, "abcdefgh");
stringprintf(s, "%s%s", s->str, s->str);
check_string(s, "abcdefghabcdefgh");
delString(s);
}
CHECK(get_stringerr() == ST_NULLPARAM_ERR);
CHECK(get_stringerr() == ST_NO_ERR);
}
int main(void)
{
test_dynamic_strings();
test_local_strings();
test_errors_and_validation();
test_comparison_ordering();
test_thread_local_error();
test_aliasing();
test_error_preserved_by_growth();
if (failures != 0)
{
fprintf(stderr, "%d test(s) failed\n", failures);
return 1;
}
puts("All SPString tests passed.");
return 0;
}