rust/src/test/run-pass/utf8_chars.rs

32 lines
1007 B
Rust
Raw Normal View History

use std;
2011-08-31 17:23:34 -05:00
import std::istr;
2011-08-15 18:38:23 -05:00
import std::vec;
fn main() {
2011-07-27 07:19:39 -05:00
// Chars of 1, 2, 3, and 4 bytes
let chs: [char] = ['e', 'é', '€', 0x10000 as char];
2011-08-31 17:23:34 -05:00
let s: istr = istr::from_chars(chs);
2011-08-31 17:23:34 -05:00
assert (istr::byte_len(s) == 10u);
assert (istr::char_len(s) == 4u);
assert (vec::len::<char>(istr::to_chars(s)) == 4u);
assert (istr::eq(istr::from_chars(istr::to_chars(s)), s));
assert (istr::char_at(s, 0u) == 'e');
assert (istr::char_at(s, 1u) == 'é');
2011-08-31 17:23:34 -05:00
assert (istr::is_utf8(istr::bytes(s)));
assert (!istr::is_utf8([0x80_u8]));
assert (!istr::is_utf8([0xc0_u8]));
assert (!istr::is_utf8([0xc0_u8, 0x10_u8]));
2011-08-31 17:23:34 -05:00
let stack = ~"a×c€";
assert (istr::pop_char(stack) == '€');
assert (istr::pop_char(stack) == 'c');
istr::push_char(stack, 'u');
assert (istr::eq(stack, ~"a×u"));
assert (istr::shift_char(stack) == 'a');
assert (istr::shift_char(stack) == '×');
istr::unshift_char(stack, 'ß');
assert (istr::eq(stack, ~"ßu"));
}