-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathsmall_table_test.go
More file actions
130 lines (122 loc) · 3.16 KB
/
small_table_test.go
File metadata and controls
130 lines (122 loc) · 3.16 KB
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
package quamina
import (
"fmt"
"testing"
"time"
)
func TestFAMergePerf(t *testing.T) {
words := readWWords(t, 0)
patterns := make([]string, 0, len(words))
for _, word := range words {
pattern := fmt.Sprintf(`{"x": [ "%s" ] }`, string(word))
patterns = append(patterns, pattern)
}
before := time.Now()
q, _ := New()
for _, pattern := range patterns {
err := q.AddPattern(pattern, pattern)
if err != nil {
t.Error("ap: " + err.Error())
}
}
elapsed := float64(time.Since(before).Milliseconds())
for _, word := range words {
event := fmt.Sprintf(`{"x": "%s"}`, string(word))
matches, err := q.MatchesForEvent([]byte(event))
if err != nil {
t.Error("M4: " + err.Error())
}
if len(matches) != 1 {
t.Errorf("wanted 1 got %d", len(matches))
}
}
perSecond := float64(len(patterns)) / (elapsed / 1000.0)
fmt.Printf("%.2f addPatterns/second with letter patterns\n\n", perSecond)
}
func TestUnpack(t *testing.T) {
st1 := newSmallTable()
nextState := faState{
table: st1,
fieldTransitions: nil,
}
st := smallTable{
ceilings: []uint8{2, 3, byte(byteCeiling)},
steps: []*faState{nil, &nextState, nil},
}
u := unpackTable(&st)
for i := range u {
if i == 2 {
if u[i] != &nextState {
t.Error("Not in pos 2")
}
} else {
if u[i] != nil {
t.Errorf("Non-nil at %d", i)
}
}
}
}
func TestDodgeBadUTF8(t *testing.T) {
st := makeSmallTable(nil, []byte{'a'}, []*faState{{}})
so := &stepOut{}
st.step(0xFE, so)
st.dStep(0xFE)
}
func TestSmallTableIterator(t *testing.T) {
bytevals := []byte{11, 22, 'a', 'b', 'c', 'z', 0xf3}
var s1, s2, s3, s4, s5, s6, s7 faState
var steps = []*faState{&s1, &s2, &s3, &s4, &s5, &s6, &s7}
st := makeSmallTable(nil, bytevals, steps)
wanted := make([]*faState, byteCeiling)
for i, byteval := range bytevals {
wanted[byteval] = steps[i]
}
iter := newSTIterator(st, nil)
for iter.hasNext() {
utf8byte, step := iter.next()
if wanted[utf8byte] != step {
t.Errorf("at u=%x wanted %p got %p", utf8byte, wanted[utf8byte], step)
}
}
iter.byteIndex = 0
iter.ceilingIndex = 0
for i := 0; i < byteCeiling; i++ {
state := iter.nextState()
if wanted[i] != state {
t.Errorf("at u=%x wanted %p got %p", i, wanted[i], state)
}
}
unpacked := unpackTable(st)
iter = newSTIterator(st, &iter)
for iter.hasNext() {
utf8byte, step := iter.next()
if unpacked[utf8byte] != step {
t.Errorf("Wrong unpacked at %x", utf8byte)
}
}
// ceilings:-| 3|-| 5|-|0x34|-| x35|-|byteCeiling|
// states:---|nil|-|&ss1|-| nil|-|&ss2|-| nil|
bytevals = []byte{3, 5, 0x34, 0x35}
var ss1, ss2 faState
steps = []*faState{nil, &ss1, nil, &ss2}
st = makeSmallTable(nil, bytevals, steps)
wanted = make([]*faState, byteCeiling)
for i, byteval := range bytevals {
wanted[byteval] = steps[i]
}
iter = newSTIterator(st, &iter)
for iter.hasNext() {
utf8byte, step := iter.next()
if wanted[utf8byte] != step {
t.Errorf("at u=%x wanted %p got %p", utf8byte, wanted[utf8byte], step)
}
}
unpacked = unpackTable(st)
iter = newSTIterator(st, &iter)
for iter.hasNext() {
utf8byte, step := iter.next()
if unpacked[utf8byte] != step {
t.Errorf("Wrong unpacked at %x", utf8byte)
}
}
}