Repository navigation
Expand file tree
/
Copy pathtester.c
More file actions
673 lines (540 loc) · 16.3 KB
/
Copy pathtester.c
File metadata and controls
673 lines (540 loc) · 16.3 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dstr.h"
static int tests_run = 0;
static int tests_failed = 0;
#define ASSERT_TRUE(expr) do { \
tests_run++; \
if (!(expr)) { \
printf(" \033[1;31m[FAIL]\033[0m %s:%d: Assertion failed: %s\n", __FILE__, __LINE__, #expr); \
tests_failed++; \
return; \
} \
} while (0)
#define ASSERT_STR_EQUAL(expected, actual) do { \
tests_run++; \
if (strcmp((expected), (actual)) != 0) { \
printf(" \033[1;31m[FAIL]\033[0m %s:%d: Strings differ.\n Expected: \"%s\"\n Got: \"%s\"\n", __FILE__, __LINE__, expected, actual); \
tests_failed++; \
return; \
} \
} while (0)
#define RUN_TEST(test_func) do { \
printf("Running %s...\n", #test_func); \
test_func(); \
} while (0)
static void test_find_basic(void) {
dstr s = dstrnew("Hello World");
ASSERT_TRUE(dstrfind(s, "o W") == 4);
ASSERT_TRUE(dstrfind(s, "Hello") == 0);
ASSERT_TRUE(dstrfind(s, "World") == 6);
dstrfree(s);
}
static void test_find_not_found(void) {
dstr s = dstrnew("System Integrity");
ASSERT_TRUE(dstrfind(s, "Hack") == -1);
ASSERT_TRUE(dstrfind(s, "system") == -1);
dstrfree(s);
}
static void test_find_edge_cases(void) {
dstr s = dstrnew("data");
ASSERT_TRUE(dstrfind(s, "") == -1);
ASSERT_TRUE(dstrfind(s, "database") == -1);
ASSERT_TRUE(dstrfind(s, NULL) == -1);
ASSERT_TRUE(dstrfind(NULL, "data") == -1);
dstrfree(s);
dstr empty = dstrnew("");
ASSERT_TRUE(dstrfind(empty, "a") == -1);
dstrfree(empty);
}
static void test_find_overlaps_and_repeats(void) {
dstr s = dstrnew("abacadabra");
ASSERT_TRUE(dstrfind(s, "a") == 0);
ASSERT_TRUE(dstrfind(s, "ab") == 0);
ASSERT_TRUE(dstrfind(s, "cad") == 3);
ASSERT_TRUE(dstrfind(s, "abra") == 6);
dstrfree(s);
}
static void test_creation_and_properties(void) {
dstr s = dstrnew("Hello");
ASSERT_TRUE(s != NULL);
ASSERT_STR_EQUAL("Hello", s);
ASSERT_TRUE(dstrlen(s) == 5);
ASSERT_TRUE(dstrcap(s) >= 6); // length + \0
dstrfree(s);
}
static void test_capacity_hint(void) {
dstr s = dstrnew("Hello", 100);
ASSERT_TRUE(s != NULL);
ASSERT_TRUE(dstrlen(s) == 5);
ASSERT_TRUE(dstrcap(s) == 100);
dstrfree(s);
}
static void test_concatenation(void) {
dstr s = dstrnew("Hello");
s = dstrcat(s, " World");
ASSERT_TRUE(s != NULL);
ASSERT_STR_EQUAL("Hello World", s);
ASSERT_TRUE(dstrlen(s) == 11);
dstrfree(s);
}
static void test_push_char(void) {
dstr s = dstrnew("Hello");
s = dstrpush(s, '!');
ASSERT_TRUE(s != NULL);
ASSERT_STR_EQUAL("Hello!", s);
ASSERT_TRUE(dstrlen(s) == 6);
dstrfree(s);
}
static void test_clear_and_zero(void) {
dstr s = dstrnew("Data");
size_t orig_cap = dstrcap(s);
dstrclear(s);
ASSERT_TRUE(dstrlen(s) == 0);
ASSERT_STR_EQUAL("", s);
ASSERT_TRUE(dstrcap(s) == orig_cap);
dstrfree(s);
}
static void test_tolower_basic(void) {
dstr s = dstrnew("Hello World! 123");
size_t len = dstrlen(s);
size_t cap = dstrcap(s);
dstrtolower(s);
ASSERT_STR_EQUAL("hello world! 123", s);
ASSERT_TRUE(dstrlen(s) == len);
ASSERT_TRUE(dstrcap(s) == cap);
dstrfree(s);
}
static void test_toupper_basic(void) {
dstr s = dstrnew("Hello World! 123");
size_t len = dstrlen(s);
size_t cap = dstrcap(s);
dstrtoupper(s);
ASSERT_STR_EQUAL("HELLO WORLD! 123", s);
ASSERT_TRUE(dstrlen(s) == len);
ASSERT_TRUE(dstrcap(s) == cap);
dstrfree(s);
}
static void test_case_conversion_already_converted(void) {
dstr lower = dstrnew("already lower");
dstrtolower(lower);
ASSERT_STR_EQUAL("already lower", lower);
dstrfree(lower);
dstr upper = dstrnew("ALREADY UPPER");
dstrtoupper(upper);
ASSERT_STR_EQUAL("ALREADY UPPER", upper);
dstrfree(upper);
}
static void test_case_conversion_empty(void) {
dstr s = dstrnew("");
dstrtolower(s);
ASSERT_STR_EQUAL("", s);
ASSERT_TRUE(dstrlen(s) == 0);
dstrtoupper(s);
ASSERT_STR_EQUAL("", s);
ASSERT_TRUE(dstrlen(s) == 0);
dstrfree(s);
}
static void test_case_conversion_null(void) {
// Must not crash on NULL input.
dstrtolower(NULL);
dstrtoupper(NULL);
ASSERT_TRUE(true);
}
static void test_case_conversion_non_ascii_untouched(void) {
// Bytes >= 0x80 (e.g. UTF-8 continuation/lead bytes) must be left
// untouched, and must not trigger undefined behavior on signed char.
dstr s = dstrnew("\xC3\x89LLO \xC3\xA9llo");
size_t len = dstrlen(s);
dstrtolower(s);
ASSERT_STR_EQUAL("\xC3\x89llo \xC3\xA9llo", s);
ASSERT_TRUE(dstrlen(s) == len);
dstr s2 = dstrnew("\xC3\x89LLO \xC3\xA9llo");
dstrtoupper(s2);
ASSERT_STR_EQUAL("\xC3\x89LLO \xC3\xA9LLO", s2);
dstrfree(s);
dstrfree(s2);
}
static void test_case_conversion_roundtrip(void) {
dstr s = dstrnew("MiXeD CaSe 42!");
dstr original = dstrdup(s);
dstrtoupper(s);
dstrtolower(s);
// Original had lowercase letters already lowercase and uppercase
// letters that become lowercase too, so compare against a fully
// lowercased reference instead of the original.
dstrtolower(original);
ASSERT_STR_EQUAL(original, s);
dstrfree(s);
dstrfree(original);
}
static void test_range_basic(void) {
dstr s = dstrnew("Hello World");
dstrrange(s, 0, 4);
ASSERT_STR_EQUAL("Hello", s);
ASSERT_TRUE(dstrlen(s) == 5);
dstrfree(s);
s = dstrnew("Hello World");
dstrrange(s, 6, 10);
ASSERT_STR_EQUAL("World", s);
dstrfree(s);
s = dstrnew("Hello World");
dstrrange(s, 0, 10);
ASSERT_STR_EQUAL("Hello World", s);
dstrfree(s);
}
static void test_range_negative_index(void) {
dstr s = dstrnew("Hello World");
dstrrange(s, -5, -2);
ASSERT_STR_EQUAL("Worl", s);
dstrfree(s);
s = dstrnew("Hello World");
dstrrange(s, -11, -7);
ASSERT_STR_EQUAL("Hello", s);
dstrfree(s);
s = dstrnew("Hello World");
dstrrange(s, 0, -2);
ASSERT_STR_EQUAL("Hello Worl", s);
dstrfree(s);
s = dstrnew("Hello World");
dstrrange(s, -5, 10);
ASSERT_STR_EQUAL("World", s);
dstrfree(s);
}
static void test_range_out_of_bounds(void) {
dstr s = dstrnew("abc");
// Indices past either end are clamped, not an error.
dstrrange(s, -100, 100);
ASSERT_STR_EQUAL("abc", s);
dstrfree(s);
s = dstrnew("abc");
dstrrange(s, 5, 10);
ASSERT_STR_EQUAL("", s);
ASSERT_TRUE(dstrlen(s) == 0);
dstrfree(s);
s = dstrnew("abc");
dstrrange(s, 2, 1);
ASSERT_STR_EQUAL("", s);
dstrfree(s);
// Both indices resolve to a clamped 0 rather than an empty range: this
// mirrors sdsrange, whose clamp-to-0 path (unlike its clamp-to-len
// path) does not force newlen to 0.
s = dstrnew("abc");
dstrrange(s, -100, -50);
ASSERT_STR_EQUAL("a", s);
dstrfree(s);
}
static void test_range_null(void) {
dstrrange(NULL, 0, 5);
}
static void test_range_empty_string(void) {
dstr s = dstrnew("");
dstrrange(s, -5, 5);
ASSERT_STR_EQUAL("", s);
dstrfree(s);
}
static void test_range_in_place(void) {
// dstrrange mutates the existing buffer, no new allocation.
dstr s = dstrnew("Hello World");
dstr same = s;
dstrrange(s, 0, 4);
ASSERT_TRUE(s == same);
ASSERT_STR_EQUAL("Hello", s);
dstrfree(s);
}
static void test_split_basic(void) {
dstr s = dstrnew("a,b,c");
size_t count = 0;
dstr* parts = dstrsplit(s, ",", &count);
ASSERT_TRUE(parts != NULL);
ASSERT_TRUE(count == 3);
ASSERT_STR_EQUAL("a", parts[0]);
ASSERT_STR_EQUAL("b", parts[1]);
ASSERT_STR_EQUAL("c", parts[2]);
dstrsplitfree(parts, count);
dstrfree(s);
}
static void test_split_adjacent_and_edge_delimiters(void) {
dstr s = dstrnew(",a,,b,");
size_t count = 0;
dstr* parts = dstrsplit(s, ",", &count);
ASSERT_TRUE(count == 5);
ASSERT_STR_EQUAL("", parts[0]);
ASSERT_STR_EQUAL("a", parts[1]);
ASSERT_STR_EQUAL("", parts[2]);
ASSERT_STR_EQUAL("b", parts[3]);
ASSERT_STR_EQUAL("", parts[4]);
dstrsplitfree(parts, count);
dstrfree(s);
}
static void test_split_multichar_delim(void) {
dstr s = dstrnew("one::two::three");
size_t count = 0;
dstr* parts = dstrsplit(s, "::", &count);
ASSERT_TRUE(count == 3);
ASSERT_STR_EQUAL("one", parts[0]);
ASSERT_STR_EQUAL("two", parts[1]);
ASSERT_STR_EQUAL("three", parts[2]);
dstrsplitfree(parts, count);
dstrfree(s);
}
static void test_split_no_match(void) {
dstr s = dstrnew("no-delimiter-here");
size_t count = 0;
dstr* parts = dstrsplit(s, ";", &count);
ASSERT_TRUE(count == 1);
ASSERT_STR_EQUAL("no-delimiter-here", parts[0]);
dstrsplitfree(parts, count);
dstrfree(s);
}
static void test_split_edge_cases(void) {
dstr s = dstrnew("data");
size_t count = 123;
ASSERT_TRUE(dstrsplit(NULL, ",", &count) == NULL);
ASSERT_TRUE(count == 0);
count = 123;
ASSERT_TRUE(dstrsplit(s, NULL, &count) == NULL);
ASSERT_TRUE(count == 0);
count = 123;
ASSERT_TRUE(dstrsplit(s, "", &count) == NULL);
ASSERT_TRUE(count == 0);
// out_count is optional.
dstr* parts = dstrsplit(s, "z", NULL);
ASSERT_TRUE(parts != NULL);
dstrsplitfree(parts, 1);
dstrsplitfree(NULL, 0);
dstrfree(s);
}
static void test_split_independent_allocations(void) {
// Mutating one part must not affect the source or the other parts.
dstr s = dstrnew("aa,bb");
size_t count = 0;
dstr* parts = dstrsplit(s, ",", &count);
dstrtoupper(parts[0]);
ASSERT_STR_EQUAL("aa,bb", s);
ASSERT_STR_EQUAL("AA", parts[0]);
ASSERT_STR_EQUAL("bb", parts[1]);
dstrsplitfree(parts, count);
dstrfree(s);
}
static void test_growth_no_unnecessary_realloc(void) {
dstr s = dstrnew("");
s = dstrreserve(s, 128);
size_t cap_before = dstrcap(s);
for (int i = 0; i < 20; i++)
s = dstrcat(s, "x");
ASSERT_TRUE(dstrlen(s) == 20);
ASSERT_TRUE(dstrcap(s) == cap_before);
dstrfree(s);
}
static void test_growth_correctness_across_reallocs(void) {
dstr s = dstrnew("");
for (int i = 0; i < 1000; i++)
s = dstrpush(s, 'a');
ASSERT_TRUE(dstrlen(s) == 1000);
ASSERT_TRUE(dstrcap(s) >= 1001);
bool all_a = true;
for (size_t i = 0; i < dstrlen(s); i++) {
if (s[i] != 'a') {
all_a = false;
break;
}
}
ASSERT_TRUE(all_a);
dstrfree(s);
}
static void test_trim_whitespace_basic(void) {
dstr s = dstrnew(" Hello World ");
dstrtrim(s);
ASSERT_STR_EQUAL("Hello World", s);
ASSERT_TRUE(dstrlen(s) == 11);
dstrfree(s);
s = dstrnew("\t\n data \r\v\f");
dstrtrim(s);
ASSERT_STR_EQUAL("data", s);
dstrfree(s);
}
static void test_trim_no_change(void) {
dstr s = dstrnew("nothing to trim");
size_t cap = dstrcap(s);
dstrtrim(s);
ASSERT_STR_EQUAL("nothing to trim", s);
ASSERT_TRUE(dstrcap(s) == cap);
dstrfree(s);
}
static void test_trim_all_whitespace(void) {
dstr s = dstrnew(" \t\n ");
dstrtrim(s);
ASSERT_STR_EQUAL("", s);
ASSERT_TRUE(dstrlen(s) == 0);
dstrfree(s);
}
static void test_trim_empty_and_null(void) {
dstr s = dstrnew("");
dstrtrim(s);
ASSERT_STR_EQUAL("", s);
dstrfree(s);
dstrtrim(NULL);
ASSERT_TRUE(true);
}
static void test_trim_custom_cutset(void) {
dstr s = dstrnew("xxxHelloxxx");
dstrtrim(s, "x");
ASSERT_STR_EQUAL("Hello", s);
dstrfree(s);
s = dstrnew("-+-value-+-");
dstrtrim(s, "+-");
ASSERT_STR_EQUAL("value", s);
dstrfree(s);
}
static void test_trim_custom_cutset_edge_cases(void) {
dstr s = dstrnew("abc");
dstrtrim(s, "");
ASSERT_STR_EQUAL("abc", s);
dstrtrim(s, NULL);
ASSERT_STR_EQUAL("abc", s);
dstrtrim(s, "abc");
ASSERT_STR_EQUAL("", s);
dstrfree(s);
}
static void test_insert_basic(void) {
dstr s = dstrnew("Hello World");
s = dstrinsert(s, 5, ",");
ASSERT_STR_EQUAL("Hello, World", s);
ASSERT_TRUE(dstrlen(s) == 12);
dstrfree(s);
}
static void test_insert_prepend_and_append(void) {
dstr s = dstrnew("World");
s = dstrinsert(s, 0, "Hello ");
ASSERT_STR_EQUAL("Hello World", s);
dstrfree(s);
s = dstrnew("Hello");
s = dstrinsert(s, 5, " World");
ASSERT_STR_EQUAL("Hello World", s);
dstrfree(s);
s = dstrnew("Hello");
s = dstrinsert(s, 1000, "!");
ASSERT_STR_EQUAL("Hello!", s);
dstrfree(s);
}
static void test_insert_negative_index(void) {
dstr s = dstrnew("Hello World");
s = dstrinsert(s, -5, "-");
ASSERT_STR_EQUAL("Hello -World", s);
dstrfree(s);
s = dstrnew("abc");
s = dstrinsert(s, -1000, "X");
ASSERT_STR_EQUAL("Xabc", s);
dstrfree(s);
}
static void test_insert_edge_cases(void) {
dstr s = dstrnew("abc");
ASSERT_TRUE(dstrinsert(NULL, 0, "x") == NULL);
dstr same = dstrinsert(s, 1, NULL);
ASSERT_TRUE(same == s);
ASSERT_STR_EQUAL("abc", s);
same = dstrinsert(s, 1, "");
ASSERT_TRUE(same == s);
ASSERT_STR_EQUAL("abc", s);
dstrfree(s);
}
static void test_insert_self_alias(void) {
// Inserting a string into itself must not read moved/corrupted memory.
dstr s = dstrnew("abc");
s = dstrinsert(s, 0, s);
ASSERT_STR_EQUAL("abcabc", s);
dstrfree(s);
s = dstrnew("abc");
s = dstrinsert(s, 1, s);
ASSERT_STR_EQUAL("aabcbc", s);
dstrfree(s);
s = dstrnew("abc");
s = dstrinsert(s, 3, s);
ASSERT_STR_EQUAL("abcabc", s);
dstrfree(s);
s = dstrnew("abcdef");
s = dstrinsert(s, 2, s + 3);
ASSERT_STR_EQUAL("abdefcdef", s);
dstrfree(s);
}
static void test_insert_growth_across_reallocs(void) {
dstr s = dstrnew("");
for (int i = 0; i < 200; i++)
s = dstrinsert(s, 0, "ab");
ASSERT_TRUE(dstrlen(s) == 400);
bool ok = true;
for (size_t i = 0; i < dstrlen(s); i += 2) {
if (s[i] != 'a' || s[i + 1] != 'b') {
ok = false;
break;
}
}
ASSERT_TRUE(ok);
dstrfree(s);
}
static void test_auto_cleanup(void) {
// Isolated block to test cleanup attribute
{
dstrauto s = dstrnew("AutoFree");
ASSERT_STR_EQUAL("AutoFree", s);
}
// If dstrauto fails or crashes, execution would stop before here.
ASSERT_TRUE(true);
}
int main(void) {
printf("\033[1;34m=== STARTING DSTR UNIT TESTS ===\033[0m\n\n");
RUN_TEST(test_find_basic);
RUN_TEST(test_find_not_found);
RUN_TEST(test_find_edge_cases);
RUN_TEST(test_find_overlaps_and_repeats);
RUN_TEST(test_creation_and_properties);
RUN_TEST(test_capacity_hint);
RUN_TEST(test_concatenation);
RUN_TEST(test_push_char);
RUN_TEST(test_clear_and_zero);
RUN_TEST(test_tolower_basic);
RUN_TEST(test_toupper_basic);
RUN_TEST(test_case_conversion_already_converted);
RUN_TEST(test_case_conversion_empty);
RUN_TEST(test_case_conversion_null);
RUN_TEST(test_case_conversion_non_ascii_untouched);
RUN_TEST(test_case_conversion_roundtrip);
RUN_TEST(test_range_basic);
RUN_TEST(test_range_negative_index);
RUN_TEST(test_range_out_of_bounds);
RUN_TEST(test_range_null);
RUN_TEST(test_range_empty_string);
RUN_TEST(test_range_in_place);
RUN_TEST(test_split_basic);
RUN_TEST(test_split_adjacent_and_edge_delimiters);
RUN_TEST(test_split_multichar_delim);
RUN_TEST(test_split_no_match);
RUN_TEST(test_split_edge_cases);
RUN_TEST(test_split_independent_allocations);
RUN_TEST(test_growth_no_unnecessary_realloc);
RUN_TEST(test_growth_correctness_across_reallocs);
RUN_TEST(test_trim_whitespace_basic);
RUN_TEST(test_trim_no_change);
RUN_TEST(test_trim_all_whitespace);
RUN_TEST(test_trim_empty_and_null);
RUN_TEST(test_trim_custom_cutset);
RUN_TEST(test_trim_custom_cutset_edge_cases);
RUN_TEST(test_insert_basic);
RUN_TEST(test_insert_prepend_and_append);
RUN_TEST(test_insert_negative_index);
RUN_TEST(test_insert_edge_cases);
RUN_TEST(test_insert_self_alias);
RUN_TEST(test_insert_growth_across_reallocs);
RUN_TEST(test_auto_cleanup);
printf("\n\033[1;34m=== FINAL REPORT ===\033[0m\n");
printf("Total assertions run: %d\n", tests_run);
if (tests_failed == 0) {
printf("\033[1;32mPASSED: All tests succeeded!\033[0m\n");
return EXIT_SUCCESS;
} else {
printf("\033[1;31mFAILED: %d assertions failed.\033[0m\n", tests_failed);
return EXIT_FAILURE;
}
}