/
sparsevector_test.go
131 lines (116 loc) · 3.61 KB
/
sparsevector_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
package sparsevector
import (
"math/rand"
"reflect"
"testing"
)
func BenchmarkSparseVector1000(b *testing.B) {
benchmarkSparseVectorM(b, 1000)
}
func BenchmarkSparseVector10000(b *testing.B) {
benchmarkSparseVectorM(b, 10000)
}
func benchmarkSparseVectorM(b *testing.B, m int) {
// Generate 2 sparse vectors using 75% of numbers 1-m
v1 := genRandomSparseVector(m)
v2 := genRandomSparseVector(m)
b.ReportAllocs()
b.ResetTimer()
var total Value
for i := 0; i < b.N; i++ {
result := v1.Cos(v2)
total += result
}
}
func genRandomSparseVector(m int) *SparseVectorUint32 {
l := m * 3 / 4
values := make([]Value, l)
for i, v := range rand.Perm(m)[:l] {
values[i] = Value(v)
}
index := make([]uint32, l)
for i, v := range rand.Perm(m)[:l] {
index[i] = uint32(v)
}
return NewSparseVectorUint32(index, values)
}
func TestAddSparseVectorUint32(t *testing.T) {
tests := []struct {
v1 *SparseVectorUint32
v2 *SparseVectorUint32
exp *SparseVectorUint32
}{{
v1: NewSparseVectorUint32([]uint32{1, 2, 3}, []Value{4, 5, 6}),
v2: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{4, 5, 6}),
exp: NewSparseVectorUint32([]uint32{1, 2, 3, 4}, []Value{8, 5, 11, 6}),
}, {
v1: NewSparseVectorUint32([]uint32{}, []Value{}),
v2: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{4, 5, 6}),
exp: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{4, 5, 6}),
}, {
v1: NewSparseVectorUint32([]uint32{2}, []Value{7}),
v2: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{4, 5, 6}),
exp: NewSparseVectorUint32([]uint32{1, 2, 3, 4}, []Value{4, 7, 5, 6}),
}, {
v1: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{4, 5, 6}),
v2: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{4, 5, 6}),
exp: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{8, 10, 12}),
}, {
v1: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{4, 5, 6}),
v2: NewSparseVectorUint32([]uint32{1, 3}, []Value{4, 5}),
exp: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{8, 10, 6}),
}, {
v1: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{4, 5, 6}),
v2: NewSparseVectorUint32([]uint32{}, []Value{}),
exp: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{4, 5, 6}),
},
}
for i, test := range tests {
v3 := test.v1.Add(test.v2)
v4 := test.v2.Add(test.v1)
if !reflect.DeepEqual(test.exp, v3) {
t.Errorf("Test %d. Sum not as expected. Have %v", i, v3)
}
if !reflect.DeepEqual(test.exp, v4) {
t.Errorf("Test %d. Reverse Sum not as expected. Have %v", i, v4)
}
}
}
func TestMultSparseVectorUint32(t *testing.T) {
tests := []struct {
v *SparseVectorUint32
l Value
exp *SparseVectorUint32
}{{
v: NewSparseVectorUint32([]uint32{1, 2, 3}, []Value{4, 5, 6}),
l: 1,
exp: NewSparseVectorUint32([]uint32{1, 2, 3}, []Value{4, 5, 6}),
}, {
v: NewSparseVectorUint32([]uint32{}, []Value{}),
l: 1,
exp: NewSparseVectorUint32([]uint32{}, []Value{}),
}, {
v: NewSparseVectorUint32([]uint32{2}, []Value{7}),
l: 2,
exp: NewSparseVectorUint32([]uint32{2}, []Value{14}),
}, {
v: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{4, 5, 6}),
l: 0,
exp: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{0, 0, 0}),
}, {
v: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{4, 5, 6}),
l: -1,
exp: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{-4, -5, -6}),
}, {
v: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{4, 5, 6}),
l: 137.4,
exp: NewSparseVectorUint32([]uint32{1, 3, 4}, []Value{549.6, 687, 824.39996}),
},
}
for i, test := range tests {
test.v.Mult(test.l)
if !reflect.DeepEqual(test.exp, test.v) {
t.Errorf("Test %d. Sum not as expected. Have %v", i, test.v)
}
}
}