-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo_test.go
136 lines (110 loc) · 3.28 KB
/
info_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
package tdb
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestInfoMethods(t *testing.T) {
// create a bad folder
folderPath := "bad_folder"
if err := createFolder(folderPath); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(folderPath)
// creation on folder will fail
_, err := createInfo(folderPath)
assert.Equal(t, ErrPathNotFile, err, "error should pop while creating info on a folder")
demoInfo := "demo_info"
defer os.Remove(demoInfo)
one, err := createInfo(demoInfo)
assert.NoError(t, err, "should have no error while creating info from non-existed file")
assert.Len(t, one.content, 0, "should have empty content")
key := "key"
value := []byte("value")
err = one.updateInfo([]string{key}, [][]byte{value})
assert.NoError(t, err, "should have no error while updating the content")
assert.Len(t, one.content, 1, "should have one content")
// get value
value1 := one.getInfoValue(key)
assert.EqualValues(t, value, value1, "get value wrong")
// get a not existed value
value2 := one.getInfoValue("not exist")
assert.Nil(t, value2, "should have nil value")
// get keys
keys := []string{key}
assert.EqualValues(t, keys, one.getInfoKeys(), "keys not correct.")
// get all
allKeys := []string{key}
allValues := [][]byte{value}
keysResult, valuesResult := one.getAllInfo()
assert.EqualValues(t, allKeys, keysResult, "keys not correct.")
assert.EqualValues(t, allValues, valuesResult, "values not correct.")
// k, v length not equal
err = one.updateInfo([]string{key}, [][]byte{value, value})
assert.Error(t, err, "should have error when k, v length not equal")
// empty k, v
err = one.updateInfo([]string{}, [][]byte{})
assert.NoError(t, err, "should have no error while updating nothing")
// load an existing content
two, err2 := createInfo(demoInfo)
assert.NoError(t, err2, "should have no error while creating info from an existed file")
assert.Len(t, two.content, 1, "should have empty content")
assert.EqualValues(t, one.content[key], two.content[key], "the values should be the same")
}
func BenchmarkLoad10(b *testing.B) {
benchmarkLoad(b, 10)
}
func BenchmarkUpdate10(b *testing.B) {
benchmarkUpdate(b, 10)
}
func BenchmarkLoad1000(b *testing.B) {
benchmarkLoad(b, 1000)
}
func BenchmarkUpdate1000(b *testing.B) {
benchmarkUpdate(b, 1000)
}
func benchmarkLoad(b *testing.B, count int) {
benchInfo := "bench_info_load"
defer os.Remove(benchInfo)
one, err := createInfo(benchInfo)
if err != nil {
b.Fatal(err)
}
var keys []string
var values [][]byte
for i := 0; i < count; i++ {
k := randBytes(30)
v := randBytes(8)
keys = append(keys, string(k))
values = append(values, v)
}
one.updateInfo(keys, values)
for i := 0; i < b.N; i++ {
one.loadInfo()
}
if len(one.content) != count {
b.Errorf("should have %d content", count)
}
}
func benchmarkUpdate(b *testing.B, count int) {
benchInfo := "bench_info_update"
defer os.Remove(benchInfo)
one, err := createInfo(benchInfo)
if err != nil {
b.Fatal(err)
}
var keys []string
var values [][]byte
for i := 0; i < count; i++ {
k := randBytes(30)
v := randBytes(8)
keys = append(keys, string(k))
values = append(values, v)
}
for i := 0; i < b.N; i++ {
one.updateInfo(keys, values)
}
if len(one.content) != count {
b.Errorf("should have %d content", count)
}
}