Repository navigation
Expand file tree
/
Copy pathfloat_range_test.go
More file actions
56 lines (50 loc) · 1.87 KB
/
Copy pathfloat_range_test.go
File metadata and controls
56 lines (50 loc) · 1.87 KB
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
package gofakeit
import (
"math"
"testing"
)
func TestFloat32RangeLargeFiniteBounds(t *testing.T) {
for _, bounds := range [][2]float32{
{-math.MaxFloat32, math.MaxFloat32},
{-math.MaxFloat32, math.MaxFloat32 / 2},
{-math.MaxFloat32 / 2, math.MaxFloat32},
{math.MaxFloat32, -math.MaxFloat32},
} {
f := New(42)
low, high := math.Min(float64(bounds[0]), float64(bounds[1])), math.Max(float64(bounds[0]), float64(bounds[1]))
for i := 0; i < 100; i++ {
value := float64(f.Float32Range(bounds[0], bounds[1]))
if math.IsNaN(value) || math.IsInf(value, 0) || value < low || value > high {
t.Fatalf("Float32Range(%v, %v) = %v, want a finite value within the bounds", bounds[0], bounds[1], value)
}
}
}
}
func TestFloat64RangeLargeFiniteBounds(t *testing.T) {
for _, bounds := range [][2]float64{
{-math.MaxFloat64, math.MaxFloat64},
{-math.MaxFloat64, math.MaxFloat64 / 2},
{-math.MaxFloat64 / 2, math.MaxFloat64},
{math.MaxFloat64, -math.MaxFloat64},
} {
f := New(42)
low, high := math.Min(bounds[0], bounds[1]), math.Max(bounds[0], bounds[1])
for i := 0; i < 100; i++ {
value := f.Float64Range(bounds[0], bounds[1])
if math.IsNaN(value) || math.IsInf(value, 0) || value < low || value > high {
t.Fatalf("Float64Range(%v, %v) = %v, want a finite value within the bounds", bounds[0], bounds[1], value)
}
}
}
}
type zeroFloatRangeSource struct{}
func (zeroFloatRangeSource) Uint64() uint64 { return 0 }
func TestFloatRangeLargeFiniteBoundsZeroDraw(t *testing.T) {
f := NewFaker(zeroFloatRangeSource{}, false)
if got := f.Float32Range(-math.MaxFloat32, math.MaxFloat32); got != -math.MaxFloat32 {
t.Errorf("Float32Range with zero draw = %v, want %v", got, -math.MaxFloat32)
}
if got := f.Float64Range(-math.MaxFloat64, math.MaxFloat64); got != -math.MaxFloat64 {
t.Errorf("Float64Range with zero draw = %v, want %v", got, -math.MaxFloat64)
}
}