Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / utf16 / utf16_test.go
blob2b9fb3d87dca345330d3903ccdfb7472608fc418
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.
5 package utf16_test
7 import (
8 "fmt"
9 "reflect"
10 "testing"
11 "unicode"
12 . "utf16"
15 type encodeTest struct {
16 in []int
17 out []uint16
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 {
30 out := Encode(tt.in)
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 {
39 j := 0
40 for _, r := range tt.in {
41 r1, r2 := EncodeRune(r)
42 if r < 0x10000 || r > unicode.MaxRune {
43 if j >= len(tt.out) {
44 t.Errorf("#%d: ran out of tt.out", i)
45 break
47 if r1 != unicode.ReplacementChar || r2 != unicode.ReplacementChar {
48 t.Errorf("EncodeRune(%#x) = %#x, %#x; want 0xfffd, 0xfffd", r, r1, r2)
50 j++
51 } else {
52 if j+1 >= len(tt.out) {
53 t.Errorf("#%d: ran out of tt.out", i)
54 break
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])
59 j += 2
60 dec := DecodeRune(r1, r2)
61 if dec != r {
62 t.Errorf("DecodeRune(%#x, %#x) = %#x; want %#x", r1, r2, dec, r)
66 if j != len(tt.out) {
67 t.Errorf("#%d: EncodeRune didn't generate enough output", i)
72 type decodeTest struct {
73 in []uint16
74 out []int
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 {
87 out := Decode(tt.in)
88 if !reflect.DeepEqual(out, tt.out) {
89 t.Errorf("Decode(%v) = %v; want %v", hex16(tt.in), hex(out), hex(tt.out))
94 type hex []int
96 func (h hex) Format(f fmt.State, c int) {
97 fmt.Fprint(f, "[")
98 for i, v := range h {
99 if i > 0 {
100 fmt.Fprint(f, " ")
102 fmt.Fprintf(f, "%x", v)
104 fmt.Fprint(f, "]")
107 type hex16 []uint16
109 func (h hex16) Format(f fmt.State, c int) {
110 fmt.Fprint(f, "[")
111 for i, v := range h {
112 if i > 0 {
113 fmt.Fprint(f, " ")
115 fmt.Fprintf(f, "%x", v)
117 fmt.Fprint(f, "]")