-
Notifications
You must be signed in to change notification settings - Fork 25
/
floats_test.go
120 lines (100 loc) · 3.03 KB
/
floats_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
// 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
import (
"math"
"testing"
)
func TestFloatConversionRoundtrip(t *testing.T) {
t.Parallel()
Check(t, func(t *T) {
u := uint32(t.s.drawBits(32))
f := math.Float32frombits(u)
if math.IsNaN(float64(f)) {
t.Skip("NaN") // we can get NaNs with different bit patterns back
}
g := float32(float64(f))
if g != f {
t.Fatalf("got %v (0x%x) back from %v (0x%x)", g, math.Float32bits(g), f, math.Float32bits(f))
}
})
}
func TestUfloat32FromParts(t *testing.T) {
t.Parallel()
Check(t, func(t *T) {
f := Float32Min(0).Draw(t, "f")
g := ufloat32FromParts(ufloat32Parts(f))
if g != f {
t.Fatalf("got %v (0x%x) back from %v (0x%x)", g, math.Float32bits(g), f, math.Float32bits(f))
}
})
}
func TestUfloat64FromParts(t *testing.T) {
t.Parallel()
Check(t, func(t *T) {
f := Float64Min(0).Draw(t, "f")
g := ufloat64FromParts(ufloat64Parts(f))
if g != f {
t.Fatalf("got %v (0x%x) back from %v (0x%x)", g, math.Float64bits(g), f, math.Float64bits(f))
}
})
}
func TestGenUfloat32Range(t *testing.T) {
t.Parallel()
Check(t, func(t *T) {
min := Float32Min(0).Draw(t, "min")
max := Float32Min(0).Draw(t, "max")
if min > max {
min, max = max, min
}
f := ufloat32FromParts(genUfloatRange(t.s, float64(min), float64(max), float32SignifBits))
if f < min || f > max {
t.Fatalf("%v (0x%x) outside of [%v, %v] ([0x%x, 0x%x])", f, math.Float32bits(f), min, max, math.Float32bits(min), math.Float32bits(max))
}
})
}
func TestGenUfloat64Range(t *testing.T) {
t.Parallel()
Check(t, func(t *T) {
min := Float64Min(0).Draw(t, "min")
max := Float64Min(0).Draw(t, "max")
if min > max {
min, max = max, min
}
f := ufloat64FromParts(genUfloatRange(t.s, min, max, float64SignifBits))
if f < min || f > max {
t.Fatalf("%v (0x%x) outside of [%v, %v] ([0x%x, 0x%x])", f, math.Float64bits(f), min, max, math.Float64bits(min), math.Float64bits(max))
}
})
}
func TestGenFloat32Range(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
}
f := float32FromParts(genFloatRange(t.s, float64(min), float64(max), float32SignifBits))
if f < min || f > max {
t.Fatalf("%v (0x%x) outside of [%v, %v] ([0x%x, 0x%x])", f, math.Float32bits(f), min, max, math.Float32bits(min), math.Float32bits(max))
}
})
}
func TestGenFloat64Range(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
}
f := float64FromParts(genFloatRange(t.s, min, max, float64SignifBits))
if f < min || f > max {
t.Fatalf("%v (0x%x) outside of [%v, %v] ([0x%x, 0x%x])", f, math.Float64bits(f), min, max, math.Float64bits(min), math.Float64bits(max))
}
})
}