This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
/
all_test.go
147 lines (128 loc) · 2.79 KB
/
all_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2015 The XC Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xc
import (
"bytes"
"fmt"
"math"
"os"
"path"
"runtime"
"strings"
"testing"
"github.com/cznic/mathutil"
)
func caller(s string, va ...interface{}) {
_, fn, fl, _ := runtime.Caller(2)
fmt.Fprintf(os.Stderr, "caller: %s:%d: ", path.Base(fn), fl)
fmt.Fprintf(os.Stderr, s, va...)
fmt.Fprintln(os.Stderr)
_, fn, fl, _ = runtime.Caller(1)
fmt.Fprintf(os.Stderr, "\tcallee: %s:%d: ", path.Base(fn), fl)
fmt.Fprintln(os.Stderr)
}
func dbg(s string, va ...interface{}) {
if s == "" {
s = strings.Repeat("%v ", len(va))
}
_, fn, fl, _ := runtime.Caller(1)
fmt.Fprintf(os.Stderr, "dbg %s:%d: ", path.Base(fn), fl)
fmt.Fprintf(os.Stderr, s, va...)
fmt.Fprintln(os.Stderr)
}
func TODO(...interface{}) string {
_, fn, fl, _ := runtime.Caller(1)
return fmt.Sprintf("TODO: %s:%d:\n", path.Base(fn), fl)
}
func use(...interface{}) int { return 42 }
var _ = use(caller, dbg, TODO)
// ============================================================================
func newRng() *mathutil.FC32 {
r, err := mathutil.NewFC32(0, math.MaxInt32, true)
if err != nil {
panic(err)
}
r.Seed(42)
return r
}
func TestDBBytes(t *testing.T) {
const n = 29e4
rng := newRng()
db := NewMemDB()
exp := map[int][]byte{}
var buf []byte
for i := 0; i < n; i++ {
buf = buf[:0]
for i, l := 0, rng.Next()%31; i < l; i++ {
buf = append(buf, byte(rng.Next()))
}
id := db.PutBytes(buf)
switch {
case len(buf) == 0:
if g, e := id, 0; g != e {
t.Fatal(i, g, e)
}
case len(buf) == 1:
if g, e := id, int(buf[0])+1; g != e {
t.Fatal(i, g, e)
}
default:
if _, ok := exp[id]; ok {
t.Fatal(i, id)
}
}
exp[id] = append([]byte(nil), buf...)
}
for id, e := range exp {
g := db.Bytes(id)
if id == 0 {
if g != nil {
t.Fatalf("|% x|", g)
}
continue
}
if !bytes.Equal(g, e) {
t.Fatalf("%v |% x| |% x|", id, g, e)
}
}
db.mu.RLock()
defer db.mu.RUnlock()
for id, e := range exp {
g := db.bytesUnlocked(id)
if id == 0 {
if g != nil {
t.Fatalf("|% x|", g)
}
continue
}
if !bytes.Equal(g, e) {
t.Fatalf("%v |% x| |% x|", id, g, e)
}
}
l, c := db.Len(), db.Cap()
w := c - l
t.Logf("len %v, cap %v, wasted %v (%.1f%%)", l, c, w, 100*float64(w)/float64(c))
}
func TestDict(t *testing.T) {
const n = 15e4
dict := NewDictionary()
rng := newRng()
m := map[string]int{}
for i := 0; i < n; i++ {
x := rng.Next()
s := fmt.Sprintf("%d.%d", x, x)
m[s] = dict.ID([]byte(s))
}
for k, v := range m {
b := dict.S(v)
if g, e := string(b), k; g != e {
t.Fatalf("%q %q", g, e)
}
}
for k, v := range m {
if g, e := dict.ID([]byte(k)), v; g != e {
t.Fatal(g, e)
}
}
}