Skip to content

Commit 58fc85d

Browse files
author
Keegan McAllister
committed
Add tests for Char::encode_utf{8,16}
1 parent e011939 commit 58fc85d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/libstd/char.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use unicode::{derived_property, property, general_category, decompose, conversio
3232

3333
#[cfg(test)] use str::Str;
3434
#[cfg(test)] use strbuf::StrBuf;
35+
#[cfg(test)] use slice::ImmutableVector;
3536

3637
#[cfg(not(test))] use cmp::{Eq, Ord};
3738
#[cfg(not(test))] use default::Default;
@@ -814,3 +815,31 @@ fn test_to_str() {
814815
let s = 't'.to_str();
815816
assert_eq!(s, ~"t");
816817
}
818+
819+
#[test]
820+
fn test_encode_utf8() {
821+
fn check(input: char, expect: &[u8]) {
822+
let mut buf = [0u8, ..4];
823+
let n = input.encode_utf8(buf /* as mut slice! */);
824+
assert_eq!(buf.slice_to(n), expect);
825+
}
826+
827+
check('x', [0x78]);
828+
check('\u00e9', [0xc3, 0xa9]);
829+
check('\ua66e', [0xea, 0x99, 0xae]);
830+
check('\U0001f4a9', [0xf0, 0x9f, 0x92, 0xa9]);
831+
}
832+
833+
#[test]
834+
fn test_encode_utf16() {
835+
fn check(input: char, expect: &[u16]) {
836+
let mut buf = [0u16, ..2];
837+
let n = input.encode_utf16(buf /* as mut slice! */);
838+
assert_eq!(buf.slice_to(n), expect);
839+
}
840+
841+
check('x', [0x0078]);
842+
check('\u00e9', [0x00e9]);
843+
check('\ua66e', [0xa66e]);
844+
check('\U0001f4a9', [0xd83d, 0xdca9]);
845+
}

0 commit comments

Comments
 (0)