| 1 |
// Copyright 2012 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 build |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"sort" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"golang.org/x/text/internal/colltab" |
| 13 |
) |
| 14 |
|
| 15 |
var largetosmall = []stridx{ |
| 16 |
{"a", 5}, |
| 17 |
{"ab", 4}, |
| 18 |
{"abc", 3}, |
| 19 |
{"abcd", 2}, |
| 20 |
{"abcde", 1}, |
| 21 |
{"abcdef", 0}, |
| 22 |
} |
| 23 |
|
| 24 |
var offsetSortTests = [][]stridx{ |
| 25 |
{ |
| 26 |
{"bcde", 1}, |
| 27 |
{"bc", 5}, |
| 28 |
{"ab", 4}, |
| 29 |
{"bcd", 3}, |
| 30 |
{"abcd", 0}, |
| 31 |
{"abc", 2}, |
| 32 |
}, |
| 33 |
largetosmall, |
| 34 |
} |
| 35 |
|
| 36 |
func TestOffsetSort(t *testing.T) { |
| 37 |
for i, st := range offsetSortTests { |
| 38 |
sort.Sort(offsetSort(st)) |
| 39 |
for j, si := range st { |
| 40 |
if j != si.index { |
| 41 |
t.Errorf("%d: failed: %v", i, st) |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
for i, tt := range genStateTests { |
| 46 |
// ensure input is well-formed |
| 47 |
sort.Sort(offsetSort(tt.in)) |
| 48 |
for j, si := range tt.in { |
| 49 |
if si.index != j+1 { |
| 50 |
t.Errorf("%dth sort failed: %v", i, tt.in) |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
var genidxtest1 = []stridx{ |
| 57 |
{"bcde", 3}, |
| 58 |
{"bc", 6}, |
| 59 |
{"ab", 2}, |
| 60 |
{"bcd", 5}, |
| 61 |
{"abcd", 0}, |
| 62 |
{"abc", 1}, |
| 63 |
{"bcdf", 4}, |
| 64 |
} |
| 65 |
|
| 66 |
var genidxSortTests = [][]stridx{ |
| 67 |
genidxtest1, |
| 68 |
largetosmall, |
| 69 |
} |
| 70 |
|
| 71 |
func TestGenIdxSort(t *testing.T) { |
| 72 |
for i, st := range genidxSortTests { |
| 73 |
sort.Sort(genidxSort(st)) |
| 74 |
for j, si := range st { |
| 75 |
if j != si.index { |
| 76 |
t.Errorf("%dth sort failed %v", i, st) |
| 77 |
break |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
var entrySortTests = []colltab.ContractTrieSet{ |
| 84 |
{ |
| 85 |
{10, 0, 1, 3}, |
| 86 |
{99, 0, 1, 0}, |
| 87 |
{20, 50, 0, 2}, |
| 88 |
{30, 0, 1, 1}, |
| 89 |
}, |
| 90 |
} |
| 91 |
|
| 92 |
func TestEntrySort(t *testing.T) { |
| 93 |
for i, et := range entrySortTests { |
| 94 |
sort.Sort(entrySort(et)) |
| 95 |
for j, fe := range et { |
| 96 |
if j != int(fe.I) { |
| 97 |
t.Errorf("%dth sort failed %v", i, et) |
| 98 |
break |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
type GenStateTest struct { |
| 105 |
in []stridx |
| 106 |
firstBlockLen int |
| 107 |
out colltab.ContractTrieSet |
| 108 |
} |
| 109 |
|
| 110 |
var genStateTests = []GenStateTest{ |
| 111 |
{[]stridx{ |
| 112 |
{"abc", 1}, |
| 113 |
}, |
| 114 |
1, |
| 115 |
colltab.ContractTrieSet{ |
| 116 |
{'a', 0, 1, noIndex}, |
| 117 |
{'b', 0, 1, noIndex}, |
| 118 |
{'c', 'c', final, 1}, |
| 119 |
}, |
| 120 |
}, |
| 121 |
{[]stridx{ |
| 122 |
{"abc", 1}, |
| 123 |
{"abd", 2}, |
| 124 |
{"abe", 3}, |
| 125 |
}, |
| 126 |
1, |
| 127 |
colltab.ContractTrieSet{ |
| 128 |
{'a', 0, 1, noIndex}, |
| 129 |
{'b', 0, 1, noIndex}, |
| 130 |
{'c', 'e', final, 1}, |
| 131 |
}, |
| 132 |
}, |
| 133 |
{[]stridx{ |
| 134 |
{"abc", 1}, |
| 135 |
{"ab", 2}, |
| 136 |
{"a", 3}, |
| 137 |
}, |
| 138 |
1, |
| 139 |
colltab.ContractTrieSet{ |
| 140 |
{'a', 0, 1, 3}, |
| 141 |
{'b', 0, 1, 2}, |
| 142 |
{'c', 'c', final, 1}, |
| 143 |
}, |
| 144 |
}, |
| 145 |
{[]stridx{ |
| 146 |
{"abc", 1}, |
| 147 |
{"abd", 2}, |
| 148 |
{"ab", 3}, |
| 149 |
{"ac", 4}, |
| 150 |
{"a", 5}, |
| 151 |
{"b", 6}, |
| 152 |
}, |
| 153 |
2, |
| 154 |
colltab.ContractTrieSet{ |
| 155 |
{'b', 'b', final, 6}, |
| 156 |
{'a', 0, 2, 5}, |
| 157 |
{'c', 'c', final, 4}, |
| 158 |
{'b', 0, 1, 3}, |
| 159 |
{'c', 'd', final, 1}, |
| 160 |
}, |
| 161 |
}, |
| 162 |
{[]stridx{ |
| 163 |
{"bcde", 2}, |
| 164 |
{"bc", 7}, |
| 165 |
{"ab", 6}, |
| 166 |
{"bcd", 5}, |
| 167 |
{"abcd", 1}, |
| 168 |
{"abc", 4}, |
| 169 |
{"bcdf", 3}, |
| 170 |
}, |
| 171 |
2, |
| 172 |
colltab.ContractTrieSet{ |
| 173 |
{'b', 3, 1, noIndex}, |
| 174 |
{'a', 0, 1, noIndex}, |
| 175 |
{'b', 0, 1, 6}, |
| 176 |
{'c', 0, 1, 4}, |
| 177 |
{'d', 'd', final, 1}, |
| 178 |
{'c', 0, 1, 7}, |
| 179 |
{'d', 0, 1, 5}, |
| 180 |
{'e', 'f', final, 2}, |
| 181 |
}, |
| 182 |
}, |
| 183 |
} |
| 184 |
|
| 185 |
func TestGenStates(t *testing.T) { |
| 186 |
for i, tt := range genStateTests { |
| 187 |
si := []stridx{} |
| 188 |
for _, e := range tt.in { |
| 189 |
si = append(si, e) |
| 190 |
} |
| 191 |
// ensure input is well-formed |
| 192 |
sort.Sort(genidxSort(si)) |
| 193 |
ct := colltab.ContractTrieSet{} |
| 194 |
n, _ := genStates(&ct, si) |
| 195 |
if nn := tt.firstBlockLen; nn != n { |
| 196 |
t.Errorf("%d: block len %v; want %v", i, n, nn) |
| 197 |
} |
| 198 |
if lv, lw := len(ct), len(tt.out); lv != lw { |
| 199 |
t.Errorf("%d: len %v; want %v", i, lv, lw) |
| 200 |
continue |
| 201 |
} |
| 202 |
for j, fe := range tt.out { |
| 203 |
const msg = "%d:%d: value %s=%v; want %v" |
| 204 |
if fe.L != ct[j].L { |
| 205 |
t.Errorf(msg, i, j, "l", ct[j].L, fe.L) |
| 206 |
} |
| 207 |
if fe.H != ct[j].H { |
| 208 |
t.Errorf(msg, i, j, "h", ct[j].H, fe.H) |
| 209 |
} |
| 210 |
if fe.N != ct[j].N { |
| 211 |
t.Errorf(msg, i, j, "n", ct[j].N, fe.N) |
| 212 |
} |
| 213 |
if fe.I != ct[j].I { |
| 214 |
t.Errorf(msg, i, j, "i", ct[j].I, fe.I) |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
func TestLookupContraction(t *testing.T) { |
| 221 |
for i, tt := range genStateTests { |
| 222 |
input := []string{} |
| 223 |
for _, e := range tt.in { |
| 224 |
input = append(input, e.str) |
| 225 |
} |
| 226 |
cts := colltab.ContractTrieSet{} |
| 227 |
h, _ := appendTrie(&cts, input) |
| 228 |
for j, si := range tt.in { |
| 229 |
str := si.str |
| 230 |
for _, s := range []string{str, str + "X"} { |
| 231 |
msg := "%d:%d: %s(%s) %v; want %v" |
| 232 |
idx, sn := lookup(&cts, h, []byte(s)) |
| 233 |
if idx != si.index { |
| 234 |
t.Errorf(msg, i, j, "index", s, idx, si.index) |
| 235 |
} |
| 236 |
if sn != len(str) { |
| 237 |
t.Errorf(msg, i, j, "sn", s, sn, len(str)) |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
func TestPrintContractionTrieSet(t *testing.T) { |
| 245 |
testdata := colltab.ContractTrieSet(genStateTests[4].out) |
| 246 |
buf := &bytes.Buffer{} |
| 247 |
print(&testdata, buf, "test") |
| 248 |
if contractTrieOutput != buf.String() { |
| 249 |
t.Errorf("output differs; found\n%s", buf.String()) |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
const contractTrieOutput = `// testCTEntries: 8 entries, 32 bytes |
| 254 |
var testCTEntries = [8]struct{L,H,N,I uint8}{ |
| 255 |
{0x62, 0x3, 1, 255}, |
| 256 |
{0x61, 0x0, 1, 255}, |
| 257 |
{0x62, 0x0, 1, 6}, |
| 258 |
{0x63, 0x0, 1, 4}, |
| 259 |
{0x64, 0x64, 0, 1}, |
| 260 |
{0x63, 0x0, 1, 7}, |
| 261 |
{0x64, 0x0, 1, 5}, |
| 262 |
{0x65, 0x66, 0, 2}, |
| 263 |
} |
| 264 |
var testContractTrieSet = colltab.ContractTrieSet( testCTEntries[:] ) |
| 265 |
` |