1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 type encodeTest
struct {
20 var encodeTests
= []encodeTest
{
21 {[]int{1, 2, 3, 4}, []uint16{1, 2, 3, 4}},
22 {[]int{0xffff, 0x10000, 0x10001, 0x12345, 0x10ffff},
23 []uint16{0xffff, 0xd800, 0xdc00, 0xd800, 0xdc01, 0xd808, 0xdf45, 0xdbff, 0xdfff}},
24 {[]int{'a', 'b', 0xd7ff, 0xd800, 0xdfff, 0xe000, 0x110000, -1},
25 []uint16{'a', 'b', 0xd7ff, 0xfffd, 0xfffd, 0xe000, 0xfffd, 0xfffd}},
28 func TestEncode(t
*testing
.T
) {
29 for _
, tt
:= range encodeTests
{
31 if !reflect
.DeepEqual(out
, tt
.out
) {
32 t
.Errorf("Encode(%v) = %v; want %v", hex(tt
.in
), hex16(out
), hex16(tt
.out
))
37 func TestEncodeRune(t
*testing
.T
) {
38 for i
, tt
:= range encodeTests
{
40 for _
, r
:= range tt
.in
{
41 r1
, r2
:= EncodeRune(r
)
42 if r
< 0x10000 || r
> unicode
.MaxRune
{
44 t
.Errorf("#%d: ran out of tt.out", i
)
47 if r1
!= unicode
.ReplacementChar || r2
!= unicode
.ReplacementChar
{
48 t
.Errorf("EncodeRune(%#x) = %#x, %#x; want 0xfffd, 0xfffd", r
, r1
, r2
)
52 if j
+1 >= len(tt
.out
) {
53 t
.Errorf("#%d: ran out of tt.out", i
)
56 if r1
!= int(tt
.out
[j
]) || r2
!= int(tt
.out
[j
+1]) {
57 t
.Errorf("EncodeRune(%#x) = %#x, %#x; want %#x, %#x", r
, r1
, r2
, tt
.out
[j
], tt
.out
[j
+1])
60 dec
:= DecodeRune(r1
, r2
)
62 t
.Errorf("DecodeRune(%#x, %#x) = %#x; want %#x", r1
, r2
, dec
, r
)
67 t
.Errorf("#%d: EncodeRune didn't generate enough output", i
)
72 type decodeTest
struct {
77 var decodeTests
= []decodeTest
{
78 {[]uint16{1, 2, 3, 4}, []int{1, 2, 3, 4}},
79 {[]uint16{0xffff, 0xd800, 0xdc00, 0xd800, 0xdc01, 0xd808, 0xdf45, 0xdbff, 0xdfff},
80 []int{0xffff, 0x10000, 0x10001, 0x12345, 0x10ffff}},
81 {[]uint16{0xd800, 'a'}, []int{0xfffd, 'a'}},
82 {[]uint16{0xdfff}, []int{0xfffd}},
85 func TestDecode(t
*testing
.T
) {
86 for _
, tt
:= range decodeTests
{
88 if !reflect
.DeepEqual(out
, tt
.out
) {
89 t
.Errorf("Decode(%v) = %v; want %v", hex16(tt
.in
), hex(out
), hex(tt
.out
))
96 func (h hex
) Format(f fmt
.State
, c
int) {
102 fmt
.Fprintf(f
, "%x", v
)
109 func (h hex16
) Format(f fmt
.State
, c
int) {
111 for i
, v
:= range h
{
115 fmt
.Fprintf(f
, "%x", v
)