-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvec_test.go
More file actions
146 lines (119 loc) · 2.51 KB
/
vec_test.go
File metadata and controls
146 lines (119 loc) · 2.51 KB
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
package fibvec
import (
"fmt"
"math/rand"
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
func TestAddGet(t *testing.T) {
vec := NewVector()
values := make([]int, 1e5)
for i := range values {
v := rand.Intn(MaxValue)
values[i] = v
vec.Add(v)
}
for i, v := range values {
if !assert.Equal(t, v, vec.Get(i)) {
break
}
}
}
func TestAddGetNegative(t *testing.T) {
vec := NewVector()
values := []int{MinValue, -3, -2, -1, 0, 1, 2, 3, MaxValue}
for _, v := range values {
vec.Add(v)
}
for i, v := range values {
if !assert.Equal(t, v, vec.Get(i)) {
break
}
}
}
func TestGetValues(t *testing.T) {
vec := NewVector()
values := make([]int, 1e3)
for i := range values {
v := rand.Intn(MaxValue)
values[i] = v
vec.Add(v)
}
for i := range values {
if !assert.Equal(t, values[0:i+1], vec.GetValues(0, i+1)) {
break
}
}
}
func TestEncodeDecode(t *testing.T) {
vec := NewVector()
values := make([]int, 1e5)
for i := range values {
v := rand.Intn(MaxValue)
values[i] = v
vec.Add(v)
}
data, _ := vec.GobEncode()
nvec := NewVector()
nvec.GobDecode(data)
for i, v := range values {
if !assert.Equal(t, v, nvec.Get(i)) {
break
}
}
}
// TestAuxOverhead calculates the
// overhead of the rank and select
// auxilliary arrays for uint32 values.
func TestAuxOverhead(t *testing.T) {
vec := NewVector()
for i := 0; i < 1e5; i++ {
v := int(rand.Uint32())
vec.Add(v)
}
rawsize := float64(vec.bits.Size())
overhead := float64(vec.Size()) - rawsize
percentage := (overhead / rawsize) * 100
fmt.Printf("=== OVERHEAD: %.2f%%\n", percentage)
}
// TestCompression calculates the
// space saved with respect to the
// raw size for random uint32 values.
func TestCompression(t *testing.T) {
vec := NewVector()
for i := 0; i < 1e5; i++ {
v := int(rand.Uint32())
vec.Add(v)
}
sizeofUint := int(unsafe.Sizeof(uint(0)))
rawsize := float64(sizeofUint * 1e5)
vecsize := float64(vec.Size())
percentage := ((rawsize - vecsize) / rawsize) * 100
fmt.Printf("=== COMPRESSION: %.2f%%\n", percentage)
}
func BenchmarkAdd(b *testing.B) {
vec := NewVector()
values := make([]int, b.N)
for i := range values {
values[i] = rand.Intn(MaxValue)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
vec.Add(values[i])
}
}
func BenchmarkGet(b *testing.B) {
vec := NewVector()
for i := 0; i < 1e5; i++ {
vec.Add(rand.Intn(MaxValue))
}
idx := make([]int, b.N)
for i := range idx {
idx[i] = rand.Intn(vec.length)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
vec.Get(idx[i])
}
}