]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/utf8_chars.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[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
12 #![feature(collections, core, str_char)]
13
14 use std::str;
15
16 pub fn main() {
17     // Chars of 1, 2, 3, and 4 bytes
18     let chs: Vec<char> = vec!('e', 'é', '€', '\u{10000}');
19     let s: String = chs.iter().cloned().collect();
20     let schs: Vec<char> = s.chars().collect();
21
22     assert_eq!(s.len(), 10);
23     assert_eq!(s.chars().count(), 4);
24     assert_eq!(schs.len(), 4);
25     assert_eq!(schs.iter().cloned().collect::<String>(), s);
26     assert_eq!(s.char_at(0), 'e');
27     assert_eq!(s.char_at(1), 'é');
28
29     assert!((str::from_utf8(s.as_bytes()).is_ok()));
30     // invalid prefix
31     assert!((!str::from_utf8(&[0x80]).is_ok()));
32     // invalid 2 byte prefix
33     assert!((!str::from_utf8(&[0xc0]).is_ok()));
34     assert!((!str::from_utf8(&[0xc0, 0x10]).is_ok()));
35     // invalid 3 byte prefix
36     assert!((!str::from_utf8(&[0xe0]).is_ok()));
37     assert!((!str::from_utf8(&[0xe0, 0x10]).is_ok()));
38     assert!((!str::from_utf8(&[0xe0, 0xff, 0x10]).is_ok()));
39     // invalid 4 byte prefix
40     assert!((!str::from_utf8(&[0xf0]).is_ok()));
41     assert!((!str::from_utf8(&[0xf0, 0x10]).is_ok()));
42     assert!((!str::from_utf8(&[0xf0, 0xff, 0x10]).is_ok()));
43     assert!((!str::from_utf8(&[0xf0, 0xff, 0xff, 0x10]).is_ok()));
44 }