-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector_test.go
150 lines (115 loc) · 3.19 KB
/
vector_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
package vector_test
import (
"testing"
"github.com/lthibault/vector"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestVector(t *testing.T) {
t.Parallel()
t.Helper()
const n = 4096
var v vector.Vector[int]
t.Run("ZeroValue", func(t *testing.T) {
assert.Zero(t, v.Len(), "zero-value vector should have zero length")
assert.Zero(t, v.Pop(), "popping empty vector should return zero-value vector")
})
t.Run("Append", func(t *testing.T) {
for i := 0; i < n; i++ {
v = v.Append(i)
}
require.NotNil(t, v, "vector should be non-nil")
require.Equal(t, n, v.Len(), "should contain %d elements", n)
require.Zero(t, v.At(0), "first element should be zero")
require.Equal(t, n-1, v.At(n-1), "last element should be %d", n-1)
v2 := v.Append()
assert.Equal(t, v, v2, "append with no args should no-op")
})
t.Run("Pop", func(t *testing.T) {
for i := n - 1; i >= 0; i-- {
v = v.Pop()
require.Equal(t, i, v.Len())
}
require.Zero(t, v, "should be zero-value vector")
})
}
func TestBulkAppend(t *testing.T) {
t.Parallel()
var v vector.Vector[int]
v = v.Append(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
assert.Equal(t, 10, v.Len(), "should bulk-insert 10 elements")
}
func TestGetSet(t *testing.T) {
t.Parallel()
t.Helper()
const n = 4096
is := make([]int, n)
for i := range is {
is[i] = i
}
v := vector.New(is...)
t.Run("Overwrite", func(t *testing.T) {
for i := 0; i < n; i++ {
v = v.Set(i, -i)
}
for i := 0; i < n; i++ {
assert.True(t, v.At(i) <= 0, "value should be overwritten")
}
})
t.Run("Append", func(t *testing.T) {
v2 := v.Set(n, -1)
assert.NotEqual(t, v, v2, "should not mutate v")
assert.Equal(t, n+1, v2.Len(), "should not append to vector")
assert.Equal(t, -1, v2.At(n), "should return appended value")
})
t.Run("OutOfBounds", func(t *testing.T) {
t.Parallel()
assert.Panics(t, func() { v.At(9001) },
"should panic when out of bounds")
assert.Panics(t, func() { v.At(-1) },
"should panic when out of bounds")
assert.Panics(t, func() { v.Set(9001, 9001) },
"should panic when out of bounds")
assert.Panics(t, func() { v.Set(-1, 9001) },
"should panic when out of bounds")
})
}
func TestNew(t *testing.T) {
t.Parallel()
const n = 4096
is := make([]int, n)
for i := range is {
is[i] = i
}
v := vector.New(is...)
assert.Equal(t, n, v.Len(), "should have length of %d", n)
for i := 0; i < n; i++ {
assert.Equal(t, i, v.At(i))
}
}
func TestBuilder(t *testing.T) {
t.Parallel()
t.Helper()
const n = 4096
b := vector.NewBuilder[int]()
t.Run("ZeroValue", func(t *testing.T) {
assert.Zero(t, b.Len(), "zero-value vector should have zero length")
})
t.Run("Append", func(t *testing.T) {
for i := 0; i < n; i++ {
b.Append(i)
}
require.NotNil(t, b, "vector should be non-nil")
require.Equal(t, n, b.Len(), "should contain %d elements", n)
require.Zero(t, b.Vector().At(0), "first element should be zero")
require.Equal(t, n-1, b.Vector().At(n-1), "last element should be %d", n-1)
})
t.Run("Pop", func(t *testing.T) {
v := b.Vector()
for i := n - 1; i >= 0; i-- {
v = v.Pop()
require.Equal(t, i, v.Len())
}
require.Zero(t, v, "should be zero-value vector")
})
}