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