LEFT | RIGHT |
(no file at all) | |
1 // © 2017 and later: Unicode, Inc. and others. | 1 // © 2017 and later: Unicode, Inc. and others. |
2 // License & terms of use: http://www.unicode.org/copyright.html#License | 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
3 package com.ibm.icu.dev.test.number; | 3 package com.ibm.icu.dev.test.number; |
4 | 4 |
5 import java.math.BigDecimal; | 5 import java.math.BigDecimal; |
| 6 import java.math.BigInteger; |
6 import java.math.MathContext; | 7 import java.math.MathContext; |
7 import java.math.RoundingMode; | 8 import java.math.RoundingMode; |
8 import java.text.ParseException; | 9 import java.text.ParseException; |
9 import java.util.ArrayList; | 10 import java.util.ArrayList; |
10 import java.util.List; | 11 import java.util.List; |
11 import java.util.Random; | 12 import java.util.Random; |
12 | 13 |
13 import org.junit.Ignore; | 14 import org.junit.Ignore; |
14 import org.junit.Test; | 15 import org.junit.Test; |
15 import org.junit.runner.RunWith; | 16 import org.junit.runner.RunWith; |
16 import org.junit.runners.JUnit4; | 17 import org.junit.runners.JUnit4; |
17 | 18 |
18 import com.ibm.icu.dev.test.TestFmwk; | 19 import com.ibm.icu.dev.test.TestFmwk; |
19 import com.ibm.icu.impl.number.DecimalFormatProperties; | 20 import com.ibm.icu.impl.number.DecimalFormatProperties; |
20 import com.ibm.icu.impl.number.DecimalQuantity; | 21 import com.ibm.icu.impl.number.DecimalQuantity; |
21 import com.ibm.icu.impl.number.DecimalQuantity_64BitBCD; | 22 import com.ibm.icu.impl.number.DecimalQuantity_64BitBCD; |
22 import com.ibm.icu.impl.number.DecimalQuantity_ByteArrayBCD; | 23 import com.ibm.icu.impl.number.DecimalQuantity_ByteArrayBCD; |
23 import com.ibm.icu.impl.number.DecimalQuantity_DualStorageBCD; | 24 import com.ibm.icu.impl.number.DecimalQuantity_DualStorageBCD; |
24 import com.ibm.icu.impl.number.DecimalQuantity_SimpleStorage; | 25 import com.ibm.icu.impl.number.DecimalQuantity_SimpleStorage; |
25 import com.ibm.icu.number.LocalizedNumberFormatter; | 26 import com.ibm.icu.number.LocalizedNumberFormatter; |
26 import com.ibm.icu.number.NumberFormatter; | 27 import com.ibm.icu.number.NumberFormatter; |
27 import com.ibm.icu.text.CompactDecimalFormat.CompactStyle; | 28 import com.ibm.icu.text.CompactDecimalFormat.CompactStyle; |
28 import com.ibm.icu.text.DecimalFormatSymbols; | 29 import com.ibm.icu.text.DecimalFormatSymbols; |
29 import com.ibm.icu.util.ULocale; | 30 import com.ibm.icu.util.ULocale; |
30 | 31 |
31 @RunWith(JUnit4.class) | 32 @RunWith(JUnit4.class) |
32 public class DecimalQuantityTest extends TestFmwk { | 33 public class DecimalQuantityTest extends TestFmwk { |
33 | 34 |
34 @Ignore | 35 @Ignore |
35 @Test | 36 @Test |
36 public void testBehavior() throws ParseException { | 37 public void testBehavior() throws ParseException { |
37 | 38 |
38 // Make a list of several formatters to test the behavior of DecimalQuantity
. | 39 // Make a list of several formatters to test the behavior of DecimalQuan
tity. |
39 List<LocalizedNumberFormatter> formats = new ArrayList<LocalizedNumberFormat
ter>(); | 40 List<LocalizedNumberFormatter> formats = new ArrayList<LocalizedNumberFo
rmatter>(); |
40 | 41 |
41 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(ULocale.ENGL
ISH); | 42 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(ULocale.
ENGLISH); |
42 | 43 |
43 DecimalFormatProperties properties = new DecimalFormatProperties(); | 44 DecimalFormatProperties properties = new DecimalFormatProperties(); |
44 formats.add(NumberFormatter.fromDecimalFormat(properties, symbols, null).loc
ale(ULocale.ENGLISH)); | 45 formats.add( |
45 | 46 NumberFormatter.fromDecimalFormat(properties, symbols, null).loc
ale(ULocale.ENGLISH)); |
46 properties = | 47 |
47 new DecimalFormatProperties() | 48 properties = new DecimalFormatProperties().setMinimumSignificantDigits(3
) |
48 .setMinimumSignificantDigits(3) | 49 .setMaximumSignificantDigits(3).setCompactStyle(CompactStyle.LON
G); |
49 .setMaximumSignificantDigits(3) | 50 formats.add( |
50 .setCompactStyle(CompactStyle.LONG); | 51 NumberFormatter.fromDecimalFormat(properties, symbols, null).loc
ale(ULocale.ENGLISH)); |
51 formats.add(NumberFormatter.fromDecimalFormat(properties, symbols, null).loc
ale(ULocale.ENGLISH)); | 52 |
52 | 53 properties = new DecimalFormatProperties().setMinimumExponentDigits(1).s
etMaximumIntegerDigits(3) |
53 properties = | 54 .setMaximumFractionDigits(1); |
54 new DecimalFormatProperties() | 55 formats.add( |
55 .setMinimumExponentDigits(1) | 56 NumberFormatter.fromDecimalFormat(properties, symbols, null).loc
ale(ULocale.ENGLISH)); |
56 .setMaximumIntegerDigits(3) | 57 |
57 .setMaximumFractionDigits(1); | 58 properties = new DecimalFormatProperties().setRoundingIncrement(new BigD
ecimal("0.5")); |
58 formats.add(NumberFormatter.fromDecimalFormat(properties, symbols, null).loc
ale(ULocale.ENGLISH)); | 59 formats.add( |
59 | 60 NumberFormatter.fromDecimalFormat(properties, symbols, null).loc
ale(ULocale.ENGLISH)); |
60 properties = new DecimalFormatProperties().setRoundingIncrement(new BigDecim
al("0.5")); | 61 |
61 formats.add(NumberFormatter.fromDecimalFormat(properties, symbols, null).loc
ale(ULocale.ENGLISH)); | 62 String[] cases = { |
62 | 63 "1.0", |
63 String[] cases = { | 64 "2.01", |
64 "1.0", | 65 "1234.56", |
65 "2.01", | 66 "3000.0", |
66 "1234.56", | 67 "0.00026418", |
67 "3000.0", | 68 "0.01789261", |
68 "0.00026418", | 69 "468160.0", |
69 "0.01789261", | 70 "999000.0", |
70 "468160.0", | 71 "999900.0", |
71 "999000.0", | 72 "999990.0", |
72 "999900.0", | 73 "0.0", |
73 "999990.0", | 74 "12345678901.0", |
74 "0.0", | 75 "-5193.48", }; |
75 "12345678901.0", | 76 |
76 "-5193.48", | 77 String[] hardCases = { |
77 }; | 78 "9999999999999900.0", |
78 | 79 "789000000000000000000000.0", |
79 String[] hardCases = { | 80 "789123123567853156372158.0", |
80 "9999999999999900.0", | 81 "987654321987654321987654321987654321987654311987654321.0", }; |
81 "789000000000000000000000.0", | 82 |
82 "789123123567853156372158.0", | 83 String[] doubleCases = { |
83 "987654321987654321987654321987654321987654311987654321.0", | 84 "512.0000000000017", |
84 }; | 85 "4095.9999999999977", |
85 | 86 "4095.999999999998", |
86 String[] doubleCases = { | 87 "4095.9999999999986", |
87 "512.0000000000017", | 88 "4095.999999999999", |
88 "4095.9999999999977", | 89 "4095.9999999999995", |
89 "4095.999999999998", | 90 "4096.000000000001", |
90 "4095.9999999999986", | 91 "4096.000000000002", |
91 "4095.999999999999", | 92 "4096.000000000003", |
92 "4095.9999999999995", | 93 "4096.000000000004", |
93 "4096.000000000001", | 94 "4096.000000000005", |
94 "4096.000000000002", | 95 "4096.0000000000055", |
95 "4096.000000000003", | 96 "4096.000000000006", |
96 "4096.000000000004", | 97 "4096.000000000007", }; |
97 "4096.000000000005", | 98 |
98 "4096.0000000000055", | 99 int i = 0; |
99 "4096.000000000006", | 100 for (String str : cases) { |
100 "4096.000000000007", | 101 testDecimalQuantity(i++, str, formats, 0); |
101 }; | 102 } |
102 | 103 |
103 int i = 0; | 104 i = 0; |
104 for (String str : cases) { | 105 for (String str : hardCases) { |
105 testDecimalQuantity(i++, str, formats, 0); | 106 testDecimalQuantity(i++, str, formats, 1); |
106 } | 107 } |
107 | 108 |
108 i = 0; | 109 i = 0; |
109 for (String str : hardCases) { | 110 for (String str : doubleCases) { |
110 testDecimalQuantity(i++, str, formats, 1); | 111 testDecimalQuantity(i++, str, formats, 2); |
111 } | 112 } |
112 | 113 } |
113 i = 0; | 114 |
114 for (String str : doubleCases) { | 115 static void testDecimalQuantity( |
115 testDecimalQuantity(i++, str, formats, 2); | 116 int t, |
116 } | 117 String str, |
117 } | 118 List<LocalizedNumberFormatter> formats, |
118 | 119 int mode) { |
119 static void testDecimalQuantity(int t, String str, List<LocalizedNumberFormatt
er> formats, int mode) { | 120 if (mode == 2) { |
120 if (mode == 2) { | 121 assertEquals("Double is not valid", Double.toString(Double.parseDoub
le(str)), str); |
121 assertEquals("Double is not valid", Double.toString(Double.parseDouble(str
)), str); | 122 } |
122 } | 123 |
123 | 124 List<DecimalQuantity> qs = new ArrayList<DecimalQuantity>(); |
124 List<DecimalQuantity> qs = new ArrayList<DecimalQuantity>(); | 125 BigDecimal d = new BigDecimal(str); |
125 BigDecimal d = new BigDecimal(str); | 126 qs.add(new DecimalQuantity_SimpleStorage(d)); |
126 qs.add(new DecimalQuantity_SimpleStorage(d)); | 127 if (mode == 0) |
127 if (mode == 0) qs.add(new DecimalQuantity_64BitBCD(d)); | 128 qs.add(new DecimalQuantity_64BitBCD(d)); |
128 qs.add(new DecimalQuantity_ByteArrayBCD(d)); | 129 qs.add(new DecimalQuantity_ByteArrayBCD(d)); |
129 qs.add(new DecimalQuantity_DualStorageBCD(d)); | 130 qs.add(new DecimalQuantity_DualStorageBCD(d)); |
130 | 131 |
131 if (new BigDecimal(Double.toString(d.doubleValue())).compareTo(d) == 0) { | 132 if (new BigDecimal(Double.toString(d.doubleValue())).compareTo(d) == 0)
{ |
132 double dv = d.doubleValue(); | 133 double dv = d.doubleValue(); |
133 qs.add(new DecimalQuantity_SimpleStorage(dv)); | 134 qs.add(new DecimalQuantity_SimpleStorage(dv)); |
134 if (mode == 0) qs.add(new DecimalQuantity_64BitBCD(dv)); | 135 if (mode == 0) |
135 qs.add(new DecimalQuantity_ByteArrayBCD(dv)); | 136 qs.add(new DecimalQuantity_64BitBCD(dv)); |
136 qs.add(new DecimalQuantity_DualStorageBCD(dv)); | 137 qs.add(new DecimalQuantity_ByteArrayBCD(dv)); |
137 } | 138 qs.add(new DecimalQuantity_DualStorageBCD(dv)); |
138 | 139 } |
139 if (new BigDecimal(Long.toString(d.longValue())).compareTo(d) == 0) { | 140 |
140 double lv = d.longValue(); | 141 if (new BigDecimal(Long.toString(d.longValue())).compareTo(d) == 0) { |
141 qs.add(new DecimalQuantity_SimpleStorage(lv)); | 142 double lv = d.longValue(); |
142 if (mode == 0) qs.add(new DecimalQuantity_64BitBCD(lv)); | 143 qs.add(new DecimalQuantity_SimpleStorage(lv)); |
143 qs.add(new DecimalQuantity_ByteArrayBCD(lv)); | 144 if (mode == 0) |
144 qs.add(new DecimalQuantity_DualStorageBCD(lv)); | 145 qs.add(new DecimalQuantity_64BitBCD(lv)); |
145 } | 146 qs.add(new DecimalQuantity_ByteArrayBCD(lv)); |
146 | 147 qs.add(new DecimalQuantity_DualStorageBCD(lv)); |
147 testDecimalQuantityExpectedOutput(qs.get(0), str); | 148 } |
148 | 149 |
149 if (qs.size() == 1) { | 150 testDecimalQuantityExpectedOutput(qs.get(0), str); |
150 return; | 151 |
151 } | 152 if (qs.size() == 1) { |
152 | 153 return; |
153 for (int i = 1; i < qs.size(); i++) { | 154 } |
154 DecimalQuantity q0 = qs.get(0); | 155 |
155 DecimalQuantity q1 = qs.get(i); | 156 for (int i = 1; i < qs.size(); i++) { |
156 testDecimalQuantityExpectedOutput(q1, str); | 157 DecimalQuantity q0 = qs.get(0); |
157 testDecimalQuantityRounding(q0, q1); | 158 DecimalQuantity q1 = qs.get(i); |
158 testDecimalQuantityRoundingInterval(q0, q1); | 159 testDecimalQuantityExpectedOutput(q1, str); |
159 testDecimalQuantityMath(q0, q1); | 160 testDecimalQuantityRounding(q0, q1); |
160 testDecimalQuantityWithFormats(q0, q1, formats); | 161 testDecimalQuantityRoundingInterval(q0, q1); |
161 } | 162 testDecimalQuantityMath(q0, q1); |
162 } | 163 testDecimalQuantityWithFormats(q0, q1, formats); |
163 | 164 } |
164 private static void testDecimalQuantityExpectedOutput(DecimalQuantity rq, Stri
ng expected) { | 165 } |
165 DecimalQuantity q0 = rq.createCopy(); | 166 |
166 // Force an accurate double | 167 private static void testDecimalQuantityExpectedOutput(DecimalQuantity rq, St
ring expected) { |
167 q0.roundToInfinity(); | 168 DecimalQuantity q0 = rq.createCopy(); |
168 q0.setIntegerLength(1, Integer.MAX_VALUE); | 169 // Force an accurate double |
169 q0.setFractionLength(1, Integer.MAX_VALUE); | 170 q0.roundToInfinity(); |
170 String actual = q0.toPlainString(); | 171 q0.setIntegerLength(1, Integer.MAX_VALUE); |
171 assertEquals("Unexpected output from simple string conversion (" + q0 + ")",
expected, actual); | 172 q0.setFractionLength(1, Integer.MAX_VALUE); |
172 } | 173 String actual = q0.toPlainString(); |
173 | 174 assertEquals("Unexpected output from simple string conversion (" + q0 +
")", expected, actual); |
174 private static final MathContext MATH_CONTEXT_HALF_EVEN = | 175 } |
175 new MathContext(0, RoundingMode.HALF_EVEN); | 176 |
176 private static final MathContext MATH_CONTEXT_CEILING = new MathContext(0, Rou
ndingMode.CEILING); | 177 private static final MathContext MATH_CONTEXT_HALF_EVEN = new MathContext(0,
RoundingMode.HALF_EVEN); |
177 private static final MathContext MATH_CONTEXT_PRECISION = | 178 private static final MathContext MATH_CONTEXT_CEILING = new MathContext(0, R
oundingMode.CEILING); |
178 new MathContext(3, RoundingMode.HALF_UP); | 179 private static final MathContext MATH_CONTEXT_FLOOR = new MathContext(0, Rou
ndingMode.FLOOR); |
179 | 180 private static final MathContext MATH_CONTEXT_PRECISION = new MathContext(3,
RoundingMode.HALF_UP); |
180 private static void testDecimalQuantityRounding(DecimalQuantity rq0, DecimalQu
antity rq1) { | 181 |
181 DecimalQuantity q0 = rq0.createCopy(); | 182 private static void testDecimalQuantityRounding(DecimalQuantity rq0, Decimal
Quantity rq1) { |
182 DecimalQuantity q1 = rq1.createCopy(); | 183 DecimalQuantity q0 = rq0.createCopy(); |
183 q0.roundToMagnitude(-1, MATH_CONTEXT_HALF_EVEN); | 184 DecimalQuantity q1 = rq1.createCopy(); |
184 q1.roundToMagnitude(-1, MATH_CONTEXT_HALF_EVEN); | 185 q0.roundToMagnitude(-1, MATH_CONTEXT_HALF_EVEN); |
185 testDecimalQuantityBehavior(q0, q1); | 186 q1.roundToMagnitude(-1, MATH_CONTEXT_HALF_EVEN); |
186 | 187 testDecimalQuantityBehavior(q0, q1); |
187 q0 = rq0.createCopy(); | 188 |
188 q1 = rq1.createCopy(); | 189 q0 = rq0.createCopy(); |
189 q0.roundToMagnitude(-1, MATH_CONTEXT_CEILING); | 190 q1 = rq1.createCopy(); |
190 q1.roundToMagnitude(-1, MATH_CONTEXT_CEILING); | 191 q0.roundToMagnitude(-1, MATH_CONTEXT_CEILING); |
191 testDecimalQuantityBehavior(q0, q1); | 192 q1.roundToMagnitude(-1, MATH_CONTEXT_CEILING); |
192 | 193 testDecimalQuantityBehavior(q0, q1); |
193 q0 = rq0.createCopy(); | 194 |
194 q1 = rq1.createCopy(); | 195 q0 = rq0.createCopy(); |
195 q0.roundToMagnitude(-1, MATH_CONTEXT_PRECISION); | 196 q1 = rq1.createCopy(); |
196 q1.roundToMagnitude(-1, MATH_CONTEXT_PRECISION); | 197 q0.roundToMagnitude(-1, MATH_CONTEXT_PRECISION); |
197 testDecimalQuantityBehavior(q0, q1); | 198 q1.roundToMagnitude(-1, MATH_CONTEXT_PRECISION); |
198 } | 199 testDecimalQuantityBehavior(q0, q1); |
199 | 200 } |
200 private static void testDecimalQuantityRoundingInterval(DecimalQuantity rq0, D
ecimalQuantity rq1) { | 201 |
201 DecimalQuantity q0 = rq0.createCopy(); | 202 private static void testDecimalQuantityRoundingInterval(DecimalQuantity rq0,
DecimalQuantity rq1) { |
202 DecimalQuantity q1 = rq1.createCopy(); | 203 DecimalQuantity q0 = rq0.createCopy(); |
203 q0.roundToIncrement(new BigDecimal("0.05"), MATH_CONTEXT_HALF_EVEN); | 204 DecimalQuantity q1 = rq1.createCopy(); |
204 q1.roundToIncrement(new BigDecimal("0.05"), MATH_CONTEXT_HALF_EVEN); | 205 q0.roundToIncrement(new BigDecimal("0.05"), MATH_CONTEXT_HALF_EVEN); |
205 testDecimalQuantityBehavior(q0, q1); | 206 q1.roundToIncrement(new BigDecimal("0.05"), MATH_CONTEXT_HALF_EVEN); |
206 | 207 testDecimalQuantityBehavior(q0, q1); |
207 q0 = rq0.createCopy(); | 208 |
208 q1 = rq1.createCopy(); | 209 q0 = rq0.createCopy(); |
209 q0.roundToIncrement(new BigDecimal("0.05"), MATH_CONTEXT_CEILING); | 210 q1 = rq1.createCopy(); |
210 q1.roundToIncrement(new BigDecimal("0.05"), MATH_CONTEXT_CEILING); | 211 q0.roundToIncrement(new BigDecimal("0.05"), MATH_CONTEXT_CEILING); |
211 testDecimalQuantityBehavior(q0, q1); | 212 q1.roundToIncrement(new BigDecimal("0.05"), MATH_CONTEXT_CEILING); |
212 } | 213 testDecimalQuantityBehavior(q0, q1); |
213 | 214 } |
214 private static void testDecimalQuantityMath(DecimalQuantity rq0, DecimalQuanti
ty rq1) { | 215 |
215 DecimalQuantity q0 = rq0.createCopy(); | 216 private static void testDecimalQuantityMath(DecimalQuantity rq0, DecimalQuan
tity rq1) { |
216 DecimalQuantity q1 = rq1.createCopy(); | 217 DecimalQuantity q0 = rq0.createCopy(); |
217 q0.adjustMagnitude(-3); | 218 DecimalQuantity q1 = rq1.createCopy(); |
218 q1.adjustMagnitude(-3); | 219 q0.adjustMagnitude(-3); |
219 testDecimalQuantityBehavior(q0, q1); | 220 q1.adjustMagnitude(-3); |
220 | 221 testDecimalQuantityBehavior(q0, q1); |
221 q0 = rq0.createCopy(); | 222 |
222 q1 = rq1.createCopy(); | 223 q0 = rq0.createCopy(); |
223 q0.multiplyBy(new BigDecimal("3.14159")); | 224 q1 = rq1.createCopy(); |
224 q1.multiplyBy(new BigDecimal("3.14159")); | 225 q0.multiplyBy(new BigDecimal("3.14159")); |
225 testDecimalQuantityBehavior(q0, q1); | 226 q1.multiplyBy(new BigDecimal("3.14159")); |
226 } | 227 testDecimalQuantityBehavior(q0, q1); |
227 | 228 } |
228 private static void testDecimalQuantityWithFormats( | 229 |
229 DecimalQuantity rq0, DecimalQuantity rq1, List<LocalizedNumberFormatter> f
ormats) { | 230 private static void testDecimalQuantityWithFormats( |
230 for (LocalizedNumberFormatter format : formats) { | 231 DecimalQuantity rq0, |
231 DecimalQuantity q0 = rq0.createCopy(); | 232 DecimalQuantity rq1, |
232 DecimalQuantity q1 = rq1.createCopy(); | 233 List<LocalizedNumberFormatter> formats) { |
233 String s1 = format.format(q0).toString(); | 234 for (LocalizedNumberFormatter format : formats) { |
234 String s2 = format.format(q1).toString(); | 235 DecimalQuantity q0 = rq0.createCopy(); |
235 assertEquals("Different output from formatter (" + q0 + ", " + q1 + ")", s
1, s2); | 236 DecimalQuantity q1 = rq1.createCopy(); |
236 } | 237 String s1 = format.format(q0).toString(); |
237 } | 238 String s2 = format.format(q1).toString(); |
238 | 239 assertEquals("Different output from formatter (" + q0 + ", " + q1 +
")", s1, s2); |
239 private static void testDecimalQuantityBehavior(DecimalQuantity rq0, DecimalQu
antity rq1) { | 240 } |
240 DecimalQuantity q0 = rq0.createCopy(); | 241 } |
241 DecimalQuantity q1 = rq1.createCopy(); | 242 |
242 | 243 private static void testDecimalQuantityBehavior(DecimalQuantity rq0, Decimal
Quantity rq1) { |
243 assertEquals("Different sign (" + q0 + ", " + q1 + ")", q0.isNegative(), q1.
isNegative()); | 244 DecimalQuantity q0 = rq0.createCopy(); |
244 | 245 DecimalQuantity q1 = rq1.createCopy(); |
245 assertEquals( | 246 |
246 "Different fingerprint (" + q0 + ", " + q1 + ")", | 247 assertEquals("Different sign (" + q0 + ", " + q1 + ")", q0.isNegative(),
q1.isNegative()); |
247 q0.getPositionFingerprint(), | 248 |
248 q1.getPositionFingerprint()); | 249 assertEquals("Different fingerprint (" + q0 + ", " + q1 + ")", |
249 | 250 q0.getPositionFingerprint(), |
250 assertDoubleEquals( | 251 q1.getPositionFingerprint()); |
251 "Different double values (" + q0 + ", " + q1 + ")", q0.toDouble(), q1.to
Double()); | 252 |
252 | 253 assertDoubleEquals("Different double values (" + q0 + ", " + q1 + ")", |
253 assertBigDecimalEquals( | 254 q0.toDouble(), |
254 "Different BigDecimal values (" + q0 + ", " + q1 + ")", | 255 q1.toDouble()); |
255 q0.toBigDecimal(), | 256 |
256 q1.toBigDecimal()); | 257 assertBigDecimalEquals("Different BigDecimal values (" + q0 + ", " + q1
+ ")", |
257 | 258 q0.toBigDecimal(), |
258 q0.roundToInfinity(); | 259 q1.toBigDecimal()); |
259 q1.roundToInfinity(); | 260 |
260 | 261 q0.roundToInfinity(); |
261 assertEquals( | 262 q1.roundToInfinity(); |
262 "Different lower display magnitude", | 263 |
263 q0.getLowerDisplayMagnitude(), | 264 assertEquals("Different lower display magnitude", |
264 q1.getLowerDisplayMagnitude()); | 265 q0.getLowerDisplayMagnitude(), |
265 assertEquals( | 266 q1.getLowerDisplayMagnitude()); |
266 "Different upper display magnitude", | 267 assertEquals("Different upper display magnitude", |
267 q0.getUpperDisplayMagnitude(), | 268 q0.getUpperDisplayMagnitude(), |
268 q1.getUpperDisplayMagnitude()); | 269 q1.getUpperDisplayMagnitude()); |
269 | 270 |
270 for (int m = q0.getUpperDisplayMagnitude(); m >= q0.getLowerDisplayMagnitude
(); m--) { | 271 for (int m = q0.getUpperDisplayMagnitude(); m >= q0.getLowerDisplayMagni
tude(); m--) { |
271 assertEquals( | 272 assertEquals("Different digit at magnitude " + m + " (" + q0 + ", "
+ q1 + ")", |
272 "Different digit at magnitude " + m + " (" + q0 + ", " + q1 + ")", | 273 q0.getDigit(m), |
273 q0.getDigit(m), | 274 q1.getDigit(m)); |
274 q1.getDigit(m)); | 275 } |
275 } | 276 |
276 | 277 if (rq0 instanceof DecimalQuantity_DualStorageBCD) { |
277 if (rq0 instanceof DecimalQuantity_DualStorageBCD) { | 278 String message = ((DecimalQuantity_DualStorageBCD) rq0).checkHealth(
); |
278 String message = ((DecimalQuantity_DualStorageBCD) rq0).checkHealth(); | 279 if (message != null) |
279 if (message != null) errln(message); | 280 errln(message); |
280 } | 281 } |
281 if (rq1 instanceof DecimalQuantity_DualStorageBCD) { | 282 if (rq1 instanceof DecimalQuantity_DualStorageBCD) { |
282 String message = ((DecimalQuantity_DualStorageBCD) rq1).checkHealth(); | 283 String message = ((DecimalQuantity_DualStorageBCD) rq1).checkHealth(
); |
283 if (message != null) errln(message); | 284 if (message != null) |
284 } | 285 errln(message); |
285 } | 286 } |
286 | 287 } |
287 @Test | 288 |
288 public void testSwitchStorage() { | 289 @Test |
289 DecimalQuantity_DualStorageBCD fq = new DecimalQuantity_DualStorageBCD(); | 290 public void testSwitchStorage() { |
290 | 291 DecimalQuantity_DualStorageBCD fq = new DecimalQuantity_DualStorageBCD()
; |
291 fq.setToLong(1234123412341234L); | 292 |
292 assertFalse("Should not be using byte array", fq.isUsingBytes()); | 293 fq.setToLong(1234123412341234L); |
293 assertEquals("Failed on initialize", "1234123412341234E0", fq.toNumberString
()); | 294 assertFalse("Should not be using byte array", fq.isUsingBytes()); |
294 assertNull("Failed health check", fq.checkHealth()); | 295 assertEquals("Failed on initialize", "1234123412341234E0", fq.toNumberSt
ring()); |
295 // Long -> Bytes | 296 assertNull("Failed health check", fq.checkHealth()); |
296 fq.appendDigit((byte) 5, 0, true); | 297 // Long -> Bytes |
297 assertTrue("Should be using byte array", fq.isUsingBytes()); | 298 fq.appendDigit((byte) 5, 0, true); |
298 assertEquals("Failed on multiply", "12341234123412345E0", fq.toNumberString(
)); | 299 assertTrue("Should be using byte array", fq.isUsingBytes()); |
299 assertNull("Failed health check", fq.checkHealth()); | 300 assertEquals("Failed on multiply", "12341234123412345E0", fq.toNumberStr
ing()); |
300 // Bytes -> Long | 301 assertNull("Failed health check", fq.checkHealth()); |
301 fq.roundToMagnitude(5, MATH_CONTEXT_HALF_EVEN); | 302 // Bytes -> Long |
302 assertFalse("Should not be using byte array", fq.isUsingBytes()); | 303 fq.roundToMagnitude(5, MATH_CONTEXT_HALF_EVEN); |
303 assertEquals("Failed on round", "123412341234E5", fq.toNumberString()); | 304 assertFalse("Should not be using byte array", fq.isUsingBytes()); |
304 assertNull("Failed health check", fq.checkHealth()); | 305 assertEquals("Failed on round", "123412341234E5", fq.toNumberString()); |
305 } | 306 assertNull("Failed health check", fq.checkHealth()); |
306 | 307 } |
307 @Test | 308 |
308 public void testAppend() { | 309 @Test |
309 DecimalQuantity_DualStorageBCD fq = new DecimalQuantity_DualStorageBCD(); | 310 public void testAppend() { |
310 fq.appendDigit((byte) 1, 0, true); | 311 DecimalQuantity_DualStorageBCD fq = new DecimalQuantity_DualStorageBCD()
; |
311 assertEquals("Failed on append", "1E0", fq.toNumberString()); | 312 fq.appendDigit((byte) 1, 0, true); |
312 assertNull("Failed health check", fq.checkHealth()); | 313 assertEquals("Failed on append", "1E0", fq.toNumberString()); |
313 fq.appendDigit((byte) 2, 0, true); | 314 assertNull("Failed health check", fq.checkHealth()); |
314 assertEquals("Failed on append", "12E0", fq.toNumberString()); | 315 fq.appendDigit((byte) 2, 0, true); |
315 assertNull("Failed health check", fq.checkHealth()); | 316 assertEquals("Failed on append", "12E0", fq.toNumberString()); |
316 fq.appendDigit((byte) 3, 1, true); | 317 assertNull("Failed health check", fq.checkHealth()); |
317 assertEquals("Failed on append", "1203E0", fq.toNumberString()); | 318 fq.appendDigit((byte) 3, 1, true); |
318 assertNull("Failed health check", fq.checkHealth()); | 319 assertEquals("Failed on append", "1203E0", fq.toNumberString()); |
319 fq.appendDigit((byte) 0, 1, true); | 320 assertNull("Failed health check", fq.checkHealth()); |
320 assertEquals("Failed on append", "1203E2", fq.toNumberString()); | 321 fq.appendDigit((byte) 0, 1, true); |
321 assertNull("Failed health check", fq.checkHealth()); | 322 assertEquals("Failed on append", "1203E2", fq.toNumberString()); |
322 fq.appendDigit((byte) 4, 0, true); | 323 assertNull("Failed health check", fq.checkHealth()); |
323 assertEquals("Failed on append", "1203004E0", fq.toNumberString()); | 324 fq.appendDigit((byte) 4, 0, true); |
324 assertNull("Failed health check", fq.checkHealth()); | 325 assertEquals("Failed on append", "1203004E0", fq.toNumberString()); |
325 fq.appendDigit((byte) 0, 0, true); | 326 assertNull("Failed health check", fq.checkHealth()); |
326 assertEquals("Failed on append", "1203004E1", fq.toNumberString()); | 327 fq.appendDigit((byte) 0, 0, true); |
327 assertNull("Failed health check", fq.checkHealth()); | 328 assertEquals("Failed on append", "1203004E1", fq.toNumberString()); |
328 fq.appendDigit((byte) 5, 0, false); | 329 assertNull("Failed health check", fq.checkHealth()); |
329 assertEquals("Failed on append", "120300405E-1", fq.toNumberString()); | 330 fq.appendDigit((byte) 5, 0, false); |
330 assertNull("Failed health check", fq.checkHealth()); | 331 assertEquals("Failed on append", "120300405E-1", fq.toNumberString()); |
331 fq.appendDigit((byte) 6, 0, false); | 332 assertNull("Failed health check", fq.checkHealth()); |
332 assertEquals("Failed on append", "1203004056E-2", fq.toNumberString()); | 333 fq.appendDigit((byte) 6, 0, false); |
333 assertNull("Failed health check", fq.checkHealth()); | 334 assertEquals("Failed on append", "1203004056E-2", fq.toNumberString()); |
334 fq.appendDigit((byte) 7, 3, false); | 335 assertNull("Failed health check", fq.checkHealth()); |
335 assertEquals("Failed on append", "12030040560007E-6", fq.toNumberString()); | 336 fq.appendDigit((byte) 7, 3, false); |
336 assertNull("Failed health check", fq.checkHealth()); | 337 assertEquals("Failed on append", "12030040560007E-6", fq.toNumberString(
)); |
337 StringBuilder baseExpected = new StringBuilder("12030040560007"); | 338 assertNull("Failed health check", fq.checkHealth()); |
338 for (int i = 0; i < 10; i++) { | 339 StringBuilder baseExpected = new StringBuilder("12030040560007"); |
339 fq.appendDigit((byte) 8, 0, false); | 340 for (int i = 0; i < 10; i++) { |
340 baseExpected.append('8'); | 341 fq.appendDigit((byte) 8, 0, false); |
341 StringBuilder expected = new StringBuilder(baseExpected); | 342 baseExpected.append('8'); |
342 expected.append("E"); | 343 StringBuilder expected = new StringBuilder(baseExpected); |
343 expected.append(-7 - i); | 344 expected.append("E"); |
344 assertEquals("Failed on append", expected.toString(), fq.toNumberString())
; | 345 expected.append(-7 - i); |
345 assertNull("Failed health check", fq.checkHealth()); | 346 assertEquals("Failed on append", expected.toString(), fq.toNumberStr
ing()); |
346 } | 347 assertNull("Failed health check", fq.checkHealth()); |
347 fq.appendDigit((byte) 9, 2, false); | 348 } |
348 baseExpected.append("009"); | 349 fq.appendDigit((byte) 9, 2, false); |
349 StringBuilder expected = new StringBuilder(baseExpected); | 350 baseExpected.append("009"); |
350 expected.append('E'); | 351 StringBuilder expected = new StringBuilder(baseExpected); |
351 expected.append("-19"); | 352 expected.append('E'); |
352 assertEquals("Failed on append", expected.toString(), fq.toNumberString()); | 353 expected.append("-19"); |
353 assertNull("Failed health check", fq.checkHealth()); | 354 assertEquals("Failed on append", expected.toString(), fq.toNumberString(
)); |
354 } | 355 assertNull("Failed health check", fq.checkHealth()); |
355 | 356 } |
356 @Ignore | 357 |
357 @Test | 358 @Ignore |
358 public void testConvertToAccurateDouble() { | 359 @Test |
359 // based on https://github.com/google/double-conversion/issues/28 | 360 public void testConvertToAccurateDouble() { |
360 double[] hardDoubles = { | 361 // based on https://github.com/google/double-conversion/issues/28 |
361 1651087494906221570.0, | 362 double[] hardDoubles = { |
362 -5074790912492772E-327, | 363 1651087494906221570.0, |
363 83602530019752571E-327, | 364 -5074790912492772E-327, |
364 2.207817077636718750000000000000, | 365 83602530019752571E-327, |
365 1.818351745605468750000000000000, | 366 2.207817077636718750000000000000, |
366 3.941719055175781250000000000000, | 367 1.818351745605468750000000000000, |
367 3.738609313964843750000000000000, | 368 3.941719055175781250000000000000, |
368 3.967735290527343750000000000000, | 369 3.738609313964843750000000000000, |
369 1.328025817871093750000000000000, | 370 3.967735290527343750000000000000, |
370 3.920967102050781250000000000000, | 371 1.328025817871093750000000000000, |
371 1.015235900878906250000000000000, | 372 3.920967102050781250000000000000, |
372 1.335227966308593750000000000000, | 373 1.015235900878906250000000000000, |
373 1.344520568847656250000000000000, | 374 1.335227966308593750000000000000, |
374 2.879127502441406250000000000000, | 375 1.344520568847656250000000000000, |
375 3.695838928222656250000000000000, | 376 2.879127502441406250000000000000, |
376 1.845344543457031250000000000000, | 377 3.695838928222656250000000000000, |
377 3.793952941894531250000000000000, | 378 1.845344543457031250000000000000, |
378 3.211402893066406250000000000000, | 379 3.793952941894531250000000000000, |
379 2.565971374511718750000000000000, | 380 3.211402893066406250000000000000, |
380 0.965156555175781250000000000000, | 381 2.565971374511718750000000000000, |
381 2.700004577636718750000000000000, | 382 0.965156555175781250000000000000, |
382 0.767097473144531250000000000000, | 383 2.700004577636718750000000000000, |
383 1.780448913574218750000000000000, | 384 0.767097473144531250000000000000, |
384 2.624839782714843750000000000000, | 385 1.780448913574218750000000000000, |
385 1.305290222167968750000000000000, | 386 2.624839782714843750000000000000, |
386 3.834922790527343750000000000000, | 387 1.305290222167968750000000000000, |
387 }; | 388 3.834922790527343750000000000000, }; |
388 | 389 |
389 double[] integerDoubles = { | 390 double[] integerDoubles = { |
390 51423, | 391 51423, |
391 51423e10, | 392 51423e10, |
392 4.503599627370496E15, | 393 4.503599627370496E15, |
393 6.789512076111555E15, | 394 6.789512076111555E15, |
394 9.007199254740991E15, | 395 9.007199254740991E15, |
395 9.007199254740992E15 | 396 9.007199254740992E15 }; |
396 }; | 397 |
397 | 398 for (double d : hardDoubles) { |
398 for (double d : hardDoubles) { | 399 checkDoubleBehavior(d, true, ""); |
399 checkDoubleBehavior(d, true, ""); | 400 } |
400 } | 401 |
401 | 402 for (double d : integerDoubles) { |
402 for (double d : integerDoubles) { | 403 checkDoubleBehavior(d, false, ""); |
403 checkDoubleBehavior(d, false, ""); | 404 } |
404 } | 405 |
405 | 406 assertEquals("NaN check failed", |
406 assertEquals("NaN check failed", Double.NaN, new DecimalQuantity_DualStorage
BCD(Double.NaN).toDouble()); | 407 Double.NaN, |
407 assertEquals( | 408 new DecimalQuantity_DualStorageBCD(Double.NaN).toDouble()); |
408 "Inf check failed", | 409 assertEquals("Inf check failed", |
409 Double.POSITIVE_INFINITY, | 410 Double.POSITIVE_INFINITY, |
410 new DecimalQuantity_DualStorageBCD(Double.POSITIVE_INFINITY).toDouble())
; | 411 new DecimalQuantity_DualStorageBCD(Double.POSITIVE_INFINITY).toD
ouble()); |
411 assertEquals( | 412 assertEquals("-Inf check failed", |
412 "-Inf check failed", | 413 Double.NEGATIVE_INFINITY, |
413 Double.NEGATIVE_INFINITY, | 414 new DecimalQuantity_DualStorageBCD(Double.NEGATIVE_INFINITY).toD
ouble()); |
414 new DecimalQuantity_DualStorageBCD(Double.NEGATIVE_INFINITY).toDouble())
; | 415 |
415 | 416 // Generate random doubles |
416 // Generate random doubles | 417 String alert = "UNEXPECTED FAILURE: PLEASE REPORT THIS MESSAGE TO THE IC
U TEAM: "; |
417 String alert = "UNEXPECTED FAILURE: PLEASE REPORT THIS MESSAGE TO THE ICU TE
AM: "; | 418 Random rnd = new Random(); |
418 Random rnd = new Random(); | 419 for (int i = 0; i < 10000; i++) { |
419 for (int i = 0; i < 10000; i++) { | 420 double d = Double.longBitsToDouble(rnd.nextLong()); |
420 double d = Double.longBitsToDouble(rnd.nextLong()); | 421 if (Double.isNaN(d) || Double.isInfinite(d)) |
421 if (Double.isNaN(d) || Double.isInfinite(d)) continue; | 422 continue; |
422 checkDoubleBehavior(d, false, alert); | 423 checkDoubleBehavior(d, false, alert); |
423 } | 424 } |
424 } | 425 } |
425 | 426 |
426 private static void checkDoubleBehavior(double d, boolean explicitRequired, St
ring alert) { | 427 private static void checkDoubleBehavior(double d, boolean explicitRequired,
String alert) { |
427 DecimalQuantity_DualStorageBCD fq = new DecimalQuantity_DualStorageBCD(d); | 428 DecimalQuantity_DualStorageBCD fq = new DecimalQuantity_DualStorageBCD(d
); |
428 if (explicitRequired) { | 429 if (explicitRequired) { |
429 assertTrue(alert + "Should be using approximate double", !fq.explicitExact
Double); | 430 assertTrue(alert + "Should be using approximate double", !fq.explici
tExactDouble); |
430 } | 431 } |
431 assertEquals(alert + "Initial construction from hard double", d, fq.toDouble
()); | 432 assertEquals(alert + "Initial construction from hard double", d, fq.toDo
uble()); |
432 fq.roundToInfinity(); | 433 fq.roundToInfinity(); |
433 if (explicitRequired) { | 434 if (explicitRequired) { |
434 assertTrue(alert + "Should not be using approximate double", fq.explicitEx
actDouble); | 435 assertTrue(alert + "Should not be using approximate double", fq.expl
icitExactDouble); |
435 } | 436 } |
436 assertDoubleEquals(alert + "After conversion to exact BCD (double)", d, fq.t
oDouble()); | 437 assertDoubleEquals(alert + "After conversion to exact BCD (double)", d,
fq.toDouble()); |
437 assertBigDecimalEquals( | 438 assertBigDecimalEquals(alert + "After conversion to exact BCD (BigDecima
l)", |
438 alert + "After conversion to exact BCD (BigDecimal)", | 439 new BigDecimal(Double.toString(d)), |
439 new BigDecimal(Double.toString(d)), | 440 fq.toBigDecimal()); |
440 fq.toBigDecimal()); | 441 } |
441 } | 442 |
442 | 443 @Test |
443 @Test | 444 public void testUseApproximateDoubleWhenAble() { |
444 public void testUseApproximateDoubleWhenAble() { | 445 Object[][] cases = { |
445 Object[][] cases = { | 446 { 1.2345678, 1, MATH_CONTEXT_HALF_EVEN, false }, |
446 {1.2345678, 1, MATH_CONTEXT_HALF_EVEN, false}, | 447 { 1.2345678, 7, MATH_CONTEXT_HALF_EVEN, false }, |
447 {1.2345678, 7, MATH_CONTEXT_HALF_EVEN, false}, | 448 { 1.2345678, 12, MATH_CONTEXT_HALF_EVEN, false }, |
448 {1.2345678, 12, MATH_CONTEXT_HALF_EVEN, false}, | 449 { 1.2345678, 13, MATH_CONTEXT_HALF_EVEN, true }, |
449 {1.2345678, 13, MATH_CONTEXT_HALF_EVEN, true}, | 450 { 1.235, 1, MATH_CONTEXT_HALF_EVEN, false }, |
450 {1.235, 1, MATH_CONTEXT_HALF_EVEN, false}, | 451 { 1.235, 2, MATH_CONTEXT_HALF_EVEN, true }, |
451 {1.235, 2, MATH_CONTEXT_HALF_EVEN, true}, | 452 { 1.235, 3, MATH_CONTEXT_HALF_EVEN, false }, |
452 {1.235, 3, MATH_CONTEXT_HALF_EVEN, false}, | 453 { 1.000000000000001, 0, MATH_CONTEXT_HALF_EVEN, false }, |
453 {1.000000000000001, 0, MATH_CONTEXT_HALF_EVEN, false}, | 454 { 1.000000000000001, 0, MATH_CONTEXT_CEILING, true }, |
454 {1.000000000000001, 0, MATH_CONTEXT_CEILING, true}, | 455 { 1.235, 1, MATH_CONTEXT_CEILING, false }, |
455 {1.235, 1, MATH_CONTEXT_CEILING, false}, | 456 { 1.235, 2, MATH_CONTEXT_CEILING, false }, |
456 {1.235, 2, MATH_CONTEXT_CEILING, false}, | 457 { 1.235, 3, MATH_CONTEXT_CEILING, true } }; |
457 {1.235, 3, MATH_CONTEXT_CEILING, true} | 458 |
458 }; | 459 for (Object[] cas : cases) { |
459 | 460 double d = (Double) cas[0]; |
460 for (Object[] cas : cases) { | 461 int maxFrac = (Integer) cas[1]; |
461 double d = (Double) cas[0]; | 462 MathContext mc = (MathContext) cas[2]; |
462 int maxFrac = (Integer) cas[1]; | 463 boolean usesExact = (Boolean) cas[3]; |
463 MathContext mc = (MathContext) cas[2]; | 464 |
464 boolean usesExact = (Boolean) cas[3]; | 465 DecimalQuantity_DualStorageBCD fq = new DecimalQuantity_DualStorageB
CD(d); |
465 | 466 assertTrue("Should be using approximate double", !fq.explicitExactDo
uble); |
466 DecimalQuantity_DualStorageBCD fq = new DecimalQuantity_DualStorageBCD(d); | 467 fq.roundToMagnitude(-maxFrac, mc); |
467 assertTrue("Should be using approximate double", !fq.explicitExactDouble); | 468 assertEquals( |
468 fq.roundToMagnitude(-maxFrac, mc); | 469 "Using approximate double after rounding: " + d + " maxFrac=
" + maxFrac + " " + mc, |
469 assertEquals( | 470 usesExact, |
470 "Using approximate double after rounding: " + d + " maxFrac=" + maxFra
c + " " + mc, | 471 fq.explicitExactDouble); |
471 usesExact, | 472 } |
472 fq.explicitExactDouble); | 473 } |
473 } | 474 |
474 } | 475 @Test |
475 | 476 public void testDecimalQuantityBehaviorStandalone() { |
476 @Test | 477 DecimalQuantity_DualStorageBCD fq = new DecimalQuantity_DualStorageBCD()
; |
477 public void testDecimalQuantityBehaviorStandalone() { | 478 assertToStringAndHealth(fq, "<DecimalQuantity 999:0:0:-999 long 0E0>"); |
478 DecimalQuantity_DualStorageBCD fq = new DecimalQuantity_DualStorageBCD(); | 479 fq.setToInt(51423); |
479 assertToStringAndHealth(fq, "<DecimalQuantity 999:0:0:-999 long 0E0>"); | 480 assertToStringAndHealth(fq, "<DecimalQuantity 999:0:0:-999 long 51423E0>
"); |
480 fq.setToInt(51423); | 481 fq.adjustMagnitude(-3); |
481 assertToStringAndHealth(fq, "<DecimalQuantity 999:0:0:-999 long 51423E0>")
; | 482 assertToStringAndHealth(fq, "<DecimalQuantity 999:0:0:-999 long 51423E-3
>"); |
482 fq.adjustMagnitude(-3); | 483 fq.setToLong(999999999999000L); |
483 assertToStringAndHealth(fq, "<DecimalQuantity 999:0:0:-999 long 51423E-3>"
); | 484 assertToStringAndHealth(fq, "<DecimalQuantity 999:0:0:-999 long 99999999
9999E3>"); |
484 fq.setToLong(999999999999000L); | 485 fq.setIntegerLength(2, 5); |
485 assertToStringAndHealth(fq, "<DecimalQuantity 999:0:0:-999 long 9999999999
99E3>"); | 486 assertToStringAndHealth(fq, "<DecimalQuantity 5:2:0:-999 long 9999999999
99E3>"); |
486 fq.setIntegerLength(2, 5); | 487 fq.setFractionLength(3, 6); |
487 assertToStringAndHealth(fq, "<DecimalQuantity 5:2:0:-999 long 999999999999
E3>"); | 488 assertToStringAndHealth(fq, "<DecimalQuantity 5:2:-3:-6 long 99999999999
9E3>"); |
488 fq.setFractionLength(3, 6); | 489 fq.setToDouble(987.654321); |
489 assertToStringAndHealth(fq, "<DecimalQuantity 5:2:-3:-6 long 999999999999E
3>"); | 490 assertToStringAndHealth(fq, "<DecimalQuantity 5:2:-3:-6 long 987654321E-
6>"); |
490 fq.setToDouble(987.654321); | 491 fq.roundToInfinity(); |
491 assertToStringAndHealth(fq, "<DecimalQuantity 5:2:-3:-6 long 987654321E-6>
"); | 492 assertToStringAndHealth(fq, "<DecimalQuantity 5:2:-3:-6 long 987654321E-
6>"); |
492 fq.roundToInfinity(); | 493 fq.roundToIncrement(new BigDecimal("0.005"), MATH_CONTEXT_HALF_EVEN); |
493 assertToStringAndHealth(fq, "<DecimalQuantity 5:2:-3:-6 long 987654321E-6>
"); | 494 assertToStringAndHealth(fq, "<DecimalQuantity 5:2:-3:-6 long 987655E-3>"
); |
494 fq.roundToIncrement(new BigDecimal("0.005"), MATH_CONTEXT_HALF_EVEN); | 495 fq.roundToMagnitude(-2, MATH_CONTEXT_HALF_EVEN); |
495 assertToStringAndHealth(fq, "<DecimalQuantity 5:2:-3:-6 long 987655E-3>"); | 496 assertToStringAndHealth(fq, "<DecimalQuantity 5:2:-3:-6 long 98766E-2>")
; |
496 fq.roundToMagnitude(-2, MATH_CONTEXT_HALF_EVEN); | 497 } |
497 assertToStringAndHealth(fq, "<DecimalQuantity 5:2:-3:-6 long 98766E-2>"); | 498 |
498 } | 499 @Test |
499 | 500 public void testFitsInLong() { |
500 static void assertDoubleEquals(String message, double d1, double d2) { | 501 DecimalQuantity_DualStorageBCD quantity = new DecimalQuantity_DualStorag
eBCD(); |
501 boolean equal = (Math.abs(d1 - d2) < 1e-6) || (Math.abs((d1 - d2) / d1) < 1e
-6); | 502 quantity.setToInt(0); |
502 handleAssert(equal, message, d1, d2, null, false); | 503 assertTrue("Zero should fit", quantity.fitsInLong()); |
503 } | 504 quantity.setToInt(42); |
504 | 505 assertTrue("Small int should fit", quantity.fitsInLong()); |
505 static void assertBigDecimalEquals(String message, String d1, BigDecimal d2) { | 506 quantity.setToDouble(0.1); |
506 assertBigDecimalEquals(message, new BigDecimal(d1), d2); | 507 assertFalse("Fraction should not fit", quantity.fitsInLong()); |
507 } | 508 quantity.setToDouble(42.1); |
508 | 509 assertFalse("Fraction should not fit", quantity.fitsInLong()); |
509 static void assertBigDecimalEquals(String message, BigDecimal d1, BigDecimal d
2) { | 510 quantity.setToLong(1000000); |
510 boolean equal = d1.compareTo(d2) == 0; | 511 assertTrue("Large low-precision int should fit", quantity.fitsInLong()); |
511 handleAssert(equal, message, d1, d2, null, false); | 512 quantity.setToLong(1000000000000000000L); |
512 } | 513 assertTrue("10^19 should fit", quantity.fitsInLong()); |
513 | 514 quantity.setToLong(1234567890123456789L); |
514 static void assertToStringAndHealth(DecimalQuantity_DualStorageBCD fq, String
expected) { | 515 assertTrue("A number between 10^19 and max long should fit", quantity.fi
tsInLong()); |
515 String actual = fq.toString(); | 516 quantity.setToLong(9223372026854775808L); |
516 assertEquals("DecimalQuantity toString", expected, actual); | 517 assertTrue("A number less than max long but with similar digits should f
it", |
517 String health = fq.checkHealth(); | 518 quantity.fitsInLong()); |
518 assertNull("DecimalQuantity health", health); | 519 quantity.setToLong(9223372036854775806L); |
519 } | 520 assertTrue("One less than max long should fit", quantity.fitsInLong()); |
| 521 quantity.setToLong(9223372036854775807L); |
| 522 assertTrue("Max long should fit", quantity.fitsInLong()); |
| 523 quantity.setToBigInteger(new BigInteger("9223372036854775808")); |
| 524 assertFalse("One greater than max long long should not fit", quantity.fi
tsInLong()); |
| 525 quantity.setToBigInteger(new BigInteger("9223372046854775806")); |
| 526 assertFalse("A number between max long and 10^20 should not fit", quanti
ty.fitsInLong()); |
| 527 quantity.setToBigInteger(new BigInteger("10000000000000000000")); |
| 528 assertFalse("10^20 should not fit", quantity.fitsInLong()); |
| 529 } |
| 530 |
| 531 static void assertDoubleEquals(String message, double d1, double d2) { |
| 532 boolean equal = (Math.abs(d1 - d2) < 1e-6) || (Math.abs((d1 - d2) / d1)
< 1e-6); |
| 533 handleAssert(equal, message, d1, d2, null, false); |
| 534 } |
| 535 |
| 536 static void assertBigDecimalEquals(String message, String d1, BigDecimal d2)
{ |
| 537 assertBigDecimalEquals(message, new BigDecimal(d1), d2); |
| 538 } |
| 539 |
| 540 static void assertBigDecimalEquals(String message, BigDecimal d1, BigDecimal
d2) { |
| 541 boolean equal = d1.compareTo(d2) == 0; |
| 542 handleAssert(equal, message, d1, d2, null, false); |
| 543 } |
| 544 |
| 545 static void assertToStringAndHealth(DecimalQuantity_DualStorageBCD fq, Strin
g expected) { |
| 546 String actual = fq.toString(); |
| 547 assertEquals("DecimalQuantity toString", expected, actual); |
| 548 String health = fq.checkHealth(); |
| 549 assertNull("DecimalQuantity health", health); |
| 550 } |
520 } | 551 } |
LEFT | RIGHT |