-
Notifications
You must be signed in to change notification settings - Fork 1
/
ranges_properties_test.go
200 lines (185 loc) · 5.92 KB
/
ranges_properties_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
package o_test
import (
"fmt"
"testing"
"github.com/antifuchs/o"
"github.com/leanovate/gopter"
"github.com/leanovate/gopter/gen"
"github.com/leanovate/gopter/prop"
)
func TestPropFIFOandLIFOMatch(t *testing.T) {
params := gopter.DefaultTestParameters()
params.MinSuccessfulTests = 1000
properties := gopter.NewProperties(params)
properties.Property("Ranges in scanners match", prop.ForAll(
func(cap, overage uint) string {
ring := o.NewRing(cap)
insert := cap + overage
for i := uint(0); i < insert; i++ {
ring.ForcePush()
}
if ring.Size() != cap {
return "Size does not match cap"
}
fifo := make([]uint, 0, cap)
lifo := make([]uint, 0, cap)
s := o.ScanLIFO(ring)
for i := 0; s.Next(); i++ {
lifo = append(lifo, s.Value())
}
s = o.ScanFIFO(ring)
for i := 0; s.Next(); i++ {
fifo = append(fifo, s.Value())
}
if len(lifo) != len(fifo) {
return "Length mismatch between lifo&fifo order"
}
if len(lifo) == 0 {
// nothing else to check
return ""
}
last := fifo[0]
for nth := range fifo {
if fifo[nth] != lifo[len(lifo)-1-nth] {
return fmt.Sprintf("lifo / fifo mismatch:\n%#v\n%#v", fifo, lifo)
}
if nth > 0 && ring.Mask(last+1) != fifo[nth] {
return fmt.Sprintf("indexes not continuous: %#v", fifo)
}
last = fifo[nth]
}
return ""
},
gen.UIntRange(0, 2000).WithLabel("ring size"),
gen.UIntRange(1, 100).WithLabel("overflow"),
))
properties.TestingRun(t)
}
func TestPropPushN(t *testing.T) {
params := gopter.DefaultTestParameters()
params.MinSuccessfulTests = 10000
properties := gopter.NewProperties(params)
properties.Property("Pushing N elements", prop.ForAll(
func(cap, fill, read, reserve uint) string {
ring := o.NewRing(cap)
var startIdx uint
for i := uint(0); i < fill; i++ {
startIdx = ring.Mask(ring.ForcePush() + 1)
}
for i := uint(0); i < read; i++ {
ring.Shift()
}
startSize := ring.Size()
overflows := startSize+reserve > cap
first, second, err := ring.PushN(reserve)
reservedAny := !first.Empty() || !second.Empty()
if overflows && err == nil {
return "expected error"
}
if !overflows && err != nil {
return "unexpected error"
}
if overflows && reservedAny {
return fmt.Sprintf("would overflow, but reserved %d elements:\n%#v %#v",
first.Length()+second.Length(), first, second)
}
if !overflows && first.Length()+second.Length() != reserve {
return fmt.Sprintf("did not reserve %d elements:\n%#v %#v",
reserve, first, second)
}
if reservedAny && startIdx != first.Start {
return fmt.Sprintf("expected reservation to start at %d, but %#v",
startIdx, first)
}
if !second.Empty() && first.End != cap {
return fmt.Sprintf("bad end bound on first range: %d expected, but %#v",
cap, first)
}
if !second.Empty() && second.Start != 0 {
return fmt.Sprintf("bad start bound on second range: 0 expected, but %#v",
second)
}
if !second.Empty() && !overflows && second.End != reserve-first.Length() {
return fmt.Sprintf("bad end bound on second range: %d expected, but %#v %#v",
reserve-first.Length(), first, second)
}
if !second.Empty() && overflows && second.End != cap-startSize-first.Length() {
return fmt.Sprintf("bad end bound on overflowing second range: %d expected, but %#v %#v",
cap-startSize-first.Length(), first, second)
}
return ""
},
gen.UIntRange(0, 2000).WithLabel("ring size"),
gen.UIntRange(0, 100).WithLabel("elements to fill in"),
gen.UIntRange(0, 100).WithLabel("elements to read"),
gen.UIntRange(0, 100).WithLabel("elements to reserve"),
))
properties.TestingRun(t)
}
func TestPropShiftN(t *testing.T) {
params := gopter.DefaultTestParameters()
params.MinSuccessfulTests = 10000
properties := gopter.NewProperties(params)
properties.Property("Shifting N elements", prop.ForAll(
func(cap, fill, skip, read uint) string {
ring := o.NewRing(cap)
for i := uint(0); i < fill; i++ {
ring.ForcePush()
}
var startIdx uint
if fill > cap && cap > 0 {
startIdx = ring.Mask(fill - cap)
}
for i := uint(0); i < skip; i++ {
idx, err := ring.Shift()
if err == nil {
startIdx = ring.Mask(idx + 1)
}
}
startSize := ring.Size()
first, second, err := ring.ShiftN(read)
overflows := read > cap || read > startSize
readAny := !first.Empty() || !second.Empty()
if overflows && err == nil {
return "expected error"
}
if !overflows && err != nil {
return "unexpected error"
}
if overflows && readAny {
return fmt.Sprintf("would overflow, but read %d elements:\n%#v %#v",
first.Length()+second.Length(), first, second)
}
if !overflows && first.Length()+second.Length() != read {
return fmt.Sprintf("did not read %d elements:\n%#v %#v",
read, first, second)
}
if readAny && startIdx != first.Start {
return fmt.Sprintf("expected reservation to start at %d, but %#v",
startIdx, first)
}
if !second.Empty() && first.End != cap {
return fmt.Sprintf("bad end bound on first range: %d expected, but %#v",
cap, first)
}
if !second.Empty() && second.Start != 0 {
return fmt.Sprintf("bad start bound on second range: 0 expected, but %#v",
second)
}
if !second.Empty() && !overflows && second.End != read-first.Length() {
return fmt.Sprintf("bad end bound on second range: %d expected, but %#v %#v",
read-first.Length(), first, second)
}
if !second.Empty() && overflows && second.End != cap-startSize-first.Length() {
return fmt.Sprintf("bad end bound on overflowing second range: %d expected, but %#v %#v",
cap-startSize-first.Length(), first, second)
}
return ""
},
gen.UIntRange(0, 2000).WithLabel("ring size"),
gen.UIntRange(0, 100).WithLabel("elements to fill in"),
gen.UIntRange(0, 100).WithLabel("elements to skip before reading"),
gen.UIntRange(0, 100).WithLabel("elements to read"),
))
properties.TestingRun(t)
}