about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorKeegan McAllister <kmcallister@mozilla.com>2014-04-11 13:44:54 -0700
committerKeegan McAllister <kmcallister@mozilla.com>2014-04-11 15:20:18 -0700
commit58fc85db93bf6c73c4da957db8c6b8b025826e93 (patch)
tree3d4dd65f6ac8f09abd3809d08c81f4ea75a42b30 /src/libstd
parente011939b1af554d2a29947feb66f01e27a2a1524 (diff)
downloadrust-58fc85db93bf6c73c4da957db8c6b8b025826e93.tar.gz
rust-58fc85db93bf6c73c4da957db8c6b8b025826e93.zip
Add tests for Char::encode_utf{8,16}
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/char.rs29
1 files changed, 29 insertions, 0 deletions
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]);
+}