From 58fc85db93bf6c73c4da957db8c6b8b025826e93 Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Fri, 11 Apr 2014 13:44:54 -0700 Subject: [PATCH] Add tests for Char::encode_utf{8,16} --- src/libstd/char.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/libstd/char.rs b/src/libstd/char.rs index 7137ffadb09..67c046986d3 100644 --- a/src/libstd/char.rs +++ b/src/libstd/char.rs @@ -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]); +}