-
Notifications
You must be signed in to change notification settings - Fork 1
/
key_test.go
65 lines (58 loc) · 1.08 KB
/
key_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
package trie
import (
"crypto/rand"
"testing"
)
func TestCommonStringLen(t *testing.T) {
table := []struct {
K Key
S string
N int
}{
{Key("foo"), "bar", 0},
{Key("bar"), "baz", 2},
{Key("foobar"), "foo", 3},
{Key("foo"), "foobar", 3},
}
for _, x := range table {
if n, _ := x.K.CommonStringLen(x.S); n != x.N {
t.Errorf("expected %d; got %d", x.N, n)
}
}
}
func TestCommonBytesLen(t *testing.T) {
table := []struct {
K Key
B []byte
N int
}{
{Key("foo"), []byte("bar"), 0},
{Key("bar"), []byte("baz"), 2},
{Key("foobar"), []byte("foo"), 3},
{Key("foo"), []byte("foobar"), 3},
}
for _, x := range table {
if n, _ := x.K.CommonBytesLen(x.B); n != x.N {
t.Errorf("expected %d; got %d", x.N, n)
}
}
}
func BenchmarkEqualToBytes(b *testing.B) {
k := make(Key, 256)
rand.Read(k)
x := make([]byte, len(k))
copy(x, k)
b.ResetTimer()
for i := 0; i < b.N; i++ {
k.EqualToBytes(x)
}
}
func BenchmarkEqualToString(b *testing.B) {
k := make(Key, 256)
rand.Read(k)
x := string(k)
b.ResetTimer()
for i := 0; i < b.N; i++ {
k.EqualToString(x)
}
}