]> git.lizzy.rs Git - rust.git/blob - src/liballoc/tests/string.rs
rustc_span: return an impl Iterator instead of a Vec from macro_backtrace.
[rust.git] / src / liballoc / tests / string.rs
1 use std::borrow::Cow;
2 use std::collections::TryReserveError::*;
3 use std::mem::size_of;
4 use std::{isize, usize};
5
6 pub trait IntoCow<'a, B: ?Sized>
7 where
8     B: ToOwned,
9 {
10     fn into_cow(self) -> Cow<'a, B>;
11 }
12
13 impl<'a> IntoCow<'a, str> for String {
14     fn into_cow(self) -> Cow<'a, str> {
15         Cow::Owned(self)
16     }
17 }
18
19 impl<'a> IntoCow<'a, str> for &'a str {
20     fn into_cow(self) -> Cow<'a, str> {
21         Cow::Borrowed(self)
22     }
23 }
24
25 #[test]
26 fn test_from_str() {
27     let owned: Option<std::string::String> = "string".parse().ok();
28     assert_eq!(owned.as_ref().map(|s| &**s), Some("string"));
29 }
30
31 #[test]
32 fn test_from_cow_str() {
33     assert_eq!(String::from(Cow::Borrowed("string")), "string");
34     assert_eq!(String::from(Cow::Owned(String::from("string"))), "string");
35 }
36
37 #[test]
38 fn test_unsized_to_string() {
39     let s: &str = "abc";
40     let _: String = (*s).to_string();
41 }
42
43 #[test]
44 fn test_from_utf8() {
45     let xs = b"hello".to_vec();
46     assert_eq!(String::from_utf8(xs).unwrap(), String::from("hello"));
47
48     let xs = "ศไทย中华Việt Nam".as_bytes().to_vec();
49     assert_eq!(String::from_utf8(xs).unwrap(), String::from("ศไทย中华Việt Nam"));
50
51     let xs = b"hello\xFF".to_vec();
52     let err = String::from_utf8(xs).unwrap_err();
53     assert_eq!(err.into_bytes(), b"hello\xff".to_vec());
54 }
55
56 #[test]
57 fn test_from_utf8_lossy() {
58     let xs = b"hello";
59     let ys: Cow<'_, str> = "hello".into_cow();
60     assert_eq!(String::from_utf8_lossy(xs), ys);
61
62     let xs = "ศไทย中华Việt Nam".as_bytes();
63     let ys: Cow<'_, str> = "ศไทย中华Việt Nam".into_cow();
64     assert_eq!(String::from_utf8_lossy(xs), ys);
65
66     let xs = b"Hello\xC2 There\xFF Goodbye";
67     assert_eq!(
68         String::from_utf8_lossy(xs),
69         String::from("Hello\u{FFFD} There\u{FFFD} Goodbye").into_cow()
70     );
71
72     let xs = b"Hello\xC0\x80 There\xE6\x83 Goodbye";
73     assert_eq!(
74         String::from_utf8_lossy(xs),
75         String::from("Hello\u{FFFD}\u{FFFD} There\u{FFFD} Goodbye").into_cow()
76     );
77
78     let xs = b"\xF5foo\xF5\x80bar";
79     assert_eq!(
80         String::from_utf8_lossy(xs),
81         String::from("\u{FFFD}foo\u{FFFD}\u{FFFD}bar").into_cow()
82     );
83
84     let xs = b"\xF1foo\xF1\x80bar\xF1\x80\x80baz";
85     assert_eq!(
86         String::from_utf8_lossy(xs),
87         String::from("\u{FFFD}foo\u{FFFD}bar\u{FFFD}baz").into_cow()
88     );
89
90     let xs = b"\xF4foo\xF4\x80bar\xF4\xBFbaz";
91     assert_eq!(
92         String::from_utf8_lossy(xs),
93         String::from("\u{FFFD}foo\u{FFFD}bar\u{FFFD}\u{FFFD}baz").into_cow()
94     );
95
96     let xs = b"\xF0\x80\x80\x80foo\xF0\x90\x80\x80bar";
97     assert_eq!(
98         String::from_utf8_lossy(xs),
99         String::from("\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}foo\u{10000}bar").into_cow()
100     );
101
102     // surrogates
103     let xs = b"\xED\xA0\x80foo\xED\xBF\xBFbar";
104     assert_eq!(
105         String::from_utf8_lossy(xs),
106         String::from("\u{FFFD}\u{FFFD}\u{FFFD}foo\u{FFFD}\u{FFFD}\u{FFFD}bar").into_cow()
107     );
108 }
109
110 #[test]
111 fn test_from_utf16() {
112     let pairs = [
113         (
114             String::from("𐍅𐌿𐌻𐍆𐌹𐌻𐌰\n"),
115             vec![
116                 0xd800, 0xdf45, 0xd800, 0xdf3f, 0xd800, 0xdf3b, 0xd800, 0xdf46, 0xd800, 0xdf39,
117                 0xd800, 0xdf3b, 0xd800, 0xdf30, 0x000a,
118             ],
119         ),
120         (
121             String::from("𐐒𐑉𐐮𐑀𐐲𐑋 𐐏𐐲𐑍\n"),
122             vec![
123                 0xd801, 0xdc12, 0xd801, 0xdc49, 0xd801, 0xdc2e, 0xd801, 0xdc40, 0xd801, 0xdc32,
124                 0xd801, 0xdc4b, 0x0020, 0xd801, 0xdc0f, 0xd801, 0xdc32, 0xd801, 0xdc4d, 0x000a,
125             ],
126         ),
127         (
128             String::from("𐌀𐌖𐌋𐌄𐌑𐌉·𐌌𐌄𐌕𐌄𐌋𐌉𐌑\n"),
129             vec![
130                 0xd800, 0xdf00, 0xd800, 0xdf16, 0xd800, 0xdf0b, 0xd800, 0xdf04, 0xd800, 0xdf11,
131                 0xd800, 0xdf09, 0x00b7, 0xd800, 0xdf0c, 0xd800, 0xdf04, 0xd800, 0xdf15, 0xd800,
132                 0xdf04, 0xd800, 0xdf0b, 0xd800, 0xdf09, 0xd800, 0xdf11, 0x000a,
133             ],
134         ),
135         (
136             String::from("𐒋𐒘𐒈𐒑𐒛𐒒 𐒕𐒓 𐒈𐒚𐒍 𐒏𐒜𐒒𐒖𐒆 𐒕𐒆\n"),
137             vec![
138                 0xd801, 0xdc8b, 0xd801, 0xdc98, 0xd801, 0xdc88, 0xd801, 0xdc91, 0xd801, 0xdc9b,
139                 0xd801, 0xdc92, 0x0020, 0xd801, 0xdc95, 0xd801, 0xdc93, 0x0020, 0xd801, 0xdc88,
140                 0xd801, 0xdc9a, 0xd801, 0xdc8d, 0x0020, 0xd801, 0xdc8f, 0xd801, 0xdc9c, 0xd801,
141                 0xdc92, 0xd801, 0xdc96, 0xd801, 0xdc86, 0x0020, 0xd801, 0xdc95, 0xd801, 0xdc86,
142                 0x000a,
143             ],
144         ),
145         // Issue #12318, even-numbered non-BMP planes
146         (String::from("\u{20000}"), vec![0xD840, 0xDC00]),
147     ];
148
149     for p in &pairs {
150         let (s, u) = (*p).clone();
151         let s_as_utf16 = s.encode_utf16().collect::<Vec<u16>>();
152         let u_as_string = String::from_utf16(&u).unwrap();
153
154         assert!(core::char::decode_utf16(u.iter().cloned()).all(|r| r.is_ok()));
155         assert_eq!(s_as_utf16, u);
156
157         assert_eq!(u_as_string, s);
158         assert_eq!(String::from_utf16_lossy(&u), s);
159
160         assert_eq!(String::from_utf16(&s_as_utf16).unwrap(), s);
161         assert_eq!(u_as_string.encode_utf16().collect::<Vec<u16>>(), u);
162     }
163 }
164
165 #[test]
166 fn test_utf16_invalid() {
167     // completely positive cases tested above.
168     // lead + eof
169     assert!(String::from_utf16(&[0xD800]).is_err());
170     // lead + lead
171     assert!(String::from_utf16(&[0xD800, 0xD800]).is_err());
172
173     // isolated trail
174     assert!(String::from_utf16(&[0x0061, 0xDC00]).is_err());
175
176     // general
177     assert!(String::from_utf16(&[0xD800, 0xd801, 0xdc8b, 0xD800]).is_err());
178 }
179
180 #[test]
181 fn test_from_utf16_lossy() {
182     // completely positive cases tested above.
183     // lead + eof
184     assert_eq!(String::from_utf16_lossy(&[0xD800]), String::from("\u{FFFD}"));
185     // lead + lead
186     assert_eq!(String::from_utf16_lossy(&[0xD800, 0xD800]), String::from("\u{FFFD}\u{FFFD}"));
187
188     // isolated trail
189     assert_eq!(String::from_utf16_lossy(&[0x0061, 0xDC00]), String::from("a\u{FFFD}"));
190
191     // general
192     assert_eq!(
193         String::from_utf16_lossy(&[0xD800, 0xd801, 0xdc8b, 0xD800]),
194         String::from("\u{FFFD}𐒋\u{FFFD}")
195     );
196 }
197
198 #[test]
199 fn test_push_bytes() {
200     let mut s = String::from("ABC");
201     unsafe {
202         let mv = s.as_mut_vec();
203         mv.extend_from_slice(&[b'D']);
204     }
205     assert_eq!(s, "ABCD");
206 }
207
208 #[test]
209 fn test_push_str() {
210     let mut s = String::new();
211     s.push_str("");
212     assert_eq!(&s[0..], "");
213     s.push_str("abc");
214     assert_eq!(&s[0..], "abc");
215     s.push_str("ประเทศไทย中华Việt Nam");
216     assert_eq!(&s[0..], "abcประเทศไทย中华Việt Nam");
217 }
218
219 #[test]
220 fn test_add_assign() {
221     let mut s = String::new();
222     s += "";
223     assert_eq!(s.as_str(), "");
224     s += "abc";
225     assert_eq!(s.as_str(), "abc");
226     s += "ประเทศไทย中华Việt Nam";
227     assert_eq!(s.as_str(), "abcประเทศไทย中华Việt Nam");
228 }
229
230 #[test]
231 fn test_push() {
232     let mut data = String::from("ประเทศไทย中");
233     data.push('华');
234     data.push('b'); // 1 byte
235     data.push('¢'); // 2 byte
236     data.push('€'); // 3 byte
237     data.push('𤭢'); // 4 byte
238     assert_eq!(data, "ประเทศไทย中华b¢€𤭢");
239 }
240
241 #[test]
242 fn test_pop() {
243     let mut data = String::from("ประเทศไทย中华b¢€𤭢");
244     assert_eq!(data.pop().unwrap(), '𤭢'); // 4 bytes
245     assert_eq!(data.pop().unwrap(), '€'); // 3 bytes
246     assert_eq!(data.pop().unwrap(), '¢'); // 2 bytes
247     assert_eq!(data.pop().unwrap(), 'b'); // 1 bytes
248     assert_eq!(data.pop().unwrap(), '华');
249     assert_eq!(data, "ประเทศไทย中");
250 }
251
252 #[test]
253 fn test_split_off_empty() {
254     let orig = "Hello, world!";
255     let mut split = String::from(orig);
256     let empty: String = split.split_off(orig.len());
257     assert!(empty.is_empty());
258 }
259
260 #[test]
261 #[should_panic]
262 fn test_split_off_past_end() {
263     let orig = "Hello, world!";
264     let mut split = String::from(orig);
265     split.split_off(orig.len() + 1);
266 }
267
268 #[test]
269 #[should_panic]
270 fn test_split_off_mid_char() {
271     let mut orig = String::from("山");
272     orig.split_off(1);
273 }
274
275 #[test]
276 fn test_split_off_ascii() {
277     let mut ab = String::from("ABCD");
278     let cd = ab.split_off(2);
279     assert_eq!(ab, "AB");
280     assert_eq!(cd, "CD");
281 }
282
283 #[test]
284 fn test_split_off_unicode() {
285     let mut nihon = String::from("日本語");
286     let go = nihon.split_off("日本".len());
287     assert_eq!(nihon, "日本");
288     assert_eq!(go, "語");
289 }
290
291 #[test]
292 fn test_str_truncate() {
293     let mut s = String::from("12345");
294     s.truncate(5);
295     assert_eq!(s, "12345");
296     s.truncate(3);
297     assert_eq!(s, "123");
298     s.truncate(0);
299     assert_eq!(s, "");
300
301     let mut s = String::from("12345");
302     let p = s.as_ptr();
303     s.truncate(3);
304     s.push_str("6");
305     let p_ = s.as_ptr();
306     assert_eq!(p_, p);
307 }
308
309 #[test]
310 fn test_str_truncate_invalid_len() {
311     let mut s = String::from("12345");
312     s.truncate(6);
313     assert_eq!(s, "12345");
314 }
315
316 #[test]
317 #[should_panic]
318 fn test_str_truncate_split_codepoint() {
319     let mut s = String::from("\u{FC}"); // ü
320     s.truncate(1);
321 }
322
323 #[test]
324 fn test_str_clear() {
325     let mut s = String::from("12345");
326     s.clear();
327     assert_eq!(s.len(), 0);
328     assert_eq!(s, "");
329 }
330
331 #[test]
332 fn test_str_add() {
333     let a = String::from("12345");
334     let b = a + "2";
335     let b = b + "2";
336     assert_eq!(b.len(), 7);
337     assert_eq!(b, "1234522");
338 }
339
340 #[test]
341 fn remove() {
342     let mut s = "ศไทย中华Việt Nam; foobar".to_string();
343     assert_eq!(s.remove(0), 'ศ');
344     assert_eq!(s.len(), 33);
345     assert_eq!(s, "ไทย中华Việt Nam; foobar");
346     assert_eq!(s.remove(17), 'ệ');
347     assert_eq!(s, "ไทย中华Vit Nam; foobar");
348 }
349
350 #[test]
351 #[should_panic]
352 fn remove_bad() {
353     "ศ".to_string().remove(1);
354 }
355
356 #[test]
357 fn test_retain() {
358     let mut s = String::from("α_β_γ");
359
360     s.retain(|_| true);
361     assert_eq!(s, "α_β_γ");
362
363     s.retain(|c| c != '_');
364     assert_eq!(s, "αβγ");
365
366     s.retain(|c| c != 'β');
367     assert_eq!(s, "αγ");
368
369     s.retain(|c| c == 'α');
370     assert_eq!(s, "α");
371
372     s.retain(|_| false);
373     assert_eq!(s, "");
374 }
375
376 #[test]
377 fn insert() {
378     let mut s = "foobar".to_string();
379     s.insert(0, 'ệ');
380     assert_eq!(s, "ệfoobar");
381     s.insert(6, 'ย');
382     assert_eq!(s, "ệfooยbar");
383 }
384
385 #[test]
386 #[should_panic]
387 fn insert_bad1() {
388     "".to_string().insert(1, 't');
389 }
390 #[test]
391 #[should_panic]
392 fn insert_bad2() {
393     "ệ".to_string().insert(1, 't');
394 }
395
396 #[test]
397 fn test_slicing() {
398     let s = "foobar".to_string();
399     assert_eq!("foobar", &s[..]);
400     assert_eq!("foo", &s[..3]);
401     assert_eq!("bar", &s[3..]);
402     assert_eq!("oob", &s[1..4]);
403 }
404
405 #[test]
406 fn test_simple_types() {
407     assert_eq!(1.to_string(), "1");
408     assert_eq!((-1).to_string(), "-1");
409     assert_eq!(200.to_string(), "200");
410     assert_eq!(2.to_string(), "2");
411     assert_eq!(true.to_string(), "true");
412     assert_eq!(false.to_string(), "false");
413     assert_eq!(("hi".to_string()).to_string(), "hi");
414 }
415
416 #[test]
417 fn test_vectors() {
418     let x: Vec<i32> = vec![];
419     assert_eq!(format!("{:?}", x), "[]");
420     assert_eq!(format!("{:?}", vec![1]), "[1]");
421     assert_eq!(format!("{:?}", vec![1, 2, 3]), "[1, 2, 3]");
422     assert!(format!("{:?}", vec![vec![], vec![1], vec![1, 1]]) == "[[], [1], [1, 1]]");
423 }
424
425 #[test]
426 fn test_from_iterator() {
427     let s = "ศไทย中华Việt Nam".to_string();
428     let t = "ศไทย中华";
429     let u = "Việt Nam";
430
431     let a: String = s.chars().collect();
432     assert_eq!(s, a);
433
434     let mut b = t.to_string();
435     b.extend(u.chars());
436     assert_eq!(s, b);
437
438     let c: String = vec![t, u].into_iter().collect();
439     assert_eq!(s, c);
440
441     let mut d = t.to_string();
442     d.extend(vec![u]);
443     assert_eq!(s, d);
444 }
445
446 #[test]
447 fn test_drain() {
448     let mut s = String::from("αβγ");
449     assert_eq!(s.drain(2..4).collect::<String>(), "β");
450     assert_eq!(s, "αγ");
451
452     let mut t = String::from("abcd");
453     t.drain(..0);
454     assert_eq!(t, "abcd");
455     t.drain(..1);
456     assert_eq!(t, "bcd");
457     t.drain(3..);
458     assert_eq!(t, "bcd");
459     t.drain(..);
460     assert_eq!(t, "");
461 }
462
463 #[test]
464 fn test_replace_range() {
465     let mut s = "Hello, world!".to_owned();
466     s.replace_range(7..12, "世界");
467     assert_eq!(s, "Hello, 世界!");
468 }
469
470 #[test]
471 #[should_panic]
472 fn test_replace_range_char_boundary() {
473     let mut s = "Hello, 世界!".to_owned();
474     s.replace_range(..8, "");
475 }
476
477 #[test]
478 fn test_replace_range_inclusive_range() {
479     let mut v = String::from("12345");
480     v.replace_range(2..=3, "789");
481     assert_eq!(v, "127895");
482     v.replace_range(1..=2, "A");
483     assert_eq!(v, "1A895");
484 }
485
486 #[test]
487 #[should_panic]
488 fn test_replace_range_out_of_bounds() {
489     let mut s = String::from("12345");
490     s.replace_range(5..6, "789");
491 }
492
493 #[test]
494 #[should_panic]
495 fn test_replace_range_inclusive_out_of_bounds() {
496     let mut s = String::from("12345");
497     s.replace_range(5..=5, "789");
498 }
499
500 #[test]
501 fn test_replace_range_empty() {
502     let mut s = String::from("12345");
503     s.replace_range(1..2, "");
504     assert_eq!(s, "1345");
505 }
506
507 #[test]
508 fn test_replace_range_unbounded() {
509     let mut s = String::from("12345");
510     s.replace_range(.., "");
511     assert_eq!(s, "");
512 }
513
514 #[test]
515 fn test_extend_ref() {
516     let mut a = "foo".to_string();
517     a.extend(&['b', 'a', 'r']);
518
519     assert_eq!(&a, "foobar");
520 }
521
522 #[test]
523 fn test_into_boxed_str() {
524     let xs = String::from("hello my name is bob");
525     let ys = xs.into_boxed_str();
526     assert_eq!(&*ys, "hello my name is bob");
527 }
528
529 #[test]
530 fn test_reserve_exact() {
531     // This is all the same as test_reserve
532
533     let mut s = String::new();
534     assert_eq!(s.capacity(), 0);
535
536     s.reserve_exact(2);
537     assert!(s.capacity() >= 2);
538
539     for _i in 0..16 {
540         s.push('0');
541     }
542
543     assert!(s.capacity() >= 16);
544     s.reserve_exact(16);
545     assert!(s.capacity() >= 32);
546
547     s.push('0');
548
549     s.reserve_exact(16);
550     assert!(s.capacity() >= 33)
551 }
552
553 #[test]
554 #[cfg_attr(miri, ignore)] // Miri does not support signalling OOM
555 fn test_try_reserve() {
556     // These are the interesting cases:
557     // * exactly isize::MAX should never trigger a CapacityOverflow (can be OOM)
558     // * > isize::MAX should always fail
559     //    * On 16/32-bit should CapacityOverflow
560     //    * On 64-bit should OOM
561     // * overflow may trigger when adding `len` to `cap` (in number of elements)
562     // * overflow may trigger when multiplying `new_cap` by size_of::<T> (to get bytes)
563
564     const MAX_CAP: usize = isize::MAX as usize;
565     const MAX_USIZE: usize = usize::MAX;
566
567     // On 16/32-bit, we check that allocations don't exceed isize::MAX,
568     // on 64-bit, we assume the OS will give an OOM for such a ridiculous size.
569     // Any platform that succeeds for these requests is technically broken with
570     // ptr::offset because LLVM is the worst.
571     let guards_against_isize = size_of::<usize>() < 8;
572
573     {
574         // Note: basic stuff is checked by test_reserve
575         let mut empty_string: String = String::new();
576
577         // Check isize::MAX doesn't count as an overflow
578         if let Err(CapacityOverflow) = empty_string.try_reserve(MAX_CAP) {
579             panic!("isize::MAX shouldn't trigger an overflow!");
580         }
581         // Play it again, frank! (just to be sure)
582         if let Err(CapacityOverflow) = empty_string.try_reserve(MAX_CAP) {
583             panic!("isize::MAX shouldn't trigger an overflow!");
584         }
585
586         if guards_against_isize {
587             // Check isize::MAX + 1 does count as overflow
588             if let Err(CapacityOverflow) = empty_string.try_reserve(MAX_CAP + 1) {
589             } else {
590                 panic!("isize::MAX + 1 should trigger an overflow!")
591             }
592
593             // Check usize::MAX does count as overflow
594             if let Err(CapacityOverflow) = empty_string.try_reserve(MAX_USIZE) {
595             } else {
596                 panic!("usize::MAX should trigger an overflow!")
597             }
598         } else {
599             // Check isize::MAX + 1 is an OOM
600             if let Err(AllocError { .. }) = empty_string.try_reserve(MAX_CAP + 1) {
601             } else {
602                 panic!("isize::MAX + 1 should trigger an OOM!")
603             }
604
605             // Check usize::MAX is an OOM
606             if let Err(AllocError { .. }) = empty_string.try_reserve(MAX_USIZE) {
607             } else {
608                 panic!("usize::MAX should trigger an OOM!")
609             }
610         }
611     }
612
613     {
614         // Same basic idea, but with non-zero len
615         let mut ten_bytes: String = String::from("0123456789");
616
617         if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10) {
618             panic!("isize::MAX shouldn't trigger an overflow!");
619         }
620         if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10) {
621             panic!("isize::MAX shouldn't trigger an overflow!");
622         }
623         if guards_against_isize {
624             if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9) {
625             } else {
626                 panic!("isize::MAX + 1 should trigger an overflow!");
627             }
628         } else {
629             if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9) {
630             } else {
631                 panic!("isize::MAX + 1 should trigger an OOM!")
632             }
633         }
634         // Should always overflow in the add-to-len
635         if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_USIZE) {
636         } else {
637             panic!("usize::MAX should trigger an overflow!")
638         }
639     }
640 }
641
642 #[test]
643 #[cfg_attr(miri, ignore)] // Miri does not support signalling OOM
644 fn test_try_reserve_exact() {
645     // This is exactly the same as test_try_reserve with the method changed.
646     // See that test for comments.
647
648     const MAX_CAP: usize = isize::MAX as usize;
649     const MAX_USIZE: usize = usize::MAX;
650
651     let guards_against_isize = size_of::<usize>() < 8;
652
653     {
654         let mut empty_string: String = String::new();
655
656         if let Err(CapacityOverflow) = empty_string.try_reserve_exact(MAX_CAP) {
657             panic!("isize::MAX shouldn't trigger an overflow!");
658         }
659         if let Err(CapacityOverflow) = empty_string.try_reserve_exact(MAX_CAP) {
660             panic!("isize::MAX shouldn't trigger an overflow!");
661         }
662
663         if guards_against_isize {
664             if let Err(CapacityOverflow) = empty_string.try_reserve_exact(MAX_CAP + 1) {
665             } else {
666                 panic!("isize::MAX + 1 should trigger an overflow!")
667             }
668
669             if let Err(CapacityOverflow) = empty_string.try_reserve_exact(MAX_USIZE) {
670             } else {
671                 panic!("usize::MAX should trigger an overflow!")
672             }
673         } else {
674             if let Err(AllocError { .. }) = empty_string.try_reserve_exact(MAX_CAP + 1) {
675             } else {
676                 panic!("isize::MAX + 1 should trigger an OOM!")
677             }
678
679             if let Err(AllocError { .. }) = empty_string.try_reserve_exact(MAX_USIZE) {
680             } else {
681                 panic!("usize::MAX should trigger an OOM!")
682             }
683         }
684     }
685
686     {
687         let mut ten_bytes: String = String::from("0123456789");
688
689         if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 10) {
690             panic!("isize::MAX shouldn't trigger an overflow!");
691         }
692         if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 10) {
693             panic!("isize::MAX shouldn't trigger an overflow!");
694         }
695         if guards_against_isize {
696             if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 9) {
697             } else {
698                 panic!("isize::MAX + 1 should trigger an overflow!");
699             }
700         } else {
701             if let Err(AllocError { .. }) = ten_bytes.try_reserve_exact(MAX_CAP - 9) {
702             } else {
703                 panic!("isize::MAX + 1 should trigger an OOM!")
704             }
705         }
706         if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE) {
707         } else {
708             panic!("usize::MAX should trigger an overflow!")
709         }
710     }
711 }