forked from zond/gotomic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list_test.go
309 lines (283 loc) · 7.57 KB
/
list_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package gotomic
import (
"fmt"
"math"
"math/rand"
"reflect"
"runtime"
"testing"
"time"
)
type c int
func (self c) Compare(t Thing) int {
if s, ok := t.(c); ok {
if self > s {
return 1
} else if self < s {
return -1
} else {
return 0
}
}
panic(fmt.Errorf("%#v can only compare to other c's, not %#v of type %T", self, t, t))
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func fiddle(t *testing.T, nr *element, do, done chan bool) {
<-do
num := 10000
for i := 0; i < num; i++ {
x := rand.Int()
nr.add(x)
}
done <- true
}
func fiddleAndAssertSort(t *testing.T, nr *element, do chan bool, ichan chan []c) {
<-do
num := 1000
var injected []c
for i := 0; i < num; i++ {
v := c(-int(math.Abs(float64(rand.Int()))))
nr.inject(v)
injected = append(injected, v)
if err := nr.verify(); err != nil {
t.Error(nr, "should be correct, but got", err)
}
}
ichan <- injected
}
func assertSlicey(t *testing.T, nr *element, cmp []Thing) {
if sl := nr.ToSlice(); !reflect.DeepEqual(sl, cmp) {
t.Errorf("%v should be %#v but is %#v", nr.Describe(), cmp, sl)
}
}
func TestPushPop(t *testing.T) {
nr := new(element)
assertSlicey(t, nr, []Thing{nil})
nr.add("hej")
assertSlicey(t, nr, []Thing{nil, "hej"})
nr.add("haj")
assertSlicey(t, nr, []Thing{nil, "haj", "hej"})
nr.add("hoj")
assertSlicey(t, nr, []Thing{nil, "hoj", "haj", "hej"})
}
func TestConcPushPop(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
nr := new(element)
assertSlicey(t, nr, []Thing{nil})
nr.add("1")
nr.add("2")
nr.add("3")
nr.add("4")
do := make(chan bool)
done := make(chan bool)
go fiddle(t, nr, do, done)
go fiddle(t, nr, do, done)
go fiddle(t, nr, do, done)
go fiddle(t, nr, do, done)
close(do)
<-done
<-done
<-done
<-done
assertSlicey(t, nr, []Thing{nil, "4", "3", "2", "1"})
}
const ANY = "ANY VALUE"
func searchTest(t *testing.T, nr *element, s c, l, n, r Thing) {
h := nr.search(s)
if (l != ANY && !reflect.DeepEqual(h.left.val(), l)) ||
(n != ANY && !reflect.DeepEqual(h.element.val(), n)) ||
(r != ANY && !reflect.DeepEqual(h.right.val(), r)) {
t.Error(nr, ".search(", s, ") should produce ", l, n, r, " but produced ", h.left.val(), h.element.val(), h.right.val())
}
}
func makeStringEntry(ch string) entry {
k := Key([]byte(ch))
return *(newRealEntry(k, unsafe.Pointer(&ch)))
}
func TestListEach(t *testing.T) {
nr := new(element)
nr.add(makeStringEntry("h"))
nr.add(makeStringEntry("g"))
nr.add(makeStringEntry("f"))
nr.add(makeStringEntry("d"))
nr.add(makeStringEntry("c"))
nr.add(makeStringEntry("b"))
var a []*entry
nr.each(func(e entry) bool {
a = append(a, e)
return false
})
exp := []entry{makeStringEntry("b"), makeStringEntry("c"), makeStringEntry("d"), makeStringEntry("f"), makeStringEntry("g"), makeStringEntry("h")}
for i, _ := range exp {
ch1 := *(*string)(exp[i].value)
ch2 := *(*string)(nr[i+1].value)
if ch1 != ch2 {
t.Error(ch1, "should be", ch2)
}
}
}
func TestListEachInterrupt(t *testing.T) {
nr := new(element)
nr.add("h")
nr.add("g")
nr.add("f")
nr.add("d")
nr.add("c")
nr.add("b")
var a []entry
interrupted := nr.each(func(e entry) bool {
a = append(a, e)
return len(a) == 2
})
if !interrupted {
t.Error("Iteration should have been interrupted.")
}
if len(a) != 2 {
t.Error("List should have 2 elements. Have", len(a))
}
}
func TestPushBefore(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
nr := new(element)
nr.add("h")
nr.add("g")
nr.add("f")
nr.add("d")
nr.add("c")
nr.add("b")
element := &element{}
if nr.addBefore("a", element, nr) {
t.Error("should not be possible")
}
if !nr.addBefore("a", element, nr.next()) {
t.Error("should be possible")
}
}
func TestSearch(t *testing.T) {
nr := &element{nil, &list_head}
nr.add(c(9))
nr.add(c(8))
nr.add(c(7))
nr.add(c(5))
nr.add(c(4))
nr.add(c(3))
assertSlicey(t, nr, []Thing{&list_head, c(3), c(4), c(5), c(7), c(8), c(9)})
searchTest(t, nr, c(1), &list_head, nil, c(3))
searchTest(t, nr, c(2), &list_head, nil, c(3))
searchTest(t, nr, c(3), &list_head, c(3), c(4))
searchTest(t, nr, c(4), c(3), c(4), c(5))
searchTest(t, nr, c(5), c(4), c(5), c(7))
searchTest(t, nr, c(6), c(5), nil, c(7))
searchTest(t, nr, c(7), c(5), c(7), c(8))
searchTest(t, nr, c(8), c(7), c(8), c(9))
searchTest(t, nr, c(9), c(8), c(9), nil)
searchTest(t, nr, c(10), c(9), nil, nil)
searchTest(t, nr, c(11), c(9), nil, nil)
}
func TestVerify(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
nr := &element{nil, &list_head}
nr.inject(c(3))
nr.inject(c(5))
nr.inject(c(9))
nr.inject(c(7))
nr.inject(c(4))
nr.inject(c(8))
assertSlicey(t, nr, []Thing{&list_head, c(3), c(4), c(5), c(7), c(8), c(9)})
if err := nr.verify(); err != nil {
t.Error(nr, "should verify as ok, got", err)
}
nr = &element{nil, &list_head}
nr.add(c(3))
nr.add(c(5))
nr.add(c(9))
nr.add(c(7))
nr.add(c(4))
nr.add(c(8))
assertSlicey(t, nr, []Thing{&list_head, c(8), c(4), c(7), c(9), c(5), c(3)})
s := fmt.Sprintf("[%v 8 4 7 9 5 3] is badly ordered. The following elements are in the wrong order: 8,4; 9,5; 5,3", &list_head)
if err := nr.verify(); err.Error() != s {
t.Error(nr, "should have errors", s, "but had", err)
}
}
func TestInjectAndSearch(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
nr := &element{nil, &list_head}
nr.inject(c(3))
nr.inject(c(5))
nr.inject(c(9))
nr.inject(c(7))
nr.inject(c(4))
nr.inject(c(8))
assertSlicey(t, nr, []Thing{&list_head, c(3), c(4), c(5), c(7), c(8), c(9)})
searchTest(t, nr, c(1), &list_head, nil, c(3))
searchTest(t, nr, c(2), &list_head, nil, c(3))
searchTest(t, nr, c(3), &list_head, c(3), c(4))
searchTest(t, nr, c(4), c(3), c(4), c(5))
searchTest(t, nr, c(5), c(4), c(5), c(7))
searchTest(t, nr, c(6), c(5), nil, c(7))
searchTest(t, nr, c(7), c(5), c(7), c(8))
searchTest(t, nr, c(8), c(7), c(8), c(9))
searchTest(t, nr, c(9), c(8), c(9), nil)
searchTest(t, nr, c(10), c(9), nil, nil)
searchTest(t, nr, c(11), c(9), nil, nil)
}
func TestConcInjectAndSearch(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
nr := &element{nil, &list_head}
nr.inject(c(3))
nr.inject(c(5))
nr.inject(c(9))
nr.inject(c(7))
nr.inject(c(4))
nr.inject(c(8))
assertSlicey(t, nr, []Thing{&list_head, c(3), c(4), c(5), c(7), c(8), c(9)})
do := make(chan bool)
ichan := make(chan []c)
var injected [][]c
for i := 0; i < runtime.NumCPU(); i++ {
go fiddleAndAssertSort(t, nr, do, ichan)
}
close(do)
for i := 0; i < runtime.NumCPU(); i++ {
searchTest(t, nr, c(1), ANY, ANY, c(3))
searchTest(t, nr, c(2), ANY, ANY, c(3))
searchTest(t, nr, c(3), ANY, c(3), c(4))
searchTest(t, nr, c(4), c(3), c(4), c(5))
searchTest(t, nr, c(5), c(4), c(5), c(7))
searchTest(t, nr, c(6), c(5), nil, c(7))
searchTest(t, nr, c(7), c(5), c(7), c(8))
searchTest(t, nr, c(8), c(7), c(8), c(9))
searchTest(t, nr, c(9), c(8), c(9), nil)
searchTest(t, nr, c(10), c(9), nil, nil)
searchTest(t, nr, c(11), c(9), nil, nil)
injected = append(injected, <-ichan)
}
assertSlicey(t, nr, []Thing{&list_head, c(3), c(4), c(5), c(7), c(8), c(9)})
imap := make(map[c]int)
for _, vals := range injected {
for _, val := range vals {
imap[val] = imap[val] + 1
}
}
for val, num := range imap {
if num2, ok := rmap[val]; ok {
if num2 != num {
t.Errorf("fiddlers injected %v of %v but removed %v", num, val, num2)
}
} else {
t.Errorf("fiddlers injected %v of %v but removed none", num, val)
}
}
for val, num := range rmap {
if num2, ok := imap[val]; ok {
if num2 != num {
t.Errorf("fiddlers removed %v of %v but injected %v", num, val, num2)
}
} else {
t.Errorf("fiddlers removed %v of %v but injected none", num, val)
}
}
}