-
Notifications
You must be signed in to change notification settings - Fork 1
/
match_test.go
110 lines (106 loc) · 2.02 KB
/
match_test.go
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
package swisstable
import (
"bytes"
"testing"
)
func TestMatchByte(t *testing.T) {
tests := []struct {
name string
c uint8
buffer []byte
wantMask uint32
wantOk bool
}{
{
"match 3",
42,
[]byte{42, 0, 0, 42, 42, 0, 17, 17, 0, 0, 0, 0, 0, 0, 0, 0},
1<<0 | 1<<3 | 1<<4,
true,
},
{
"match 1 at end",
42,
[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42},
1 << 15,
true,
},
{
"match 2 at start and end",
42,
[]byte{42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42},
1<<0 | 1<<15,
true,
},
{
"match all",
42,
[]byte{42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42},
1<<16 - 1,
true,
},
{
"match none - no match",
255,
[]byte{42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42},
0,
true,
},
{
"match none - len short by 1",
42,
[]byte{42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
0,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotMask, gotOk := MatchByte(tt.c, tt.buffer)
if gotMask != tt.wantMask {
t.Errorf("MatchByte() gotMask = %v, want %v", gotMask, tt.wantMask)
}
if gotOk != tt.wantOk {
t.Errorf("MatchByte() gotOk = %v, want %v", gotOk, tt.wantOk)
}
})
}
}
func TestMatchByteAlignment(t *testing.T) {
tests := []struct {
name string
c uint8
buffer []byte
wantMask uint32
wantOk bool
}{
{
"match all",
42,
bytes.Repeat([]byte{42}, 10000),
1<<16 - 1,
true,
},
{
"match none",
255,
bytes.Repeat([]byte{42}, 10000),
0,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for i := 0; i < len(tt.buffer)-16; i++ {
buffer := tt.buffer[i : i+16]
gotMask, gotOk := MatchByte(tt.c, buffer)
if gotMask != tt.wantMask {
t.Fatalf("MatchByte() offset %d gotMask = %v, want %v", i, gotMask, tt.wantMask)
}
if gotOk != tt.wantOk {
t.Fatalf("MatchByte() offset %d gotOk = %v, want %v", i, gotOk, tt.wantOk)
}
}
})
}
}