-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcround_test.go
123 lines (108 loc) · 3.24 KB
/
cround_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
// +build cgo
package cround
import "testing"
import "math"
func TestRound(t *testing.T) {
tests := []struct {
x float64
toNearestEven float64
toNearestAway float64
toZero float64
awayFromZero float64
toPositiveInf float64
toNegativeInf float64
}{
{+5.5, +6.0, +6.0, +5.0, +6.0, +6.0, +5.0},
{+2.5, +2.0, +3.0, +2.0, +3.0, +3.0, +2.0},
{+1.6, +2.0, +2.0, +1.0, +2.0, +2.0, +1.0},
{+1.1, +1.0, +1.0, +1.0, +2.0, +2.0, +1.0},
{+1.0, +1.0, +1.0, +1.0, +1.0, +1.0, +1.0},
{+0.0, +0.0, +0.0, +0.0, +0.0, +0.0, +0.0},
{-0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0},
{-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0},
{-1.1, -1.0, -1.0, -1.0, -2.0, -1.0, -2.0},
{-1.6, -2.0, -2.0, -1.0, -2.0, -1.0, -2.0},
{-2.5, -2.0, -3.0, -2.0, -3.0, -2.0, -3.0},
{-5.5, -6.0, -6.0, -5.0, -6.0, -5.0, -6.0},
{math.Inf(+1), math.Inf(+1), math.Inf(+1), math.Inf(+1), math.Inf(+1), math.Inf(+1), math.Inf(+1)},
{math.Inf(-1), math.Inf(-1), math.Inf(-1), math.Inf(-1), math.Inf(-1), math.Inf(-1), math.Inf(-1)},
}
dpTests := []struct {
x float64
dp float64
result float64
}{
{1234.5678, -4, 0000.0000},
{1234.5678, -3, 1000.0000},
{1234.5678, -2, 1200.0000},
{1234.5678, -1, 1230.0000},
{1234.5678, +0, 1235.0000},
{1234.5678, +1, 1234.6000},
{1234.5678, +2, 1234.5700},
{1234.5678, +3, 1234.5680},
{1234.5678, +4, 1234.5678},
}
t.Run("Round", func(t *testing.T) {
for _, tt := range tests {
result := Round(tt.x)
if result != tt.toNearestEven {
t.Errorf("Round(%.1f) = %.1f, expected %.1f", tt.x, result, tt.toNearestEven)
}
}
})
t.Run("RoundTo", func(t *testing.T) {
for _, tt := range tests {
result := RoundTo(tt.x, 0)
if result != tt.toNearestEven {
t.Errorf("RoundTo(%.1f, 0) = %.1f, expected %.1f", tt.x, result, tt.toNearestEven)
}
}
for _, tt := range dpTests {
result := RoundTo(tt.x, tt.dp)
if result != tt.result {
t.Errorf("RoundTo(%.4f, %.0f) = %.4f, expected %.4f", tt.x, tt.dp, result, tt.result)
}
}
})
t.Run("ToNearestEven", func(t *testing.T) {
for _, tt := range tests {
result := ToNearestEven(tt.x)
if result != tt.toNearestEven {
t.Errorf("ToNearestEven(%.1f) = %.1f, expected %.1f", tt.x, result, tt.toNearestEven)
}
}
})
t.Run("ToNearestAway", func(t *testing.T) {
for _, tt := range tests {
result := ToNearestAway(tt.x)
if result != tt.toNearestAway {
t.Errorf("ToNearestAway(%.1f) = %.1f, expected %.1f", tt.x, result, tt.toNearestAway)
}
}
})
t.Run("ToZero", func(t *testing.T) {
for _, tt := range tests {
result := ToZero(tt.x)
if result != tt.toZero {
t.Errorf("ToZero(%.1f) = %.1f, expected %.1f", tt.x, result, tt.toZero)
}
}
})
// There is no AwayFromZero equivalent in C, so it cannot be tested.
t.Run("ToPositiveInf", func(t *testing.T) {
for _, tt := range tests {
result := ToPositiveInf(tt.x)
if result != tt.toPositiveInf {
t.Errorf("ToPositiveInf(%.1f) = %.1f, expected %.1f", tt.x, result, tt.toPositiveInf)
}
}
})
t.Run("ToNegativeInf", func(t *testing.T) {
for _, tt := range tests {
result := ToNegativeInf(tt.x)
if result != tt.toNegativeInf {
t.Errorf("ToNegativeInf(%.1f) = %.1f, expected %.1f", tt.x, result, tt.toNegativeInf)
}
}
})
}