]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/utf8_chars.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / test / run-pass / utf8_chars.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 //
11 // ignore-lexer-test FIXME #15679
12
13 use std::str;
14
15 pub fn main() {
16     // Chars of 1, 2, 3, and 4 bytes
17     let chs: Vec<char> = vec!('e', 'é', '€', '\U00010000');
18     let s: String = chs.iter().cloned().collect();
19     let schs: Vec<char> = s.chars().collect();
20
21     assert!(s.len() == 10u);
22     assert!(s.chars().count() == 4u);
23     assert!(schs.len() == 4u);
24     assert!(schs.iter().cloned().collect::<String>() == s);
25     assert!(s.char_at(0u) == 'e');
26     assert!(s.char_at(1u) == 'é');
27
28     assert!((str::from_utf8(s.as_bytes()).is_ok()));
29     // invalid prefix
30     assert!((!str::from_utf8(&[0x80_u8]).is_ok()));
31     // invalid 2 byte prefix
32     assert!((!str::from_utf8(&[0xc0_u8]).is_ok()));
33     assert!((!str::from_utf8(&[0xc0_u8, 0x10_u8]).is_ok()));
34     // invalid 3 byte prefix
35     assert!((!str::from_utf8(&[0xe0_u8]).is_ok()));
36     assert!((!str::from_utf8(&[0xe0_u8, 0x10_u8]).is_ok()));
37     assert!((!str::from_utf8(&[0xe0_u8, 0xff_u8, 0x10_u8]).is_ok()));
38     // invalid 4 byte prefix
39     assert!((!str::from_utf8(&[0xf0_u8]).is_ok()));
40     assert!((!str::from_utf8(&[0xf0_u8, 0x10_u8]).is_ok()));
41     assert!((!str::from_utf8(&[0xf0_u8, 0xff_u8, 0x10_u8]).is_ok()));
42     assert!((!str::from_utf8(&[0xf0_u8, 0xff_u8, 0xff_u8, 0x10_u8]).is_ok()));
43 }