summaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/text/internal/colltab/contract_test.go
blob: ce2871dd4f6c8c9fb1d81794e201461a6754689a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package colltab

import (
	"testing"
)

type lookupStrings struct {
	str    string
	offset int
	n      int // bytes consumed from input
}

type LookupTest struct {
	lookup []lookupStrings
	n      int
	tries  ContractTrieSet
}

var lookupTests = []LookupTest{{
	[]lookupStrings{
		{"abc", 1, 3},
		{"a", 0, 0},
		{"b", 0, 0},
		{"c", 0, 0},
		{"d", 0, 0},
	},
	1,
	ContractTrieSet{
		{'a', 0, 1, 0xFF},
		{'b', 0, 1, 0xFF},
		{'c', 'c', 0, 1},
	},
}, {
	[]lookupStrings{
		{"abc", 1, 3},
		{"abd", 2, 3},
		{"abe", 3, 3},
		{"a", 0, 0},
		{"ab", 0, 0},
		{"d", 0, 0},
		{"f", 0, 0},
	},
	1,
	ContractTrieSet{
		{'a', 0, 1, 0xFF},
		{'b', 0, 1, 0xFF},
		{'c', 'e', 0, 1},
	},
}, {
	[]lookupStrings{
		{"abc", 1, 3},
		{"ab", 2, 2},
		{"a", 3, 1},
		{"abcd", 1, 3},
		{"abe", 2, 2},
	},
	1,
	ContractTrieSet{
		{'a', 0, 1, 3},
		{'b', 0, 1, 2},
		{'c', 'c', 0, 1},
	},
}, {
	[]lookupStrings{
		{"abc", 1, 3},
		{"abd", 2, 3},
		{"ab", 3, 2},
		{"ac", 4, 2},
		{"a", 5, 1},
		{"b", 6, 1},
		{"ba", 6, 1},
	},
	2,
	ContractTrieSet{
		{'b', 'b', 0, 6},
		{'a', 0, 2, 5},
		{'c', 'c', 0, 4},
		{'b', 0, 1, 3},
		{'c', 'd', 0, 1},
	},
}, {
	[]lookupStrings{
		{"bcde", 2, 4},
		{"bc", 7, 2},
		{"ab", 6, 2},
		{"bcd", 5, 3},
		{"abcd", 1, 4},
		{"abc", 4, 3},
		{"bcdf", 3, 4},
	},
	2,
	ContractTrieSet{
		{'b', 3, 1, 0xFF},
		{'a', 0, 1, 0xFF},
		{'b', 0, 1, 6},
		{'c', 0, 1, 4},
		{'d', 'd', 0, 1},
		{'c', 0, 1, 7},
		{'d', 0, 1, 5},
		{'e', 'f', 0, 2},
	},
}}

func lookup(c *ContractTrieSet, nnode int, s []uint8) (i, n int) {
	scan := c.scanner(0, nnode, s)
	scan.scan(0)
	return scan.result()
}

func TestLookupContraction(t *testing.T) {
	for i, tt := range lookupTests {
		cts := ContractTrieSet(tt.tries)
		for j, lu := range tt.lookup {
			str := lu.str
			for _, s := range []string{str, str + "X"} {
				const msg = `%d:%d: %s of "%s" %v; want %v`
				offset, n := lookup(&cts, tt.n, []byte(s))
				if offset != lu.offset {
					t.Errorf(msg, i, j, "offset", s, offset, lu.offset)
				}
				if n != lu.n {
					t.Errorf(msg, i, j, "bytes consumed", s, n, len(str))
				}
			}
		}
	}
}