@@ -431,13 +431,20 @@ static void test_api_mutable() {
431
431
assert_scoped (str s = " obsolete" , s.assign (s, 4 ), s == " lete" ); // Partial self-assignment
432
432
assert_scoped (str s = " obsolete" , s.assign (s, 4 , 3 ), s == " let" ); // Partial self-assignment
433
433
434
+ // Self-assignment is a special case of assignment.
435
+ assert_scoped (str s = " obsolete" , s = s, s == " obsolete" );
436
+ assert_scoped (str s = " obsolete" , s.assign (s), s == " obsolete" );
437
+ assert_scoped (str s = " obsolete" , s.assign (s.data (), 2 ), s == " ob" );
438
+ assert_scoped (str s = " obsolete" , s.assign (s.data (), s.size ()), s == " obsolete" );
439
+
434
440
// Allocations, capacity and memory management.
435
441
assert_scoped (str s, s.reserve (10 ), s.capacity () >= 10 );
436
442
assert_scoped (str s, s.resize (10 ), s.size () == 10 );
437
443
assert_scoped (str s, s.resize (10 , ' a' ), s.size () == 10 && s == " aaaaaaaaaa" );
438
444
assert (str ().max_size () > 0 );
439
445
assert (str ().get_allocator () == std::allocator<char >());
440
446
assert (std::strcmp (str (" c_str" ).c_str (), " c_str" ) == 0 );
447
+ assert_scoped (str s = " hello" , s.shrink_to_fit (), s.capacity () <= sz::string::min_capacity);
441
448
442
449
// Concatenation.
443
450
// Following are missing in strings, but are present in vectors.
@@ -627,6 +634,28 @@ static void test_api_readonly_extensions() {
627
634
void test_api_mutable_extensions () {
628
635
using str = sz::string;
629
636
637
+ // Try methods.
638
+ assert (str (" obsolete" ).try_assign (" hello" ));
639
+ assert (str ().try_reserve (10 ));
640
+ assert (str ().try_resize (10 ));
641
+ assert (str (" __" ).try_insert (1 , " test" ));
642
+ assert (str (" test" ).try_erase (1 , 2 ));
643
+ assert (str (" test" ).try_clear ());
644
+ assert (str (" test" ).try_replace (1 , 2 , " aaaa" ));
645
+ assert (str (" test" ).try_push_back (' a' ));
646
+ assert (str (" test" ).try_shrink_to_fit ());
647
+
648
+ // Self-referencing methods.
649
+ assert_scoped (str s = " test" , s.try_assign (s.view ()), s == " test" );
650
+ assert_scoped (str s = " test" , s.try_assign (s.view ().sub (1 , 2 )), s == " e" );
651
+ assert_scoped (str s = " test" , s.try_append (s.view ().sub (1 , 2 )), s == " teste" );
652
+
653
+ // Try methods going beyond and beneath capacity threshold.
654
+ assert_scoped (str s = " 0123456789012345678901234567890123456789012345678901234567890123" , // 64 symbols at start
655
+ s.try_append (s) && s.try_append (s) && s.try_append (s) && s.try_append (s) && s.try_clear () &&
656
+ s.try_shrink_to_fit (),
657
+ s.capacity () < sz::string::min_capacity);
658
+
630
659
// Same length replacements.
631
660
assert_scoped (str s = " hello" , s.replace_all (" xx" , " xx" ), s == " hello" );
632
661
assert_scoped (str s = " hello" , s.replace_all (" l" , " 1" ), s == " he11o" );
0 commit comments