-
Notifications
You must be signed in to change notification settings - Fork 4
/
db_test.go
64 lines (46 loc) · 1.26 KB
/
db_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
// db_test.go -- test suite for dbreader/dbwriter
package bbhash
import (
"fmt"
"os"
"testing"
"flag"
"github.com/opencoff/go-fasthash"
)
var keep bool
func init() {
flag.BoolVar(&keep, "keep", false, "Keep test DB")
}
func TestDB(t *testing.T) {
assert := newAsserter(t)
vals := make([][]byte, len(keyw))
keys := make([][]byte, len(keyw))
for i, s := range keyw {
h := fasthash.Hash64(0xdeadbeefbaadf00d, []byte(s))
vals[i] = []byte(fmt.Sprintf("%#x", h))
keys[i] = []byte(s)
}
fn := fmt.Sprintf("%s/mph%d.db", os.TempDir(), rand64())
wr, err := NewDBWriter(fn)
assert(err == nil, "can't create db: %s", err)
defer func() {
if keep {
t.Logf("DB in %s retained after test\n", fn)
} else {
os.Remove(fn)
}
}()
n, err := wr.AddKeyVals(keys, vals)
assert(err == nil, "can't add key-val: %s", err)
assert(int(n) == len(keys), "fewer keys added; exp %d, saw %d", len(keys), n)
err = wr.Freeze(2.0)
assert(err == nil, "freeze failed: %s", err)
rd, err := NewDBReader(fn, 10)
assert(err == nil, "read failed: %s", err)
for i, k := range keys {
v := vals[i]
s, err := rd.Find(k)
assert(err == nil, "can't find key %s: %s", k, err)
assert(string(s) == string(v), "key %s: value mismatch; exp %s, saw %s", k, v, string(s))
}
}