Add tests for Char::encode_utf{8,16}

This commit is contained in:
Keegan McAllister 2014-04-11 13:44:54 -07:00
parent e011939b1a
commit 58fc85db93

View File

@ -32,6 +32,7 @@ use unicode::{derived_property, property, general_category, decompose, conversio
#[cfg(test)] use str::Str;
#[cfg(test)] use strbuf::StrBuf;
#[cfg(test)] use slice::ImmutableVector;
#[cfg(not(test))] use cmp::{Eq, Ord};
#[cfg(not(test))] use default::Default;
@ -814,3 +815,31 @@ fn test_to_str() {
let s = 't'.to_str();
assert_eq!(s, ~"t");
}
#[test]
fn test_encode_utf8() {
fn check(input: char, expect: &[u8]) {
let mut buf = [0u8, ..4];
let n = input.encode_utf8(buf /* as mut slice! */);
assert_eq!(buf.slice_to(n), expect);
}
check('x', [0x78]);
check('\u00e9', [0xc3, 0xa9]);
check('\ua66e', [0xea, 0x99, 0xae]);
check('\U0001f4a9', [0xf0, 0x9f, 0x92, 0xa9]);
}
#[test]
fn test_encode_utf16() {
fn check(input: char, expect: &[u16]) {
let mut buf = [0u16, ..2];
let n = input.encode_utf16(buf /* as mut slice! */);
assert_eq!(buf.slice_to(n), expect);
}
check('x', [0x0078]);
check('\u00e9', [0x00e9]);
check('\ua66e', [0xa66e]);
check('\U0001f4a9', [0xd83d, 0xdca9]);
}