forked from chen3feng/stl4go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_test.go
225 lines (198 loc) · 4.34 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package stl4go
import (
"testing"
)
func Test_Vector_Interface(t *testing.T) {
v := MakeVector[int]()
_ = Container(&v)
}
func Test_MakeVector(t *testing.T) {
MakeVector[int]()
_ = make(Vector[int], 1)
_ = make(Vector[int], 1, 2)
var v Vector[int]
_ = v
v = Vector[int]{1, 2, 3}
}
func Test_MakeVectorCap(t *testing.T) {
v := MakeVectorCap[int](10)
expectEq(t, v.Len(), 0)
expectEq(t, v.Cap(), 10)
}
func Test_VectorOf(t *testing.T) {
v := VectorOf(1, 2, 3)
expectEq(t, v.Len(), 3)
expectEq(t, v.Cap(), 3)
expectEq(t, v[0], 1)
expectEq(t, v[1], 2)
expectEq(t, v[2], 3)
}
func Test_AsVector(t *testing.T) {
s := []int{1, 2, 3}
v := AsVector(s)
expectTrue(t, Equal(s, v))
expectEq(t, &s[0], &v[0])
}
func Test_VectorCap(t *testing.T) {
v := MakeVectorCap[int](10)
v.PushBack(1)
expectEq(t, v.Len(), 1)
expectFalse(t, v.IsEmpty())
expectEq(t, v.Cap(), 10)
}
func Test_Vector_Clear(t *testing.T) {
v := VectorOf(1, 2, 3)
v.Clear()
expectEq(t, v.Len(), 0)
expectTrue(t, v.IsEmpty())
expectGt(t, v.Cap(), 0)
}
func Test_Vector_Reserve(t *testing.T) {
v := VectorOf(1, 2, 3)
v.Reserve(1)
expectEq(t, v.Cap(), 3)
v.Reserve(5)
expectEq(t, v.Cap(), 5)
expectEq(t, v.Len(), 3)
}
func Test_Vector_Shrink(t *testing.T) {
v := MakeVectorCap[int](10)
v.Append(1, 2, 3)
expectEq(t, v.Cap(), 10)
v.Shrink()
expectEq(t, v.Len(), v.Cap())
}
func Test_Vector_At_Set(t *testing.T) {
v := VectorOf(1, 2, 3)
expectEq(t, v.At(0), 1)
expectEq(t, v[0], 1)
v[0] = 2
expectEq(t, v[0], 2)
expectPanic(t, func() { v.Set(3, 2) })
}
func Test_Vector_PushBack(t *testing.T) {
v := VectorOf(1, 2, 3)
v.PushBack(4)
expectTrue(t, Equal(v, []int{1, 2, 3, 4}))
}
func Test_Vector_PopBack(t *testing.T) {
v := VectorOf(1, 2)
expectEq(t, v.PopBack(), 2)
n, ok := v.TryPopBack()
expectEq(t, n, 1)
expectTrue(t, ok)
n, ok = v.TryPopBack()
expectEq(t, n, 0)
expectFalse(t, ok)
expectPanic(t, func() { v.PopBack() })
}
func Test_Vector_Back(t *testing.T) {
v := VectorOf(1)
expectEq(t, v.Back(), 1)
v.PopBack()
expectPanic(t, func() { v.Back() })
}
func Test_Vector_Insert(t *testing.T) {
v := VectorOf(1, 2, 3)
v.Insert(0, 1, 2, 3)
expectTrue(t, Equal(v, []int{1, 2, 3, 1, 2, 3}))
}
func Test_Vector_Insert_Tail(t *testing.T) {
v := VectorOf(1, 2, 3)
v.Insert(3, 1, 2, 3)
expectTrue(t, Equal(v, []int{1, 2, 3, 1, 2, 3}))
}
func Test_Vector_Insert_Mid(t *testing.T) {
v := VectorOf(1, 2, 3)
v.Insert(2, 1, 2)
expectTrue(t, Equal(v, []int{1, 2, 1, 2, 3}))
}
func Test_Vector_Insert_Cap(t *testing.T) {
v := VectorOf(1, 2, 3)
v.Reserve(8)
v.Insert(2, 1, 2)
expectTrue(t, Equal(v, []int{1, 2, 1, 2, 3}))
}
func Test_Vector_Remove(t *testing.T) {
v := VectorOf(1, 2, 3)
v.Remove(1)
expectEq(t, v.Len(), 2)
expectEq(t, v.Cap(), 3)
expectEq(t, v[0], 1)
expectEq(t, v[1], 3)
}
func Test_Vector_RemoveRange(t *testing.T) {
v := VectorOf(1, 2, 3, 4)
v.RemoveRange(1, 3)
expectEq(t, v.Len(), 2)
expectEq(t, v.Cap(), 4)
expectEq(t, v[0], 1)
expectEq(t, v[1], 4)
}
func Test_Vector_RemoveLength(t *testing.T) {
v := VectorOf(1, 2, 3, 4)
v.RemoveLength(1, 2)
expectEq(t, v.Len(), 2)
expectEq(t, v.Cap(), 4)
expectEq(t, v[0], 1)
expectEq(t, v[1], 4)
}
func Test_Vector_ForEach(t *testing.T) {
a := []int{1, 2, 3}
v := VectorOf(a...)
var b []int
v.ForEach(func(n int) {
b = append(b, n)
})
expectEq(t, len(b), 3)
expectTrue(t, Equal(a, b))
}
func Test_Vector_ForEachIf(t *testing.T) {
v := VectorOf(1, 2, 3)
c := 0
v.ForEachIf(func(n int) bool {
c = n
return n != 2
})
expectEq(t, c, 2)
}
func Test_Vector_ForEachMutable(t *testing.T) {
a := []int{1, 2, 3}
v := VectorOf(1, 2, 3)
v.ForEachMutable(func(n *int) {
*n = -*n
})
expectEq(t, v.Len(), 3)
for i := range v {
expectEq(t, a[i], -v[i])
}
}
func Test_Vector_ForEachMutableIf(t *testing.T) {
v := VectorOf(1, 2, 3)
c := 0
v.ForEachMutableIf(func(n *int) bool {
c = *n
return *n != 2
})
expectEq(t, c, 2)
}
func Test_Vector_Iterate(t *testing.T) {
v := VectorOf(1, 2, 3, 4)
i := 1
for it := v.Iterate(); it.IsNotEnd(); it.MoveToNext() {
expectEq(t, it.Value(), i)
expectEq(t, *it.Pointer(), it.Value())
i++
}
expectEq(t, i, 5)
}
func Test_Vector_IterateRange(t *testing.T) {
v := VectorOf(1, 2, 3, 4)
i := 2
for it := v.IterateRange(1, 3); it.IsNotEnd(); it.MoveToNext() {
expectEq(t, it.Value(), i)
expectEq(t, *it.Pointer(), it.Value())
i++
}
expectEq(t, i, 4)
}