-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstringbank_test.go
116 lines (97 loc) · 1.99 KB
/
stringbank_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
package stringbank
import (
"fmt"
"runtime"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestStringbank(t *testing.T) {
sb := Stringbank{}
s1 := sb.Save("hello")
s2 := sb.Save("goodbye")
s3 := sb.Save("cheese")
assert.Equal(t, "hello", sb.Get(s1))
assert.Equal(t, "goodbye", sb.Get(s2))
assert.Equal(t, "cheese", sb.Get(s3))
}
func TestStringbankSize(t *testing.T) {
sb := Stringbank{}
assert.Zero(t, sb.Size())
sb.Save("hello")
assert.Equal(t, stringbankSize, sb.Size())
}
func TestPackageBank(t *testing.T) {
s1 := Save("hello")
s2 := Save("goodbye")
s3 := Save("cheese")
assert.Equal(t, "hello", s1.String())
assert.Equal(t, "goodbye", s2.String())
assert.Equal(t, "cheese", s3.String())
}
func TestLengths(t *testing.T) {
tests := []struct {
len int
}{
{1},
{127},
{128},
{254},
{255},
{256},
{0xFFFFFFFFFF},
}
for _, test := range tests {
t.Run(strconv.Itoa(test.len), func(t *testing.T) {
buf := make([]byte, 10)
l := writeLength(test.len, buf)
assert.Equal(t, l, spaceForLength(test.len))
len, lenlen := readLength(buf)
assert.Equal(t, l, lenlen)
assert.Equal(t, test.len, len)
})
}
}
func TestGC(t *testing.T) {
sb := Stringbank{}
for i := 0; i < 10000000; i++ {
sb.Save(strconv.Itoa(i))
}
runtime.GC()
start := time.Now()
runtime.GC()
assert.True(t, time.Since(start) < 1000*time.Microsecond)
runtime.KeepAlive(sb)
}
func BenchmarkStringbank(b *testing.B) {
s := make([]string, b.N)
for i := range s {
s[i] = strconv.Itoa(i)
}
index := make([]int, b.N)
b.ReportAllocs()
b.ResetTimer()
sb := Stringbank{}
for i, v := range s {
index[i] = sb.Save(v)
}
var out string
for _, i := range index {
out = sb.Get(i)
}
if out != s[b.N-1] {
b.Fatalf("final string should be %s, is %s", s[b.N-1], out)
}
}
func ExampleSave() {
i := Save("hello")
fmt.Println(i)
// Output: hello
}
func ExampleStringbank() {
sb := Stringbank{}
i := sb.Save("goodbye")
fmt.Println(sb.Get(i))
// Output: goodbye
}