rust/library/core/tests/str_lossy.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 lines
2.1 KiB
Rust
Raw Normal View History

#[test]
fn chunks() {
2022-08-20 11:49:20 -05:00
macro_rules! assert_chunks {
( $string:expr, $(($valid:expr, $invalid:expr)),* $(,)? ) => {{
2024-04-13 21:34:38 -05:00
let mut iter = $string.utf8_chunks();
2022-08-20 11:49:20 -05:00
$(
let chunk = iter.next().expect("missing chunk");
assert_eq!($valid, chunk.valid());
assert_eq!($invalid, chunk.invalid());
)*
assert_eq!(None, iter.next());
}};
}
2022-08-20 11:49:20 -05:00
assert_chunks!(b"hello", ("hello", b""));
assert_chunks!("ศไทย中华Việt Nam".as_bytes(), ("ศไทย中华Việt Nam", b""));
assert_chunks!(
b"Hello\xC2 There\xFF Goodbye",
("Hello", b"\xC2"),
(" There", b"\xFF"),
(" Goodbye", b""),
);
assert_chunks!(
b"Hello\xC0\x80 There\xE6\x83 Goodbye",
("Hello", b"\xC0"),
("", b"\x80"),
(" There", b"\xE6\x83"),
(" Goodbye", b""),
);
assert_chunks!(
b"\xF5foo\xF5\x80bar",
("", b"\xF5"),
("foo", b"\xF5"),
("", b"\x80"),
("bar", b""),
);
assert_chunks!(
b"\xF1foo\xF1\x80bar\xF1\x80\x80baz",
("", b"\xF1"),
("foo", b"\xF1\x80"),
("bar", b"\xF1\x80\x80"),
("baz", b""),
);
assert_chunks!(
b"\xF4foo\xF4\x80bar\xF4\xBFbaz",
("", b"\xF4"),
("foo", b"\xF4\x80"),
("bar", b"\xF4"),
("", b"\xBF"),
("baz", b""),
);
assert_chunks!(
b"\xF0\x80\x80\x80foo\xF0\x90\x80\x80bar",
("", b"\xF0"),
("", b"\x80"),
("", b"\x80"),
("", b"\x80"),
("foo\u{10000}bar", b""),
);
// surrogates
2022-08-20 11:49:20 -05:00
assert_chunks!(
b"\xED\xA0\x80foo\xED\xBF\xBFbar",
("", b"\xED"),
("", b"\xA0"),
("", b"\x80"),
("foo", b"\xED"),
("", b"\xBF"),
("", b"\xBF"),
("bar", b""),
);
}
#[test]
fn debug() {
assert_eq!(
2022-08-20 11:49:20 -05:00
"\"Hello\\xC0\\x80 There\\xE6\\x83 Goodbye\\u{10d4ea}\"",
&format!(
"{:?}",
2024-04-13 21:34:38 -05:00
b"Hello\xC0\x80 There\xE6\x83 Goodbye\xf4\x8d\x93\xaa".utf8_chunks().debug(),
2022-08-20 11:49:20 -05:00
),
);
}