-
Notifications
You must be signed in to change notification settings - Fork 25
/
failure_external_test.go
173 lines (140 loc) · 3.63 KB
/
failure_external_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
// Copyright 2019 Gregory Petrosyan <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid_test
import (
"math"
"testing"
. "pgregory.net/rapid"
)
// wrapper to test (*T).Helper()
func fatalf(t *T, format string, args ...any) {
t.Helper()
t.Fatalf(format, args...)
}
func TestFailure_ImpossibleData(t *testing.T) {
t.Skip("expected failure")
Check(t, func(t *T) {
_ = Int().Filter(func(i int) bool { return false }).Draw(t, "i")
})
}
func TestFailure_Trivial(t *testing.T) {
t.Skip("expected failure")
Check(t, func(t *T) {
i := Int().Draw(t, "i")
if i > 1000000000 {
fatalf(t, "got a huge integer: %v", i)
}
})
}
func TestFailure_SimpleCollection(t *testing.T) {
t.Skip("expected failure")
Check(t, func(t *T) {
s := SliceOf(Int().Filter(func(i int) bool { return i%2 == -1 })).Draw(t, "s")
if len(s) > 3 {
fatalf(t, "got a long sequence: %v", s)
}
})
}
func TestFailure_CollectionElements(t *testing.T) {
t.Skip("expected failure")
Check(t, func(t *T) {
s := SliceOfN(Int(), 2, -1).Draw(t, "s")
n := 0
for _, i := range s {
if i > 1000000 {
n++
}
}
if n > 1 {
fatalf(t, "got %v huge elements", n)
}
})
}
func TestFailure_TrivialString(t *testing.T) {
t.Skip("expected failure")
Check(t, func(t *T) {
s := String().Draw(t, "s")
if len(s) > 7 {
fatalf(t, "got bad string %v", s)
}
})
}
func TestFailure_Make(t *testing.T) {
t.Skip("expected failure")
Check(t, func(t *T) {
n := IntMin(0).Draw(t, "n")
_ = make([]int, n)
})
}
func TestFailure_Mean(t *testing.T) {
t.Skip("expected failure")
Check(t, func(t *T) {
s := SliceOf(Float64()).Draw(t, "s")
mean := 0.0
for _, f := range s {
mean += f
}
mean /= float64(len(s))
min, max := math.Inf(0), math.Inf(-1)
for _, f := range s {
if f < min {
min = f
}
if f > max {
max = f
}
}
if mean < min || mean > max {
t.Fatalf("got mean %v for range [%v, %v]", mean, min, max)
}
})
}
func TestFailure_ExampleParseDate(t *testing.T) {
t.Skip("expected failure")
Check(t, testParseDate)
}
func TestFailure_ExampleQueue(t *testing.T) {
t.Skip("expected failure")
Check(t, testQueue)
}
// LastIndex returns the index of the last instance of x in list, or
// -1 if x is not present. The loop condition has a fault that
// causes some tests to fail. Change it to i >= 0 to see them pass.
func LastIndex(list []int, x int) int {
for i := len(list) - 1; i > 0; i-- {
if list[i] == x {
return i
}
}
return -1
}
// This can be a good example of property-based test; however, it is unclear
// what is the "best" way to generate input. Either it is concise (like in
// the test below), but requires high quality data generation (and even then
// is flaky), or it can be verbose, explicitly covering important input classes --
// however, how do we know them when writing a test?
func TestFailure_LastIndex(t *testing.T) {
t.Skip("expected failure (flaky)")
Check(t, func(t *T) {
s := SliceOf(Int()).Draw(t, "s")
x := Int().Draw(t, "x")
ix := LastIndex(s, x)
// index is either -1 or in bounds
if ix != -1 && (ix < 0 || ix >= len(s)) {
t.Fatalf("%v is not a valid last index", ix)
}
// index is either -1 or a valid index of x
if ix != -1 && s[ix] != x {
t.Fatalf("%v is not a valid index of %v", ix, x)
}
// no valid index of x is bigger than ix
for i := ix + 1; i < len(s); i++ {
if s[i] == x {
t.Fatalf("%v is not the last index of %v (%v is bigger)", ix, x, i)
}
}
})
}