OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [unicode/] [utf16/] [utf16_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
5
package utf16_test
6
 
7
import (
8
        "reflect"
9
        "testing"
10
        "unicode"
11
        . "unicode/utf16"
12
)
13
 
14
type encodeTest struct {
15
        in  []rune
16
        out []uint16
17
}
18
 
19
var encodeTests = []encodeTest{
20
        {[]rune{1, 2, 3, 4}, []uint16{1, 2, 3, 4}},
21
        {[]rune{0xffff, 0x10000, 0x10001, 0x12345, 0x10ffff},
22
                []uint16{0xffff, 0xd800, 0xdc00, 0xd800, 0xdc01, 0xd808, 0xdf45, 0xdbff, 0xdfff}},
23
        {[]rune{'a', 'b', 0xd7ff, 0xd800, 0xdfff, 0xe000, 0x110000, -1},
24
                []uint16{'a', 'b', 0xd7ff, 0xfffd, 0xfffd, 0xe000, 0xfffd, 0xfffd}},
25
}
26
 
27
func TestEncode(t *testing.T) {
28
        for _, tt := range encodeTests {
29
                out := Encode(tt.in)
30
                if !reflect.DeepEqual(out, tt.out) {
31
                        t.Errorf("Encode(%x) = %x; want %x", tt.in, out, tt.out)
32
                }
33
        }
34
}
35
 
36
func TestEncodeRune(t *testing.T) {
37
        for i, tt := range encodeTests {
38
                j := 0
39
                for _, r := range tt.in {
40
                        r1, r2 := EncodeRune(r)
41
                        if r < 0x10000 || r > unicode.MaxRune {
42
                                if j >= len(tt.out) {
43
                                        t.Errorf("#%d: ran out of tt.out", i)
44
                                        break
45
                                }
46
                                if r1 != unicode.ReplacementChar || r2 != unicode.ReplacementChar {
47
                                        t.Errorf("EncodeRune(%#x) = %#x, %#x; want 0xfffd, 0xfffd", r, r1, r2)
48
                                }
49
                                j++
50
                        } else {
51
                                if j+1 >= len(tt.out) {
52
                                        t.Errorf("#%d: ran out of tt.out", i)
53
                                        break
54
                                }
55
                                if r1 != rune(tt.out[j]) || r2 != rune(tt.out[j+1]) {
56
                                        t.Errorf("EncodeRune(%#x) = %#x, %#x; want %#x, %#x", r, r1, r2, tt.out[j], tt.out[j+1])
57
                                }
58
                                j += 2
59
                                dec := DecodeRune(r1, r2)
60
                                if dec != r {
61
                                        t.Errorf("DecodeRune(%#x, %#x) = %#x; want %#x", r1, r2, dec, r)
62
                                }
63
                        }
64
                }
65
                if j != len(tt.out) {
66
                        t.Errorf("#%d: EncodeRune didn't generate enough output", i)
67
                }
68
        }
69
}
70
 
71
type decodeTest struct {
72
        in  []uint16
73
        out []rune
74
}
75
 
76
var decodeTests = []decodeTest{
77
        {[]uint16{1, 2, 3, 4}, []rune{1, 2, 3, 4}},
78
        {[]uint16{0xffff, 0xd800, 0xdc00, 0xd800, 0xdc01, 0xd808, 0xdf45, 0xdbff, 0xdfff},
79
                []rune{0xffff, 0x10000, 0x10001, 0x12345, 0x10ffff}},
80
        {[]uint16{0xd800, 'a'}, []rune{0xfffd, 'a'}},
81
        {[]uint16{0xdfff}, []rune{0xfffd}},
82
}
83
 
84
func TestDecode(t *testing.T) {
85
        for _, tt := range decodeTests {
86
                out := Decode(tt.in)
87
                if !reflect.DeepEqual(out, tt.out) {
88
                        t.Errorf("Decode(%x) = %x; want %x", tt.in, out, tt.out)
89
                }
90
        }
91
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.