This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_test.go
159 lines (140 loc) · 3.36 KB
/
module_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
package bigarray
import (
"sync"
"testing"
)
func RunBigArrayBasicTests(t *testing.T, opts ...Option) {
t.Helper()
opts = append(opts,
PageSize(32),
NumValues(64))
ba, err := New(opts...)
if err != nil {
t.Errorf("New: error: %v", err)
return
}
defer ba.Close()
if 64 != ba.Len() {
t.Errorf("BigArray.Len: expected 64, got %d", ba.Len())
}
value, err := ba.ValueAt(42)
if err != nil {
t.Errorf("BigArray.ValueAt 42 [1/2]: error: %v", err)
}
if value != 0 {
t.Error("42: expected false, got true")
}
err = ba.SetValueAt(42, 0xcc)
if err != nil {
t.Errorf("BigArray.SetValueAt 42: error: %v", err)
}
value, err = ba.ValueAt(42)
if err != nil {
t.Errorf("BigArray.ValueAt 42 [2/2]: error: %v", err)
}
if value != 0xcc {
t.Error("42: expected true, got false")
}
value, err = ba.ValueAt(43)
if err != nil {
t.Errorf("BigArray.ValueAt 43: error: %v", err)
}
if value != 0 {
t.Error("43: expected false, got true")
}
value, err = ba.ValueAt(41)
if err != nil {
t.Errorf("BigArray.ValueAt 41: error: %v", err)
}
if value != 0 {
t.Error("41: expected false, got true")
}
_, err = ba.ValueAt(0)
if err != nil {
t.Errorf("BigArray.ValueAt 0: error: %v", err)
}
_, err = ba.ValueAt(63)
if err != nil {
t.Errorf("BigArray.ValueAt 63: error: %v", err)
}
iter := ba.Iterate(0, ba.Len())
defer iter.Close()
n := uint64(0)
for iter.Next() {
index := iter.Index()
if n != index {
t.Errorf("BigArray.Iterate out of order: expected [%d], got [%d]", n, index)
}
iter.SetValue(index)
n++
}
if err := iter.Close(); err != nil {
t.Errorf("BigArray.Iterate: error: %v", err)
}
if n != ba.Len() {
t.Errorf("BigArray.Iterate only produced %d values", n)
}
iter = ba.ReverseIterate(0, ba.Len())
n = 0
for iter.Next() {
n++
expect := ba.Len() - n
index := iter.Index()
if expect != index {
t.Errorf("BigArray.ReverseIterate out of order: expected [%d], got [%d]", expect, index)
}
if iter.Value() != index {
t.Errorf("BigArray.ReverseIterate gave wrong value at [%d]: expected %d, got %d", expect, index, iter.Value())
}
}
if err := iter.Close(); err != nil {
t.Errorf("BigArray.ReverseIterate: error: %v", err)
}
if n != ba.Len() {
t.Errorf("BigArray.ReverseIterate only produced %d values", n)
}
for i := uint64(0); i < ba.Len(); i++ {
if err := ba.SetValueAt(i, 0); err != nil {
t.Errorf("BigArray.SetValueAt %d: error: %v", i, err)
}
}
for i := uint64(0); i < ba.Len(); i++ {
value, err := ba.ValueAt(i)
if err != nil {
t.Errorf("BigArray.ValueAt %d: error: %v", i, err)
continue
}
if value != 0 {
t.Errorf("BigArray.ValueAt %d: wrong value: expected 0, got %d", i, value)
}
}
}
func TestBigArray_InMemory(t *testing.T) {
for _, bpv := range []byte{1, 2, 4, 8} {
t.Logf("running tests with bpv=%d", bpv)
RunBigArrayBasicTests(t,
BytesPerValue(bpv))
}
}
func TestBigArray_OnDisk_NoPool(t *testing.T) {
for _, bpv := range []byte{1, 2, 4, 8} {
t.Logf("running tests with bpv=%d", bpv)
RunBigArrayBasicTests(t,
BytesPerValue(bpv),
OnDiskThreshold(0))
}
}
func TestBigArray_OnDisk_WithPool(t *testing.T) {
pool := &sync.Pool{
New: func() interface{} {
return make([]byte, 64)
},
}
for _, bpv := range []byte{1, 2, 4, 8} {
t.Logf("running tests with bpv=%d", bpv)
RunBigArrayBasicTests(t,
BytesPerValue(bpv),
OnDiskThreshold(0),
WithPool(pool))
}
}