-
Notifications
You must be signed in to change notification settings - Fork 1
/
round_test.go
118 lines (106 loc) · 3.44 KB
/
round_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
package dec
import "testing"
type roundTestCase struct {
n Decimal // number.
r Precision // round rescaleTo.
e string // expected.
}
func Test_RoundHalf(t *testing.T) {
const (
num = "1.123456789"
neg = "-1.123456789"
)
for _, tc := range []roundTestCase{
{n: Nano.MustParse(num), r: 5, e: "1.12346"},
{n: Nano.MustParse(num), r: 4, e: "1.1235"},
{n: Nano.MustParse(num), r: 3, e: "1.123"},
{n: Nano.MustParse(num), r: 0, e: "1"},
{n: Nano.MustParse(neg), r: 5, e: "-1.12346"},
{n: Nano.MustParse(neg), r: 4, e: "-1.1235"},
{n: Nano.MustParse(neg), r: 3, e: "-1.123"},
{n: Nano.MustParse(neg), r: 0, e: "-1"},
} {
if got, expected := tc.n.Round(tc.r, HalfUp).String(), tc.e; got != expected {
t.Fatalf("invalid Round Up, expected %s, got %s", expected, got)
}
}
}
func Test_RoundHalfUp(t *testing.T) {
for _, tc := range []roundTestCase{
{n: Nano.MustParse("1.555555555"), r: 8, e: "1.55555556"},
{n: Nano.MustParse("1.555555555"), r: 7, e: "1.5555556"},
{n: Nano.MustParse("-1.555555555"), r: 8, e: "-1.55555555"},
{n: Nano.MustParse("-1.555555555"), r: 7, e: "-1.5555556"},
{n: Milli.MustParse("1.5"), r: 0, e: "2"},
{n: Milli.MustParse("-1.5"), r: 0, e: "-1"},
} {
if b := tc.e == "1"; b {
_ = b
}
if got, expected := tc.n.Round(tc.r, HalfUp).String(), tc.e; got != expected {
t.Fatalf("invalid Round Up, expected %s, got %s", expected, got)
}
}
}
func Test_RoundHalfDown(t *testing.T) {
for _, tc := range []roundTestCase{
{n: Nano.MustParse("1.555555555"), r: 8, e: "1.55555555"},
{n: Nano.MustParse("1.555555555"), r: 7, e: "1.5555556"},
{n: Nano.MustParse("-1.555555555"), r: 8, e: "-1.55555556"},
{n: Nano.MustParse("-1.555555555"), r: 7, e: "-1.5555556"},
{n: Milli.MustParse("1.5"), r: 0, e: "1"},
{n: Milli.MustParse("-1.5"), r: 0, e: "-2"},
} {
if b := tc.e == "1"; b {
_ = b
}
if got, expected := tc.n.Round(tc.r, HalfDown).String(), tc.e; got != expected {
t.Fatalf("invalid Round Up, expected %s, got %s", expected, got)
}
}
}
func Test_RoundModeToZero(t *testing.T) {
const (
ns = "1.123456789"
nns = "-1.123456789"
)
for _, tc := range []roundTestCase{
{n: Nano.MustParse(ns), r: 5, e: "1.12345"},
{n: Nano.MustParse(ns), r: 4, e: "1.1234"},
{n: Nano.MustParse(ns), r: 3, e: "1.123"},
{n: Nano.MustParse(ns), r: 0, e: "1"},
{n: Nano.MustParse(nns), r: 5, e: "-1.12345"},
{n: Nano.MustParse(nns), r: 4, e: "-1.1234"},
{n: Nano.MustParse(nns), r: 3, e: "-1.123"},
{n: Nano.MustParse(nns), r: 0, e: "-1"},
} {
if got, expected := tc.n.Round(tc.r, ToZero).String(), tc.e; got != expected {
t.Fatalf("invalid Round, expected %s, got %s", expected, got)
}
}
}
func Test_RoundModeAwayFromZero(t *testing.T) {
const (
ns = "1.123456789"
nns = "-1.123456789"
)
for _, tc := range []roundTestCase{
{n: Nano.MustParse(ns), r: 5, e: "1.12346"},
{n: Nano.MustParse(ns), r: 4, e: "1.1235"},
{n: Nano.MustParse(ns), r: 3, e: "1.124"},
{n: Nano.MustParse(ns), r: 2, e: "1.13"},
{n: Nano.MustParse(ns), r: 0, e: "2"},
{n: Nano.MustParse(nns), r: 5, e: "-1.12346"},
{n: Nano.MustParse(nns), r: 4, e: "-1.1235"},
{n: Nano.MustParse(nns), r: 3, e: "-1.124"},
{n: Nano.MustParse(nns), r: 2, e: "-1.13"},
{n: Nano.MustParse(nns), r: 0, e: "-2"},
} {
if b := tc.e == "-1.12346"; b {
_ = b
}
if got, expected := tc.n.Round(tc.r, AwayFromZero).String(), tc.e; got != expected {
t.Fatalf("invalid Round, expected %s, got %s", expected, got)
}
}
}