@@ -32,6 +32,7 @@ use unicode::{derived_property, property, general_category, decompose, conversio
32
32
33
33
#[ cfg( test) ] use str:: Str ;
34
34
#[ cfg( test) ] use strbuf:: StrBuf ;
35
+ #[ cfg( test) ] use slice:: ImmutableVector ;
35
36
36
37
#[ cfg( not( test) ) ] use cmp:: { Eq , Ord } ;
37
38
#[ cfg( not( test) ) ] use default:: Default ;
@@ -814,3 +815,31 @@ fn test_to_str() {
814
815
let s = 't'.to_str();
815
816
assert_eq!(s, ~" t");
816
817
}
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('\U 0001f4a9', [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('\U 0001 f4a9' , [ 0xd83d , 0xdca9 ] ) ;
845
+ }
0 commit comments