-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlists_test.go
256 lines (232 loc) · 5.01 KB
/
lists_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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package genh
import (
"encoding/json"
"log"
"math/rand"
"sort"
"sync"
"testing"
)
func init() {
log.SetFlags(log.Lshortfile)
}
type S struct {
X int
}
func TestLists(t *testing.T) {
var l List[S]
var exp []S
for i := 0; i < 10; i++ {
l.Push(S{i})
exp = append(exp, S{i})
}
b, err := json.Marshal(l)
if err != nil {
t.Fatal(err)
}
var ll List[S]
err = json.Unmarshal(b, &ll)
if err != nil {
t.Fatal(err)
}
if !Equal(exp, ll.Slice()) {
t.Fatal("exp != ll", exp, ll)
}
b, err = MarshalMsgpack(l)
if err != nil {
t.Fatal(err)
}
var ll2 List[S]
err = UnmarshalMsgpack(b, &ll2)
if err != nil {
t.Error(err)
}
if !Equal(exp, ll.Slice()) {
t.Fatal("exp != ll", exp, ll)
}
lls := l.ListAt(5, 10)
if !Equal(lls.Slice(), exp[5:]) || lls.Len() != 5 {
t.Fatal("exp != ll", exp[5:], lls.Slice(), lls.Len())
}
lls = l.ListAt(5, -1)
if !Equal(lls.Slice(), exp[5:]) || lls.Len() != 5 {
t.Fatal("exp != ll", exp[5:], lls.Slice(), lls.Len())
}
mls := l.AppendList(ListOf(exp[:5]...))
if !Equal(mls.Slice(), append(exp, exp[0:5]...)) || mls.Len() != 15 {
t.Fatal("exp != ll", exp[5:], mls.Slice(), mls.Len())
}
}
func TestListIter(t *testing.T) {
var l List[S]
var exp []S
for i := 5; i < 10; i++ {
l.Push(S{i})
exp = append(exp, S{i})
}
it := l.Iter()
i := 0
for v, ok := it.Next(); ok; v, ok = it.Next() {
if v != exp[i] {
t.Fatal("v != exp[i]", v, exp[i])
}
i++
}
l.Prepend(S{44})
it = l.Iter()
for v, ok := it.Next(); ok; v, ok = it.Next() {
if v.X == 7 {
it.Set(S{77})
}
if v.X == 5 || v.X == 8 {
it.Delete()
}
}
exp = []S{{44}, {6}, {77}, {9}}
if !Equal(l.Slice(), exp) || l.Len() != 4 {
t.Fatal("exp != ll", exp, l.Slice(), l.Len())
}
}
func TestListSort(t *testing.T) {
var l List[int]
var nums []int
for i := 0; i < 25; i++ {
nums = append(nums, rand.Int()%100)
}
for _, n := range nums {
l.PushSort(n, func(a, b int) bool { return a >= b })
}
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
if !Equal(nums, l.Slice()) {
t.Log(nums)
t.Log(l.Slice())
t.Fatal("neq")
}
t.Log(l.Slice())
}
func TestListClip(t *testing.T) {
var l, cl, cl2 List[int]
var nums []int
for i := 0; i < 25; i++ {
nums = append(nums, rand.Int()%100)
}
for i, n := range nums {
if i == 10 {
cl = l.Clip()
}
if i == 20 {
cl2 = l.Clip()
}
l.Push(n)
}
if l.Len() != 25 || l.Len() != l.count() || cap(l.Slice()) != l.Len() {
t.Fatalf("unexpected, should have been 10, got %d %v", cap(l.Slice()), l.Slice())
}
cl.Push(99)
if cl.Len() != 11 || cl.Len() != cl.count() || cap(cl.Slice()) != cl.Len() {
t.Fatalf("unexpected, should have been 11, got %d %v %d", cap(cl.Slice()), cl.Slice(), cl.count())
}
if cl2.Len() != 20 || cl2.Len() != cl2.count() || cap(cl2.Slice()) != cl2.Len() {
t.Fatalf("unexpected, should have been 20, got %d %v %d", cap(cl2.Slice()), cl2.Slice(), cl2.count())
}
i := 0
it := cl2.Iter()
for v, ok := it.Next(); ok; v, ok = it.Next() {
if v != nums[i] {
t.Fatalf("unexpected, should have been %d, got %d", nums[i], v)
}
i++
}
for i, v := range l.Slice() {
if v != nums[i] {
t.Fatalf("unexpected, should have been %d, got %d", nums[i], v)
}
}
// run with race
var wg sync.WaitGroup
var mux sync.Mutex
for i := 0; i < 10000; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
mux.Lock()
l.Push(i)
cl := l.Clip()
mux.Unlock()
_ = cl.Slice()
}(i)
}
wg.Wait()
if l.Len() != 10025 || cap(l.Slice()) != l.Len() {
t.Fatalf("unexpected, should have been 20, got %d %d", cap(l.Slice()), l.count())
}
}
// func TestSliceList(t *testing.T) {
// var nums []int
// for i := 0; i < 25; i++ {
// nums = append(nums, rand.Int()%100)
// }
// ls := NewSliceList[int](25)
// for _, n := range nums {
// ls.Append(n)
// }
// if !Equal(ls.Slice(), nums) {
// t.Fatal("neq")
// }
//
// clipped := ls.Clip(false)
// clipped.Append(99999, 5555, 4444)
//
// j, err := json.Marshal(ls)
// if err != nil {
// t.Fatal(err)
// }
// t.Log(string(j))
// j, _ = json.Marshal(clipped)
// t.Log(string(j))
// t.Log(ls.Get(0))
//
// clipped.Set(25, -999999)
// t.Log(clipped.Get(25))
// }
//
// func BenchmarkSliceList(b *testing.B) {
// b.Run("SliceList.Append", func(b *testing.B) {
// ls := NewSliceList[int](0)
// for i := 0; i < b.N; i++ {
// ls.Append(i)
// }
// })
// b.Run("SliceList.ForEach", func(b *testing.B) {
// ls := NewSliceList[int](0)
// ls.Append(make([]int, 100_000)...)
// b.ResetTimer()
// var sink int
// for i := 0; i < b.N; i++ {
// ls.ForEach(func(v int) bool {
// sink = v
// return true
// })
// }
// _ = sink
// })
// b.Run("List.Append", func(b *testing.B) {
// var ls List[int]
// for i := 0; i < b.N; i++ {
// ls = ls.Append(i)
// }
// })
// b.Run("List.ForEach", func(b *testing.B) {
// ls := NewSliceList[int](0)
// ls.Append(make([]int, 100_000)...)
// b.ResetTimer()
// var sink int
// for i := 0; i < b.N; i++ {
// ls.ForEach(func(v int) bool {
// sink = v
// return true
// })
// }
// _ = sink
// })
// }