-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfluky_float_test.go
More file actions
82 lines (71 loc) · 3.4 KB
/
fluky_float_test.go
File metadata and controls
82 lines (71 loc) · 3.4 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
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
package fluky
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestFluky_Float_NoOptions(t *testing.T) {
mRng := new(RngMock)
mRng.On("Float64").Return(0.31415).Times(1)
mRng.On("Float64").Return(0.11543).Times(1)
mRng.On("Float64").Return(0.123456789).Times(1)
// ref - https://play.golang.com/p/JZWbrcw0_fh
mRng.On("Float64").Return(0.99999999999999988897769753748434595763683319091796875).Times(1)
mRng.On("Float64").Return(0.00000000000000011102230246251565404236316680908203125).Times(1)
f := NewFluky(mRng)
assert.Equal(t, 0.31415, f.Float())
assert.Equal(t, 0.11543, f.Float())
assert.Equal(t, 0.123456789, f.Float())
assert.Equal(t, 0.99999999999999988897769753748434595763683319091796875, f.Float())
assert.Equal(t, 0.00000000000000011102230246251565404236316680908203125, f.Float())
mRng.AssertExpectations(t)
}
func TestFluky_Float_WithOptions_Precision(t *testing.T) {
mRng := new(RngMock)
mRng.On("Float64").Return(0.31415).Times(1)
mRng.On("Float64").Return(0.11543).Times(1)
mRng.On("Float64").Return(0.123456789).Times(1)
mRng.On("Float64").Return(0.99999999999999988897769753748434595763683319091796875).Times(1)
mRng.On("Float64").Return(0.00000000000000011102230246251565404236316680908203125)
f := NewFluky(mRng)
assert.Equal(t, 0.314, f.Float(WithPrecision(3)))
assert.Equal(t, 0.1154, f.Float(WithPrecision(4)))
assert.Equal(t, 0.123457, f.Float(WithPrecision(6)))
expected := 0.9999999999999998889777
actual := f.Float(WithPrecision(20))
assert.Equal(t, expected, actual, "%.53f != %.53f", expected, actual)
assert.Equal(t, 0.0000000000000001, f.Float(WithPrecision(16)))
assert.Equal(t, 0.00000000000000011, f.Float(WithPrecision(17)))
assert.Equal(t, 0.000000000000000111, f.Float(WithPrecision(18)))
assert.Equal(t, 0.0000000000000001110, f.Float(WithPrecision(19)))
assert.Equal(t, 0.00000000000000011102, f.Float(WithPrecision(20)))
assert.Equal(t, 0.00000000000000011102, f.Float(WithPrecision(21+20)))
mRng.AssertExpectations(t)
}
func TestFluky_Float_WithOptions_MinMax(t *testing.T) {
mRng := new(RngMock)
mRng.On("Float64").Return(0.31415).Times(1)
mRng.On("Float64").Return(0.11543).Times(1)
mRng.On("Float64").Return(0.123456789).Times(1)
mRng.On("Float64").Return(0.99999999999999988897769753748434595763683319091796875).Times(1)
mRng.On("Float64").Return(0.00000000000000011102230246251565404236316680908203125).Times(1)
f := NewFluky(mRng)
assert.InDelta(t, -0.7434, f.Float(WithFloatRange(-2, 2)), 1e-15)
assert.InDelta(t, -1.53828, f.Float(WithFloatRange(-2, 2)), 1e-15)
assert.InDelta(t, -1.506172844, f.Float(WithFloatRange(-2, 2)), 1e-15)
assert.InDelta(t, 1.9999999999999996, f.Float(WithFloatRange(-2, 2)), 1e-15)
assert.InDelta(t, -1.9999999999999996, f.Float(WithFloatRange(-2, 2)), 1e-15)
mRng.AssertExpectations(t)
}
func TestFluky_Float_WithOptions(t *testing.T) {
mRng := new(RngMock)
mRng.On("Float64").Return(0.95).Times(1)
mRng.On("Float64").Return(0.31415).Times(1)
mRng.On("Float64").Return(0.11543).Times(1)
mRng.On("Float64").Return(0.123456789).Times(1)
f := NewFluky(mRng)
assert.InDelta(t, 2, f.Float(WithPrecision(0), WithFloatRange(-2, 2)), 1e-15)
assert.InDelta(t, -0.74, f.Float(WithPrecision(2), WithFloatRange(-2, 2)), 1e-15)
assert.InDelta(t, -1.5383, f.Float(WithPrecision(4), WithFloatRange(-2, 2)), 1e-15)
assert.InDelta(t, -1.50617284, f.Float(WithPrecision(8), WithFloatRange(-2, 2)), 1e-15)
mRng.AssertExpectations(t)
}