]> git.lizzy.rs Git - rust.git/blob - src/test/ui/utf8_chars.rs
Merge commit '3e7c6dec244539970b593824334876f8b6ed0b18' into clippyup
[rust.git] / src / test / ui / utf8_chars.rs
1 // run-pass
2
3 use std::str;
4
5 pub fn main() {
6     // Chars of 1, 2, 3, and 4 bytes
7     let chs: Vec<char> = vec!['e', 'é', '€', '\u{10000}'];
8     let s: String = chs.iter().cloned().collect();
9     let schs: Vec<char> = s.chars().collect();
10
11     assert_eq!(s.len(), 10);
12     assert_eq!(s.chars().count(), 4);
13     assert_eq!(schs.len(), 4);
14     assert_eq!(schs.iter().cloned().collect::<String>(), s);
15
16     assert!((str::from_utf8(s.as_bytes()).is_ok()));
17     // invalid prefix
18     assert!((!str::from_utf8(&[0x80]).is_ok()));
19     // invalid 2 byte prefix
20     assert!((!str::from_utf8(&[0xc0]).is_ok()));
21     assert!((!str::from_utf8(&[0xc0, 0x10]).is_ok()));
22     // invalid 3 byte prefix
23     assert!((!str::from_utf8(&[0xe0]).is_ok()));
24     assert!((!str::from_utf8(&[0xe0, 0x10]).is_ok()));
25     assert!((!str::from_utf8(&[0xe0, 0xff, 0x10]).is_ok()));
26     // invalid 4 byte prefix
27     assert!((!str::from_utf8(&[0xf0]).is_ok()));
28     assert!((!str::from_utf8(&[0xf0, 0x10]).is_ok()));
29     assert!((!str::from_utf8(&[0xf0, 0xff, 0x10]).is_ok()));
30     assert!((!str::from_utf8(&[0xf0, 0xff, 0xff, 0x10]).is_ok()));
31 }