]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/utf8_chars.rs
2602e395bcc324e6459aedbbc81662d6c589060a
[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(core)]
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
27     assert!((str::from_utf8(s.as_bytes()).is_ok()));
28     // invalid prefix
29     assert!((!str::from_utf8(&[0x80]).is_ok()));
30     // invalid 2 byte prefix
31     assert!((!str::from_utf8(&[0xc0]).is_ok()));
32     assert!((!str::from_utf8(&[0xc0, 0x10]).is_ok()));
33     // invalid 3 byte prefix
34     assert!((!str::from_utf8(&[0xe0]).is_ok()));
35     assert!((!str::from_utf8(&[0xe0, 0x10]).is_ok()));
36     assert!((!str::from_utf8(&[0xe0, 0xff, 0x10]).is_ok()));
37     // invalid 4 byte prefix
38     assert!((!str::from_utf8(&[0xf0]).is_ok()));
39     assert!((!str::from_utf8(&[0xf0, 0x10]).is_ok()));
40     assert!((!str::from_utf8(&[0xf0, 0xff, 0x10]).is_ok()));
41     assert!((!str::from_utf8(&[0xf0, 0xff, 0xff, 0x10]).is_ok()));
42 }