]> git.lizzy.rs Git - rust.git/blob - library/core/tests/str_lossy.rs
Rollup merge of #106823 - m-ou-se:format-args-as-str-guarantees, r=dtolnay
[rust.git] / library / core / tests / str_lossy.rs
1 use core::str::Utf8Chunks;
2
3 #[test]
4 fn chunks() {
5     macro_rules! assert_chunks {
6         ( $string:expr, $(($valid:expr, $invalid:expr)),* $(,)? ) => {{
7             let mut iter = Utf8Chunks::new($string);
8             $(
9                 let chunk = iter.next().expect("missing chunk");
10                 assert_eq!($valid, chunk.valid());
11                 assert_eq!($invalid, chunk.invalid());
12             )*
13             assert_eq!(None, iter.next());
14         }};
15     }
16
17     assert_chunks!(b"hello", ("hello", b""));
18     assert_chunks!("ศไทย中华Việt Nam".as_bytes(), ("ศไทย中华Việt Nam", b""));
19     assert_chunks!(
20         b"Hello\xC2 There\xFF Goodbye",
21         ("Hello", b"\xC2"),
22         (" There", b"\xFF"),
23         (" Goodbye", b""),
24     );
25     assert_chunks!(
26         b"Hello\xC0\x80 There\xE6\x83 Goodbye",
27         ("Hello", b"\xC0"),
28         ("", b"\x80"),
29         (" There", b"\xE6\x83"),
30         (" Goodbye", b""),
31     );
32     assert_chunks!(
33         b"\xF5foo\xF5\x80bar",
34         ("", b"\xF5"),
35         ("foo", b"\xF5"),
36         ("", b"\x80"),
37         ("bar", b""),
38     );
39     assert_chunks!(
40         b"\xF1foo\xF1\x80bar\xF1\x80\x80baz",
41         ("", b"\xF1"),
42         ("foo", b"\xF1\x80"),
43         ("bar", b"\xF1\x80\x80"),
44         ("baz", b""),
45     );
46     assert_chunks!(
47         b"\xF4foo\xF4\x80bar\xF4\xBFbaz",
48         ("", b"\xF4"),
49         ("foo", b"\xF4\x80"),
50         ("bar", b"\xF4"),
51         ("", b"\xBF"),
52         ("baz", b""),
53     );
54     assert_chunks!(
55         b"\xF0\x80\x80\x80foo\xF0\x90\x80\x80bar",
56         ("", b"\xF0"),
57         ("", b"\x80"),
58         ("", b"\x80"),
59         ("", b"\x80"),
60         ("foo\u{10000}bar", b""),
61     );
62
63     // surrogates
64     assert_chunks!(
65         b"\xED\xA0\x80foo\xED\xBF\xBFbar",
66         ("", b"\xED"),
67         ("", b"\xA0"),
68         ("", b"\x80"),
69         ("foo", b"\xED"),
70         ("", b"\xBF"),
71         ("", b"\xBF"),
72         ("bar", b""),
73     );
74 }
75
76 #[test]
77 fn debug() {
78     assert_eq!(
79         "\"Hello\\xC0\\x80 There\\xE6\\x83 Goodbye\\u{10d4ea}\"",
80         &format!(
81             "{:?}",
82             Utf8Chunks::new(b"Hello\xC0\x80 There\xE6\x83 Goodbye\xf4\x8d\x93\xaa").debug(),
83         ),
84     );
85 }