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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#![allow(
    dead_code,
    unused_imports,
    clippy::unnecessary_safety_comment,
    reason = "residue of Rc-test removal: orphaned helpers/imports kept to preserve surrounding test bodies verbatim"
)]

//! Tests for [`String`]: the growable arena-backed string builder.

#![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
#![allow(clippy::std_instead_of_core, reason = "tests use std")]
#![allow(clippy::unwrap_used, reason = "test code")]
#![allow(clippy::approx_constant, reason = "test uses 3.14 intentionally as a display value")]

mod common;

use core::cmp::Ordering;

use multitude::strings::String;
use multitude::vec::CollectIn;
use multitude::{Arena, FromIn};

#[test]
fn clear_and_reuse() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.push_str("hello");
    let cap_before = s.capacity();
    s.clear();
    assert_eq!(s.len(), 0);
    assert_eq!(s.capacity(), cap_before);
    s.push_str("world");
    assert_eq!(s.as_str(), "world");
}

#[test]
fn clear_when_unallocated_is_noop() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.clear();
    assert!(s.is_empty());
}

#[test]
fn size_40_bytes_on_64bit() {
    // 40 bytes = data ptr + cached len + cap + a freeze-prefix flag + arena
    // ref. The cached len (added for perf) avoids a chunk-memory load on every
    // read; the freeze-prefix flag records whether the backing buffer carries
    // the `Arc<[T]>` freeze prefix, letting `into_arc` / `into_boxed_slice`
    // reuse the buffer in place with no copy.
    if size_of::<usize>() == 8 {
        assert_eq!(size_of::<String<'_>>(), 40);
    }
}

#[test]
fn push_single_char() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.push('a');
    s.push('é');
    s.push('');
    assert_eq!(s.as_str(), "aé日");
}

#[test]
fn reserve_grows_capacity() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.reserve(100);
    assert!(s.capacity() >= 100);
    s.push_str("hi");
    assert_eq!(s.as_str(), "hi");
}

#[test]
fn reserve_noop_when_already_large() {
    let arena = Arena::new();
    let mut s = arena.alloc_string_with_capacity(200);
    let cap = s.capacity();
    s.reserve(50);
    assert_eq!(s.capacity(), cap);
}

#[test]
fn as_str_when_empty_returns_empty_slice() {
    let arena = Arena::new();
    let s = arena.alloc_string();
    assert_eq!(s.as_str(), "");
    assert_eq!(s.len(), 0);
    assert_eq!(s.capacity(), 0);
}

#[test]
fn extend_chars() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.extend(['a', 'b', 'c'].iter().copied());
    assert_eq!(s.as_str(), "abc");
}

#[test]
fn extend_chars_empty_iter() {
    // Hits the `lower == 0` branch in Extend<char>::extend.
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    let empty: [char; 0] = [];
    s.extend(empty.iter().copied());
    assert_eq!(s.as_str(), "");
}

#[test]
fn extend_strs() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.extend(["foo", "bar", "baz"].iter().copied());
    assert_eq!(s.as_str(), "foobarbaz");
}

#[test]
fn collect_in_chars() {
    let arena = Arena::new();
    let s: String<'_> = "héllo".chars().collect_in(&arena);
    assert_eq!(s.as_str(), "héllo");
}

#[test]
fn traits_compile() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.push_str("hi");
    let _: &str = s.as_ref();
    let r: &str = core::borrow::Borrow::borrow(&s);
    assert_eq!(r, "hi");
    assert_eq!(format!("{s:?}"), "\"hi\"");
    assert_eq!(format!("{s}"), "hi");
    let mut other = arena.alloc_string();
    other.push_str("hi");
    let mut big = arena.alloc_string();
    big.push_str("z");
    assert_eq!(s, other);
    assert!(s < big);
    assert_eq!(s.cmp(&big), Ordering::Less);
    assert_eq!(s.partial_cmp(&big), Some(Ordering::Less));
    assert_eq!(common::hash_of(&s), common::hash_of(&other));
}

#[test]
fn try_push_succeeds() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.try_push('a').unwrap();
    s.try_push('b').unwrap();
    assert_eq!(&*s, "ab");
}

#[test]
fn try_push_str_succeeds() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.try_push_str("hello").unwrap();
    s.try_push_str(" world").unwrap();
    assert_eq!(&*s, "hello world");
}

#[test]
fn try_push_str_empty_is_noop_ok() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.try_push_str("").unwrap();
    assert!(s.is_empty());
}

#[test]
fn try_reserve_succeeds() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.try_reserve(64).unwrap();
    assert!(s.capacity() >= 64);
}

#[test]
fn try_with_capacity_in_succeeds() {
    let arena = Arena::new();
    let s = arena.try_alloc_string_with_capacity(32).unwrap();
    assert!(s.capacity() >= 32);
    assert!(s.is_empty());
}

#[test]
fn try_with_capacity_in_zero_does_not_allocate() {
    let arena = Arena::new();
    let s = arena.try_alloc_string_with_capacity(0).unwrap();
    assert_eq!(s.capacity(), 0);
}

#[test]
fn try_push_str_returns_err_on_alloc_failure() {
    use allocator_api2::alloc::AllocError;
    // FailingAllocator with 0 budget: every allocate() fails.
    let alloc = common::FailingAllocator::new(0);
    let arena = Arena::new_in(alloc);
    let mut s = arena.alloc_string();
    let err: AllocError = s.try_push_str("x").unwrap_err();
    let _ = err;
}

#[test]
fn try_push_returns_err_on_alloc_failure() {
    let alloc = common::FailingAllocator::new(0);
    let arena = Arena::new_in(alloc);
    let mut s = arena.alloc_string();
    let _ = s.try_push('x').unwrap_err();
}

#[test]
fn try_reserve_returns_err_on_alloc_failure() {
    let alloc = common::FailingAllocator::new(0);
    let arena = Arena::new_in(alloc);
    let mut s = arena.alloc_string();
    let _ = s.try_reserve(16).unwrap_err();
}

#[test]
fn try_with_capacity_in_returns_err_on_alloc_failure() {
    let alloc = common::FailingAllocator::new(0);
    let arena = Arena::new_in(alloc);
    let result = arena.try_alloc_string_with_capacity(16);
    let _ = result.unwrap_err();
}

#[test]
fn try_grow_path_via_push_str_after_initial() {
    // Drives try_grow_to_at_least's slow path (cap > 0 branch).
    let arena = Arena::new();
    let mut s = arena.try_alloc_string_with_capacity(4).unwrap();
    s.try_push_str("abcd").unwrap(); // fills initial cap exactly
    s.try_push_str("e").unwrap(); // forces grow
    assert_eq!(&*s, "abcde");
    assert!(s.capacity() >= 5);
}

#[test]
fn from_in_str_copies_content() {
    let arena = Arena::new();
    let s = String::from_in("hello, world", &arena);
    assert_eq!(s.as_str(), "hello, world");
    assert!(s.capacity() >= "hello, world".len());
}

#[test]
fn from_in_str_empty() {
    let arena = Arena::new();
    let s = String::from_in("", &arena);
    assert!(s.is_empty());
    assert_eq!(s.capacity(), 0);
    assert_eq!(s.as_str(), "");
}

#[test]
fn as_bytes_returns_correct_bytes() {
    let arena = Arena::new();
    let s = String::from_in("héllo", &arena);
    assert_eq!(s.as_bytes(), "héllo".as_bytes());
}

#[test]
fn as_bytes_empty() {
    let arena = Arena::new();
    let s = arena.alloc_string();
    assert_eq!(s.as_bytes(), b"");
}

#[test]
fn as_mut_str_allows_mutation() {
    let arena = Arena::new();
    let mut s = String::from_in("hello", &arena);
    s.as_mut_str().make_ascii_uppercase();
    assert_eq!(s.as_str(), "HELLO");
}

#[test]
fn as_mut_str_empty() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    assert_eq!(s.as_mut_str(), "");
}

#[test]
fn as_ptr_and_as_mut_ptr() {
    let arena = Arena::new();
    let mut s = String::from_in("hi", &arena);
    // Read the bytes through a shared pointer *before* taking any mutable
    // pointer: `as_mut_ptr` reborrows `&mut str`/`&mut [u8]`, which would
    // invalidate an earlier shared pointer under Stacked Borrows — exactly as
    // it does in `std`, where both pointers come via `Deref`/`DerefMut` to
    // `str`.
    let p = s.as_ptr();
    // SAFETY: `p` addresses the first of the two initialized bytes "hi".
    unsafe {
        assert_eq!(*p, b'h');
    }
    // SAFETY: offset 1 is within the two initialized bytes.
    let p1 = unsafe { p.add(1) };
    // SAFETY: `p1` addresses the second initialized byte.
    unsafe {
        assert_eq!(*p1, b'i');
    }
    let const_addr = p.addr();
    // `as_ptr` and `as_mut_ptr` address the same buffer (compare addresses
    // only; the pointers' borrow tags differ).
    let mut_addr = s.as_mut_ptr().addr();
    assert_eq!(const_addr, mut_addr);
}

#[test]
fn pop_returns_chars_in_reverse() {
    let arena = Arena::new();
    let mut s = String::from_in("a💖é", &arena);
    assert_eq!(s.pop(), Some('é'));
    assert_eq!(s.pop(), Some('💖'));
    assert_eq!(s.pop(), Some('a'));
    assert_eq!(s.pop(), None);
    assert!(s.is_empty());
}

#[test]
fn truncate_shortens() {
    let arena = Arena::new();
    let mut s = String::from_in("hello", &arena);
    let cap = s.capacity();
    s.truncate(3);
    assert_eq!(s.as_str(), "hel");
    assert_eq!(s.capacity(), cap, "capacity unchanged");
}

#[test]
fn truncate_noop_when_longer() {
    let arena = Arena::new();
    let mut s = String::from_in("hi", &arena);
    s.truncate(50);
    assert_eq!(s.as_str(), "hi");
}

#[test]
#[should_panic(expected = "char boundary")]
fn truncate_panics_on_non_boundary() {
    let arena = Arena::new();
    let mut s = String::from_in("é", &arena); // 2 bytes
    s.truncate(1);
}

#[test]
fn shrink_to_fit_reclaims_when_at_cursor() {
    let arena = Arena::new();
    let mut s = arena.alloc_string_with_capacity(1024);
    s.push_str("short");
    let _len = s.len();
    s.shrink_to_fit();
    // Buffer is at the bump cursor, so shrink should succeed.
    assert_eq!(s.capacity(), 5);
    assert_eq!(s.as_str(), "short");
}

#[test]
fn shrink_to_fit_empty_or_full_noop() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.shrink_to_fit();
    assert_eq!(s.capacity(), 0);

    let mut s2 = arena.alloc_string_with_capacity(4);
    s2.push_str("abcd");
    let cap = s2.capacity();
    s2.shrink_to_fit();
    assert_eq!(s2.capacity(), cap);
}

#[test]
fn leak_reclaims_unused_capacity_tail() {
    // `String::leak` hands its unused `[len, cap)` byte tail back to the
    // chunk when the buffer is still at the bump cursor, so the next
    // allocation reuses that space.
    let arena = Arena::new();
    let mut s = arena.alloc_string_with_capacity(64);
    s.push_str("abc");
    let base = s.as_str().as_ptr() as usize;
    let leaked: &mut str = s.leak();
    assert_eq!(leaked, "abc");
    assert_eq!(leaked.as_ptr() as usize, base);
    // Reclaimed tail reused: the next str lands right after "abc".
    let next = arena.alloc_str("XY");
    assert_eq!(next.as_ptr() as usize, base + 3);
}

#[test]
fn drop_at_cursor_reclaims_storage() {
    // Dropping a `String` whose buffer ends at the bump cursor returns its
    // whole storage to the chunk; the next allocation reuses it.
    let arena = Arena::new();
    let base = {
        let mut s = arena.alloc_string_with_capacity(64);
        s.push_str("abc");
        s.as_str().as_ptr() as usize
    }; // `s` dropped here -> reclaims its backing bytes.
    let next = arena.alloc_str("WXYZ");
    assert_eq!(next.as_ptr() as usize, base);
}

#[test]
fn insert_at_various_positions() {
    let arena = Arena::new();
    let mut s = String::from_in("ac", &arena);
    s.insert(1, 'b');
    assert_eq!(s.as_str(), "abc");
    s.insert(0, 'Z');
    assert_eq!(s.as_str(), "Zabc");
    s.insert(s.len(), '!');
    assert_eq!(s.as_str(), "Zabc!");
}

#[test]
fn insert_multibyte_char() {
    let arena = Arena::new();
    let mut s = String::from_in("ab", &arena);
    s.insert(1, '💖');
    assert_eq!(s.as_str(), "a💖b");
}

#[test]
fn insert_str_grows() {
    let arena = Arena::new();
    let mut s = String::from_in("ad", &arena);
    s.insert_str(1, "bc");
    assert_eq!(s.as_str(), "abcd");
}

#[test]
fn insert_str_empty_is_noop() {
    let arena = Arena::new();
    let mut s = String::from_in("hi", &arena);
    s.insert_str(1, "");
    assert_eq!(s.as_str(), "hi");
}

#[test]
#[should_panic(expected = "char boundary")]
fn insert_panics_on_bad_index() {
    let arena = Arena::new();
    let mut s = String::from_in("é", &arena);
    s.insert(1, 'x');
}

#[test]
#[should_panic(expected = "insertion index out of bounds")]
fn insert_panics_when_idx_past_end() {
    let arena = Arena::new();
    let mut s = String::from_in("hi", &arena);
    s.insert(99, 'x');
}

#[test]
fn remove_returns_char() {
    let arena = Arena::new();
    let mut s = String::from_in("a💖c", &arena);
    let ch = s.remove(1);
    assert_eq!(ch, '💖');
    assert_eq!(s.as_str(), "ac");
}

#[test]
fn remove_first_and_last() {
    let arena = Arena::new();
    let mut s = String::from_in("abcd", &arena);
    assert_eq!(s.remove(0), 'a');
    assert_eq!(s.as_str(), "bcd");
    assert_eq!(s.remove(s.len() - 1), 'd');
    assert_eq!(s.as_str(), "bc");
}

#[test]
#[should_panic(expected = "out of bounds")]
fn remove_panics_when_empty() {
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    let _ = s.remove(0);
}

#[test]
fn retain_filters_chars() {
    let arena = Arena::new();
    let mut s = String::from_in("a1b2c3", &arena);
    s.retain(|c| c.is_ascii_alphabetic());
    assert_eq!(s.as_str(), "abc");
}

#[test]
fn retain_removes_all() {
    let arena = Arena::new();
    let mut s = String::from_in("hello", &arena);
    s.retain(|_| false);
    assert!(s.is_empty());
}

#[test]
fn retain_keeps_all() {
    let arena = Arena::new();
    let mut s = String::from_in("hello", &arena);
    s.retain(|_| true);
    assert_eq!(s.as_str(), "hello");
}

#[test]
fn retain_with_multibyte() {
    let arena = Arena::new();
    let mut s = String::from_in("a💖b💖c", &arena);
    s.retain(|c| c != '💖');
    assert_eq!(s.as_str(), "abc");
}

#[test]
fn replace_range_same_length() {
    let arena = Arena::new();
    let mut s = String::from_in("hello world", &arena);
    s.replace_range(6..11, "earth");
    assert_eq!(s.as_str(), "hello earth");
}

#[test]
fn replace_range_grow() {
    let arena = Arena::new();
    let mut s = String::from_in("hi world", &arena);
    s.replace_range(0..2, "hello");
    assert_eq!(s.as_str(), "hello world");
}

#[test]
fn replace_range_shrink() {
    let arena = Arena::new();
    let mut s = String::from_in("hello world", &arena);
    s.replace_range(0..5, "hi");
    assert_eq!(s.as_str(), "hi world");
}

#[test]
fn replace_range_unbounded() {
    let arena = Arena::new();
    let mut s = String::from_in("hello", &arena);
    s.replace_range(.., "goodbye");
    assert_eq!(s.as_str(), "goodbye");
}

#[test]
fn replace_range_empty_replacement() {
    let arena = Arena::new();
    let mut s = String::from_in("hello world", &arena);
    s.replace_range(5..11, "");
    assert_eq!(s.as_str(), "hello");
}

#[test]
fn replace_range_inclusive() {
    let arena = Arena::new();
    let mut s = String::from_in("abcdef", &arena);
    s.replace_range(1..=3, "XYZW");
    assert_eq!(s.as_str(), "aXYZWef");
}

#[test]
#[should_panic(expected = "char boundary")]
fn replace_range_panics_on_non_boundary() {
    let arena = Arena::new();
    let mut s = String::from_in("é", &arena);
    s.replace_range(0..1, "x");
}

#[test]
fn clone_produces_equal_independent_string() {
    let arena = Arena::new();
    let original = String::from_in("hello", &arena);
    let mut cloned = original.clone();
    assert_eq!(original.as_str(), cloned.as_str());
    // Independent buffers
    assert_ne!(original.as_ptr(), cloned.as_ptr());
    cloned.push_str(" world");
    assert_eq!(original.as_str(), "hello");
    assert_eq!(cloned.as_str(), "hello world");
}

#[test]
fn clone_empty() {
    let arena = Arena::new();
    let original = arena.alloc_string();
    let cloned = original.clone();
    assert_eq!(cloned.as_str(), "");
    assert_eq!(cloned.capacity(), 0);
}

#[test]
fn deref_mut_allows_mutation() {
    let arena = Arena::new();
    let mut s = String::from_in("hello", &arena);
    let r: &mut str = &mut s;
    r.make_ascii_uppercase();
    assert_eq!(s.as_str(), "HELLO");
}

#[test]
fn as_mut_trait_allows_mutation() {
    let arena = Arena::new();
    let mut s = String::from_in("abc", &arena);
    let r: &mut str = AsMut::as_mut(&mut s);
    r.make_ascii_uppercase();
    assert_eq!(s.as_str(), "ABC");
}

#[test]
fn borrow_mut_trait_allows_mutation() {
    let arena = Arena::new();
    let mut s = String::from_in("xyz", &arena);
    let r: &mut str = core::borrow::BorrowMut::borrow_mut(&mut s);
    r.make_ascii_uppercase();
    assert_eq!(s.as_str(), "XYZ");
}

#[test]
fn collect_str_slices_into_string() {
    let arena = Arena::new();
    let parts = ["hello", ", ", "world", "!"];
    let s: String = parts.into_iter().collect_in(&arena);
    assert_eq!(s.as_str(), "hello, world!");
}

#[test]
fn collect_empty_str_slices() {
    let arena = Arena::new();
    let parts: [&str; 0] = [];
    let s: String = parts.into_iter().collect_in(&arena);
    assert_eq!(s.as_str(), "");
    assert!(s.is_empty());
}

#[test]
fn collect_single_str_slice() {
    let arena = Arena::new();
    let s: String = core::iter::once("only").collect_in(&arena);
    assert_eq!(s.as_str(), "only");
}

#[test]
fn collect_str_slices_with_unicode() {
    let arena = Arena::new();
    let parts = ["café", " ", "naïve", " ", "résumé"];
    let s: String = parts.into_iter().collect_in(&arena);
    assert_eq!(s.as_str(), "café naïve résumé");
}

#[test]
fn collect_chars_into_string() {
    let arena = Arena::new();
    let s: String = "hello".chars().collect_in(&arena);
    assert_eq!(s.as_str(), "hello");
}

#[test]
fn collect_chars_empty() {
    let arena = Arena::new();
    let s: String = "".chars().collect_in(&arena);
    assert!(s.is_empty());
}

#[test]
fn write_macro_formats_into_string() {
    use core::fmt::Write;
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    write!(s, "x = {}, y = {}", 42, 3.14).unwrap();
    assert_eq!(s.as_str(), "x = 42, y = 3.14");
}

#[test]
fn write_macro_appends() {
    use core::fmt::Write;
    let arena = Arena::new();
    let mut s = String::from_in("prefix:", &arena);
    write!(s, " {}", 100).unwrap();
    assert_eq!(s.as_str(), "prefix: 100");
}

#[test]
fn write_char_via_trait() {
    use core::fmt::Write;
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.write_char('A').unwrap();
    s.write_char('B').unwrap();
    s.write_char('C').unwrap();
    assert_eq!(s.as_str(), "ABC");
}

#[test]
fn write_str_via_trait() {
    use core::fmt::Write;
    let arena = Arena::new();
    let mut s = arena.alloc_string();
    s.write_str("hello").unwrap();
    s.write_str(", world").unwrap();
    assert_eq!(s.as_str(), "hello, world");
}

mod arena_str {
    #![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
    #![allow(clippy::std_instead_of_core, reason = "tests use std")]
    #![allow(clippy::unwrap_used, reason = "test code")]
    #![allow(clippy::redundant_clone, reason = "tests exercise Clone explicitly")]
    use core::cmp::Ordering;
    use std::collections::{BTreeMap, HashMap};

    use multitude::Arena;

    use crate::common;

    #[test]
    fn arena_arc_str_basic() {
        let arena = Arena::new();
        let s = arena.alloc_str_arc("hi");
        assert_eq!(&*s, "hi");
        assert_eq!(s.len(), 2);
        assert!(!s.is_empty());
        let s2 = s.clone();
        let h = std::thread::spawn(move || {
            assert_eq!(&*s2, "hi");
        });
        h.join().unwrap();
    }

    #[test]
    fn arena_arc_str_empty() {
        let arena = Arena::new();
        let s = arena.alloc_str_arc("");
        assert_eq!(s.len(), 0);
        assert!(s.is_empty());
    }

    #[test]
    fn arena_arc_str_traits_compile() {
        let arena = Arena::new();
        let s = arena.alloc_str_arc("hi");
        let _: &str = s.as_ref();
        let r: &str = core::borrow::Borrow::borrow(&s);
        assert_eq!(r, "hi");
        assert_eq!(format!("{s:?}"), "\"hi\"");
        assert_eq!(format!("{s}"), "hi");
        let other = arena.alloc_str_arc("hi");
        let big = arena.alloc_str_arc("z");
        assert_eq!(s, other);
        assert!(s < big);
        assert_eq!(s.cmp(&big), Ordering::Less);
        assert_eq!(s.partial_cmp(&big), Some(Ordering::Less));
        assert_eq!(common::hash_of(&s), common::hash_of(&other));
    }

    #[test]
    fn arena_arc_str_outlives_arena() {
        // Drives the `teardown_chunk(chunk, false)` branch in
        // Arc<str>::Drop when this is the LAST reference and the arena
        // itself has already been dropped.
        let s: multitude::Arc<str> = {
            let arena = Arena::new();
            arena.alloc_str_arc("survives the arena")
        };
        assert_eq!(&*s, "survives the arena");
        drop(s); // teardown_chunk(chunk, false) for the Chunk.
    }

    #[test]
    fn from_arena_arc_str_to_arena_arc_byte_slice() {
        use multitude::Arc;
        let arena = Arena::new();
        let s: Arc<str> = arena.alloc_str_arc("payload");
        let bytes: Arc<[u8]> = s.into();
        assert_eq!(&*bytes, b"payload");
    }

    #[test]
    fn arena_arc_byte_slice_is_send_sync() {
        use multitude::Arc;
        let arena = Arena::new();
        let s: Arc<str> = arena.alloc_str_arc("threaded");
        let bytes: Arc<[u8]> = s.into();
        let bytes2 = bytes.clone();
        let h = std::thread::spawn(move || bytes2.len());
        assert_eq!(h.join().unwrap(), bytes.len());
    }
}

mod arena_box_str {
    #![allow(clippy::clone_on_ref_ptr, reason = "tests prefer concise method-call form")]
    #![allow(clippy::std_instead_of_core, reason = "tests use std")]
    #![allow(clippy::unwrap_used, reason = "test code")]

    use core::cmp::Ordering;
    use std::collections::{BTreeMap, HashMap};

    use multitude::{Arena, Box};

    use crate::common;

    #[test]
    fn arena_box_str_basic() {
        let arena = Arena::new();
        let s = arena.alloc_str_box("hello, world");
        assert_eq!(&*s, "hello, world");
        assert_eq!(s.len(), 12);
        assert!(!s.is_empty());
    }

    #[test]
    fn arena_box_str_empty() {
        let arena = Arena::new();
        let s = arena.alloc_str_box("");
        assert_eq!(&*s, "");
        assert_eq!(s.len(), 0);
        assert!(s.is_empty());
    }

    #[test]
    fn arena_box_str_is_eight_bytes() {
        // The whole reason `Box<str>` exists rather than `ArenaBox<str>`
        // (16 bytes via fat pointer): single-pointer compactness.
        assert_eq!(size_of::<Box<str>>(), size_of::<usize>());
    }

    #[test]
    fn arena_box_str_mutable_in_place() {
        let arena = Arena::new();
        let mut s = arena.alloc_str_box("hello");
        s.make_ascii_uppercase();
        assert_eq!(&*s, "HELLO");
    }

    #[test]
    fn arena_box_str_as_mut_via_deref_mut() {
        let arena = Arena::new();
        let mut s = arena.alloc_str_box("Mixed Case");
        let m: &mut str = &mut s;
        m.make_ascii_lowercase();
        assert_eq!(&*s, "mixed case");
    }

    #[test]
    fn arena_box_str_accepts_string() {
        // impl AsRef<str> covers both &str and String.
        let arena = Arena::new();
        let owned = std::string::String::from("from String");
        let s = arena.alloc_str_box(owned);
        assert_eq!(&*s, "from String");
    }

    #[test]
    fn try_alloc_str_box_succeeds() {
        let arena = Arena::new();
        let s = arena.try_alloc_str_box("ok").unwrap();
        assert_eq!(&*s, "ok");
    }

    #[test]
    fn arena_box_str_traits_compile() {
        let arena = Arena::new();
        let s = arena.alloc_str_box("hi");
        let _: &str = s.as_ref();
        let r: &str = core::borrow::Borrow::borrow(&s);
        assert_eq!(r, "hi");
        assert_eq!(format!("{s:?}"), "\"hi\"");
        assert_eq!(format!("{s}"), "hi");
        let other = arena.alloc_str_box("hi");
        let big = arena.alloc_str_box("z");
        assert_eq!(s, other);
        assert!(s < big);
        assert_eq!(s.cmp(&big), Ordering::Less);
        assert_eq!(s.partial_cmp(&big), Some(Ordering::Less));
        assert_eq!(common::hash_of(&s), common::hash_of(&other));
    }

    #[test]
    fn arena_box_str_eq_and_hash_via_hashmap() {
        let arena = Arena::new();
        let key = arena.alloc_str_box("key");
        let mut map: HashMap<Box<str>, i32> = HashMap::new();
        let _ = map.insert(key, 1);
        // Borrow<str> lookup also works, so we don't need the original key.
        assert_eq!(map.get("key"), Some(&1));
    }

    #[test]
    fn arena_box_str_works_as_btreemap_key() {
        let arena = Arena::new();
        let mut m: BTreeMap<Box<str>, u32> = BTreeMap::new();
        let _ = m.insert(arena.alloc_str_box("a"), 1);
        let _ = m.insert(arena.alloc_str_box("b"), 2);
        assert_eq!(m.get("a"), Some(&1));
        assert_eq!(m.get("b"), Some(&2));
    }

    #[test]
    fn arena_box_str_drop_releases_chunk_immediately() {
        // Box<str> drops its chunk hold the moment the smart pointer is dropped.
        // Subsequent allocations in the arena must still work, exercising
        // the dec_ref + (optional) teardown_chunk path in Box<str>::Drop.
        let arena = Arena::new();
        let s = arena.alloc_str_box("temporary");
        assert_eq!(&*s, "temporary");
        drop(s);
        // Arena still works.
        let s2 = arena.alloc_str_box("after-drop");
        assert_eq!(&*s2, "after-drop");
    }

    #[test]
    fn arena_box_str_lifetime_bound_to_arena() {
        // The borrow checker must reject use of an `Box<str>` whose
        // arena has been dropped. We can't write a runtime test for the
        // negative case (it's a compile error), but we can verify positive
        // case: dropping the box BEFORE the arena is fine.
        let arena = Arena::new();
        {
            let s = arena.alloc_str_box("inner");
            assert_eq!(&*s, "inner");
        }
        let s2 = arena.alloc_str_box("outer");
        assert_eq!(&*s2, "outer");
    }

    #[test]
    fn many_arena_box_str_allocations_force_chunk_rotation() {
        let arena = Arena::builder().build();
        let mut handles = std::vec::Vec::new();
        for i in 0..200 {
            handles.push(arena.alloc_str_box(format!("item{i}")));
        }
        assert_eq!(&*handles[0], "item0");
        assert_eq!(&*handles[199], "item199");
    }

    #[test]
    fn arena_box_str_round_trip_through_drop_does_not_corrupt() {
        let arena = Arena::new();
        // 256 transient boxes still exercise repeated alloc/drop teardown on
        // the same arena without paying for 1000 interpreted drops under miri.
        for i in 0..256 {
            let s = arena.alloc_str_box(format!("transient-{i}"));
            // Each iteration: alloc, mutate, drop. The dec_ref + teardown
            // must keep the arena healthy across many iterations.
            let _ = s.len();
        }
        // Final allocation works too.
        let s = arena.alloc_str_box("final");
        assert_eq!(&*s, "final");
    }

    #[test]
    fn arena_box_str_borrow_mut_and_pointer() {
        use core::borrow::BorrowMut;
        let arena = Arena::new();
        let mut s = arena.alloc_str_box("hello");
        let m: &mut str = s.borrow_mut();
        m.make_ascii_uppercase();
        assert_eq!(&*s, "HELLO");
        let p = format!("{s:p}");
        assert!(p.starts_with("0x"), "Pointer format: {p}");
    }
}

mod mutants_for_string {
    #![allow(clippy::std_instead_of_core, reason = "test code")]
    #![allow(clippy::unwrap_used, reason = "test code")]
    #![allow(clippy::doc_markdown, reason = "doc comments cite raw identifier names")]
    use multitude::Arena;
    use multitude::strings::String as MString;

    #[expect(unused_imports, reason = "merged test module re-exports common helpers")]
    use crate::common;

    #[test]
    fn with_capacity_zero_does_not_allocate() {
        let arena = Arena::new();
        let s0 = arena.alloc_string_with_capacity(0);
        assert_eq!(s0.capacity(), 0);
        // Compare against `new_in` (the documented no-alloc constructor).
        let s_new = arena.alloc_string();
        assert_eq!(s_new.capacity(), 0);
        // Both have the same dangling data pointer (== 1 by NonNull::dangling()).
        // ptr identity is the strongest observable signal here.
        assert_eq!(s0.as_ptr() as usize, s_new.as_ptr() as usize);
    }

    #[test]
    fn string_reserve_at_exact_fit_does_not_regrow() {
        let arena = Arena::new();
        let mut s = arena.alloc_string_with_capacity(16);
        let cap = s.capacity();
        let ptr_before = s.as_ptr();
        s.reserve(cap); // additional == cap → needed == cap (len was 0)
        assert_eq!(s.capacity(), cap);
        assert_eq!(s.as_ptr(), ptr_before);
    }

    #[test]
    fn into_box_handles_empty_and_non_empty() {
        let arena = Arena::new();

        let s_empty = arena.alloc_string();
        let b_empty = s_empty.into_boxed_str();
        assert_eq!(&*b_empty, "");
        assert_eq!(b_empty.len(), 0);

        let mut s = arena.alloc_string_with_capacity(16);
        s.push_str("hello");
        let b = s.into_boxed_str();
        assert_eq!(&*b, "hello");
        assert_eq!(b.len(), 5);
    }

    /// `String::into_arc` freezes into a shared, reference-counted
    /// `Arc<str>` whose contents match the builder, for both empty and
    /// non-empty inputs, and which can be cloned and outlive the arena.
    #[test]
    fn into_arc_handles_empty_and_non_empty() {
        use multitude::Arc;

        let arena = Arena::new();

        let s_empty = arena.alloc_string();
        let a_empty: Arc<str> = Arc::from(s_empty);
        assert_eq!(&*a_empty, "");
        assert_eq!(a_empty.len(), 0);

        let mut s = arena.alloc_string_with_capacity(16);
        s.push_str("hello");
        let a: Arc<str> = Arc::from(s);
        assert_eq!(&*a, "hello");
        assert_eq!(a.len(), 5);

        // Cloning shares the same backing allocation.
        let a2 = a.clone();
        assert_eq!(&*a2, "hello");
        assert_eq!(a.as_ptr(), a2.as_ptr());
    }

    /// An `Arc<str>` produced by `into_arc` outlives the arena it was
    /// built from (the backing chunk is held by the refcount).
    #[test]
    fn into_arc_outlives_arena() {
        use multitude::Arc;

        let escaped: Arc<str> = {
            let arena = Arena::new();
            let mut s = arena.alloc_string();
            s.push_str("survives");
            let a = Arc::from(s);
            drop(arena);
            a
        };
        assert_eq!(&*escaped, "survives");
    }

    #[test]
    fn reclaim_tail_does_not_corrupt_frozen_string() {
        let arena = Arena::new();
        let mut s = arena.alloc_string_with_capacity(256);
        s.push_str("frozen!");
        let frozen = s.into_boxed_str();
        let _filler: multitude::vec::Vec<'_, u64> = {
            let mut v = arena.alloc_vec_with_capacity::<u64>(64);
            for i in 0..64 {
                v.push(i);
            }
            v
        };
        assert_eq!(&*frozen, "frozen!");
    }
}

mod format_macro {
    #![allow(clippy::std_instead_of_core, reason = "tests use std")]
    #![allow(clippy::unwrap_used, reason = "test code")]
    use multitude::Arena;

    #[expect(unused_imports, reason = "merged test module re-exports common helpers")]
    use crate::common;

    #[test]
    fn format_macro_basic() {
        let arena = Arena::new();
        let name = "world";
        let s = multitude::strings::format!(in &arena, "hello, {name}!");
        assert_eq!(&*s, "hello, world!");
    }

    #[test]
    fn format_macro_with_multiple_args() {
        let arena = Arena::new();
        let s = multitude::strings::format!(in &arena, "{}+{}={}", 2, 3, 5);
        assert_eq!(&*s, "2+3=5");
    }

    #[test]
    fn format_macro_empty_format_string() {
        let arena = Arena::new();
        let s = multitude::strings::format!(in &arena, "");
        assert_eq!(&*s, "");
    }

    #[test]
    fn arena_string_is_a_fmt_write_target() {
        use core::fmt::Write;
        let arena = Arena::new();
        let mut s = arena.alloc_string();
        write!(&mut s, "x={}", 42).unwrap();
        assert_eq!(s.as_str(), "x=42");

        s.write_char('!').unwrap();
        assert_eq!(s.as_str(), "x=42!");
    }
}

/// Zero-copy freeze: `String::into_boxed_str` / `into_arc_str` reuse the
/// backing `Vec<u8>` storage in place (retag `[u8] → str`, no copy).
mod string_zero_copy_freeze {
    use std::thread;

    use multitude::strings::String;
    use multitude::{Arc, Arena, Box};

    #[test]
    fn into_boxed_str_reuses_buffer_in_place() {
        let arena = Arena::new();
        let mut s = arena.alloc_string();
        s.push_str("hello, world");
        let data_ptr = s.as_str().as_ptr();
        let b: Box<str> = s.into_boxed_str();
        assert_eq!(&*b, "hello, world");
        assert_eq!(b.as_str().as_ptr(), data_ptr, "into_boxed_str must not copy");
    }

    #[test]
    fn into_arc_str_reuses_buffer_in_place() {
        let arena = Arena::new();
        let mut s = arena.alloc_string();
        s.push_str("shared string");
        let data_ptr = s.as_str().as_ptr();
        let a: Arc<str> = Arc::from(s);
        assert_eq!(&*a, "shared string");
        assert_eq!(a.as_str().as_ptr(), data_ptr, "into_arc_str must not copy");
        let a2 = a.clone();
        assert!(Arc::ptr_eq(&a, &a2), "clone shares the same frozen payload");
        assert_eq!(&*a2, "shared string");
    }

    #[test]
    fn frozen_arc_str_outlives_arena_and_crosses_threads() {
        let arc: Arc<str> = {
            let arena = Arena::new();
            let mut s = arena.alloc_string();
            s.push_str("survives teardown");
            Arc::from(s)
        };
        let clone = arc.clone();
        let len = thread::spawn(move || clone.len()).join().unwrap();
        assert_eq!(len, "survives teardown".len());
        assert_eq!(&*arc, "survives teardown");
    }

    #[test]
    fn grown_string_freezes_in_place_after_relocation() {
        let arena = Arena::new();
        let mut s = arena.alloc_string();
        for _ in 0..64 {
            s.push_str("abcd");
        }
        let data_ptr = s.as_str().as_ptr();
        let b = s.into_boxed_str();
        assert_eq!(b.len(), 256);
        assert_eq!(b.as_str().as_ptr(), data_ptr, "relocated buffer still freezes in place");
    }

    #[test]
    fn empty_string_freezes_to_empty_str() {
        let arena = Arena::new();
        let s = arena.alloc_string();
        let b = s.into_boxed_str();
        assert_eq!(&*b, "");
        let s2 = arena.alloc_string();
        let a: Arc<str> = Arc::from(s2);
        assert_eq!(&*a, "");
    }
}