Skip to content

Commit

Permalink
Add tests for Char::encode_utf{8,16}
Browse files Browse the repository at this point in the history
  • Loading branch information
Keegan McAllister committed Apr 11, 2014
1 parent e011939 commit 58fc85d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/libstd/char.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}

0 comments on commit 58fc85d

Please sign in to comment.