-
Notifications
You must be signed in to change notification settings - Fork 2
/
intern_test.go
199 lines (178 loc) · 4.37 KB
/
intern_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package intern
import (
"fmt"
"testing"
)
func TestIntern(t *testing.T) {
repo := NewRepository()
if repo.Intern("foo") != 1 ||
repo.Intern("bar") != 2 {
t.Error("invalid Intern() result")
}
if repo.Intern("qux") != 3 ||
repo.Intern("foo") != 1 ||
repo.Intern("bar") != 2 {
t.Error("invalid Intern() result")
}
}
func TestLookup(t *testing.T) {
repo := NewRepository()
repo.Intern("foo")
if id, ok := repo.Lookup("foo"); !ok || id != 1 {
t.Error("invalid Lookup() result")
}
if _, ok := repo.Lookup("bar"); ok {
t.Error("invalid Lookup() result")
}
}
func TestAllocatedBytes(t *testing.T) {
repo := NewRepository()
if repo.AllocatedBytes() == 0 {
t.Error("invalid AllocatedBytes() result")
}
}
func TestPageSize(t *testing.T) {
repo := NewRepository()
if repo.PageSize() == 0 {
t.Error("invalid PageSize() result")
}
}
func TestLookupID(t *testing.T) {
repo := NewRepository()
repo.Intern("foo")
if str, ok := repo.LookupID(1); !ok || str != "foo" {
t.Error("invalid Lookup() result")
}
if _, ok := repo.LookupID(2); ok {
t.Error("invalid Lookup() result")
}
}
func TestCount(t *testing.T) {
repo := NewRepository()
if repo.Count() != 0 {
t.Error("invalid Count() result")
}
repo.Intern("foo")
if repo.Count() != 1 {
t.Error("invalid Count() result")
}
repo.Intern("foo")
if repo.Count() != 1 {
t.Error("invalid Count() result")
}
repo.Intern("bar")
if repo.Count() != 2 {
t.Error("invalid Count() result")
}
}
func TestLargeRepository(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
count := 100000
repo := NewRepository()
startSize := repo.AllocatedBytes()
for i := 1; i <= count; i++ {
str := fmt.Sprintf("x%d", i)
id := repo.Intern(str)
if int(id) != i {
t.Error("invalid Intern() result")
}
if id != repo.Intern(str) {
t.Error("Intern() is not idempotent")
}
if lookupID, ok := repo.Lookup(str); !ok || lookupID != id {
t.Error("invalid Lookup() result")
}
if lookupStr, ok := repo.LookupID(id); !ok || lookupStr != str {
t.Error("invalid LookupID() result")
}
}
endSize := repo.AllocatedBytes()
if startSize >= endSize {
t.Error("invalid AllocatedBytes() result")
}
if int(repo.Count()) != count {
t.Error("invalid Count() result")
}
cursor := repo.Cursor()
i := 0
// the cursor is invalid until Next() is called
if cursor.String() != "" || cursor.ID() != 0 {
t.Error("invalid cursor position")
}
for cursor.Next() {
i++
str := fmt.Sprintf("x%d", i)
if int(cursor.ID()) != i || cursor.String() != str {
t.Error("invalid cursor position")
}
}
if i != count {
t.Error("invalid cursor operation(s)")
}
// the cursor is now invalid
if cursor.String() != "" || cursor.ID() != 0 {
t.Error("invalid cursor position")
}
}
func assertStrings(t *testing.T, repo *Repository, expected []string) {
if len(expected) != int(repo.Count()) {
t.Error("unexpected count")
}
for i, str := range expected {
if id, ok := repo.Lookup(str); !ok || int(id) != i+1 {
t.Error("invalid repository")
}
}
if _, ok := repo.LookupID(uint32(len(expected) + 1)); ok {
t.Error("unexpected extra strings in repository")
}
}
func TestSnapshotRestore(t *testing.T) {
repo := NewRepository()
start := repo.Snapshot()
repo.Intern("foo")
repo.Intern("bar")
mid := repo.Snapshot()
repo.Intern("qux")
repo.Intern("xyz")
end := repo.Snapshot()
if err := repo.Restore(end); err != nil {
t.Error(err)
}
assertStrings(t, repo, []string{"foo", "bar", "qux", "xyz"})
if err := repo.Restore(mid); err != nil {
t.Error(err)
}
assertStrings(t, repo, []string{"foo", "bar"})
if err := repo.Restore(start); err != nil {
t.Error(err)
}
assertStrings(t, repo, []string{})
// restoring to start invalidates the mid snapshot
if err := repo.Restore(mid); err == nil {
t.Error("expected an error when restoring to an invalid snapshot")
} else if err != ErrInvalidSnapshot {
t.Error("unexpected error")
}
}
func TestOptimize(t *testing.T) {
repo := NewRepository()
for _, str := range []string{"foo", "bar", "baz"} {
repo.Intern(str)
}
freq := NewFrequency()
freq.Add(2)
optimized := repo.Optimize(freq)
assertStrings(t, optimized, []string{"bar"})
freq.Add(3)
freq.Add(3)
optimized = repo.Optimize(freq)
assertStrings(t, optimized, []string{"baz", "bar"})
freq.AddAll(repo)
freq.Add(3)
freq.Add(2)
optimized = repo.Optimize(freq)
assertStrings(t, optimized, []string{"baz", "bar", "foo"})
}