Debian Codegraph @

debian/rust-vendor/fixed_decimal/tests/rounding.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 use core::ops::RangeInclusive;
6 use fixed_decimal::UnsignedDecimal;
7 use fixed_decimal::UnsignedRoundingMode;
8 use writeable::Writeable;
9
10 #[test]
11 pub fn test_ecma402_table() {
12 // Source: <https://tc39.es/ecma402/#table-intl-rounding-modes>
13 let cases: [(_, _, _, _, _, _); 5] = [
14 ("expand", UnsignedRoundingMode::Expand, 1, 1, 1, 2),
15 ("trunc", UnsignedRoundingMode::Trunc, 0, 0, 0, 1),
16 ("half_expand", UnsignedRoundingMode::HalfExpand, 0, 1, 1, 2),
17 ("half_trunc", UnsignedRoundingMode::HalfTrunc, 0, 0, 1, 1),
18 ("half_even", UnsignedRoundingMode::HalfEven, 0, 0, 1, 2),
19 ];
20 for (name, mode, e2, e3, e4, e5) in cases {
21 let mut fd2: UnsignedDecimal = "0.4".parse().unwrap();
22 let mut fd3: UnsignedDecimal = "0.5".parse().unwrap();
23 let mut fd4: UnsignedDecimal = "0.6".parse().unwrap();
24 let mut fd5: UnsignedDecimal = "1.5".parse().unwrap();
25 fd2.round_with_mode(0, mode);
26 fd3.round_with_mode(0, mode);
27 fd4.round_with_mode(0, mode);
28 fd5.round_with_mode(0, mode);
29 assert_eq!(
30 fd2.write_to_string(),
31 e2.write_to_string(),
32 "0.4 failed for {name}"
33 );
34 assert_eq!(
35 fd3.write_to_string(),
36 e3.write_to_string(),
37 "0.5 failed for {name}"
38 );
39 assert_eq!(
40 fd4.write_to_string(),
41 e4.write_to_string(),
42 "0.6 failed for {name}"
43 );
44 assert_eq!(
45 fd5.write_to_string(),
46 e5.write_to_string(),
47 "1.5 failed for {name}"
48 );
49 }
50 }
51
52 #[test]
53 pub fn test_within_ranges() {
54 struct TestCase {
55 rounding_mode_name: &'static str,
56 rounding_mode: UnsignedRoundingMode,
57 range_0: RangeInclusive<u32>,
58 range_1000: RangeInclusive<u32>,
59 range_2000: RangeInclusive<u32>,
60 }
61 let cases: [TestCase; 5] = [
62 TestCase {
63 rounding_mode_name: "expand",
64 rounding_mode: UnsignedRoundingMode::Expand,
65 range_0: 0..=0,
66 range_1000: 1..=1000,
67 range_2000: 1001..=2000,
68 },
69 TestCase {
70 rounding_mode_name: "trunc",
71 rounding_mode: UnsignedRoundingMode::Trunc,
72 range_0: 0..=999,
73 range_1000: 1000..=1999,
74 range_2000: 2000..=2999,
75 },
76 TestCase {
77 rounding_mode_name: "half_expand",
78 rounding_mode: UnsignedRoundingMode::HalfExpand,
79 range_0: 0..=449,
80 range_1000: 500..=1449,
81 range_2000: 1500..=2449,
82 },
83 TestCase {
84 rounding_mode_name: "half_trunc",
85 rounding_mode: UnsignedRoundingMode::HalfTrunc,
86 range_0: 0..=500,
87 range_1000: 501..=1500,
88 range_2000: 1501..=2500,
89 },
90 TestCase {
91 rounding_mode_name: "half_even",
92 rounding_mode: UnsignedRoundingMode::HalfEven,
93 range_0: 0..=500,
94 range_1000: 501..=1449,
95 range_2000: 1500..=2500,
96 },
97 ];
98 for TestCase {
99 rounding_mode_name,
100 rounding_mode,
101 range_0,
102 range_1000,
103 range_2000,
104 } in cases
105 {
106 for n in range_0 {
107 let mut fd = UnsignedDecimal::from(n);
108 fd.round_with_mode(3, rounding_mode);
109 assert_eq!(fd.write_to_string(), "000", "{rounding_mode_name}: {n}");
110 let (mut fd, expected) = (
111 UnsignedDecimal::from(n + 1000000).multiplied_pow10(-5),
112 "10.00",
113 );
114 fd.round_with_mode(-2, rounding_mode);
115 assert_eq!(
116 fd.write_to_string(),
117 expected,
118 "{rounding_mode_name}: {n} ÷ 10^5 ± 10"
119 );
120 }
121 for n in range_1000 {
122 let mut fd = UnsignedDecimal::from(n);
123 fd.round_with_mode(3, rounding_mode);
124 assert_eq!(fd.write_to_string(), "1000", "{rounding_mode_name}: {n}");
125 let mut fd = UnsignedDecimal::from(n + 1000000).multiplied_pow10(-5);
126 fd.round_with_mode(-2, rounding_mode);
127 assert_eq!(
128 fd.write_to_string(),
129 "10.01",
130 "{rounding_mode_name}: {n} ÷ 10^5 ± 10"
131 );
132 }
133 for n in range_2000 {
134 let mut fd = UnsignedDecimal::from(n);
135 fd.round_with_mode(3, rounding_mode);
136 assert_eq!(fd.write_to_string(), "2000", "{rounding_mode_name}: {n}");
137 let mut fd = UnsignedDecimal::from(n + 1000000).multiplied_pow10(-5);
138 fd.round_with_mode(-2, rounding_mode);
139 assert_eq!(
140 fd.write_to_string(),
141 "10.02",
142 "{rounding_mode_name}: {n} ÷ 10^5 ± 10"
143 );
144 }
145 }
146 }
147
148 #[test]
149 pub fn extra_rounding_mode_cases() {
150 struct TestCase {
151 input: &'static str,
152 position: i16,
153 // expand, trunc, half_expand, half_trunc, half_even
154 all_expected: [&'static str; 5],
155 }
156 let cases: [TestCase; 8] = [
157 TestCase {
158 input: "505.050",
159 position: -3,
160 all_expected: ["505.050", "505.050", "505.050", "505.050", "505.050"],
161 },
162 TestCase {
163 input: "505.050",
164 position: -2,
165 all_expected: ["505.05", "505.05", "505.05", "505.05", "505.05"],
166 },
167 TestCase {
168 input: "505.050",
169 position: -1,
170 all_expected: ["505.1", "505.0", "505.1", "505.0", "505.0"],
171 },
172 TestCase {
173 input: "505.050",
174 position: 0,
175 all_expected: ["506", "505", "505", "505", "505"],
176 },
177 TestCase {
178 input: "505.050",
179 position: 1,
180 all_expected: ["510", "500", "510", "510", "510"],
181 },
182 TestCase {
183 input: "505.050",
184 position: 2,
185 all_expected: ["600", "500", "500", "500", "500"],
186 },
187 TestCase {
188 input: "505.050",
189 position: 3,
190 all_expected: ["1000", "000", "1000", "1000", "1000"],
191 },
192 TestCase {
193 input: "505.050",
194 position: 4,
195 all_expected: ["10000", "0000", "0000", "0000", "0000"],
196 },
197 ];
198 let rounding_modes: [(&'static str, UnsignedRoundingMode); 5] = [
199 ("expand", UnsignedRoundingMode::Expand),
200 ("trunc", UnsignedRoundingMode::Trunc),
201 ("half_expand", UnsignedRoundingMode::HalfExpand),
202 ("half_trunc", UnsignedRoundingMode::HalfTrunc),
203 ("half_even", UnsignedRoundingMode::HalfEven),
204 ];
205 for TestCase {
206 input,
207 position,
208 all_expected,
209 } in cases
210 {
211 for ((rounding_mode_name, rounding_mode), expected) in
212 rounding_modes.iter().zip(all_expected.iter())
213 {
214 let mut fd: UnsignedDecimal = input.parse().unwrap();
215 fd.round_with_mode(position, *rounding_mode);
216 assert_eq!(
217 &*fd.write_to_string(),
218 *expected,
219 "{input}: {rounding_mode_name} @ {position}"
220 )
221 }
222 }
223 }
224
225 #[test]
226 pub fn test_ecma402_table_with_increments() {
227 use fixed_decimal::RoundingIncrement;
228
229 #[rustfmt::skip] // Don't split everything on its own line. Makes it look a lot nicer.
230 let cases: [(_, _, [(_, _, _, _, _, _); 5]); 3] = [
231 ("two", RoundingIncrement::MultiplesOf2, [
232 ("expand", UnsignedRoundingMode::Expand, "0.4", "0.6", "0.6", "1.6"),
233 ("trunc", UnsignedRoundingMode::Trunc, "0.4", "0.4", "0.6", "1.4"),
234 ("half_expand", UnsignedRoundingMode::HalfExpand, "0.4", "0.6", "0.6", "1.6"),
235 ("half_trunc", UnsignedRoundingMode::HalfTrunc, "0.4", "0.4", "0.6", "1.4"),
236 ("half_even", UnsignedRoundingMode::HalfEven, "0.4", "0.4", "0.6", "1.6"),
237 ]),
238 ("five", RoundingIncrement::MultiplesOf5, [
239 ("expand", UnsignedRoundingMode::Expand, "0.5", "0.5", "1.0", "1.5"),
240 ("trunc", UnsignedRoundingMode::Trunc, "0.0", "0.5", "0.5", "1.5"),
241 ("half_expand", UnsignedRoundingMode::HalfExpand, "0.5", "0.5", "0.5", "1.5"),
242 ("half_trunc", UnsignedRoundingMode::HalfTrunc, "0.5", "0.5", "0.5", "1.5"),
243 ("half_even", UnsignedRoundingMode::HalfEven, "0.5", "0.5", "0.5", "1.5"),
244 ]),
245 ("twenty-five", RoundingIncrement::MultiplesOf25, [
246 ("expand", UnsignedRoundingMode::Expand, "2.5", "2.5", "2.5", "2.5"),
247 ("trunc", UnsignedRoundingMode::Trunc, "0.0", "0.0", "0.0", "0.0"),
248 ("half_expand", UnsignedRoundingMode::HalfExpand, "0.0", "0.0", "0.0", "2.5"),
249 ("half_trunc", UnsignedRoundingMode::HalfTrunc, "0.0", "0.0", "0.0", "2.5"),
250 ("half_even", UnsignedRoundingMode::HalfEven, "0.0", "0.0", "0.0", "2.5"),
251 ]),
252 ];
253
254 for (increment_str, increment, cases) in cases {
255 for (rounding_mode_name, rounding_mode, e2, e3, e4, e5) in cases {
256 let mut fd2: UnsignedDecimal = "0.4".parse().unwrap();
257 let mut fd3: UnsignedDecimal = "0.5".parse().unwrap();
258 let mut fd4: UnsignedDecimal = "0.6".parse().unwrap();
259 let mut fd5: UnsignedDecimal = "1.5".parse().unwrap();
260 // The original ECMA-402 table tests rounding at magnitude 0.
261 // However, testing rounding at magnitude -1 gives more
262 // interesting test cases for increments.
263 fd2.round_with_mode_and_increment(-1, rounding_mode, increment);
264 fd3.round_with_mode_and_increment(-1, rounding_mode, increment);
265 fd4.round_with_mode_and_increment(-1, rounding_mode, increment);
266 fd5.round_with_mode_and_increment(-1, rounding_mode, increment);
267
268 assert_eq!(
269 fd2.write_to_string(),
270 e2,
271 "0.4 failed for {rounding_mode_name} with increments of {increment_str}"
272 );
273 assert_eq!(
274 fd3.write_to_string(),
275 e3,
276 "0.5 failed for {rounding_mode_name} with increments of {increment_str}"
277 );
278 assert_eq!(
279 fd4.write_to_string(),
280 e4,
281 "0.6 failed for {rounding_mode_name} with increments of {increment_str}"
282 );
283 assert_eq!(
284 fd5.write_to_string(),
285 e5,
286 "1.5 failed for {rounding_mode_name} with increments of {increment_str}"
287 );
288 }
289 }
290 }