-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathstring.cpp
More file actions
2059 lines (1791 loc) · 88.6 KB
/
Copy pathstring.cpp
File metadata and controls
2059 lines (1791 loc) · 88.6 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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @brief Arithmetic/struct plumbing, ASCII utilities, memory, STL-compat, conversions, extensions, and the string class.
* @file scripts/test_string.cpp
* @author Ash Vardanian
* @date June 16, 2026
*/
#undef NDEBUG // ! Enable all assertions for testing
/**
* The Visual C++ run-time library detects incorrect iterator use,
* and asserts and displays a dialog box at run time on Windows.
*/
#if !defined(_ITERATOR_DEBUG_LEVEL) || _ITERATOR_DEBUG_LEVEL == 0
#define _ITERATOR_DEBUG_LEVEL 1
#endif
/**
* ! Overload the following with caution.
* ! Those parameters must never be explicitly set during releases,
* ! but they come handy during development, if you want to validate
* ! different ISA-specific implementations.
#define SZ_USE_WESTMERE 0
#define SZ_USE_HASWELL 0
#define SZ_USE_GOLDMONT 0
#define SZ_USE_SKYLAKE 0
#define SZ_USE_ICELAKE 0
#define SZ_USE_NEON 0
#define SZ_USE_SVE 0
#define SZ_USE_SVE2 0
*/
#define SZ_USE_MISALIGNED_LOADS 0
#if defined(SZ_DEBUG)
#undef SZ_DEBUG
#endif
#define SZ_DEBUG 1 // ! Enforce aggressive logging in this translation unit
/**
* Make sure to include the StringZilla headers before anything else,
* to intercept missing `#include` directives and other issues.
*/
#include <stringzilla/stringzilla.h> // Primary C API
#include <stringzilla/stringzilla.hpp> // C++ string class replacement
#if defined(__SANITIZE_ADDRESS__)
#include <sanitizer/asan_interface.h> // We use ASAN API to poison memory addresses
#endif
#include <cstdio> // `std::printf`
#include <cstring> // `std::memcpy`
#include <algorithm> // `std::transform`
#include <forward_list> // `std::forward_list`
#include <iterator> // `std::distance`
#include <map> // `std::map`
#include <memory> // `std::allocator`
#include <numeric> // `std::accumulate`
#include <random> // `std::random_device`
#include <set> // `std::set`
#include <sstream> // `std::ostringstream`
#include <unordered_map> // `std::unordered_map`
#include <unordered_set> // `std::unordered_set`
#include <vector> // `std::vector`
#include <string> // Baseline
#include <string_view> // Baseline
#if !SZ_IS_CPP11_
#error "This test requires C++11 or later."
#endif
#include "stringzilla.hpp" // `global_random_generator`, `random_string`
namespace sz = ashvardanian::stringzilla;
using namespace sz::scripts;
using sz::literals::operator""_sv; // for `sz::string_view`
using sz::literals::operator""_bs; // for `sz::byteset`
#if SZ_IS_CPP17_
using namespace std::literals; // for ""sv
#endif
#pragma region Helpers
/** @brief Compares two byte ranges and aborts with a localized diagnostic on the first mismatch. */
inline void expect_equality(char const *first, char const *second, std::size_t size) {
if (std::memcmp(first, second, size) == 0) return;
std::size_t mismatch_position = 0;
for (; mismatch_position < size; ++mismatch_position)
if (first[mismatch_position] != second[mismatch_position]) break;
std::fprintf(stderr, "Mismatch at position %zu: %c != %c\n", mismatch_position, first[mismatch_position],
second[mismatch_position]);
verify(false);
}
/**
* @brief The sum of an arithmetic progression.
* @see https://en.wikipedia.org/wiki/Arithmetic_progression
*/
inline std::size_t arithmetic_sum(std::size_t first, std::size_t last, std::size_t step = 1) {
std::size_t n = (last >= first) ? ((last - first) / step + 1) : 0;
if (n == 0) return 0;
std::size_t sum = n / 2 * (2 * first + (n - 1) * step);
// If n is odd, handle the remaining term separately to avoid overflow
if (n % 2 == 1) sum += (2 * first + (n - 1) * step) / 2;
return sum;
}
/** @brief Allocator wrapper that counts the number of allocated and deallocated bytes. */
struct accounting_allocator : public std::allocator<char> {
inline static bool &verbose_ref() {
static bool global_value = false;
return global_value;
}
inline static std::size_t &counter_ref() {
static std::size_t global_value = 0ul;
return global_value;
}
template <typename... args_types_>
static void print_if_verbose(char const *fmt, args_types_... args) {
if (!verbose_ref()) return;
std::printf(fmt, args...);
}
char *allocate(std::size_t n) {
counter_ref() += n;
print_if_verbose("alloc %zd -> %zd\n", n, counter_ref());
return std::allocator<char>::allocate(n);
}
void deallocate(char *val, std::size_t n) {
verify(n <= counter_ref());
counter_ref() -= n;
print_if_verbose("dealloc: %zd -> %zd\n", n, counter_ref());
std::allocator<char>::deallocate(val, n);
}
template <typename callback_type>
static std::size_t account_block(callback_type callback) {
auto before = accounting_allocator::counter_ref();
print_if_verbose("starting block: %zd\n", before);
callback();
auto after = accounting_allocator::counter_ref();
print_if_verbose("ending block: %zd\n", after);
return after - before;
}
};
/** @brief Runs @p callback and asserts that it leaves the global allocation counter unchanged. */
template <typename callback_type>
void assert_balanced_memory(callback_type callback) {
auto bytes = accounting_allocator::account_block(callback);
verify(bytes == 0);
}
/**
* @brief Runs one movement backend (copy/move/fill) through hand-verifiable known-answer vectors.
*
* Mirrors the SHA256 known-answer helper in `test_hash.cpp`: each ISA tier feeds its kernel pointers here,
* so the dispatched C API and every natively-compiled backend share a single ground-truth check. Guard bytes
* past `length` catch stray writes.
*/
static void check_memory_unit_(sz_copy_t copy, sz_move_t move, sz_fill_t fill) {
// `copy` duplicates a known buffer byte-for-byte. We over-allocate the target so a stray write
// past `length` is visible as a corrupted guard byte.
{
char const source[] = "The quick brown fox"; // 19 bytes + terminator
sz_size_t const length = (sz_size_t)(sizeof(source) - 1);
char target[sizeof(source) + 1];
std::memset(target, '#', sizeof(target));
copy(target, source, length);
verify(std::memcmp(target, source, length) == 0);
verify(target[length] == '#'); // No overwrite past `length`
}
// `move` handles overlapping regions. Shifting "abcdef" left-into-itself by two yields "cdef" at the front.
{
char const expected[] = "cdef"; // After moving "cdef" (offset 2, 4 bytes) to offset 0
char buffer[] = "abcdef";
move(buffer, buffer + 2, 4);
verify(std::memcmp(buffer, expected, 4) == 0);
}
// `fill` writes a known byte across a known span, leaving a guard byte untouched.
{
char const expected[] = "*****"; // Five asterisks
char target[5 + 1];
std::memset(target, '#', sizeof(target));
fill(target, 5, (sz_u8_t)'*');
verify(std::memcmp(target, expected, 5) == 0);
verify(target[5] == '#'); // No overwrite past `length`
}
}
/**
* @brief Runs one byte-lookup backend through a hand-verifiable known-answer vector.
*
* The upper-casing table maps "Hello, World!" to "HELLO, WORLD!" while leaving punctuation and digits intact;
* a guard byte past `length` catches stray writes.
*/
static void check_lookup_unit_(sz_lookup_t lookup) {
// An ASCII upper-casing table, built locally so the known-answer is verified against an external ground truth.
char upper_table[256];
for (sz_size_t byte_value = 0; byte_value != 256; ++byte_value) {
char const character = (char)(unsigned char)byte_value;
upper_table[byte_value] = (character >= 'a' && character <= 'z') ? (char)(character - 'a' + 'A') : character;
}
char const source[] = "Hello, World!"; // 13 bytes
char const expected[] = "HELLO, WORLD!";
sz_size_t const length = (sz_size_t)(sizeof(source) - 1);
char target[sizeof(source) + 1];
std::memset(target, '#', sizeof(target));
lookup(target, length, source, upper_table);
verify(std::memcmp(target, expected, length) == 0);
verify(target[length] == '#'); // No overwrite past `length`
}
#pragma endregion // Helpers
#pragma region Arithmetic
/**
* @brief Several string processing operations rely on computing integer logarithms.
* Failures in such operations will result in wrong `resize` outcomes and heap corruption.
*/
void test_arithmetic_unit() {
verify(sz_u64_clz(0x0000000000000001ull) == 63);
verify(sz_u64_clz(0x0000000000000002ull) == 62);
verify(sz_u64_clz(0x0000000000000003ull) == 62);
verify(sz_u64_clz(0x0000000000000004ull) == 61);
verify(sz_u64_clz(0x0000000000000007ull) == 61);
verify(sz_u64_clz(0x8000000000000001ull) == 0);
verify(sz_u64_clz(0xffffffffffffffffull) == 0);
verify(sz_u64_clz(0x4000000000000000ull) == 1);
verify(sz_size_log2i_nonzero(1) == 0);
verify(sz_size_log2i_nonzero(2) == 1);
verify(sz_size_log2i_nonzero(3) == 1);
verify(sz_size_log2i_nonzero(4) == 2);
verify(sz_size_log2i_nonzero(5) == 2);
verify(sz_size_log2i_nonzero(7) == 2);
verify(sz_size_log2i_nonzero(8) == 3);
verify(sz_size_log2i_nonzero(9) == 3);
verify(sz_size_bit_ceil(0) == 0);
verify(sz_size_bit_ceil(1) == 1);
verify(sz_size_bit_ceil(2) == 2);
verify(sz_size_bit_ceil(3) == 4);
verify(sz_size_bit_ceil(4) == 4);
verify(sz_size_bit_ceil(77) == 128);
verify(sz_size_bit_ceil(127) == 128);
verify(sz_size_bit_ceil(128) == 128);
verify(sz_size_bit_ceil(1000000ull) == (1ull << 20));
verify(sz_size_bit_ceil(2000000ull) == (1ull << 21));
verify(sz_size_bit_ceil(4000000ull) == (1ull << 22));
verify(sz_size_bit_ceil(8000000ull) == (1ull << 23));
verify(sz_size_bit_ceil(16000000ull) == (1ull << 24));
verify(sz_size_bit_ceil(32000000ull) == (1ull << 25));
verify(sz_size_bit_ceil(64000000ull) == (1ull << 26));
verify(sz_size_bit_ceil(128000000ull) == (1ull << 27));
verify(sz_size_bit_ceil(256000000ull) == (1ull << 28));
verify(sz_size_bit_ceil(512000000ull) == (1ull << 29));
verify(sz_size_bit_ceil(1000000000ull) == (1ull << 30));
verify(sz_size_bit_ceil(2000000000ull) == (1ull << 31));
#if SZ_IS_64BIT_
verify(sz_size_bit_ceil(4000000000ull) == (1ull << 32));
verify(sz_size_bit_ceil(8000000000ull) == (1ull << 33));
verify(sz_size_bit_ceil(16000000000ull) == (1ull << 34));
verify(sz_size_bit_ceil((1ull << 62)) == (1ull << 62));
verify(sz_size_bit_ceil((1ull << 62) + 1) == (1ull << 63));
verify(sz_size_bit_ceil((1ull << 63)) == (1ull << 63));
#endif
}
#pragma endregion // Arithmetic
#pragma region Sequence
/** @brief Validates `sz_sequence_t` and related construction utilities. */
void test_sequence_unit() {
// Make sure the sequence helper functions work as expected
// for both trivial c-style arrays and more complicated STL containers.
{
sz_sequence_t sequence;
sz_cptr_t strings[] = {"banana", "apple", "cherry"};
sz_sequence_from_null_terminated_strings(strings, 3, &sequence);
verify(sequence.count == 3);
verify("banana"_sv == sequence.get_start(sequence.handle, 0));
verify("apple"_sv == sequence.get_start(sequence.handle, 1));
verify("cherry"_sv == sequence.get_start(sequence.handle, 2));
verify(sequence.get_length(sequence.handle, 0) == 6);
verify(sequence.get_length(sequence.handle, 1) == 5);
verify(sequence.get_length(sequence.handle, 2) == 6);
}
// Empty sequences, empty members, and duplicates are all legal.
{
sz_sequence_t sequence;
sz_cptr_t strings[] = {"", "apple", "apple", ""};
sz_sequence_from_null_terminated_strings(strings, 0, &sequence);
verify(sequence.count == 0);
sz_sequence_from_null_terminated_strings(strings, 4, &sequence);
verify(sequence.count == 4);
verify(sequence.get_length(sequence.handle, 0) == 0);
verify(sequence.get_length(sequence.handle, 3) == 0);
verify("apple"_sv == sequence.get_start(sequence.handle, 1));
verify("apple"_sv == sequence.get_start(sequence.handle, 2));
}
// Do the same for STL:
{
using strings_vector_t = std::vector<std::string>;
strings_vector_t strings = {"banana", "apple", "cherry"};
sz_sequence_t sequence;
sequence.handle = &strings;
sequence.count = strings.size();
sequence.get_start = reinterpret_cast<sz_sequence_member_start_t>(
+[](void *handle, sz_size_t index) noexcept -> sz_cptr_t {
auto const &strings = *static_cast<strings_vector_t *>(handle);
return strings[index].c_str();
});
sequence.get_length = reinterpret_cast<sz_sequence_member_length_t>(
+[](void *handle, sz_size_t index) noexcept -> sz_size_t {
auto const &strings = *static_cast<strings_vector_t *>(handle);
return strings[index].size();
});
verify(sequence.count == 3);
verify("banana"_sv == sequence.get_start(sequence.handle, 0));
verify("apple"_sv == sequence.get_start(sequence.handle, 1));
verify("cherry"_sv == sequence.get_start(sequence.handle, 2));
}
}
/**
* @brief Validates that `arrow_strings_tape::try_assign` works with multi-pass forward iterators.
* It walks the range twice, once to measure and once to copy, so single-pass input
* iterators like `std::istream_iterator` are rejected at compile time.
*/
void test_strings_tape_assign_unit() {
sz::arrow_strings_tape<char, std::uint32_t, std::allocator<char>> tape;
// A forward list can only be walked forward, but any number of times - exactly what `try_assign` needs.
std::forward_list<std::string> strings {"alpha", "", "gamma"};
verify(tape.try_assign(strings.begin(), strings.end()) == sz::status_t::success_k);
verify(tape.size() == 3);
verify(sz::string_view(tape[0].data(), tape[0].size()) == "alpha"_sv);
verify(tape[1].size() == 0);
verify(sz::string_view(tape[2].data(), tape[2].size()) == "gamma"_sv);
}
/** @brief Validates that `arrow_strings_tape` refuses to grow past the range of its offset type. */
void test_strings_tape_overflow_unit() {
// 8-bit offsets hit the same code path as 32-bit offsets past 4 GiB, but already at 256 bytes.
using tape_t = sz::arrow_strings_tape<char, std::uint8_t, std::allocator<char>>;
// Appending past the offset range must fail cleanly and leave the stored strings untouched.
{
tape_t tape;
std::string const oversized_string(200, 'x');
verify(tape.try_append(sz::to_view(oversized_string)) == sz::status_t::success_k);
// Two 200-byte strings need 402 bytes of buffer, past the 255 maximum of 8-bit offsets.
verify(tape.try_append(sz::to_view(oversized_string)) == sz::status_t::overflow_risk_k);
verify(tape.size() == 1);
// The first string must still sit at offset 0, ending at 201 with its NULL terminator.
verify(tape.offsets()[0] == 0);
verify(tape.offsets()[1] == 201);
verify(std::memcmp(tape.buffer().data(), oversized_string.data(), oversized_string.size()) == 0);
}
// Same for bulk assignment: the combined size must fit the offset range.
{
tape_t tape;
std::string const stored_string(10, 'z');
verify(tape.try_append(sz::to_view(stored_string)) == sz::status_t::success_k);
std::vector<std::string> strings {std::string(200, 'x'), std::string(200, 'y')};
verify(tape.try_assign(strings.begin(), strings.end()) == sz::status_t::overflow_risk_k);
// A rejected assignment releases the old contents, so the tape must not keep reporting them.
verify(tape.size() == 0);
}
}
#pragma endregion // Sequence
#pragma region Allocator
/** @brief Validates `sz_memory_allocator_t` and related construction utilities. */
void test_allocator_unit() {
// Our behavior for `malloc(0)` is to return a NULL pointer,
// while the standard is implementation-defined.
{
sz_memory_allocator_t alloc;
sz_memory_allocator_init_default(&alloc);
verify(alloc.allocate(0, alloc.handle) == nullptr);
}
// Non-NULL allocation
{
sz_memory_allocator_t alloc;
sz_memory_allocator_init_default(&alloc);
void *byte = alloc.allocate(1, alloc.handle);
verify(byte != nullptr);
alloc.free(byte, 1, alloc.handle);
}
// Use a fixed buffer
{
char buffer[1024];
sz_memory_allocator_t alloc;
sz_memory_allocator_init_fixed(&alloc, buffer, sizeof(buffer));
void *byte = alloc.allocate(1, alloc.handle);
verify(byte != nullptr);
alloc.free(byte, 1, alloc.handle);
}
}
#pragma endregion // Allocator
#pragma region Byteset
/** @brief Validates `sz_byteset_t` and related construction utilities. */
void test_byteset_unit() {
sz_byteset_t s;
sz_byteset_init(&s);
verify(sz_byteset_contains(&s, 'a') == sz_false_k);
sz_byteset_add(&s, 'a');
verify(sz_byteset_contains(&s, 'a') == sz_true_k);
sz_byteset_add(&s, 'z');
verify(sz_byteset_contains(&s, 'z') == sz_true_k);
sz_byteset_invert(&s);
verify(sz_byteset_contains(&s, 'a') == sz_false_k);
verify(sz_byteset_contains(&s, 'z') == sz_false_k);
verify(sz_byteset_contains(&s, 'b') == sz_true_k);
sz_byteset_init_ascii(&s);
verify(sz_byteset_contains(&s, 'A') == sz_true_k);
}
/**
* @brief Tests various ASCII-based methods (e.g., `is_alpha`, `is_digit`)
* provided by `sz::string` and `sz::string_view`.
*/
template <typename string_type>
void test_ascii_unit() {
using str = string_type;
verify("aaa"_bs.size() == 1ull);
verify("\0\0"_bs.size() == 1ull);
verify("abc"_bs.size() == 3ull);
verify("a\0bc"_bs.size() == 4ull);
verify(!"abc"_bs.contains('\0'));
verify(str("bca").contains_only("abc"_bs));
verify(!str("").is_alpha());
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").is_alpha());
verify(!str("abc9").is_alpha());
verify(!str("").is_alnum());
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789").is_alnum());
verify(!str("abc!").is_alnum());
verify(str("").is_ascii());
verify(str("\x00x7F").is_ascii());
verify(!str("abc123🔥").is_ascii());
verify(!str("").is_digit());
verify(str("0123456789").is_digit());
verify(!str("012a").is_digit());
verify(!str("").is_lower());
verify(str("abcdefghijklmnopqrstuvwxyz").is_lower());
verify(!str("abcA").is_lower());
verify(!str("abc\n").is_lower());
verify(!str("").is_space());
verify(str(" \t\n\r\f\v").is_space());
verify(!str(" \t\r\na").is_space());
verify(!str("").is_upper());
verify(str("ABCDEFGHIJKLMNOPQRSTUVWXYZ").is_upper());
verify(!str("ABCa").is_upper());
verify(str("").is_printable());
verify(str("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+").is_printable());
verify(!str("012🔥").is_printable());
verify(str("").contains_only("abc"_bs));
verify(str("abc").contains_only("abc"_bs));
verify(!str("abcd").contains_only("abc"_bs));
}
#pragma endregion // Byteset
#pragma region Memory
/**
* @brief Known-answer + coverage for the memory primitives - the C-level building blocks of the string class.
*
* Starts with known-answer vectors that exercise each function through the dispatched C API (automatic
* kernel resolution), through the natively-compiled backend kernels directly (manual propagation to a specific
* kernel), and through the C++ `sz::` wrappers, so a regression that the serial-vs-SIMD agreement tests would miss -
* because both share a wrong constant - is still caught against an external ground truth. It then mirrors a large set
* of `sz::memcpy`, `sz::memset`, and `sz::memmove` operations against their `std::` counterparts, using a large
* heap-allocated buffer to cover the larger-than-L2-cache code paths, various chunk sizes, overlapping regions, and
* both forward and backward traversals.
*/
void test_memory_unit(std::size_t max_l2_size) {
std::printf(" - testing memory primitive known-answer vectors...\n");
// Movement known-answers, through the dispatched C API and every natively-compiled backend.
check_memory_unit_(sz_copy, sz_move, sz_fill); // Dispatched (automatic kernel)
check_memory_unit_(sz_copy_serial, sz_move_serial, sz_fill_serial); // Manual: serial kernel
#if SZ_USE_HASWELL
check_memory_unit_(sz_copy_haswell, sz_move_haswell, sz_fill_haswell);
#endif
#if SZ_USE_SKYLAKE
check_memory_unit_(sz_copy_skylake, sz_move_skylake, sz_fill_skylake);
#endif
#if SZ_USE_NEON
check_memory_unit_(sz_copy_neon, sz_move_neon, sz_fill_neon);
#endif
#if SZ_USE_SVE
check_memory_unit_(sz_copy_sve, sz_move_sve, sz_fill_sve);
#endif
#if SZ_USE_V128
check_memory_unit_(sz_copy_v128, sz_move_v128, sz_fill_v128);
#endif
#if SZ_USE_V128RELAXED
check_memory_unit_(sz_copy_v128relaxed, sz_move_v128relaxed, sz_fill_v128relaxed);
#endif
#if SZ_USE_RVV
check_memory_unit_(sz_copy_rvv, sz_move_rvv, sz_fill_rvv);
#endif
#if SZ_USE_LASX
check_memory_unit_(sz_copy_lasx, sz_move_lasx, sz_fill_lasx);
#endif
#if SZ_USE_POWERVSX
check_memory_unit_(sz_copy_powervsx, sz_move_powervsx, sz_fill_powervsx);
#endif
// Lookup known-answers, through the dispatched C API and every natively-compiled backend.
check_lookup_unit_(sz_lookup); // Dispatched (automatic kernel)
check_lookup_unit_(sz_lookup_serial); // Manual: serial kernel
#if SZ_USE_HASWELL
check_lookup_unit_(sz_lookup_haswell);
#endif
#if SZ_USE_ICELAKE
check_lookup_unit_(sz_lookup_icelake);
#endif
#if SZ_USE_NEON
check_lookup_unit_(sz_lookup_neon);
#endif
#if SZ_USE_SVE
check_lookup_unit_(sz_lookup_sve);
#endif
#if SZ_USE_V128
check_lookup_unit_(sz_lookup_v128);
#endif
#if SZ_USE_V128RELAXED
check_lookup_unit_(sz_lookup_v128relaxed);
#endif
#if SZ_USE_RVV
check_lookup_unit_(sz_lookup_rvv);
#endif
#if SZ_USE_LASX
check_lookup_unit_(sz_lookup_lasx);
#endif
#if SZ_USE_POWERVSX
check_lookup_unit_(sz_lookup_powervsx);
#endif
// C++ wrapper sanity: a couple of `sz::string` / `sz::string_view` known-answer reads alongside the C API.
{
sz::string_view const view = "Hello, World!"_sv;
verify(view.size() == 13u);
verify(view.substr(7, 5) == "World"_sv);
verify(view.front() == 'H' && view.back() == '!');
sz::string const owned = "Hello, World!";
verify(owned.size() == 13u);
verify(owned == view);
verify(sz::string("apple").compare("banana") < 0);
}
// The C++ movement wrappers must agree with the known-answers, including overlapping `memmove`.
{
char const fox[] = "The quick brown fox";
char target[sizeof(fox) + 1];
let_verify(std::memset(target, '#', sizeof(target)), //
(sz::memcpy(target, fox, sizeof(fox) - 1), std::memcmp(target, fox, sizeof(fox) - 1) == 0) &&
target[sizeof(fox) - 1] == '#');
char overlap[] = "abcdef";
let_verify(sz::memmove(overlap, overlap + 2, 4), std::memcmp(overlap, "cdef", 4) == 0);
char asterisks[5 + 1];
let_verify(std::memset(asterisks, '#', sizeof(asterisks)), //
(sz::memset(asterisks, '*', 5), std::memcmp(asterisks, "*****", 5) == 0) && asterisks[5] == '#');
}
// Embedded NUL must be preserved verbatim by a stored `sz::string`: the size is the full byte length, and
// indexing past the interior NUL reaches the trailing bytes rather than stopping at the C-string boundary.
{
char const with_nul[] = {'a', 'b', '\0', 'c', 'd'};
sz::string const owned(with_nul, sizeof(with_nul));
verify(owned.size() == sizeof(with_nul)); // Full length, NUL is a stored byte
verify(owned[2] == '\0'); // The interior NUL survives
verify(owned[3] == 'c' && owned[4] == 'd'); // Indexing past the NUL works
verify(owned == sz::string_view(with_nul, sizeof(with_nul)));
}
// We will be mirroring the operations on both standard and StringZilla strings.
std::string text_stl(max_l2_size, '-');
std::string text_sz(max_l2_size, '-');
expect_equality(text_stl.data(), text_sz.data(), max_l2_size);
// The traditional `memset` and `memcpy` functions are undefined for zero-length buffers and NULL pointers
// for older C standards. However, with the N3322 proposal for C2y, that issue has been resolved.
// https://developers.redhat.com/articles/2024/12/11/making-memcpynull-null-0-well-defined
//
// Let's make sure, that our versions don't trigger any undefined behavior.
sz::memset(NULL, 0, 0);
sz::memcpy(NULL, NULL, 0);
sz::memmove(NULL, NULL, 0);
// First start with simple deterministic tests.
// Let's use `memset` to fill the strings with a pattern like "122333444455555...00000000000011111111111..."
std::size_t count_groups = 0;
for (std::size_t offset = 0, fill_length = 1; offset < max_l2_size;
offset += fill_length, ++fill_length, ++count_groups) {
char fill_value = '0' + fill_length % 10;
fill_length = offset + fill_length > max_l2_size ? max_l2_size - offset : fill_length;
std::memset((void *)(text_stl.data() + offset), fill_value, fill_length);
sz::memset((void *)(text_sz.data() + offset), fill_value, fill_length);
expect_equality(text_stl.data(), text_sz.data(), max_l2_size);
}
// Let's copy those chunks to an empty buffer one by one, validating the overall equivalency after every copy.
std::string copy_stl(max_l2_size, '-');
std::string copy_sz(max_l2_size, '-');
for (std::size_t offset = 0, fill_length = 1; offset < max_l2_size; offset += fill_length, ++fill_length) {
fill_length = offset + fill_length > max_l2_size ? max_l2_size - offset : fill_length;
std::memcpy((void *)(copy_stl.data() + offset), (void *)(text_stl.data() + offset), fill_length);
sz::memcpy((void *)(copy_sz.data() + offset), (void *)(text_sz.data() + offset), fill_length);
expect_equality(copy_stl.data(), copy_sz.data(), max_l2_size);
}
expect_equality(text_stl.data(), copy_stl.data(), max_l2_size);
expect_equality(text_sz.data(), copy_sz.data(), max_l2_size);
// Let's simulate a realistic `memmove` workloads, compacting parts of this buffer, removing all odd values,
// so the buffer will look like "224444666666..."
for (std::size_t offset = 0, fill_length = 1; offset < max_l2_size; offset += fill_length, ++fill_length) {
if (fill_length % 2 == 0) continue; // Skip even chunks
if (offset + fill_length >= max_l2_size) break; // This is the last & there are no more even chunks to shift
// Make sure we don't overflow the buffer
std::size_t next_offset = offset + fill_length;
std::size_t next_fill_length = fill_length + 1;
next_fill_length = next_offset + next_fill_length > max_l2_size ? max_l2_size - next_offset : next_fill_length;
std::memmove((void *)(text_stl.data() + offset), (void *)(text_stl.data() + next_offset), next_fill_length);
sz::memmove((void *)(text_sz.data() + offset), (void *)(text_sz.data() + next_offset), next_fill_length);
expect_equality(text_stl.data(), text_sz.data(), max_l2_size);
}
// Now the opposite workload, expanding the buffer, inserting a dash "-" before every group of equal characters.
// We will need to navigate right-to left to avoid overwriting the groups.
std::size_t dashed_capacity = copy_stl.size() + count_groups;
std::size_t dashed_length = 0;
copy_stl.resize(dashed_capacity);
copy_sz.resize(dashed_capacity);
for (std::size_t reverse_offset = 0; reverse_offset < max_l2_size;) {
// Walk backwards to find the length of the current group
std::size_t offset = max_l2_size - reverse_offset - 1;
std::size_t fill_length = 1;
while (offset > 0 && copy_stl[offset - 1] == copy_stl[offset]) --offset, ++fill_length;
std::size_t new_offset = dashed_capacity - dashed_length - fill_length;
std::memmove((void *)(copy_stl.data() + new_offset), (void *)(copy_stl.data() + offset), fill_length);
sz::memmove((void *)(copy_sz.data() + new_offset), (void *)(copy_sz.data() + offset), fill_length);
expect_equality(copy_stl.data(), copy_sz.data(), max_l2_size);
copy_stl[new_offset] = '-';
copy_sz[new_offset] = '-';
dashed_length += fill_length + 1;
reverse_offset += fill_length;
}
}
/**
* @brief Tests memory utilities on large buffers (>1MB) that trigger special code paths
* in AVX2/AVX512 implementations. This specifically tests the bidirectional
* traversal optimization used for huge buffers.
*/
void test_memory_large_unit() {
// Test sizes that trigger the "huge buffer" path (> 1MB)
std::vector<std::size_t> test_sizes = {
1024ull * 1024ull + 1, // Just over 1MB
1024ull * 10ull * 103ull, // From GitHub issue #228: 1,055,360 bytes
2ull * 1024ull * 1024ull, // 2MB
3ull * 1024ull * 1024ull + 7 // 3MB + 7 (unaligned size)
};
for (std::size_t size : test_sizes) {
// Test memcpy with aligned buffers
{
std::vector<char> source(size);
std::vector<char> target_std(size);
std::vector<char> target_sz(size);
// Fill source with pattern to detect copying errors
for (std::size_t i = 0; i < size; i++) { source[i] = static_cast<char>('A' + (i % 26)); }
std::memcpy(target_std.data(), source.data(), size);
sz::memcpy(target_sz.data(), source.data(), size);
expect_equality(target_std.data(), target_sz.data(), size);
}
// Test memcpy with unaligned buffers
{
std::vector<char> source_buffer(size + 64);
std::vector<char> target_std_buffer(size + 64);
std::vector<char> target_sz_buffer(size + 64);
// Use unaligned pointers
char *source = source_buffer.data() + 7;
char *target_std = target_std_buffer.data() + 11;
char *target_sz = target_sz_buffer.data() + 11;
for (std::size_t i = 0; i < size; i++) { source[i] = static_cast<char>('a' + (i % 26)); }
std::memcpy(target_std, source, size);
sz::memcpy(target_sz, source, size);
expect_equality(target_std, target_sz, size);
}
// Test memset
{
std::vector<char> buf_std(size);
std::vector<char> buf_sz(size);
std::memset(buf_std.data(), 'Z', size);
sz::memset(buf_sz.data(), 'Z', size);
expect_equality(buf_std.data(), buf_sz.data(), size);
}
// Test memmove with overlapping regions
{
std::vector<char> buf_std(size);
std::vector<char> buf_sz(size);
for (std::size_t i = 0; i < size; i++) { buf_std[i] = buf_sz[i] = static_cast<char>('0' + (i % 10)); }
// Move overlapping region forward
std::size_t overlap_size = size / 2;
std::memmove(buf_std.data() + 100, buf_std.data(), overlap_size);
sz::memmove(buf_sz.data() + 100, buf_sz.data(), overlap_size);
expect_equality(buf_std.data(), buf_sz.data(), size);
}
}
}
#pragma endregion // Memory
#pragma region STL Reads
/**
* @brief Invokes different C++ member methods of immutable strings to cover all STL APIs.
* This test guarantees API @b compatibility with STL `std::basic_string` template.
*/
template <typename string_type>
void test_stl_reads_unit() {
using str = string_type;
// Constructors.
verify(str().empty());
verify(str().size() == 0);
verify(str("").empty());
verify(str("").size() == 0);
verify(str("hello").size() == 5);
verify(str("hello", 4) == "hell");
// Element access.
verify(str("rest")[0] == 'r');
verify(str("rest").at(1) == 'e');
verify(*str("rest").data() == 'r');
verify(str("front").front() == 'f');
verify(str("back").back() == 'k');
// Iterators.
verify(*str("begin").begin() == 'b' && *str("cbegin").cbegin() == 'c');
verify(*str("rbegin").rbegin() == 'n' && *str("crbegin").crbegin() == 'n');
verify(str("size").size() == 4 && str("length").length() == 6);
// Slices... out-of-bounds exceptions are asymmetric!
// Moreover, `std::string` has no `remove_prefix` and `remove_suffix` methods.
// scope_verify(str s = "hello", s.remove_prefix(1), s == "ello");
// scope_verify(str s = "hello", s.remove_suffix(1), s == "hell");
verify(str("hello world").substr(0, 5) == "hello");
verify(str("hello world").substr(6, 5) == "world");
verify(str("hello world").substr(6) == "world");
verify(str("hello world").substr(6, 100) == "world"); // 106 is beyond the length of the string, but its OK
throws_verify(str("hello world").substr(100), std::out_of_range); // 100 is beyond the length of the string
throws_verify(str("hello world").substr(20, 5), std::out_of_range); // 20 is beyond the length of the string
#if defined(__GNUC__) && !defined(__NVCC__) // -1 casts to unsigned without warnings on GCC, but not NVCC
throws_verify(str("hello world").substr(-1, 5), std::out_of_range);
verify(str("hello world").substr(0, -1) == "hello world");
#endif
// Character search in normal and reverse directions.
verify(str("hello").find('e') == 1);
verify(str("hello").find('e', 1) == 1);
verify(str("hello").find('e', 2) == str::npos);
verify(str("hello").rfind('l') == 3);
verify(str("hello").rfind('l', 2) == 2);
verify(str("hello").rfind('l', 1) == str::npos);
// Substring search in normal and reverse directions.
verify(str("hello").find("ell") == 1);
verify(str("hello").find("ell", 1) == 1);
verify(str("hello").find("ell", 2) == str::npos);
verify(str("hello").find("el", 1) == 1);
verify(str("hello").find("ell", 1, 2) == 1);
verify(str("hello").rfind("l") == 3);
verify(str("hello").rfind("l", 2) == 2);
verify(str("hello").rfind("l", 1) == str::npos);
// The second argument is the last possible value of the returned offset.
verify(str("hello").rfind("el", 1) == 1);
verify(str("hello").rfind("ell", 1) == 1);
verify(str("hello").rfind("ello", 1) == 1);
verify(str("hello").rfind("ell", 1, 2) == 1);
// More complex queries.
verify(str("abbabbaaaaaa").find("aa") == 6);
verify(str("abbabbaaaaaa").find("ba") == 2);
verify(str("abbabbaaaaaa").find("bb") == 1);
verify(str("abbabbaaaaaa").find("bab") == 2);
verify(str("abbabbaaaaaa").find("babb") == 2);
verify(str("abbabbaaaaaa").find("babba") == 2);
verify(str("abcdabcd").substr(2, 4).find("abc") == str::npos);
verify(str("hello, world!").substr(0, 11).find("world") == str::npos);
verify(str("axabbcxcaaabbccc").find("aaabbccc") == 8);
verify(str("abcdabcdabc________").find("abcd") == 0);
verify(str("________abcdabcdabc").find("abcd") == 8);
// Cover every SWAR case for unique string sequences.
auto lowercase_alphabet = str("abcdefghijklmnopqrstuvwxyz");
for (std::size_t one_byte_offset = 0; one_byte_offset + 1 <= lowercase_alphabet.size(); ++one_byte_offset)
verify(lowercase_alphabet.find(lowercase_alphabet.substr(one_byte_offset, 1)) == one_byte_offset);
for (std::size_t two_byte_offset = 0; two_byte_offset + 2 <= lowercase_alphabet.size(); ++two_byte_offset)
verify(lowercase_alphabet.find(lowercase_alphabet.substr(two_byte_offset, 2)) == two_byte_offset);
for (std::size_t four_byte_offset = 0; four_byte_offset + 4 <= lowercase_alphabet.size(); ++four_byte_offset)
verify(lowercase_alphabet.find(lowercase_alphabet.substr(four_byte_offset, 4)) == four_byte_offset);
for (std::size_t three_byte_offset = 0; three_byte_offset + 3 <= lowercase_alphabet.size(); ++three_byte_offset)
verify(lowercase_alphabet.find(lowercase_alphabet.substr(three_byte_offset, 3)) == three_byte_offset);
for (std::size_t five_byte_offset = 0; five_byte_offset + 5 <= lowercase_alphabet.size(); ++five_byte_offset)
verify(lowercase_alphabet.find(lowercase_alphabet.substr(five_byte_offset, 5)) == five_byte_offset);
// Simple repeating patterns - with one "almost match" before an actual match in each direction.
verify(str("_ab_abc_").find("abc") == 4);
verify(str("_abc_ab_").rfind("abc") == 1);
verify(str("_abc_abcd_").find("abcd") == 5);
verify(str("_abcd_abc_").rfind("abcd") == 1);
verify(str("_abcd_abcde_").find("abcde") == 6);
verify(str("_abcde_abcd_").rfind("abcde") == 1);
verify(str("_abcde_abcdef_").find("abcdef") == 7);
verify(str("_abcdef_abcde_").rfind("abcdef") == 1);
verify(str("_abcdef_abcdefg_").find("abcdefg") == 8);
verify(str("_abcdefg_abcdef_").rfind("abcdefg") == 1);
// ! `rfind` and `find_last_of` are not consistent in meaning of their arguments.
verify(str("hello").find_first_of("le") == 1);
verify(str("hello").find_first_of("le", 1) == 1);
verify(str("hello").find_last_of("le") == 3);
verify(str("hello").find_last_of("le", 2) == 2);
verify(str("hello").find_first_not_of("hel") == 4);
verify(str("hello").find_first_not_of("hel", 1) == 4);
verify(str("hello").find_last_not_of("hel") == 4);
verify(str("hello").find_last_not_of("hel", 4) == 4);
// Try longer strings to enforce SIMD.
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find('x') == 23);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find('X') == 49);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").rfind('x') == 23);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").rfind('X') == 49);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find("xy") == 23);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find("XY") == 49);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find("yz") == 24);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find("YZ") == 50);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").rfind("xy") == 23);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").rfind("XY") == 49);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find("xyz") == 23);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find("XYZ") == 49);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").rfind("xyz") == 23);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").rfind("XYZ") == 49);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find("xyzA") == 23);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find("XYZ0") == 49);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").rfind("xyzA") == 23);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").rfind("XYZ0") == 49);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find_first_of("xyz") == 23);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find_first_of("XYZ") == 49);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find_last_of("xyz") == 25);
verify(str("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-").find_last_of("XYZ") == 51);
// Using single-byte non-ASCII values, e.g., À (0xC0), Æ (0xC6). The `\xFA`/`0` boundary is
// load-bearing: a literal hex digit after `\xFA` would extend the escape, so keep it split.
{
char const *non_ascii_set = "abcdefgh\x01\xC6ijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\xC0\xFA" //
"0123456789+-"; // 68 bytes
verify(str(non_ascii_set, 68).find_first_of("\xC6\xC7") == 9);
verify(str(non_ascii_set, 68).find_first_of("\xC0\xC1") == 54);
verify(str(non_ascii_set, 68).find_last_of("\xC6\xC7") == 9);
verify(str(non_ascii_set, 68).find_last_of("\xC0\xC1") == 54);
}
// Boundary conditions.
verify(str("hello").find_first_of("ox", 4) == 4);
verify(str("hello").find_first_of("ox", 5) == str::npos);
verify(str("hello").find_last_of("ox", 4) == 4);
verify(str("hello").find_last_of("ox", 5) == 4);
verify(str("hello").find_first_of("hx", 0) == 0);
verify(str("hello").find_last_of("hx", 0) == 0);
// More complex relative patterns
verify(str("0123456789012345678901234567890123456789012345678901234567890123") <=
str("0123456789012345678901234567890123456789012345678901234567890123"));
verify(str("0123456789012345678901234567890123456789012345678901234567890123") <=
str("0223456789012345678901234567890123456789012345678901234567890123"));
verify(str("0123456789012345678901234567890123456789012345678901234567890123") <=
str("0213456789012345678901234567890123456789012345678901234567890123"));
verify(str("12341234") <= str("12341234"));
verify(str("12341234") > str("12241224"));
verify(str("12341234") < str("13241324"));
verify(str("0123456789012345678901234567890123456789012345678901234567890123") ==
str("0123456789012345678901234567890123456789012345678901234567890123"));
verify(str("0123456789012345678901234567890123456789012345678901234567890123") !=
str("0223456789012345678901234567890123456789012345678901234567890123"));
// Comparisons.
verify(str("a") != str("b"));
verify(str("a") < str("b"));
verify(str("a") <= str("b"));
verify(str("b") > str("a"));
verify(str("b") >= str("a"));
verify(str("a") < str("aa"));
#if SZ_IS_CPP20_ && defined(__cpp_lib_three_way_comparison)
// Spaceship operator instead of conventional comparions.
verify((str("a") <=> str("b")) == std::strong_ordering::less);
verify((str("b") <=> str("a")) == std::strong_ordering::greater);
verify((str("b") <=> str("b")) == std::strong_ordering::equal);
verify((str("a") <=> str("aa")) == std::strong_ordering::less);
#endif
// Compare with another `str`.
verify(str("test").compare(str("test")) == 0);
verify(str("apple").compare(str("banana")) < 0);
verify(str("banana").compare(str("apple")) > 0);
// Compare with a C-string.
verify(str("test").compare("test") == 0);
verify(str("alpha").compare("beta") < 0);
verify(str("beta").compare("alpha") > 0);
// Compare substring with another `str`.
verify(str("hello world").compare(0, 5, str("hello")) == 0);
verify(str("hello world").compare(6, 5, str("earth")) > 0);
verify(str("hello world").compare(6, 5, str("worlds")) < 0);
throws_verify(str("hello world").compare(20, 5, str("worlds")), std::out_of_range);
// Compare substring with another `str`'s substring.
verify(str("hello world").compare(0, 5, str("say hello"), 4, 5) == 0);
verify(str("hello world").compare(6, 5, str("world peace"), 0, 5) == 0);
verify(str("hello world").compare(6, 5, str("a better world"), 9, 5) == 0);
// Out of bounds cases for both compared strings.