forked from flyingmutant/rapid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
floats_external_test.go
145 lines (124 loc) · 3.31 KB
/
floats_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
// Copyright 2019 Gregory Petrosyan <gregory.petrosyan@gmail.com>
//
// 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"
"sort"
"testing"
. "pgregory.net/rapid"
)
func TestFloatNoInf(t *testing.T) {
t.Parallel()
gens := []*Generator[any]{
Float32().AsAny(),
Float32Min(0).AsAny(),
Float32Max(0).AsAny(),
Float64().AsAny(),
Float64Min(0).AsAny(),
Float64Max(0).AsAny(),
}
for _, g := range gens {
t.Run(g.String(), MakeCheck(func(t *T) {
f := g.Draw(t, "f")
if math.IsInf(rv(f).Float(), 0) {
t.Fatalf("got infinity: %v", f)
}
}))
}
}
func TestFloatExamples(t *testing.T) {
gens := []*Generator[any]{
Float32().AsAny(),
Float32Min(-0.1).AsAny(),
Float32Min(1).AsAny(),
Float32Max(0.1).AsAny(),
Float32Max(2.5).AsAny(),
Float32Range(0.3, 0.30001).AsAny(),
Float32Range(0.3, 0.301).AsAny(),
Float32Range(0.3, 0.7).AsAny(),
Float32Range(math.E, math.Pi).AsAny(),
Float32Range(0, 1).AsAny(),
Float32Range(1, 2.5).AsAny(),
Float32Range(0, 100).AsAny(),
Float32Range(0, 10000).AsAny(),
Float64().AsAny(),
Float64Min(-0.1).AsAny(),
Float64Min(1).AsAny(),
Float64Max(0.1).AsAny(),
Float64Max(2.5).AsAny(),
Float64Range(0.3, 0.30000001).AsAny(),
Float64Range(0.3, 0.301).AsAny(),
Float64Range(0.3, 0.7).AsAny(),
Float64Range(math.E, math.Pi).AsAny(),
Float64Range(0, 1).AsAny(),
Float64Range(1, 2.5).AsAny(),
Float64Range(0, 100).AsAny(),
Float64Range(0, 10000).AsAny(),
}
for _, g := range gens {
t.Run(g.String(), func(t *testing.T) {
var vals []float64
var vals32 bool
for i := 0; i < 100; i++ {
f := g.Example()
_, vals32 = f.(float32)
vals = append(vals, rv(f).Float())
}
sort.Float64s(vals)
for _, f := range vals {
if vals32 {
t.Logf("%30g %10.3g % 5d % 20d % 16x", f, f, int(math.Log10(math.Abs(f))), int64(f), math.Float32bits(float32(f)))
} else {
t.Logf("%30g %10.3g % 5d % 20d % 16x", f, f, int(math.Log10(math.Abs(f))), int64(f), math.Float64bits(f))
}
}
})
}
}
func TestFloat32BoundCoverage(t *testing.T) {
t.Parallel()
Check(t, func(t *T) {
min := Float32().Draw(t, "min")
max := Float32().Draw(t, "max")
if min > max {
min, max = max, min
}
g := Float32Range(min, max)
var gotMin, gotMax, gotZero bool
for i := 0; i < 400; i++ {
f := g.Example(i)
gotMin = gotMin || f == min
gotMax = gotMax || f == max
gotZero = gotZero || f == 0
if gotMin && gotMax && (min > 0 || max < 0 || gotZero) {
return
}
}
t.Fatalf("[%v, %v]: got min %v, got max %v, got zero %v", min, max, gotMin, gotMax, gotZero)
})
}
func TestFloat64BoundCoverage(t *testing.T) {
t.Parallel()
Check(t, func(t *T) {
min := Float64().Draw(t, "min")
max := Float64().Draw(t, "max")
if min > max {
min, max = max, min
}
g := Float64Range(min, max)
var gotMin, gotMax, gotZero bool
for i := 0; i < 400; i++ {
f := g.Example(i)
gotMin = gotMin || f == min
gotMax = gotMax || f == max
gotZero = gotZero || f == 0
if gotMin && gotMax && (min > 0 || max < 0 || gotZero) {
return
}
}
t.Fatalf("[%v, %v]: got min %v, got max %v, got zero %v", min, max, gotMin, gotMax, gotZero)
})
}