forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmvmap_test.go
95 lines (87 loc) · 2.14 KB
/
mvmap_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
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package mvmap
import (
"bytes"
"encoding/binary"
"fmt"
"hash/fnv"
"testing"
)
func TestMVMap(t *testing.T) {
m := NewMVMap()
var vals [][]byte
m.Put([]byte("abc"), []byte("abc1"))
m.Put([]byte("abc"), []byte("abc2"))
m.Put([]byte("def"), []byte("def1"))
m.Put([]byte("def"), []byte("def2"))
vals = m.Get([]byte("abc"), vals[:0])
if fmt.Sprintf("%s", vals) != "[abc1 abc2]" {
t.FailNow()
}
vals = m.Get([]byte("def"), vals[:0])
if fmt.Sprintf("%s", vals) != "[def1 def2]" {
t.FailNow()
}
if m.Len() != 4 {
t.FailNow()
}
results := []string{"abc abc1", "abc abc2", "def def1", "def def2"}
it := m.NewIterator()
for i := 0; i < 4; i++ {
key, val := it.Next()
if fmt.Sprintf("%s %s", key, val) != results[i] {
t.FailNow()
}
}
key, val := it.Next()
if key != nil || val != nil {
t.FailNow()
}
}
func BenchmarkMVMapPut(b *testing.B) {
m := NewMVMap()
buffer := make([]byte, 8)
for i := 0; i < b.N; i++ {
binary.BigEndian.PutUint64(buffer, uint64(i))
m.Put(buffer, buffer)
}
}
func BenchmarkMVMapGet(b *testing.B) {
m := NewMVMap()
buffer := make([]byte, 8)
for i := 0; i < b.N; i++ {
binary.BigEndian.PutUint64(buffer, uint64(i))
m.Put(buffer, buffer)
}
val := make([][]byte, 0, 8)
b.ResetTimer()
for i := 0; i < b.N; i++ {
binary.BigEndian.PutUint64(buffer, uint64(i))
val = m.Get(buffer, val[:0])
if len(val) != 1 || bytes.Compare(val[0], buffer) != 0 {
b.FailNow()
}
}
}
func TestFNVHash(t *testing.T) {
b := []byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}
sum1 := fnvHash64(b)
hash := fnv.New64()
hash.Reset()
hash.Write(b)
sum2 := hash.Sum64()
if sum1 != sum2 {
t.FailNow()
}
}