generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrounding_test.go
138 lines (130 loc) · 3.89 KB
/
rounding_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package dosh_test
import (
. "github.com/dogmatiq/dosh"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)
var _ = Describe("type Amount (rounding methods)", func() {
Describe("func Floor()", func() {
DescribeTable(
"it returns an amount with the magnitude rounded down to the nearest integer",
func(a, expect string) {
Expect(
FromString("XYZ", a).Floor().EqualTo(
FromString("XYZ", expect),
),
).To(BeTrue())
},
Entry("zero", "0", "0"),
Entry("positive", "1.2", "1"),
Entry("negative", "-1.2", "-2"),
)
})
Describe("func Ceil()", func() {
DescribeTable(
"it returns an amount with the magnitude rounded up to the nearest integer",
func(a, expect string) {
Expect(
FromString("XYZ", a).Ceil().EqualTo(
FromString("XYZ", expect),
),
).To(BeTrue())
},
Entry("zero", "0", "0"),
Entry("positive", "1.2", "2"),
Entry("negative", "-1.2", "-1"),
)
})
Describe("func Truncate()", func() {
DescribeTable(
"it returns an amount with the magnitude truncated to the given number of decimal places",
func(a, expect string) {
Expect(
FromString("XYZ", a).Truncate(1).EqualTo(
FromString("XYZ", expect),
),
).To(BeTrue())
},
Entry("zero", "0", "0"),
Entry("positive", "1.23", "1.2"),
Entry("negative", "-1.23", "-1.2"),
)
})
Describe("func Round()", func() {
DescribeTable(
"it returns an amount with the magnitude rounded to the given number of decimal places",
func(a, expect string) {
Expect(
FromString("XYZ", a).Round(1).EqualTo(
FromString("XYZ", expect),
),
).To(BeTrue())
},
Entry("zero", "0", "0"),
Entry("positive (down)", "1.23", "1.2"),
Entry("positive (up)", "1.27", "1.3"),
Entry("positive (half)", "1.25", "1.3"),
Entry("negative (up)", "-1.23", "-1.2"),
Entry("negative (down)", "-1.27", "-1.3"),
Entry("negative (half)", "-1.25", "-1.3"),
)
DescribeTable(
"it returns an amount with the magnitude rounded to the given number of integer places when n is negative",
func(a, expect string) {
Expect(
FromString("XYZ", a).Round(-1).EqualTo(
FromString("XYZ", expect),
),
).To(BeTrue())
},
Entry("zero", "0", "0"),
Entry("positive (down)", "123", "120"),
Entry("positive (up)", "127", "130"),
Entry("positive (half)", "125", "130"),
Entry("negative (up)", "-123", "-120"),
Entry("negative (down)", "-127", "-130"),
Entry("negative (half)", "-125", "-130"),
)
})
Describe("func RoundBank()", func() {
DescribeTable(
"it returns an amount with the magnitude rounded to the given number of decimal places",
func(a, expect string) {
Expect(
FromString("XYZ", a).RoundBank(1).EqualTo(
FromString("XYZ", expect),
),
).To(BeTrue())
},
Entry("zero", "0", "0"),
Entry("positive (down)", "1.23", "1.2"),
Entry("positive (up)", "1.27", "1.3"),
Entry("positive (half, even)", "1.25", "1.2"),
Entry("positive (half, odd)", "1.15", "1.2"),
Entry("negative (up)", "-1.23", "-1.2"),
Entry("negative (down)", "-1.27", "-1.3"),
Entry("negative (half, even)", "-1.25", "-1.2"),
Entry("negative (half, odd)", "-1.15", "-1.2"),
)
DescribeTable(
"it returns an amount with the magnitude rounded to the given number of integer places when n is negative",
func(a, expect string) {
Expect(
FromString("XYZ", a).RoundBank(-1).EqualTo(
FromString("XYZ", expect),
),
).To(BeTrue())
},
Entry("zero", "0", "0"),
Entry("positive (down)", "123", "120"),
Entry("positive (up)", "127", "130"),
Entry("positive (half, even)", "125", "120"),
Entry("positive (half, odd)", "115", "120"),
Entry("negative (up)", "-123", "-120"),
Entry("negative (down)", "-127", "-130"),
Entry("negative (half, even)", "-125", "-120"),
Entry("negative (half, odd)", "-115", "-120"),
)
})
})