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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [exp/] [norm/] [trie_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 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 norm
6
 
7
import (
8
        "testing"
9
        "unicode/utf8"
10
)
11
 
12
// Test data is located in triedata_test.go; generated by maketesttables.
13
var testdata = testdataTrie
14
 
15
type rangeTest struct {
16
        block   uint8
17
        lookup  byte
18
        result  uint16
19
        table   []valueRange
20
        offsets []uint16
21
}
22
 
23
var range1Off = []uint16{0, 2}
24
var range1 = []valueRange{
25
        {0, 1, 0},
26
        {1, 0x80, 0x80},
27
        {0, 2, 0},
28
        {1, 0x80, 0x80},
29
        {9, 0xff, 0xff},
30
}
31
 
32
var rangeTests = []rangeTest{
33
        {10, 0x80, 1, range1, range1Off},
34
        {10, 0x00, 0, range1, range1Off},
35
        {11, 0x80, 1, range1, range1Off},
36
        {11, 0xff, 9, range1, range1Off},
37
        {11, 0x00, 0, range1, range1Off},
38
}
39
 
40
func TestLookupSparse(t *testing.T) {
41
        for i, test := range rangeTests {
42
                n := trie{sparse: test.table, sparseOffset: test.offsets, cutoff: 10}
43
                v := n.lookupValue(test.block, test.lookup)
44
                if v != test.result {
45
                        t.Errorf("LookupSparse:%d: found %X; want %X", i, v, test.result)
46
                }
47
        }
48
}
49
 
50
// Test cases for illegal runes.
51
type trietest struct {
52
        size  int
53
        bytes []byte
54
}
55
 
56
var tests = []trietest{
57
        // illegal runes
58
        {1, []byte{0x80}},
59
        {1, []byte{0xFF}},
60
        {1, []byte{t2, tx - 1}},
61
        {1, []byte{t2, t2}},
62
        {2, []byte{t3, tx, tx - 1}},
63
        {2, []byte{t3, tx, t2}},
64
        {1, []byte{t3, tx - 1, tx}},
65
        {3, []byte{t4, tx, tx, tx - 1}},
66
        {3, []byte{t4, tx, tx, t2}},
67
        {1, []byte{t4, t2, tx, tx - 1}},
68
        {2, []byte{t4, tx, t2, tx - 1}},
69
 
70
        // short runes
71
        {0, []byte{t2}},
72
        {0, []byte{t3, tx}},
73
        {0, []byte{t4, tx, tx}},
74
 
75
        // we only support UTF-8 up to utf8.UTFMax bytes (4 bytes)
76
        {1, []byte{t5, tx, tx, tx, tx}},
77
        {1, []byte{t6, tx, tx, tx, tx, tx}},
78
}
79
 
80
func mkUTF8(r rune) ([]byte, int) {
81
        var b [utf8.UTFMax]byte
82
        sz := utf8.EncodeRune(b[:], r)
83
        return b[:sz], sz
84
}
85
 
86
func TestLookup(t *testing.T) {
87
        for i, tt := range testRunes {
88
                b, szg := mkUTF8(tt)
89
                v, szt := testdata.lookup(b)
90
                if int(v) != i {
91
                        t.Errorf("lookup(%U): found value %#x, expected %#x", tt, v, i)
92
                }
93
                if szt != szg {
94
                        t.Errorf("lookup(%U): found size %d, expected %d", tt, szt, szg)
95
                }
96
        }
97
        for i, tt := range tests {
98
                v, sz := testdata.lookup(tt.bytes)
99
                if int(v) != 0 {
100
                        t.Errorf("lookup of illegal rune, case %d: found value %#x, expected 0", i, v)
101
                }
102
                if sz != tt.size {
103
                        t.Errorf("lookup of illegal rune, case %d: found size %d, expected %d", i, sz, tt.size)
104
                }
105
        }
106
}
107
 
108
func TestLookupUnsafe(t *testing.T) {
109
        for i, tt := range testRunes {
110
                b, _ := mkUTF8(tt)
111
                v := testdata.lookupUnsafe(b)
112
                if int(v) != i {
113
                        t.Errorf("lookupUnsafe(%U): found value %#x, expected %#x", i, v, i)
114
                }
115
        }
116
}
117
 
118
func TestLookupString(t *testing.T) {
119
        for i, tt := range testRunes {
120
                b, szg := mkUTF8(tt)
121
                v, szt := testdata.lookupString(string(b))
122
                if int(v) != i {
123
                        t.Errorf("lookup(%U): found value %#x, expected %#x", i, v, i)
124
                }
125
                if szt != szg {
126
                        t.Errorf("lookup(%U): found size %d, expected %d", i, szt, szg)
127
                }
128
        }
129
        for i, tt := range tests {
130
                v, sz := testdata.lookupString(string(tt.bytes))
131
                if int(v) != 0 {
132
                        t.Errorf("lookup of illegal rune, case %d: found value %#x, expected 0", i, v)
133
                }
134
                if sz != tt.size {
135
                        t.Errorf("lookup of illegal rune, case %d: found size %d, expected %d", i, sz, tt.size)
136
                }
137
        }
138
}
139
 
140
func TestLookupStringUnsafe(t *testing.T) {
141
        for i, tt := range testRunes {
142
                b, _ := mkUTF8(tt)
143
                v := testdata.lookupStringUnsafe(string(b))
144
                if int(v) != i {
145
                        t.Errorf("lookupUnsafe(%U): found value %#x, expected %#x", i, v, i)
146
                }
147
        }
148
}

powered by: WebSVN 2.1.0

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