]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/utf8_chars.rs
0e85c67edb7846a086ac2f21b41c4e4daa1afda2
[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 use std::str;
12
13 pub fn main() {
14     // Chars of 1, 2, 3, and 4 bytes
15     let chs: ~[char] = ~['e', 'é', '€', '\U00010000'];
16     let s: ~str = str::from_chars(chs);
17     let schs: ~[char] = s.chars().collect();
18
19     assert!(s.len() == 10u);
20     assert!(s.char_len() == 4u);
21     assert!(schs.len() == 4u);
22     assert!(str::from_chars(schs) == s);
23     assert!(s.char_at(0u) == 'e');
24     assert!(s.char_at(1u) == 'é');
25
26     assert!((str::is_utf8(s.as_bytes())));
27     // invalid prefix
28     assert!((!str::is_utf8([0x80_u8])));
29     // invalid 2 byte prefix
30     assert!((!str::is_utf8([0xc0_u8])));
31     assert!((!str::is_utf8([0xc0_u8, 0x10_u8])));
32     // invalid 3 byte prefix
33     assert!((!str::is_utf8([0xe0_u8])));
34     assert!((!str::is_utf8([0xe0_u8, 0x10_u8])));
35     assert!((!str::is_utf8([0xe0_u8, 0xff_u8, 0x10_u8])));
36     // invalid 4 byte prefix
37     assert!((!str::is_utf8([0xf0_u8])));
38     assert!((!str::is_utf8([0xf0_u8, 0x10_u8])));
39     assert!((!str::is_utf8([0xf0_u8, 0xff_u8, 0x10_u8])));
40     assert!((!str::is_utf8([0xf0_u8, 0xff_u8, 0xff_u8, 0x10_u8])));
41
42     let mut stack = ~"a×c€";
43     assert_eq!(stack.pop_char(), Some('€'));
44     assert_eq!(stack.pop_char(), Some('c'));
45     stack.push_char('u');
46     assert!(stack == ~"a×u");
47     assert_eq!(stack.shift_char(), Some('a'));
48     assert_eq!(stack.shift_char(), Some('×'));
49     stack.unshift_char('ß');
50     assert!(stack == ~"ßu");
51 }