1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.math;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertNull;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 import java.lang.reflect.Constructor;
27 import java.lang.reflect.Modifier;
28 import java.math.BigDecimal;
29 import java.math.BigInteger;
30 import java.math.RoundingMode;
31 import java.text.NumberFormat;
32 import java.text.ParseException;
33 import java.util.function.Function;
34
35 import org.apache.commons.lang3.AbstractLangTest;
36 import org.junit.jupiter.api.Test;
37
38
39
40
41 public class NumberUtilsTest extends AbstractLangTest {
42
43 private static void assertCreateNumberZero(final String number, final Object zero, final Object negativeZero) {
44 assertEquals(zero, NumberUtils.createNumber(number), () -> "Input: " + number);
45 assertEquals(zero, NumberUtils.createNumber("+" + number), () -> "Input: +" + number);
46 assertEquals(negativeZero, NumberUtils.createNumber("-" + number), () -> "Input: -" + number);
47 }
48
49 private boolean checkCreateNumber(final String val) {
50 try {
51 final Object obj = NumberUtils.createNumber(val);
52 return obj != null;
53 } catch (final NumberFormatException e) {
54 return false;
55 }
56 }
57
58 @Test
59 public void compareByte() {
60 assertTrue(NumberUtils.compare((byte) -3, (byte) 0) < 0);
61 assertEquals(0, NumberUtils.compare((byte) 113, (byte) 113));
62 assertTrue(NumberUtils.compare((byte) 123, (byte) 32) > 0);
63 }
64
65 @Test
66 public void compareInt() {
67 assertTrue(NumberUtils.compare(-3, 0) < 0);
68 assertEquals(0, NumberUtils.compare(113, 113));
69 assertTrue(NumberUtils.compare(213, 32) > 0);
70 }
71
72 private void compareIsCreatableWithCreateNumber(final String val, final boolean expected) {
73 final boolean isValid = NumberUtils.isCreatable(val);
74 final boolean canCreate = checkCreateNumber(val);
75 assertTrue(isValid == expected && canCreate == expected, "Expecting " + expected
76 + " for isCreatable/createNumber using \"" + val + "\" but got " + isValid + " and " + canCreate);
77 }
78
79 @SuppressWarnings("deprecation")
80 private void compareIsNumberWithCreateNumber(final String val, final boolean expected) {
81 final boolean isValid = NumberUtils.isNumber(val);
82 final boolean canCreate = checkCreateNumber(val);
83 assertTrue(isValid == expected && canCreate == expected, "Expecting " + expected
84 + " for isNumber/createNumber using \"" + val + "\" but got " + isValid + " and " + canCreate);
85 }
86
87 @Test
88 public void compareLong() {
89 assertTrue(NumberUtils.compare(-3L, 0L) < 0);
90 assertEquals(0, NumberUtils.compare(113L, 113L));
91 assertTrue(NumberUtils.compare(213L, 32L) > 0);
92 }
93
94 @Test
95 public void compareShort() {
96 assertTrue(NumberUtils.compare((short) -3, (short) 0) < 0);
97 assertEquals(0, NumberUtils.compare((short) 113, (short) 113));
98 assertTrue(NumberUtils.compare((short) 213, (short) 32) > 0);
99 }
100
101 private boolean isApplyNonNull(final String s, final Function<String, ?> function) {
102 try {
103 assertNotNull(function.apply(s));
104 return true;
105 } catch (final Exception e) {
106 if (!s.matches(".*\\s.*")) {
107 e.printStackTrace();
108 }
109 return false;
110 }
111 }
112
113 private boolean isNumberFormatParsable(final String s) {
114 final NumberFormat instance = NumberFormat.getInstance();
115 try {
116
117 assertNotNull(instance.parse(s));
118 return true;
119 } catch (final ParseException e) {
120 return false;
121 }
122 }
123
124 private boolean isNumberIntegerOnlyFormatParsable(final String s) {
125 final NumberFormat instance = NumberFormat.getInstance();
126 instance.setParseIntegerOnly(true);
127 try {
128
129 assertNotNull(instance.parse(s));
130 return true;
131 } catch (final ParseException e) {
132 return false;
133 }
134 }
135
136 private boolean isParsableByte(final String s) {
137 final boolean parsable = NumberUtils.isParsable(s);
138 assertTrue(isNumberFormatParsable(s), s);
139 assertTrue(isNumberIntegerOnlyFormatParsable(s), s);
140 assertEquals(parsable, isApplyNonNull(s, Byte::parseByte), s);
141 return parsable;
142 }
143
144 private boolean isParsableDouble(final String s) {
145 final boolean parsable = NumberUtils.isParsable(s);
146 assertTrue(isNumberFormatParsable(s), s);
147 assertTrue(isNumberIntegerOnlyFormatParsable(s), s);
148 assertEquals(parsable, isApplyNonNull(s, Double::parseDouble), s);
149 return parsable;
150 }
151
152 private boolean isParsableFloat(final String s) {
153 final boolean parsable = NumberUtils.isParsable(s);
154 assertTrue(isNumberFormatParsable(s), s);
155 assertTrue(isNumberIntegerOnlyFormatParsable(s), s);
156 assertEquals(parsable, isApplyNonNull(s, Float::parseFloat), s);
157 return parsable;
158 }
159
160 private boolean isParsableInteger(final String s) {
161 final boolean parsable = NumberUtils.isParsable(s);
162 assertTrue(isNumberFormatParsable(s), s);
163 assertTrue(isNumberIntegerOnlyFormatParsable(s), s);
164 assertEquals(parsable, isApplyNonNull(s, Integer::parseInt), s);
165 return parsable;
166 }
167
168 private boolean isParsableLong(final String s) {
169 final boolean parsable = NumberUtils.isParsable(s);
170 assertTrue(isNumberFormatParsable(s), s);
171 assertTrue(isNumberIntegerOnlyFormatParsable(s), s);
172 assertEquals(parsable, isApplyNonNull(s, Long::parseLong), s);
173 return parsable;
174 }
175
176 private boolean isParsableShort(final String s) {
177 final boolean parsable = NumberUtils.isParsable(s);
178 assertTrue(isNumberFormatParsable(s), s);
179 assertTrue(isNumberIntegerOnlyFormatParsable(s), s);
180 assertEquals(parsable, isApplyNonNull(s, Short::parseShort), s);
181 return parsable;
182 }
183
184
185
186
187 @Test
188 public void testBigIntegerToDoubleBigInteger() {
189 assertEquals(0.0d, NumberUtils.toDouble((BigDecimal) null), "toDouble(BigInteger) 1 failed");
190 assertEquals(8.5d, NumberUtils.toDouble(BigDecimal.valueOf(8.5d)), "toDouble(BigInteger) 2 failed");
191 }
192
193
194
195
196 @Test
197 public void testBigIntegerToDoubleBigIntegerD() {
198 assertEquals(1.1d, NumberUtils.toDouble((BigDecimal) null, 1.1d), "toDouble(BigInteger) 1 failed");
199 assertEquals(8.5d, NumberUtils.toDouble(BigDecimal.valueOf(8.5d), 1.1d), "toDouble(BigInteger) 2 failed");
200 }
201
202
203 @Test
204 public void testCompareDouble() {
205 assertEquals(0, Double.compare(Double.NaN, Double.NaN));
206 assertEquals(Double.compare(Double.NaN, Double.POSITIVE_INFINITY), +1);
207 assertEquals(Double.compare(Double.NaN, Double.MAX_VALUE), +1);
208 assertEquals(Double.compare(Double.NaN, 1.2d), +1);
209 assertEquals(Double.compare(Double.NaN, 0.0d), +1);
210 assertEquals(Double.compare(Double.NaN, -0.0d), +1);
211 assertEquals(Double.compare(Double.NaN, -1.2d), +1);
212 assertEquals(Double.compare(Double.NaN, -Double.MAX_VALUE), +1);
213 assertEquals(Double.compare(Double.NaN, Double.NEGATIVE_INFINITY), +1);
214
215 assertEquals(Double.compare(Double.POSITIVE_INFINITY, Double.NaN), -1);
216 assertEquals(0, Double.compare(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY));
217 assertEquals(Double.compare(Double.POSITIVE_INFINITY, Double.MAX_VALUE), +1);
218 assertEquals(Double.compare(Double.POSITIVE_INFINITY, 1.2d), +1);
219 assertEquals(Double.compare(Double.POSITIVE_INFINITY, 0.0d), +1);
220 assertEquals(Double.compare(Double.POSITIVE_INFINITY, -0.0d), +1);
221 assertEquals(Double.compare(Double.POSITIVE_INFINITY, -1.2d), +1);
222 assertEquals(Double.compare(Double.POSITIVE_INFINITY, -Double.MAX_VALUE), +1);
223 assertEquals(Double.compare(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY), +1);
224
225 assertEquals(Double.compare(Double.MAX_VALUE, Double.NaN), -1);
226 assertEquals(Double.compare(Double.MAX_VALUE, Double.POSITIVE_INFINITY), -1);
227 assertEquals(0, Double.compare(Double.MAX_VALUE, Double.MAX_VALUE));
228 assertEquals(Double.compare(Double.MAX_VALUE, 1.2d), +1);
229 assertEquals(Double.compare(Double.MAX_VALUE, 0.0d), +1);
230 assertEquals(Double.compare(Double.MAX_VALUE, -0.0d), +1);
231 assertEquals(Double.compare(Double.MAX_VALUE, -1.2d), +1);
232 assertEquals(Double.compare(Double.MAX_VALUE, -Double.MAX_VALUE), +1);
233 assertEquals(Double.compare(Double.MAX_VALUE, Double.NEGATIVE_INFINITY), +1);
234
235 assertEquals(Double.compare(1.2d, Double.NaN), -1);
236 assertEquals(Double.compare(1.2d, Double.POSITIVE_INFINITY), -1);
237 assertEquals(Double.compare(1.2d, Double.MAX_VALUE), -1);
238 assertEquals(0, Double.compare(1.2d, 1.2d));
239 assertEquals(Double.compare(1.2d, 0.0d), +1);
240 assertEquals(Double.compare(1.2d, -0.0d), +1);
241 assertEquals(Double.compare(1.2d, -1.2d), +1);
242 assertEquals(Double.compare(1.2d, -Double.MAX_VALUE), +1);
243 assertEquals(Double.compare(1.2d, Double.NEGATIVE_INFINITY), +1);
244
245 assertEquals(Double.compare(0.0d, Double.NaN), -1);
246 assertEquals(Double.compare(0.0d, Double.POSITIVE_INFINITY), -1);
247 assertEquals(Double.compare(0.0d, Double.MAX_VALUE), -1);
248 assertEquals(Double.compare(0.0d, 1.2d), -1);
249 assertEquals(0, Double.compare(0.0d, 0.0d));
250 assertEquals(Double.compare(0.0d, -0.0d), +1);
251 assertEquals(Double.compare(0.0d, -1.2d), +1);
252 assertEquals(Double.compare(0.0d, -Double.MAX_VALUE), +1);
253 assertEquals(Double.compare(0.0d, Double.NEGATIVE_INFINITY), +1);
254
255 assertEquals(Double.compare(-0.0d, Double.NaN), -1);
256 assertEquals(Double.compare(-0.0d, Double.POSITIVE_INFINITY), -1);
257 assertEquals(Double.compare(-0.0d, Double.MAX_VALUE), -1);
258 assertEquals(Double.compare(-0.0d, 1.2d), -1);
259 assertEquals(Double.compare(-0.0d, 0.0d), -1);
260 assertEquals(0, Double.compare(-0.0d, -0.0d));
261 assertEquals(Double.compare(-0.0d, -1.2d), +1);
262 assertEquals(Double.compare(-0.0d, -Double.MAX_VALUE), +1);
263 assertEquals(Double.compare(-0.0d, Double.NEGATIVE_INFINITY), +1);
264
265 assertEquals(Double.compare(-1.2d, Double.NaN), -1);
266 assertEquals(Double.compare(-1.2d, Double.POSITIVE_INFINITY), -1);
267 assertEquals(Double.compare(-1.2d, Double.MAX_VALUE), -1);
268 assertEquals(Double.compare(-1.2d, 1.2d), -1);
269 assertEquals(Double.compare(-1.2d, 0.0d), -1);
270 assertEquals(Double.compare(-1.2d, -0.0d), -1);
271 assertEquals(0, Double.compare(-1.2d, -1.2d));
272 assertEquals(Double.compare(-1.2d, -Double.MAX_VALUE), +1);
273 assertEquals(Double.compare(-1.2d, Double.NEGATIVE_INFINITY), +1);
274
275 assertEquals(Double.compare(-Double.MAX_VALUE, Double.NaN), -1);
276 assertEquals(Double.compare(-Double.MAX_VALUE, Double.POSITIVE_INFINITY), -1);
277 assertEquals(Double.compare(-Double.MAX_VALUE, Double.MAX_VALUE), -1);
278 assertEquals(Double.compare(-Double.MAX_VALUE, 1.2d), -1);
279 assertEquals(Double.compare(-Double.MAX_VALUE, 0.0d), -1);
280 assertEquals(Double.compare(-Double.MAX_VALUE, -0.0d), -1);
281 assertEquals(Double.compare(-Double.MAX_VALUE, -1.2d), -1);
282 assertEquals(0, Double.compare(-Double.MAX_VALUE, -Double.MAX_VALUE));
283 assertEquals(Double.compare(-Double.MAX_VALUE, Double.NEGATIVE_INFINITY), +1);
284
285 assertEquals(Double.compare(Double.NEGATIVE_INFINITY, Double.NaN), -1);
286 assertEquals(Double.compare(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), -1);
287 assertEquals(Double.compare(Double.NEGATIVE_INFINITY, Double.MAX_VALUE), -1);
288 assertEquals(Double.compare(Double.NEGATIVE_INFINITY, 1.2d), -1);
289 assertEquals(Double.compare(Double.NEGATIVE_INFINITY, 0.0d), -1);
290 assertEquals(Double.compare(Double.NEGATIVE_INFINITY, -0.0d), -1);
291 assertEquals(Double.compare(Double.NEGATIVE_INFINITY, -1.2d), -1);
292 assertEquals(Double.compare(Double.NEGATIVE_INFINITY, -Double.MAX_VALUE), -1);
293 assertEquals(0, Double.compare(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY));
294 }
295
296 @Test
297 public void testCompareFloat() {
298 assertEquals(0, Float.compare(Float.NaN, Float.NaN));
299 assertEquals(Float.compare(Float.NaN, Float.POSITIVE_INFINITY), +1);
300 assertEquals(Float.compare(Float.NaN, Float.MAX_VALUE), +1);
301 assertEquals(Float.compare(Float.NaN, 1.2f), +1);
302 assertEquals(Float.compare(Float.NaN, 0.0f), +1);
303 assertEquals(Float.compare(Float.NaN, -0.0f), +1);
304 assertEquals(Float.compare(Float.NaN, -1.2f), +1);
305 assertEquals(Float.compare(Float.NaN, -Float.MAX_VALUE), +1);
306 assertEquals(Float.compare(Float.NaN, Float.NEGATIVE_INFINITY), +1);
307
308 assertEquals(Float.compare(Float.POSITIVE_INFINITY, Float.NaN), -1);
309 assertEquals(0, Float.compare(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY));
310 assertEquals(Float.compare(Float.POSITIVE_INFINITY, Float.MAX_VALUE), +1);
311 assertEquals(Float.compare(Float.POSITIVE_INFINITY, 1.2f), +1);
312 assertEquals(Float.compare(Float.POSITIVE_INFINITY, 0.0f), +1);
313 assertEquals(Float.compare(Float.POSITIVE_INFINITY, -0.0f), +1);
314 assertEquals(Float.compare(Float.POSITIVE_INFINITY, -1.2f), +1);
315 assertEquals(Float.compare(Float.POSITIVE_INFINITY, -Float.MAX_VALUE), +1);
316 assertEquals(Float.compare(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY), +1);
317
318 assertEquals(Float.compare(Float.MAX_VALUE, Float.NaN), -1);
319 assertEquals(Float.compare(Float.MAX_VALUE, Float.POSITIVE_INFINITY), -1);
320 assertEquals(0, Float.compare(Float.MAX_VALUE, Float.MAX_VALUE));
321 assertEquals(Float.compare(Float.MAX_VALUE, 1.2f), +1);
322 assertEquals(Float.compare(Float.MAX_VALUE, 0.0f), +1);
323 assertEquals(Float.compare(Float.MAX_VALUE, -0.0f), +1);
324 assertEquals(Float.compare(Float.MAX_VALUE, -1.2f), +1);
325 assertEquals(Float.compare(Float.MAX_VALUE, -Float.MAX_VALUE), +1);
326 assertEquals(Float.compare(Float.MAX_VALUE, Float.NEGATIVE_INFINITY), +1);
327
328 assertEquals(Float.compare(1.2f, Float.NaN), -1);
329 assertEquals(Float.compare(1.2f, Float.POSITIVE_INFINITY), -1);
330 assertEquals(Float.compare(1.2f, Float.MAX_VALUE), -1);
331 assertEquals(0, Float.compare(1.2f, 1.2f));
332 assertEquals(Float.compare(1.2f, 0.0f), +1);
333 assertEquals(Float.compare(1.2f, -0.0f), +1);
334 assertEquals(Float.compare(1.2f, -1.2f), +1);
335 assertEquals(Float.compare(1.2f, -Float.MAX_VALUE), +1);
336 assertEquals(Float.compare(1.2f, Float.NEGATIVE_INFINITY), +1);
337
338 assertEquals(Float.compare(0.0f, Float.NaN), -1);
339 assertEquals(Float.compare(0.0f, Float.POSITIVE_INFINITY), -1);
340 assertEquals(Float.compare(0.0f, Float.MAX_VALUE), -1);
341 assertEquals(Float.compare(0.0f, 1.2f), -1);
342 assertEquals(0, Float.compare(0.0f, 0.0f));
343 assertEquals(Float.compare(0.0f, -0.0f), +1);
344 assertEquals(Float.compare(0.0f, -1.2f), +1);
345 assertEquals(Float.compare(0.0f, -Float.MAX_VALUE), +1);
346 assertEquals(Float.compare(0.0f, Float.NEGATIVE_INFINITY), +1);
347
348 assertEquals(Float.compare(-0.0f, Float.NaN), -1);
349 assertEquals(Float.compare(-0.0f, Float.POSITIVE_INFINITY), -1);
350 assertEquals(Float.compare(-0.0f, Float.MAX_VALUE), -1);
351 assertEquals(Float.compare(-0.0f, 1.2f), -1);
352 assertEquals(Float.compare(-0.0f, 0.0f), -1);
353 assertEquals(0, Float.compare(-0.0f, -0.0f));
354 assertEquals(Float.compare(-0.0f, -1.2f), +1);
355 assertEquals(Float.compare(-0.0f, -Float.MAX_VALUE), +1);
356 assertEquals(Float.compare(-0.0f, Float.NEGATIVE_INFINITY), +1);
357
358 assertEquals(Float.compare(-1.2f, Float.NaN), -1);
359 assertEquals(Float.compare(-1.2f, Float.POSITIVE_INFINITY), -1);
360 assertEquals(Float.compare(-1.2f, Float.MAX_VALUE), -1);
361 assertEquals(Float.compare(-1.2f, 1.2f), -1);
362 assertEquals(Float.compare(-1.2f, 0.0f), -1);
363 assertEquals(Float.compare(-1.2f, -0.0f), -1);
364 assertEquals(0, Float.compare(-1.2f, -1.2f));
365 assertEquals(Float.compare(-1.2f, -Float.MAX_VALUE), +1);
366 assertEquals(Float.compare(-1.2f, Float.NEGATIVE_INFINITY), +1);
367
368 assertEquals(Float.compare(-Float.MAX_VALUE, Float.NaN), -1);
369 assertEquals(Float.compare(-Float.MAX_VALUE, Float.POSITIVE_INFINITY), -1);
370 assertEquals(Float.compare(-Float.MAX_VALUE, Float.MAX_VALUE), -1);
371 assertEquals(Float.compare(-Float.MAX_VALUE, 1.2f), -1);
372 assertEquals(Float.compare(-Float.MAX_VALUE, 0.0f), -1);
373 assertEquals(Float.compare(-Float.MAX_VALUE, -0.0f), -1);
374 assertEquals(Float.compare(-Float.MAX_VALUE, -1.2f), -1);
375 assertEquals(0, Float.compare(-Float.MAX_VALUE, -Float.MAX_VALUE));
376 assertEquals(Float.compare(-Float.MAX_VALUE, Float.NEGATIVE_INFINITY), +1);
377
378 assertEquals(Float.compare(Float.NEGATIVE_INFINITY, Float.NaN), -1);
379 assertEquals(Float.compare(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY), -1);
380 assertEquals(Float.compare(Float.NEGATIVE_INFINITY, Float.MAX_VALUE), -1);
381 assertEquals(Float.compare(Float.NEGATIVE_INFINITY, 1.2f), -1);
382 assertEquals(Float.compare(Float.NEGATIVE_INFINITY, 0.0f), -1);
383 assertEquals(Float.compare(Float.NEGATIVE_INFINITY, -0.0f), -1);
384 assertEquals(Float.compare(Float.NEGATIVE_INFINITY, -1.2f), -1);
385 assertEquals(Float.compare(Float.NEGATIVE_INFINITY, -Float.MAX_VALUE), -1);
386 assertEquals(0, Float.compare(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY));
387 }
388
389 @SuppressWarnings("cast")
390 @Test
391 public void testConstants() {
392 assertTrue(NumberUtils.LONG_ZERO instanceof Long);
393 assertTrue(NumberUtils.LONG_ONE instanceof Long);
394 assertTrue(NumberUtils.LONG_MINUS_ONE instanceof Long);
395 assertTrue(NumberUtils.INTEGER_ZERO instanceof Integer);
396 assertTrue(NumberUtils.INTEGER_ONE instanceof Integer);
397 assertTrue(NumberUtils.INTEGER_MINUS_ONE instanceof Integer);
398 assertTrue(NumberUtils.SHORT_ZERO instanceof Short);
399 assertTrue(NumberUtils.SHORT_ONE instanceof Short);
400 assertTrue(NumberUtils.SHORT_MINUS_ONE instanceof Short);
401 assertTrue(NumberUtils.BYTE_ZERO instanceof Byte);
402 assertTrue(NumberUtils.BYTE_ONE instanceof Byte);
403 assertTrue(NumberUtils.BYTE_MINUS_ONE instanceof Byte);
404 assertTrue(NumberUtils.DOUBLE_ZERO instanceof Double);
405 assertTrue(NumberUtils.DOUBLE_ONE instanceof Double);
406 assertTrue(NumberUtils.DOUBLE_MINUS_ONE instanceof Double);
407 assertTrue(NumberUtils.FLOAT_ZERO instanceof Float);
408 assertTrue(NumberUtils.FLOAT_ONE instanceof Float);
409 assertTrue(NumberUtils.FLOAT_MINUS_ONE instanceof Float);
410
411 assertEquals(0, NumberUtils.LONG_ZERO.longValue());
412 assertEquals(1, NumberUtils.LONG_ONE.longValue());
413 assertEquals(NumberUtils.LONG_MINUS_ONE.longValue(), -1);
414 assertEquals(0, NumberUtils.INTEGER_ZERO.intValue());
415 assertEquals(1, NumberUtils.INTEGER_ONE.intValue());
416 assertEquals(NumberUtils.INTEGER_MINUS_ONE.intValue(), -1);
417 assertEquals(0, NumberUtils.SHORT_ZERO.shortValue());
418 assertEquals(1, NumberUtils.SHORT_ONE.shortValue());
419 assertEquals(NumberUtils.SHORT_MINUS_ONE.shortValue(), -1);
420 assertEquals(0, NumberUtils.BYTE_ZERO.byteValue());
421 assertEquals(1, NumberUtils.BYTE_ONE.byteValue());
422 assertEquals(NumberUtils.BYTE_MINUS_ONE.byteValue(), -1);
423 assertEquals(0.0d, NumberUtils.DOUBLE_ZERO.doubleValue());
424 assertEquals(1.0d, NumberUtils.DOUBLE_ONE.doubleValue());
425 assertEquals(NumberUtils.DOUBLE_MINUS_ONE.doubleValue(), -1.0d);
426 assertEquals(0.0f, NumberUtils.FLOAT_ZERO.floatValue());
427 assertEquals(1.0f, NumberUtils.FLOAT_ONE.floatValue());
428 assertEquals(NumberUtils.FLOAT_MINUS_ONE.floatValue(), -1.0f);
429 }
430
431 @Test
432 public void testConstructor() {
433 assertNotNull(new NumberUtils());
434 final Constructor<?>[] cons = NumberUtils.class.getDeclaredConstructors();
435 assertEquals(1, cons.length);
436 assertTrue(Modifier.isPublic(cons[0].getModifiers()));
437 assertTrue(Modifier.isPublic(NumberUtils.class.getModifiers()));
438 assertFalse(Modifier.isFinal(NumberUtils.class.getModifiers()));
439 }
440
441 @Test
442 public void testCreateBigDecimal() {
443 assertEquals(new BigDecimal("1234.5"), NumberUtils.createBigDecimal("1234.5"),
444 "createBigDecimal(String) failed");
445 assertNull(NumberUtils.createBigDecimal(null), "createBigDecimal(null) failed");
446 this.testCreateBigDecimalFailure("");
447 this.testCreateBigDecimalFailure(" ");
448 this.testCreateBigDecimalFailure("\b\t\n\f\r");
449
450 this.testCreateBigDecimalFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
451
452 this.testCreateBigDecimalFailure("-");
453
454 this.testCreateBigDecimalFailure("--");
455 this.testCreateBigDecimalFailure("--0");
456
457 this.testCreateBigDecimalFailure("+");
458
459 this.testCreateBigDecimalFailure("++");
460 this.testCreateBigDecimalFailure("++0");
461 }
462
463 protected void testCreateBigDecimalFailure(final String str) {
464 assertThrows(NumberFormatException.class, () -> NumberUtils.createBigDecimal(str),
465 "createBigDecimal(\"" + str + "\") should have failed.");
466 }
467
468 @Test
469 public void testCreateBigInteger() {
470 assertEquals(new BigInteger("12345"), NumberUtils.createBigInteger("12345"), "createBigInteger(String) failed");
471 assertNull(NumberUtils.createBigInteger(null), "createBigInteger(null) failed");
472 this.testCreateBigIntegerFailure("");
473 this.testCreateBigIntegerFailure(" ");
474 this.testCreateBigIntegerFailure("\b\t\n\f\r");
475
476 this.testCreateBigIntegerFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
477 assertEquals(new BigInteger("255"), NumberUtils.createBigInteger("0xff"), "createBigInteger(String) failed");
478 assertEquals(new BigInteger("255"), NumberUtils.createBigInteger("0Xff"), "createBigInteger(String) failed");
479 assertEquals(new BigInteger("255"), NumberUtils.createBigInteger("#ff"), "createBigInteger(String) failed");
480 assertEquals(new BigInteger("-255"), NumberUtils.createBigInteger("-0xff"), "createBigInteger(String) failed");
481 assertEquals(new BigInteger("255"), NumberUtils.createBigInteger("0377"), "createBigInteger(String) failed");
482 assertEquals(new BigInteger("-255"), NumberUtils.createBigInteger("-0377"), "createBigInteger(String) failed");
483 assertEquals(new BigInteger("-255"), NumberUtils.createBigInteger("-0377"), "createBigInteger(String) failed");
484 assertEquals(new BigInteger("-0"), NumberUtils.createBigInteger("-0"), "createBigInteger(String) failed");
485 assertEquals(new BigInteger("0"), NumberUtils.createBigInteger("0"), "createBigInteger(String) failed");
486 testCreateBigIntegerFailure("#");
487 testCreateBigIntegerFailure("-#");
488 testCreateBigIntegerFailure("0x");
489 testCreateBigIntegerFailure("-0x");
490
491 assertEquals(new BigInteger("+FFFFFFFFFFFFFFFF", 16), NumberUtils.createBigInteger("+0xFFFFFFFFFFFFFFFF"));
492 assertEquals(new BigInteger("+FFFFFFFFFFFFFFFF", 16), NumberUtils.createBigInteger("+#FFFFFFFFFFFFFFFF"));
493 assertEquals(new BigInteger("+1234567", 8), NumberUtils.createBigInteger("+01234567"));
494 }
495
496 protected void testCreateBigIntegerFailure(final String str) {
497 assertThrows(NumberFormatException.class, () -> NumberUtils.createBigInteger(str),
498 "createBigInteger(\"" + str + "\") should have failed.");
499 }
500
501 @Test
502 public void testCreateDouble() {
503 assertEquals(Double.valueOf("1234.5"), NumberUtils.createDouble("1234.5"), "createDouble(String) failed");
504 assertNull(NumberUtils.createDouble(null), "createDouble(null) failed");
505 this.testCreateDoubleFailure("");
506 this.testCreateDoubleFailure(" ");
507 this.testCreateDoubleFailure("\b\t\n\f\r");
508
509 this.testCreateDoubleFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
510 }
511
512 protected void testCreateDoubleFailure(final String str) {
513 assertThrows(NumberFormatException.class, () -> NumberUtils.createDouble(str),
514 "createDouble(\"" + str + "\") should have failed.");
515 }
516
517 @Test
518 public void testCreateFloat() {
519 assertEquals(Float.valueOf("1234.5"), NumberUtils.createFloat("1234.5"), "createFloat(String) failed");
520 assertNull(NumberUtils.createFloat(null), "createFloat(null) failed");
521 this.testCreateFloatFailure("");
522 this.testCreateFloatFailure(" ");
523 this.testCreateFloatFailure("\b\t\n\f\r");
524
525 this.testCreateFloatFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
526 }
527
528 protected void testCreateFloatFailure(final String str) {
529 assertThrows(NumberFormatException.class, () -> NumberUtils.createFloat(str),
530 "createFloat(\"" + str + "\") should have failed.");
531 }
532
533 @Test
534 public void testCreateInteger() {
535 assertEquals(Integer.valueOf("12345"), NumberUtils.createInteger("12345"), "createInteger(String) failed");
536 assertNull(NumberUtils.createInteger(null), "createInteger(null) failed");
537 this.testCreateIntegerFailure("");
538 this.testCreateIntegerFailure(" ");
539 this.testCreateIntegerFailure("\b\t\n\f\r");
540
541 this.testCreateIntegerFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
542
543 assertEquals(Integer.decode("+0xF"), NumberUtils.createInteger("+0xF"));
544 }
545
546 protected void testCreateIntegerFailure(final String str) {
547 assertThrows(NumberFormatException.class, () -> NumberUtils.createInteger(str),
548 "createInteger(\"" + str + "\") should have failed.");
549 }
550
551 @Test
552 public void testCreateLong() {
553 assertEquals(Long.valueOf("12345"), NumberUtils.createLong("12345"), "createLong(String) failed");
554 assertNull(NumberUtils.createLong(null), "createLong(null) failed");
555 this.testCreateLongFailure("");
556 this.testCreateLongFailure(" ");
557 this.testCreateLongFailure("\b\t\n\f\r");
558
559 this.testCreateLongFailure("\u00A0\uFEFF\u000B\u000C\u001C\u001D\u001E\u001F");
560
561 assertEquals(Long.decode("+0xFFFFFFFF"), NumberUtils.createLong("+0xFFFFFFFF"));
562 }
563
564 protected void testCreateLongFailure(final String str) {
565 assertThrows(NumberFormatException.class, () -> NumberUtils.createLong(str),
566 "createLong(\"" + str + "\") should have failed.");
567 }
568
569 @Test
570 public void testCreateNumber() {
571
572 assertEquals(Float.valueOf("1234.5"), NumberUtils.createNumber("1234.5"), "createNumber(String) 1 failed");
573 assertEquals(Integer.valueOf("12345"), NumberUtils.createNumber("12345"), "createNumber(String) 2 failed");
574 assertEquals(Double.valueOf("1234.5"), NumberUtils.createNumber("1234.5D"), "createNumber(String) 3 failed");
575 assertEquals(Double.valueOf("1234.5"), NumberUtils.createNumber("1234.5d"), "createNumber(String) 3 failed");
576 assertEquals(Float.valueOf("1234.5"), NumberUtils.createNumber("1234.5F"), "createNumber(String) 4 failed");
577 assertEquals(Float.valueOf("1234.5"), NumberUtils.createNumber("1234.5f"), "createNumber(String) 4 failed");
578 assertEquals(Long.valueOf(Integer.MAX_VALUE + 1L), NumberUtils.createNumber("" + (Integer.MAX_VALUE + 1L)),
579 "createNumber(String) 5 failed");
580 assertEquals(Long.valueOf(12345), NumberUtils.createNumber("12345L"), "createNumber(String) 6 failed");
581 assertEquals(Long.valueOf(12345), NumberUtils.createNumber("12345l"), "createNumber(String) 6 failed");
582 assertEquals(Float.valueOf("-1234.5"), NumberUtils.createNumber("-1234.5"), "createNumber(String) 7 failed");
583 assertEquals(Integer.valueOf("-12345"), NumberUtils.createNumber("-12345"), "createNumber(String) 8 failed");
584 assertEquals(0xFADE, NumberUtils.createNumber("0xFADE").intValue(), "createNumber(String) 9a failed");
585 assertEquals(0xFADE, NumberUtils.createNumber("0Xfade").intValue(), "createNumber(String) 9b failed");
586 assertEquals(-0xFADE, NumberUtils.createNumber("-0xFADE").intValue(), "createNumber(String) 10a failed");
587 assertEquals(-0xFADE, NumberUtils.createNumber("-0Xfade").intValue(), "createNumber(String) 10b failed");
588 assertEquals(Double.valueOf("1.1E200"), NumberUtils.createNumber("1.1E200"), "createNumber(String) 11 failed");
589 assertEquals(Float.valueOf("1.1E20"), NumberUtils.createNumber("1.1E20"), "createNumber(String) 12 failed");
590 assertEquals(Double.valueOf("-1.1E200"), NumberUtils.createNumber("-1.1E200"),
591 "createNumber(String) 13 failed");
592 assertEquals(Double.valueOf("1.1E-200"), NumberUtils.createNumber("1.1E-200"),
593 "createNumber(String) 14 failed");
594 assertNull(NumberUtils.createNumber(null), "createNumber(null) failed");
595 assertEquals(new BigInteger("12345678901234567890"), NumberUtils.createNumber("12345678901234567890L"),
596 "createNumber(String) failed");
597
598 assertEquals(new BigDecimal("1.1E-700"), NumberUtils.createNumber("1.1E-700F"),
599 "createNumber(String) 15 failed");
600
601 assertEquals(Long.valueOf("10" + Integer.MAX_VALUE), NumberUtils.createNumber("10" + Integer.MAX_VALUE + "L"),
602 "createNumber(String) 16 failed");
603 assertEquals(Long.valueOf("10" + Integer.MAX_VALUE), NumberUtils.createNumber("10" + Integer.MAX_VALUE),
604 "createNumber(String) 17 failed");
605 assertEquals(new BigInteger("10" + Long.MAX_VALUE), NumberUtils.createNumber("10" + Long.MAX_VALUE),
606 "createNumber(String) 18 failed");
607
608
609 assertEquals(Float.valueOf("2."), NumberUtils.createNumber("2."), "createNumber(String) LANG-521 failed");
610
611
612 assertFalse(checkCreateNumber("1eE"), "createNumber(String) succeeded");
613
614
615 assertEquals(Double.valueOf(Double.MAX_VALUE), NumberUtils.createNumber("" + Double.MAX_VALUE),
616 "createNumber(String) LANG-693 failed");
617
618
619
620 final Number bigNum = NumberUtils.createNumber("-1.1E-700F");
621 assertNotNull(bigNum);
622 assertEquals(BigDecimal.class, bigNum.getClass());
623
624
625 assertEquals(Double.valueOf("-160952.54"), NumberUtils.createNumber("-160952.54"),
626 "createNumber(String) LANG-1018 failed");
627
628 assertEquals(Double.valueOf("6264583.33"), NumberUtils.createNumber("6264583.33"),
629 "createNumber(String) LANG-1187 failed");
630
631 assertEquals(Double.valueOf("193343.82"), NumberUtils.createNumber("193343.82"),
632 "createNumber(String) LANG-1215 failed");
633
634 assertEquals(Double.valueOf("001234.5678"), NumberUtils.createNumber("001234.5678"),
635 "createNumber(String) LANG-1060a failed");
636 assertEquals(Double.valueOf("+001234.5678"), NumberUtils.createNumber("+001234.5678"),
637 "createNumber(String) LANG-1060b failed");
638 assertEquals(Double.valueOf("-001234.5678"), NumberUtils.createNumber("-001234.5678"),
639 "createNumber(String) LANG-1060c failed");
640 assertEquals(Double.valueOf("0000.00000"), NumberUtils.createNumber("0000.00000d"),
641 "createNumber(String) LANG-1060d failed");
642 assertEquals(Float.valueOf("001234.56"), NumberUtils.createNumber("001234.56"),
643 "createNumber(String) LANG-1060e failed");
644 assertEquals(Float.valueOf("+001234.56"), NumberUtils.createNumber("+001234.56"),
645 "createNumber(String) LANG-1060f failed");
646 assertEquals(Float.valueOf("-001234.56"), NumberUtils.createNumber("-001234.56"),
647 "createNumber(String) LANG-1060g failed");
648 assertEquals(Float.valueOf("0000.10"), NumberUtils.createNumber("0000.10"),
649 "createNumber(String) LANG-1060h failed");
650 assertEquals(Float.valueOf("001.1E20"), NumberUtils.createNumber("001.1E20"),
651 "createNumber(String) LANG-1060i failed");
652 assertEquals(Float.valueOf("+001.1E20"), NumberUtils.createNumber("+001.1E20"),
653 "createNumber(String) LANG-1060j failed");
654 assertEquals(Float.valueOf("-001.1E20"), NumberUtils.createNumber("-001.1E20"),
655 "createNumber(String) LANG-1060k failed");
656 assertEquals(Double.valueOf("001.1E200"), NumberUtils.createNumber("001.1E200"),
657 "createNumber(String) LANG-1060l failed");
658 assertEquals(Double.valueOf("+001.1E200"), NumberUtils.createNumber("+001.1E200"),
659 "createNumber(String) LANG-1060m failed");
660 assertEquals(Double.valueOf("-001.1E200"), NumberUtils.createNumber("-001.1E200"),
661 "createNumber(String) LANG-1060n failed");
662
663 assertEquals(Integer.decode("+0xF"), NumberUtils.createNumber("+0xF"),
664 "createNumber(String) LANG-1645a failed");
665 assertEquals(Long.decode("+0xFFFFFFFF"), NumberUtils.createNumber("+0xFFFFFFFF"),
666 "createNumber(String) LANG-1645b failed");
667 assertEquals(new BigInteger("+FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("+0xFFFFFFFFFFFFFFFF"),
668 "createNumber(String) LANG-1645c failed");
669 }
670
671 @Test
672
673 public void testCreateNumberFailure_1() {
674 assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("--1.1E-700F"));
675 }
676
677 @Test
678
679 public void testCreateNumberFailure_2() {
680 assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("-1.1E+0-7e00"));
681 }
682
683 @Test
684
685 public void testCreateNumberFailure_3() {
686 assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("-11E+0-7e00"));
687 }
688
689 @Test
690
691 public void testCreateNumberFailure_4() {
692 assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("1eE+00001"));
693 }
694
695 @Test
696
697 public void testCreateNumberFailure_5() {
698 assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("1234.5ff"));
699 }
700
701 @Test
702
703 public void testCreateNumberFailure_6() {
704 assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("1234.5FF"));
705 }
706
707 @Test
708
709 public void testCreateNumberFailure_7() {
710 assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("1234.5dd"));
711 }
712
713 @Test
714
715 public void testCreateNumberFailure_8() {
716 assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("1234.5DD"));
717 }
718
719
720
721 @Test
722 public void testCreateNumberMagnitude() {
723
724 assertEquals(Float.valueOf(Float.MAX_VALUE), NumberUtils.createNumber("3.4028235e+38"));
725 assertEquals(Double.valueOf(3.4028236e+38), NumberUtils.createNumber("3.4028236e+38"));
726
727
728 assertEquals(Double.valueOf(Double.MAX_VALUE), NumberUtils.createNumber("1.7976931348623157e+308"));
729
730 assertEquals(new BigDecimal("1.7976931348623159e+308"), NumberUtils.createNumber("1.7976931348623159e+308"));
731
732
733 final Double nonZero1 = Double.valueOf((double) Float.MIN_VALUE / 2);
734 assertEquals(nonZero1, NumberUtils.createNumber(nonZero1.toString()));
735 assertEquals(nonZero1, NumberUtils.createNumber(nonZero1 + "F"));
736
737
738 final BigDecimal nonZero2 = new BigDecimal("4.9e-325");
739 assertEquals(nonZero2, NumberUtils.createNumber("4.9e-325"));
740 assertEquals(nonZero2, NumberUtils.createNumber("4.9e-325D"));
741 final BigDecimal nonZero3 = new BigDecimal("1e-325");
742 assertEquals(nonZero3, NumberUtils.createNumber("1e-325"));
743 assertEquals(nonZero3, NumberUtils.createNumber("1e-325D"));
744 final BigDecimal nonZero4 = new BigDecimal("0.1e-325");
745 assertEquals(nonZero4, NumberUtils.createNumber("0.1e-325"));
746 assertEquals(nonZero4, NumberUtils.createNumber("0.1e-325D"));
747
748 assertEquals(Integer.valueOf(0x12345678), NumberUtils.createNumber("0x12345678"));
749 assertEquals(Long.valueOf(0x123456789L), NumberUtils.createNumber("0x123456789"));
750
751 assertEquals(Long.valueOf(0x7fffffffffffffffL), NumberUtils.createNumber("0x7fffffffffffffff"));
752
753 assertEquals(new BigInteger("7fffffffffffffff0", 16), NumberUtils.createNumber("0x7fffffffffffffff0"));
754
755 assertEquals(Long.valueOf(0x7fffffffffffffffL), NumberUtils.createNumber("#7fffffffffffffff"));
756 assertEquals(new BigInteger("7fffffffffffffff0", 16), NumberUtils.createNumber("#7fffffffffffffff0"));
757
758 assertEquals(Integer.valueOf(017777777777), NumberUtils.createNumber("017777777777"));
759 assertEquals(Long.valueOf(037777777777L), NumberUtils.createNumber("037777777777"));
760
761
762 assertEquals(Long.valueOf(0777777777777777777777L), NumberUtils.createNumber("0777777777777777777777"));
763
764 assertEquals(new BigInteger("1777777777777777777777", 8), NumberUtils.createNumber("01777777777777777777777"));
765 }
766
767
768
769
770 @Test
771 public void testCreateNumberZero() {
772
773 assertEquals(Integer.valueOf(0), NumberUtils.createNumber("0"));
774 assertEquals(Integer.valueOf(0), NumberUtils.createNumber("-0"));
775 assertEquals(Long.valueOf(0), NumberUtils.createNumber("0L"));
776 assertEquals(Long.valueOf(0), NumberUtils.createNumber("-0L"));
777
778
779
780
781
782
783 final int[] exponents = {-2345, 0, 13};
784 final String[] zeros = {"0.", ".0", "0.0", "0"};
785 final Float f0 = Float.valueOf(0);
786 final Float fn0 = Float.valueOf(-0F);
787 final Double d0 = Double.valueOf(0);
788 final Double dn0 = Double.valueOf(-0D);
789
790 for (final String zero : zeros) {
791
792
793 if (zero.indexOf('.') != -1) {
794 assertCreateNumberZero(zero, f0, fn0);
795 }
796 for (final int exp : exponents) {
797 assertCreateNumberZero(zero + "e" + exp, f0, fn0);
798 }
799
800 assertCreateNumberZero(zero + "F", f0, fn0);
801 assertCreateNumberZero(zero + "D", d0, dn0);
802 for (final int exp : exponents) {
803 final String number = zero + "e" + exp;
804 assertCreateNumberZero(number + "F", f0, fn0);
805 assertCreateNumberZero(number + "D", d0, dn0);
806 }
807 }
808 }
809
810 @Test
811 public void testInvalidNumber() {
812 assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("E123e.3"));
813 assertThrows(NumberFormatException.class, () -> NumberUtils.createNumber("-"));
814 }
815
816
817
818
819
820 @Test
821 public void testIsCreatable() {
822 compareIsCreatableWithCreateNumber("12345", true);
823 compareIsCreatableWithCreateNumber("1234.5", true);
824 compareIsCreatableWithCreateNumber(".12345", true);
825 compareIsCreatableWithCreateNumber("1234E5", true);
826 compareIsCreatableWithCreateNumber("1234E+5", true);
827 compareIsCreatableWithCreateNumber("1234E-5", true);
828 compareIsCreatableWithCreateNumber("123.4E5", true);
829 compareIsCreatableWithCreateNumber("-1234", true);
830 compareIsCreatableWithCreateNumber("-1234.5", true);
831 compareIsCreatableWithCreateNumber("-.12345", true);
832 compareIsCreatableWithCreateNumber("-1234E5", true);
833 compareIsCreatableWithCreateNumber("0", true);
834 compareIsCreatableWithCreateNumber("0.1", true);
835 compareIsCreatableWithCreateNumber("-0", true);
836 compareIsCreatableWithCreateNumber("01234", true);
837 compareIsCreatableWithCreateNumber("-01234", true);
838 compareIsCreatableWithCreateNumber("-0xABC123", true);
839 compareIsCreatableWithCreateNumber("-0x0", true);
840 compareIsCreatableWithCreateNumber("123.4E21D", true);
841 compareIsCreatableWithCreateNumber("-221.23F", true);
842 compareIsCreatableWithCreateNumber("22338L", true);
843
844 compareIsCreatableWithCreateNumber(null, false);
845 compareIsCreatableWithCreateNumber("", false);
846 compareIsCreatableWithCreateNumber(" ", false);
847 compareIsCreatableWithCreateNumber("\r\n\t", false);
848 compareIsCreatableWithCreateNumber("--2.3", false);
849 compareIsCreatableWithCreateNumber(".12.3", false);
850 compareIsCreatableWithCreateNumber("-123E", false);
851 compareIsCreatableWithCreateNumber("-123E+-212", false);
852 compareIsCreatableWithCreateNumber("-123E2.12", false);
853 compareIsCreatableWithCreateNumber("0xGF", false);
854 compareIsCreatableWithCreateNumber("0xFAE-1", false);
855 compareIsCreatableWithCreateNumber(".", false);
856 compareIsCreatableWithCreateNumber("-0ABC123", false);
857 compareIsCreatableWithCreateNumber("123.4E-D", false);
858 compareIsCreatableWithCreateNumber("123.4ED", false);
859 compareIsCreatableWithCreateNumber("1234E5l", false);
860 compareIsCreatableWithCreateNumber("11a", false);
861 compareIsCreatableWithCreateNumber("1a", false);
862 compareIsCreatableWithCreateNumber("a", false);
863 compareIsCreatableWithCreateNumber("11g", false);
864 compareIsCreatableWithCreateNumber("11z", false);
865 compareIsCreatableWithCreateNumber("11def", false);
866 compareIsCreatableWithCreateNumber("11d11", false);
867 compareIsCreatableWithCreateNumber("11 11", false);
868 compareIsCreatableWithCreateNumber(" 1111", false);
869 compareIsCreatableWithCreateNumber("1111 ", false);
870
871 compareIsCreatableWithCreateNumber("2.", true);
872 compareIsCreatableWithCreateNumber("1.1L", false);
873 compareIsCreatableWithCreateNumber("+0xF", true);
874 compareIsCreatableWithCreateNumber("+0xFFFFFFFF", true);
875 compareIsCreatableWithCreateNumber("+0xFFFFFFFFFFFFFFFF", true);
876 compareIsCreatableWithCreateNumber(".0", true);
877 compareIsCreatableWithCreateNumber("0.", true);
878 compareIsCreatableWithCreateNumber("0.D", true);
879 compareIsCreatableWithCreateNumber("0e1", true);
880 compareIsCreatableWithCreateNumber("0e1D", true);
881 compareIsCreatableWithCreateNumber(".D", false);
882 compareIsCreatableWithCreateNumber(".e10", false);
883 compareIsCreatableWithCreateNumber(".e10D", false);
884 }
885
886 @Test
887 public void testIsDigits() {
888 assertFalse(NumberUtils.isDigits(null), "isDigits(null) failed");
889 assertFalse(NumberUtils.isDigits(""), "isDigits('') failed");
890 assertTrue(NumberUtils.isDigits("12345"), "isDigits(String) failed");
891 assertFalse(NumberUtils.isDigits("1234.5"), "isDigits(String) neg 1 failed");
892 assertFalse(NumberUtils.isDigits("1ab"), "isDigits(String) neg 3 failed");
893 assertFalse(NumberUtils.isDigits("abc"), "isDigits(String) neg 4 failed");
894 }
895
896
897
898
899
900 @Test
901 public void testIsNumber() {
902 compareIsNumberWithCreateNumber("12345", true);
903 compareIsNumberWithCreateNumber("1234.5", true);
904 compareIsNumberWithCreateNumber(".12345", true);
905 compareIsNumberWithCreateNumber("1234E5", true);
906 compareIsNumberWithCreateNumber("1234E+5", true);
907 compareIsNumberWithCreateNumber("1234E-5", true);
908 compareIsNumberWithCreateNumber("123.4E5", true);
909 compareIsNumberWithCreateNumber("-1234", true);
910 compareIsNumberWithCreateNumber("-1234.5", true);
911 compareIsNumberWithCreateNumber("-.12345", true);
912 compareIsNumberWithCreateNumber("-0001.12345", true);
913 compareIsNumberWithCreateNumber("-000.12345", true);
914 compareIsNumberWithCreateNumber("+00.12345", true);
915 compareIsNumberWithCreateNumber("+0002.12345", true);
916 compareIsNumberWithCreateNumber("-1234E5", true);
917 compareIsNumberWithCreateNumber("0", true);
918 compareIsNumberWithCreateNumber("-0", true);
919 compareIsNumberWithCreateNumber("01234", true);
920 compareIsNumberWithCreateNumber("-01234", true);
921 compareIsNumberWithCreateNumber("-0xABC123", true);
922 compareIsNumberWithCreateNumber("-0x0", true);
923 compareIsNumberWithCreateNumber("123.4E21D", true);
924 compareIsNumberWithCreateNumber("-221.23F", true);
925 compareIsNumberWithCreateNumber("22338L", true);
926
927 compareIsNumberWithCreateNumber(null, false);
928 compareIsNumberWithCreateNumber("", false);
929 compareIsNumberWithCreateNumber(" ", false);
930 compareIsNumberWithCreateNumber("\r\n\t", false);
931 compareIsNumberWithCreateNumber("--2.3", false);
932
933 compareIsNumberWithCreateNumber(".12.3", false);
934 compareIsNumberWithCreateNumber("-123E", false);
935 compareIsNumberWithCreateNumber("-123E+-212", false);
936 compareIsNumberWithCreateNumber("-123E2.12", false);
937 compareIsNumberWithCreateNumber("0xGF", false);
938 compareIsNumberWithCreateNumber("0xFAE-1", false);
939 compareIsNumberWithCreateNumber(".", false);
940 compareIsNumberWithCreateNumber("-0ABC123", false);
941 compareIsNumberWithCreateNumber("123.4E-D", false);
942 compareIsNumberWithCreateNumber("123.4ED", false);
943 compareIsNumberWithCreateNumber("+000E.12345", false);
944 compareIsNumberWithCreateNumber("-000E.12345", false);
945 compareIsNumberWithCreateNumber("1234E5l", false);
946 compareIsNumberWithCreateNumber("11a", false);
947 compareIsNumberWithCreateNumber("1a", false);
948 compareIsNumberWithCreateNumber("a", false);
949 compareIsNumberWithCreateNumber("11g", false);
950 compareIsNumberWithCreateNumber("11z", false);
951 compareIsNumberWithCreateNumber("11def", false);
952 compareIsNumberWithCreateNumber("11d11", false);
953 compareIsNumberWithCreateNumber("11 11", false);
954 compareIsNumberWithCreateNumber(" 1111", false);
955 compareIsNumberWithCreateNumber("1111 ", false);
956
957 compareIsNumberWithCreateNumber("2.", true);
958 compareIsNumberWithCreateNumber("1.1L", false);
959 compareIsNumberWithCreateNumber("+0xF", true);
960 compareIsNumberWithCreateNumber("+0xFFFFFFFF", true);
961 compareIsNumberWithCreateNumber("+0xFFFFFFFFFFFFFFFF", true);
962 compareIsNumberWithCreateNumber(".0", true);
963 compareIsNumberWithCreateNumber("0.", true);
964 compareIsNumberWithCreateNumber("0.D", true);
965 compareIsNumberWithCreateNumber("0e1", true);
966 compareIsNumberWithCreateNumber("0e1D", true);
967 compareIsNumberWithCreateNumber(".D", false);
968 compareIsNumberWithCreateNumber(".e10", false);
969 compareIsNumberWithCreateNumber(".e10D", false);
970 }
971
972 @Test
973 public void testIsNumberLANG1252() {
974 compareIsNumberWithCreateNumber("+2", true);
975 compareIsNumberWithCreateNumber("+2.0", true);
976 }
977
978 @Test
979 public void testIsNumberLANG1385() {
980 compareIsNumberWithCreateNumber("L", false);
981 }
982
983 @Test
984 public void testIsNumberLANG971() {
985 compareIsNumberWithCreateNumber("0085", false);
986 compareIsNumberWithCreateNumber("085", false);
987 compareIsNumberWithCreateNumber("08", false);
988 compareIsNumberWithCreateNumber("07", true);
989 compareIsNumberWithCreateNumber("00", true);
990 }
991
992 @Test
993 public void testIsNumberLANG972() {
994 compareIsNumberWithCreateNumber("0xABCD", true);
995 compareIsNumberWithCreateNumber("0XABCD", true);
996 }
997
998 @Test
999 public void testIsNumberLANG992() {
1000 compareIsNumberWithCreateNumber("0.0", true);
1001 compareIsNumberWithCreateNumber("0.4790", true);
1002 }
1003
1004 @Test
1005 public void testIsParsable() {
1006 assertFalse(NumberUtils.isParsable(null));
1007 assertFalse(NumberUtils.isParsable(""));
1008 assertFalse(NumberUtils.isParsable("0xC1AB"));
1009 assertFalse(NumberUtils.isParsable("65CBA2"));
1010 assertFalse(NumberUtils.isParsable("pendro"));
1011 assertFalse(NumberUtils.isParsable("64, 2"));
1012 assertFalse(NumberUtils.isParsable("64.2.2"));
1013 assertFalse(NumberUtils.isParsable("64."));
1014 assertFalse(NumberUtils.isParsable("64L"));
1015 assertFalse(NumberUtils.isParsable("-"));
1016 assertFalse(NumberUtils.isParsable("--2"));
1017 assertTrue(NumberUtils.isParsable("64.2"));
1018 assertTrue(NumberUtils.isParsable("64"));
1019 assertTrue(NumberUtils.isParsable("018"));
1020 assertTrue(NumberUtils.isParsable(".18"));
1021 assertTrue(NumberUtils.isParsable("-65"));
1022 assertTrue(NumberUtils.isParsable("-018"));
1023 assertTrue(NumberUtils.isParsable("-018.2"));
1024 assertTrue(NumberUtils.isParsable("-.236"));
1025 }
1026
1027 @Test
1028 public void testLang1087() {
1029
1030 assertEquals(Float.class, NumberUtils.createNumber("0.0").getClass());
1031 assertEquals(Float.valueOf("0.0"), NumberUtils.createNumber("0.0"));
1032
1033 assertEquals(Float.class, NumberUtils.createNumber("+0.0").getClass());
1034 assertEquals(Float.valueOf("+0.0"), NumberUtils.createNumber("+0.0"));
1035
1036 assertEquals(Float.class, NumberUtils.createNumber("-0.0").getClass());
1037 assertEquals(Float.valueOf("-0.0"), NumberUtils.createNumber("-0.0"));
1038 }
1039
1040 @Test
1041 public void testLANG1252() {
1042 compareIsCreatableWithCreateNumber("+2", true);
1043 compareIsCreatableWithCreateNumber("+2.0", true);
1044 }
1045
1046 @Test
1047 public void testLang1729IsParsableByte() {
1048 assertTrue(isParsableByte("1"));
1049 assertFalse(isParsableByte("1 2 3"));
1050 assertTrue(isParsableByte("123"));
1051 assertFalse(isParsableByte("1 2 3"));
1052 }
1053
1054 @Test
1055 public void testLang1729IsParsableDouble() {
1056 assertTrue(isParsableDouble("1"));
1057 assertFalse(isParsableDouble("1 2 3"));
1058
1059
1060 assertFalse(isParsableDouble("1 2 3"));
1061 }
1062
1063 @Test
1064 public void testLang1729IsParsableFloat() {
1065 assertTrue(isParsableFloat("1"));
1066 assertFalse(isParsableFloat("1 2 3"));
1067
1068
1069 assertFalse(isParsableFloat("1 2 3"));
1070 }
1071
1072 @Test
1073 public void testLang1729IsParsableInteger() {
1074 assertTrue(isParsableInteger("1"));
1075 assertFalse(isParsableInteger("1 2 3"));
1076 assertTrue(isParsableInteger("123"));
1077 assertFalse(isParsableInteger("1 2 3"));
1078 }
1079
1080 @Test
1081 public void testLang1729IsParsableLong() {
1082 assertTrue(isParsableLong("1"));
1083 assertFalse(isParsableLong("1 2 3"));
1084 assertTrue(isParsableLong("123"));
1085 assertFalse(isParsableLong("1 2 3"));
1086 }
1087
1088 @Test
1089 public void testLang1729IsParsableShort() {
1090 assertTrue(isParsableShort("1"));
1091 assertFalse(isParsableShort("1 2 3"));
1092 assertTrue(isParsableShort("123"));
1093 assertFalse(isParsableShort("1 2 3"));
1094 }
1095
1096 @Test
1097 public void testLang300() {
1098 NumberUtils.createNumber("-1l");
1099 NumberUtils.createNumber("01l");
1100 NumberUtils.createNumber("1l");
1101 }
1102
1103 @Test
1104 public void testLang381() {
1105 assertTrue(Double.isNaN(NumberUtils.min(1.2, 2.5, Double.NaN)));
1106 assertTrue(Double.isNaN(NumberUtils.max(1.2, 2.5, Double.NaN)));
1107 assertTrue(Float.isNaN(NumberUtils.min(1.2f, 2.5f, Float.NaN)));
1108 assertTrue(Float.isNaN(NumberUtils.max(1.2f, 2.5f, Float.NaN)));
1109
1110 final double[] a = {1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN};
1111 assertTrue(Double.isNaN(NumberUtils.max(a)));
1112 assertTrue(Double.isNaN(NumberUtils.min(a)));
1113
1114 final double[] b = {Double.NaN, 1.2, Double.NaN, 3.7, 27.0, 42.0, Double.NaN};
1115 assertTrue(Double.isNaN(NumberUtils.max(b)));
1116 assertTrue(Double.isNaN(NumberUtils.min(b)));
1117
1118 final float[] aF = {1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN};
1119 assertTrue(Float.isNaN(NumberUtils.max(aF)));
1120
1121 final float[] bF = {Float.NaN, 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN};
1122 assertTrue(Float.isNaN(NumberUtils.max(bF)));
1123 }
1124
1125 @Test
1126 public void TestLang747() {
1127 assertEquals(Integer.valueOf(0x8000), NumberUtils.createNumber("0x8000"));
1128 assertEquals(Integer.valueOf(0x80000), NumberUtils.createNumber("0x80000"));
1129 assertEquals(Integer.valueOf(0x800000), NumberUtils.createNumber("0x800000"));
1130 assertEquals(Integer.valueOf(0x8000000), NumberUtils.createNumber("0x8000000"));
1131 assertEquals(Integer.valueOf(0x7FFFFFFF), NumberUtils.createNumber("0x7FFFFFFF"));
1132 assertEquals(Long.valueOf(0x80000000L), NumberUtils.createNumber("0x80000000"));
1133 assertEquals(Long.valueOf(0xFFFFFFFFL), NumberUtils.createNumber("0xFFFFFFFF"));
1134
1135
1136 assertEquals(Integer.valueOf(0x8000000), NumberUtils.createNumber("0x08000000"));
1137 assertEquals(Integer.valueOf(0x7FFFFFFF), NumberUtils.createNumber("0x007FFFFFFF"));
1138 assertEquals(Long.valueOf(0x80000000L), NumberUtils.createNumber("0x080000000"));
1139 assertEquals(Long.valueOf(0xFFFFFFFFL), NumberUtils.createNumber("0x00FFFFFFFF"));
1140
1141 assertEquals(Long.valueOf(0x800000000L), NumberUtils.createNumber("0x800000000"));
1142 assertEquals(Long.valueOf(0x8000000000L), NumberUtils.createNumber("0x8000000000"));
1143 assertEquals(Long.valueOf(0x80000000000L), NumberUtils.createNumber("0x80000000000"));
1144 assertEquals(Long.valueOf(0x800000000000L), NumberUtils.createNumber("0x800000000000"));
1145 assertEquals(Long.valueOf(0x8000000000000L), NumberUtils.createNumber("0x8000000000000"));
1146 assertEquals(Long.valueOf(0x80000000000000L), NumberUtils.createNumber("0x80000000000000"));
1147 assertEquals(Long.valueOf(0x800000000000000L), NumberUtils.createNumber("0x800000000000000"));
1148 assertEquals(Long.valueOf(0x7FFFFFFFFFFFFFFFL), NumberUtils.createNumber("0x7FFFFFFFFFFFFFFF"));
1149
1150 assertEquals(new BigInteger("8000000000000000", 16), NumberUtils.createNumber("0x8000000000000000"));
1151 assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("0xFFFFFFFFFFFFFFFF"));
1152
1153
1154 assertEquals(Long.valueOf(0x80000000000000L), NumberUtils.createNumber("0x00080000000000000"));
1155 assertEquals(Long.valueOf(0x800000000000000L), NumberUtils.createNumber("0x0800000000000000"));
1156 assertEquals(Long.valueOf(0x7FFFFFFFFFFFFFFFL), NumberUtils.createNumber("0x07FFFFFFFFFFFFFFF"));
1157
1158 assertEquals(new BigInteger("8000000000000000", 16), NumberUtils.createNumber("0x00008000000000000000"));
1159 assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("0x0FFFFFFFFFFFFFFFF"));
1160 }
1161
1162 @Test
1163 public void testLANG971() {
1164 compareIsCreatableWithCreateNumber("0085", false);
1165 compareIsCreatableWithCreateNumber("085", false);
1166 compareIsCreatableWithCreateNumber("08", false);
1167 compareIsCreatableWithCreateNumber("07", true);
1168 compareIsCreatableWithCreateNumber("00", true);
1169 }
1170
1171 @Test
1172 public void testLANG972() {
1173 compareIsCreatableWithCreateNumber("0xABCD", true);
1174 compareIsCreatableWithCreateNumber("0XABCD", true);
1175 }
1176
1177 @Test
1178 public void testLANG992() {
1179 compareIsCreatableWithCreateNumber("0.0", true);
1180 compareIsCreatableWithCreateNumber("0.4790", true);
1181 }
1182
1183 @Test
1184 public void testMaxByte() {
1185 assertEquals((byte) 5, NumberUtils.max((byte) 5), "max(byte[]) failed for array length 1");
1186 assertEquals((byte) 9, NumberUtils.max((byte) 6, (byte) 9), "max(byte[]) failed for array length 2");
1187 assertEquals((byte) 10, NumberUtils.max((byte) -10, (byte) -5, (byte) 0, (byte) 5, (byte) 10),
1188 "max(byte[]) failed for array length 5");
1189 assertEquals((byte) 10, NumberUtils.max((byte) -10, (byte) -5, (byte) 0, (byte) 5, (byte) 10));
1190 assertEquals((byte) 10, NumberUtils.max((byte) -5, (byte) 0, (byte) 10, (byte) 5, (byte) -10));
1191 }
1192
1193 @Test
1194 public void testMaxByte_emptyArray() {
1195 assertThrows(IllegalArgumentException.class, NumberUtils::max);
1196 }
1197
1198 @Test
1199 public void testMaxByte_nullArray() {
1200 assertThrows(NullPointerException.class, () -> NumberUtils.max((byte[]) null));
1201 }
1202
1203 @Test
1204 public void testMaxDouble() {
1205 final double[] d = null;
1206 assertThrows(NullPointerException.class, () -> NumberUtils.max(d), "No exception was thrown for null input.");
1207
1208 assertThrows(IllegalArgumentException.class, NumberUtils::max, "No exception was thrown for empty input.");
1209
1210 assertEquals(5.1f, NumberUtils.max(5.1f), "max(double[]) failed for array length 1");
1211 assertEquals(9.2f, NumberUtils.max(6.3f, 9.2f), "max(double[]) failed for array length 2");
1212 assertEquals(10.4f, NumberUtils.max(-10.5f, -5.6f, 0, 5.7f, 10.4f), "max(double[]) failed for float length 5");
1213 assertEquals(10, NumberUtils.max(-10, -5, 0, 5, 10), 0.0001);
1214 assertEquals(10, NumberUtils.max(-5, 0, 10, 5, -10), 0.0001);
1215 }
1216
1217 @Test
1218 public void testMaxDouble_emptyArray() {
1219 assertThrows(IllegalArgumentException.class, NumberUtils::max);
1220 }
1221
1222 @Test
1223 public void testMaxDouble_nullArray() {
1224 assertThrows(NullPointerException.class, () -> NumberUtils.max((double[]) null));
1225 }
1226
1227 @Test
1228 public void testMaxFloat() {
1229 assertEquals(5.1f, NumberUtils.max(5.1f), "max(float[]) failed for array length 1");
1230 assertEquals(9.2f, NumberUtils.max(6.3f, 9.2f), "max(float[]) failed for array length 2");
1231 assertEquals(10.4f, NumberUtils.max(-10.5f, -5.6f, 0, 5.7f, 10.4f), "max(float[]) failed for float length 5");
1232 assertEquals(10, NumberUtils.max(-10, -5, 0, 5, 10), 0.0001f);
1233 assertEquals(10, NumberUtils.max(-5, 0, 10, 5, -10), 0.0001f);
1234 }
1235
1236 @Test
1237 public void testMaxFloat_emptyArray() {
1238 assertThrows(IllegalArgumentException.class, NumberUtils::max);
1239 }
1240
1241 @Test
1242 public void testMaxFloat_nullArray() {
1243 assertThrows(NullPointerException.class, () -> NumberUtils.max((float[]) null));
1244 }
1245
1246 @Test
1247 public void testMaximumByte() {
1248 final byte low = 123;
1249 final byte mid = 123 + 1;
1250 final byte high = 123 + 2;
1251 assertEquals(high, NumberUtils.max(low, mid, high), "maximum(byte, byte, byte) 1 failed");
1252 assertEquals(high, NumberUtils.max(mid, low, high), "maximum(byte, byte, byte) 2 failed");
1253 assertEquals(high, NumberUtils.max(mid, high, low), "maximum(byte, byte, byte) 3 failed");
1254 assertEquals(high, NumberUtils.max(high, mid, high), "maximum(byte, byte, byte) 4 failed");
1255 }
1256
1257 @Test
1258 public void testMaximumDouble() {
1259 final double low = 12.3;
1260 final double mid = 12.3 + 1;
1261 final double high = 12.3 + 2;
1262 assertEquals(high, NumberUtils.max(low, mid, high), 0.0001);
1263 assertEquals(high, NumberUtils.max(mid, low, high), 0.0001);
1264 assertEquals(high, NumberUtils.max(mid, high, low), 0.0001);
1265 assertEquals(mid, NumberUtils.max(low, mid, low), 0.0001);
1266 assertEquals(high, NumberUtils.max(high, mid, high), 0.0001);
1267 }
1268
1269 @Test
1270 public void testMaximumFloat() {
1271 final float low = 12.3f;
1272 final float mid = 12.3f + 1;
1273 final float high = 12.3f + 2;
1274 assertEquals(high, NumberUtils.max(low, mid, high), 0.0001f);
1275 assertEquals(high, NumberUtils.max(mid, low, high), 0.0001f);
1276 assertEquals(high, NumberUtils.max(mid, high, low), 0.0001f);
1277 assertEquals(mid, NumberUtils.max(low, mid, low), 0.0001f);
1278 assertEquals(high, NumberUtils.max(high, mid, high), 0.0001f);
1279 }
1280
1281 @Test
1282 public void testMaximumInt() {
1283 assertEquals(12345, NumberUtils.max(12345, 12345 - 1, 12345 - 2), "maximum(int, int, int) 1 failed");
1284 assertEquals(12345, NumberUtils.max(12345 - 1, 12345, 12345 - 2), "maximum(int, int, int) 2 failed");
1285 assertEquals(12345, NumberUtils.max(12345 - 1, 12345 - 2, 12345), "maximum(int, int, int) 3 failed");
1286 assertEquals(12345, NumberUtils.max(12345 - 1, 12345, 12345), "maximum(int, int, int) 4 failed");
1287 assertEquals(12345, NumberUtils.max(12345, 12345, 12345), "maximum(int, int, int) 5 failed");
1288 }
1289
1290 @Test
1291 public void testMaximumLong() {
1292 assertEquals(12345L, NumberUtils.max(12345L, 12345L - 1L, 12345L - 2L), "maximum(long, long, long) 1 failed");
1293 assertEquals(12345L, NumberUtils.max(12345L - 1L, 12345L, 12345L - 2L), "maximum(long, long, long) 2 failed");
1294 assertEquals(12345L, NumberUtils.max(12345L - 1L, 12345L - 2L, 12345L), "maximum(long, long, long) 3 failed");
1295 assertEquals(12345L, NumberUtils.max(12345L - 1L, 12345L, 12345L), "maximum(long, long, long) 4 failed");
1296 assertEquals(12345L, NumberUtils.max(12345L, 12345L, 12345L), "maximum(long, long, long) 5 failed");
1297 }
1298
1299 @Test
1300 public void testMaximumShort() {
1301 final short low = 1234;
1302 final short mid = 1234 + 1;
1303 final short high = 1234 + 2;
1304 assertEquals(high, NumberUtils.max(low, mid, high), "maximum(short, short, short) 1 failed");
1305 assertEquals(high, NumberUtils.max(mid, low, high), "maximum(short, short, short) 2 failed");
1306 assertEquals(high, NumberUtils.max(mid, high, low), "maximum(short, short, short) 3 failed");
1307 assertEquals(high, NumberUtils.max(high, mid, high), "maximum(short, short, short) 4 failed");
1308 }
1309
1310 @Test
1311 public void testMaxInt() {
1312 assertEquals(5, NumberUtils.max(5), "max(int[]) failed for array length 1");
1313 assertEquals(9, NumberUtils.max(6, 9), "max(int[]) failed for array length 2");
1314 assertEquals(10, NumberUtils.max(-10, -5, 0, 5, 10), "max(int[]) failed for array length 5");
1315 assertEquals(10, NumberUtils.max(-10, -5, 0, 5, 10));
1316 assertEquals(10, NumberUtils.max(-5, 0, 10, 5, -10));
1317 }
1318
1319 @Test
1320 public void testMaxInt_emptyArray() {
1321 assertThrows(IllegalArgumentException.class, NumberUtils::max);
1322 }
1323
1324 @Test
1325 public void testMaxInt_nullArray() {
1326 assertThrows(NullPointerException.class, () -> NumberUtils.max((int[]) null));
1327 }
1328
1329 @Test
1330 public void testMaxLong() {
1331 assertEquals(5L, NumberUtils.max(5L), "max(long[]) failed for array length 1");
1332 assertEquals(9L, NumberUtils.max(6L, 9L), "max(long[]) failed for array length 2");
1333 assertEquals(10L, NumberUtils.max(-10L, -5L, 0L, 5L, 10L), "max(long[]) failed for array length 5");
1334 assertEquals(10L, NumberUtils.max(-10L, -5L, 0L, 5L, 10L));
1335 assertEquals(10L, NumberUtils.max(-5L, 0L, 10L, 5L, -10L));
1336 }
1337
1338 @Test
1339 public void testMaxLong_emptyArray() {
1340 assertThrows(IllegalArgumentException.class, NumberUtils::max);
1341 }
1342
1343 @Test
1344 public void testMaxLong_nullArray() {
1345 assertThrows(NullPointerException.class, () -> NumberUtils.max((long[]) null));
1346 }
1347
1348 @Test
1349 public void testMaxShort() {
1350 assertEquals((short) 5, NumberUtils.max((short) 5), "max(short[]) failed for array length 1");
1351 assertEquals((short) 9, NumberUtils.max((short) 6, (short) 9), "max(short[]) failed for array length 2");
1352 assertEquals((short) 10, NumberUtils.max((short) -10, (short) -5, (short) 0, (short) 5, (short) 10),
1353 "max(short[]) failed for array length 5");
1354 assertEquals((short) 10, NumberUtils.max((short) -10, (short) -5, (short) 0, (short) 5, (short) 10));
1355 assertEquals((short) 10, NumberUtils.max((short) -5, (short) 0, (short) 10, (short) 5, (short) -10));
1356 }
1357
1358 @Test
1359 public void testMaxShort_emptyArray() {
1360 assertThrows(IllegalArgumentException.class, NumberUtils::max);
1361 }
1362
1363 @Test
1364 public void testMaxShort_nullArray() {
1365 assertThrows(NullPointerException.class, () -> NumberUtils.max((short[]) null));
1366 }
1367
1368 @Test
1369 public void testMinByte() {
1370 assertEquals((byte) 5, NumberUtils.min((byte) 5), "min(byte[]) failed for array length 1");
1371 assertEquals((byte) 6, NumberUtils.min((byte) 6, (byte) 9), "min(byte[]) failed for array length 2");
1372
1373 assertEquals((byte) -10, NumberUtils.min((byte) -10, (byte) -5, (byte) 0, (byte) 5, (byte) 10));
1374 assertEquals((byte) -10, NumberUtils.min((byte) -5, (byte) 0, (byte) -10, (byte) 5, (byte) 10));
1375 }
1376
1377 @Test
1378 public void testMinByte_emptyArray() {
1379 assertThrows(IllegalArgumentException.class, NumberUtils::min);
1380 }
1381
1382 @Test
1383 public void testMinByte_nullArray() {
1384 assertThrows(NullPointerException.class, () -> NumberUtils.min((byte[]) null));
1385 }
1386
1387 @Test
1388 public void testMinDouble() {
1389 assertEquals(5.12, NumberUtils.min(5.12), "min(double[]) failed for array length 1");
1390 assertEquals(6.23, NumberUtils.min(6.23, 9.34), "min(double[]) failed for array length 2");
1391 assertEquals(-10.45, NumberUtils.min(-10.45, -5.56, 0, 5.67, 10.78), "min(double[]) failed for array length 5");
1392 assertEquals(-10, NumberUtils.min(-10, -5, 0, 5, 10), 0.0001);
1393 assertEquals(-10, NumberUtils.min(-5, 0, -10, 5, 10), 0.0001);
1394 assertEquals(5.12, NumberUtils.min(6.11, 5.12));
1395 }
1396
1397 @Test
1398 public void testMinDouble_emptyArray() {
1399 assertThrows(IllegalArgumentException.class, NumberUtils::min);
1400 }
1401
1402 @Test
1403 public void testMinDouble_nullArray() {
1404 assertThrows(NullPointerException.class, () -> NumberUtils.min((double[]) null));
1405 }
1406
1407 @Test
1408 public void testMinFloat() {
1409 assertEquals(5.9f, NumberUtils.min(5.9f), "min(float[]) failed for array length 1");
1410 assertEquals(6.8f, NumberUtils.min(6.8f, 9.7f), "min(float[]) failed for array length 2");
1411 assertEquals(-10.6f, NumberUtils.min(-10.6f, -5.5f, 0, 5.4f, 10.3f), "min(float[]) failed for array length 5");
1412 assertEquals(-10, NumberUtils.min(-10, -5, 0, 5, 10), 0.0001f);
1413 assertEquals(-10, NumberUtils.min(-5, 0, -10, 5, 10), 0.0001f);
1414 assertEquals(Float.NaN, NumberUtils.min(6.8f, Float.NaN));
1415 assertEquals(3.7f, NumberUtils.min(6.8f, 3.7f));
1416 }
1417
1418 @Test
1419 public void testMinFloat_emptyArray() {
1420 assertThrows(IllegalArgumentException.class, NumberUtils::min);
1421 }
1422
1423 @Test
1424 public void testMinFloat_nullArray() {
1425 assertThrows(NullPointerException.class, () -> NumberUtils.min((float[]) null));
1426 }
1427
1428 @Test
1429 public void testMinimumByte() {
1430 final byte low = 123;
1431 final byte mid = 123 + 1;
1432 final byte high = 123 + 2;
1433 assertEquals(low, NumberUtils.min(low, mid, high), "minimum(byte, byte, byte) 1 failed");
1434 assertEquals(low, NumberUtils.min(mid, low, high), "minimum(byte, byte, byte) 2 failed");
1435 assertEquals(low, NumberUtils.min(mid, high, low), "minimum(byte, byte, byte) 3 failed");
1436 assertEquals(low, NumberUtils.min(low, mid, low), "minimum(byte, byte, byte) 4 failed");
1437 }
1438
1439 @Test
1440 public void testMinimumDouble() {
1441 final double low = 12.3;
1442 final double mid = 12.3 + 1;
1443 final double high = 12.3 + 2;
1444 assertEquals(low, NumberUtils.min(low, mid, high), 0.0001);
1445 assertEquals(low, NumberUtils.min(mid, low, high), 0.0001);
1446 assertEquals(low, NumberUtils.min(mid, high, low), 0.0001);
1447 assertEquals(low, NumberUtils.min(low, mid, low), 0.0001);
1448 assertEquals(mid, NumberUtils.min(high, mid, high), 0.0001);
1449 }
1450
1451 @Test
1452 public void testMinimumFloat() {
1453 final float low = 12.3f;
1454 final float mid = 12.3f + 1;
1455 final float high = 12.3f + 2;
1456 assertEquals(low, NumberUtils.min(low, mid, high), 0.0001f);
1457 assertEquals(low, NumberUtils.min(mid, low, high), 0.0001f);
1458 assertEquals(low, NumberUtils.min(mid, high, low), 0.0001f);
1459 assertEquals(low, NumberUtils.min(low, mid, low), 0.0001f);
1460 assertEquals(mid, NumberUtils.min(high, mid, high), 0.0001f);
1461 }
1462
1463 @Test
1464 public void testMinimumInt() {
1465 assertEquals(12345, NumberUtils.min(12345, 12345 + 1, 12345 + 2), "minimum(int, int, int) 1 failed");
1466 assertEquals(12345, NumberUtils.min(12345 + 1, 12345, 12345 + 2), "minimum(int, int, int) 2 failed");
1467 assertEquals(12345, NumberUtils.min(12345 + 1, 12345 + 2, 12345), "minimum(int, int, int) 3 failed");
1468 assertEquals(12345, NumberUtils.min(12345 + 1, 12345, 12345), "minimum(int, int, int) 4 failed");
1469 assertEquals(12345, NumberUtils.min(12345, 12345, 12345), "minimum(int, int, int) 5 failed");
1470 }
1471
1472 @Test
1473 public void testMinimumLong() {
1474 assertEquals(12345L, NumberUtils.min(12345L, 12345L + 1L, 12345L + 2L), "minimum(long, long, long) 1 failed");
1475 assertEquals(12345L, NumberUtils.min(12345L + 1L, 12345L, 12345 + 2L), "minimum(long, long, long) 2 failed");
1476 assertEquals(12345L, NumberUtils.min(12345L + 1L, 12345L + 2L, 12345L), "minimum(long, long, long) 3 failed");
1477 assertEquals(12345L, NumberUtils.min(12345L + 1L, 12345L, 12345L), "minimum(long, long, long) 4 failed");
1478 assertEquals(12345L, NumberUtils.min(12345L, 12345L, 12345L), "minimum(long, long, long) 5 failed");
1479 }
1480
1481 @Test
1482 public void testMinimumShort() {
1483 final short low = 1234;
1484 final short mid = 1234 + 1;
1485 final short high = 1234 + 2;
1486 assertEquals(low, NumberUtils.min(low, mid, high), "minimum(short, short, short) 1 failed");
1487 assertEquals(low, NumberUtils.min(mid, low, high), "minimum(short, short, short) 2 failed");
1488 assertEquals(low, NumberUtils.min(mid, high, low), "minimum(short, short, short) 3 failed");
1489 assertEquals(low, NumberUtils.min(low, mid, low), "minimum(short, short, short) 4 failed");
1490 }
1491
1492 @Test
1493 public void testMinInt() {
1494 assertEquals(5, NumberUtils.min(5), "min(int[]) failed for array length 1");
1495 assertEquals(6, NumberUtils.min(6, 9), "min(int[]) failed for array length 2");
1496
1497 assertEquals(-10, NumberUtils.min(-10, -5, 0, 5, 10));
1498 assertEquals(-10, NumberUtils.min(-5, 0, -10, 5, 10));
1499 }
1500
1501 @Test
1502 public void testMinInt_emptyArray() {
1503 assertThrows(IllegalArgumentException.class, NumberUtils::min);
1504 }
1505
1506 @Test
1507 public void testMinInt_nullArray() {
1508 assertThrows(NullPointerException.class, () -> NumberUtils.min((int[]) null));
1509 }
1510
1511 @Test
1512 public void testMinLong() {
1513 assertEquals(5L, NumberUtils.min(5L), "min(long[]) failed for array length 1");
1514 assertEquals(6L, NumberUtils.min(6L, 9L), "min(long[]) failed for array length 2");
1515
1516 assertEquals(-10L, NumberUtils.min(-10L, -5L, 0L, 5L, 10L));
1517 assertEquals(-10L, NumberUtils.min(-5L, 0L, -10L, 5L, 10L));
1518 }
1519
1520 @Test
1521 public void testMinLong_emptyArray() {
1522 assertThrows(IllegalArgumentException.class, NumberUtils::min);
1523 }
1524
1525 @Test
1526 public void testMinLong_nullArray() {
1527 assertThrows(NullPointerException.class, () -> NumberUtils.min((long[]) null));
1528 }
1529
1530 @Test
1531 public void testMinShort() {
1532 assertEquals((short) 5, NumberUtils.min((short) 5), "min(short[]) failed for array length 1");
1533 assertEquals((short) 6, NumberUtils.min((short) 6, (short) 9), "min(short[]) failed for array length 2");
1534
1535 assertEquals((short) -10, NumberUtils.min((short) -10, (short) -5, (short) 0, (short) 5, (short) 10));
1536 assertEquals((short) -10, NumberUtils.min((short) -5, (short) 0, (short) -10, (short) 5, (short) 10));
1537 }
1538
1539 @Test
1540 public void testMinShort_emptyArray() {
1541 assertThrows(IllegalArgumentException.class, NumberUtils::min);
1542 }
1543
1544 @Test
1545 public void testMinShort_nullArray() {
1546 assertThrows(NullPointerException.class, () -> NumberUtils.min((short[]) null));
1547 }
1548
1549
1550
1551
1552 @Test
1553 public void testStringCreateNumberEnsureNoPrecisionLoss() {
1554 assertTrue(NumberUtils.createNumber("1.23") instanceof Float);
1555 assertTrue(NumberUtils.createNumber("3.40282354e+38") instanceof Double);
1556 assertTrue(NumberUtils.createNumber("1.797693134862315759e+308") instanceof BigDecimal);
1557
1558 assertTrue(NumberUtils.createNumber("001.12") instanceof Float);
1559 assertTrue(NumberUtils.createNumber("-001.12") instanceof Float);
1560 assertTrue(NumberUtils.createNumber("+001.12") instanceof Float);
1561 assertTrue(NumberUtils.createNumber("003.40282354e+38") instanceof Double);
1562 assertTrue(NumberUtils.createNumber("-003.40282354e+38") instanceof Double);
1563 assertTrue(NumberUtils.createNumber("+003.40282354e+38") instanceof Double);
1564 assertTrue(NumberUtils.createNumber("0001.797693134862315759e+308") instanceof BigDecimal);
1565 assertTrue(NumberUtils.createNumber("-001.797693134862315759e+308") instanceof BigDecimal);
1566 assertTrue(NumberUtils.createNumber("+001.797693134862315759e+308") instanceof BigDecimal);
1567
1568 assertTrue(NumberUtils.createNumber("2.2250738585072014E-308") instanceof Double);
1569 assertTrue(NumberUtils.createNumber("2.2250738585072014E-308D") instanceof Double);
1570 assertTrue(NumberUtils.createNumber("2.2250738585072014E-308F") instanceof Double);
1571 assertTrue(NumberUtils.createNumber("4.9E-324") instanceof Double);
1572 assertTrue(NumberUtils.createNumber("4.9E-324D") instanceof Double);
1573 assertTrue(NumberUtils.createNumber("4.9E-324F") instanceof Double);
1574 assertTrue(NumberUtils.createNumber("1.7976931348623157E308") instanceof Double);
1575 assertTrue(NumberUtils.createNumber("1.7976931348623157E308D") instanceof Double);
1576 assertTrue(NumberUtils.createNumber("1.7976931348623157E308F") instanceof Double);
1577 assertTrue(NumberUtils.createNumber("4.9e-324D") instanceof Double);
1578 assertTrue(NumberUtils.createNumber("4.9e-324F") instanceof Double);
1579 }
1580
1581
1582
1583
1584 @Test
1585 public void testStringToDoubleString() {
1586 assertEquals(NumberUtils.toDouble("-1.2345"), -1.2345d, "toDouble(String) 1 failed");
1587 assertEquals(1.2345d, NumberUtils.toDouble("1.2345"), "toDouble(String) 2 failed");
1588 assertEquals(0.0d, NumberUtils.toDouble("abc"), "toDouble(String) 3 failed");
1589
1590 assertEquals(NumberUtils.toDouble("-001.2345"), -1.2345d, "toDouble(String) 4 failed");
1591 assertEquals(1.2345d, NumberUtils.toDouble("+001.2345"), "toDouble(String) 5 failed");
1592 assertEquals(1.2345d, NumberUtils.toDouble("001.2345"), "toDouble(String) 6 failed");
1593 assertEquals(0d, NumberUtils.toDouble("000.00000"), "toDouble(String) 7 failed");
1594
1595 assertEquals(NumberUtils.toDouble(Double.MAX_VALUE + ""), Double.MAX_VALUE,
1596 "toDouble(Double.MAX_VALUE) failed");
1597 assertEquals(NumberUtils.toDouble(Double.MIN_VALUE + ""), Double.MIN_VALUE,
1598 "toDouble(Double.MIN_VALUE) failed");
1599 assertEquals(0.0d, NumberUtils.toDouble(""), "toDouble(empty) failed");
1600 assertEquals(0.0d, NumberUtils.toDouble((String) null), "toDouble(null) failed");
1601 }
1602
1603
1604
1605
1606 @Test
1607 public void testStringToDoubleStringD() {
1608 assertEquals(1.2345d, NumberUtils.toDouble("1.2345", 5.1d), "toDouble(String, int) 1 failed");
1609 assertEquals(5.0d, NumberUtils.toDouble("a", 5.0d), "toDouble(String, int) 2 failed");
1610
1611 assertEquals(1.2345d, NumberUtils.toDouble("001.2345", 5.1d), "toDouble(String, int) 3 failed");
1612 assertEquals(NumberUtils.toDouble("-001.2345", 5.1d), -1.2345d, "toDouble(String, int) 4 failed");
1613 assertEquals(1.2345d, NumberUtils.toDouble("+001.2345", 5.1d), "toDouble(String, int) 5 failed");
1614 assertEquals(0d, NumberUtils.toDouble("000.00", 5.1d), "toDouble(String, int) 7 failed");
1615 assertEquals(5.1d, NumberUtils.toDouble("", 5.1d));
1616 assertEquals(5.1d, NumberUtils.toDouble((String) null, 5.1d));
1617 }
1618
1619
1620
1621
1622 @Test
1623 public void testToByteString() {
1624 assertEquals(123, NumberUtils.toByte("123"), "toByte(String) 1 failed");
1625 assertEquals(0, NumberUtils.toByte("abc"), "toByte(String) 2 failed");
1626 assertEquals(0, NumberUtils.toByte(""), "toByte(empty) failed");
1627 assertEquals(0, NumberUtils.toByte(null), "toByte(null) failed");
1628 }
1629
1630
1631
1632
1633 @Test
1634 public void testToByteStringI() {
1635 assertEquals(123, NumberUtils.toByte("123", (byte) 5), "toByte(String, byte) 1 failed");
1636 assertEquals(5, NumberUtils.toByte("12.3", (byte) 5), "toByte(String, byte) 2 failed");
1637 assertEquals(5, NumberUtils.toByte("", (byte) 5));
1638 assertEquals(5, NumberUtils.toByte(null, (byte) 5));
1639 }
1640
1641
1642
1643
1644 @Test
1645 public void testToFloatString() {
1646 assertEquals(NumberUtils.toFloat("-1.2345"), -1.2345f, "toFloat(String) 1 failed");
1647 assertEquals(1.2345f, NumberUtils.toFloat("1.2345"), "toFloat(String) 2 failed");
1648 assertEquals(0.0f, NumberUtils.toFloat("abc"), "toFloat(String) 3 failed");
1649
1650 assertEquals(NumberUtils.toFloat("-001.2345"), -1.2345f, "toFloat(String) 4 failed");
1651 assertEquals(1.2345f, NumberUtils.toFloat("+001.2345"), "toFloat(String) 5 failed");
1652 assertEquals(1.2345f, NumberUtils.toFloat("001.2345"), "toFloat(String) 6 failed");
1653 assertEquals(0f, NumberUtils.toFloat("000.00"), "toFloat(String) 7 failed");
1654
1655 assertEquals(NumberUtils.toFloat(Float.MAX_VALUE + ""), Float.MAX_VALUE, "toFloat(Float.MAX_VALUE) failed");
1656 assertEquals(NumberUtils.toFloat(Float.MIN_VALUE + ""), Float.MIN_VALUE, "toFloat(Float.MIN_VALUE) failed");
1657 assertEquals(0.0f, NumberUtils.toFloat(""), "toFloat(empty) failed");
1658 assertEquals(0.0f, NumberUtils.toFloat(null), "toFloat(null) failed");
1659 }
1660
1661
1662
1663
1664 @Test
1665 public void testToFloatStringF() {
1666 assertEquals(1.2345f, NumberUtils.toFloat("1.2345", 5.1f), "toFloat(String, int) 1 failed");
1667 assertEquals(5.0f, NumberUtils.toFloat("a", 5.0f), "toFloat(String, int) 2 failed");
1668
1669 assertEquals(5.0f, NumberUtils.toFloat("-001Z.2345", 5.0f), "toFloat(String, int) 3 failed");
1670 assertEquals(5.0f, NumberUtils.toFloat("+001AB.2345", 5.0f), "toFloat(String, int) 4 failed");
1671 assertEquals(5.0f, NumberUtils.toFloat("001Z.2345", 5.0f), "toFloat(String, int) 5 failed");
1672 assertEquals(5.0f, NumberUtils.toFloat("", 5.0f));
1673 assertEquals(5.0f, NumberUtils.toFloat(null, 5.0f));
1674 }
1675
1676
1677
1678
1679 @Test
1680 public void testToIntString() {
1681 assertEquals(12345, NumberUtils.toInt("12345"), "toInt(String) 1 failed");
1682 assertEquals(0, NumberUtils.toInt("abc"), "toInt(String) 2 failed");
1683 assertEquals(0, NumberUtils.toInt(""), "toInt(empty) failed");
1684 assertEquals(0, NumberUtils.toInt(null), "toInt(null) failed");
1685 }
1686
1687
1688
1689
1690 @Test
1691 public void testToIntStringI() {
1692 assertEquals(12345, NumberUtils.toInt("12345", 5), "toInt(String, int) 1 failed");
1693 assertEquals(5, NumberUtils.toInt("1234.5", 5), "toInt(String, int) 2 failed");
1694 assertEquals(5, NumberUtils.toInt("", 5));
1695 assertEquals(5, NumberUtils.toInt(null, 5));
1696 }
1697
1698
1699
1700
1701 @Test
1702 public void testToLongString() {
1703 assertEquals(12345L, NumberUtils.toLong("12345"), "toLong(String) 1 failed");
1704 assertEquals(0L, NumberUtils.toLong("abc"), "toLong(String) 2 failed");
1705 assertEquals(0L, NumberUtils.toLong("1L"), "toLong(String) 3 failed");
1706 assertEquals(0L, NumberUtils.toLong("1l"), "toLong(String) 4 failed");
1707 assertEquals(NumberUtils.toLong(Long.MAX_VALUE + ""), Long.MAX_VALUE, "toLong(Long.MAX_VALUE) failed");
1708 assertEquals(NumberUtils.toLong(Long.MIN_VALUE + ""), Long.MIN_VALUE, "toLong(Long.MIN_VALUE) failed");
1709 assertEquals(0L, NumberUtils.toLong(""), "toLong(empty) failed");
1710 assertEquals(0L, NumberUtils.toLong(null), "toLong(null) failed");
1711 }
1712
1713
1714
1715
1716 @Test
1717 public void testToLongStringL() {
1718 assertEquals(12345L, NumberUtils.toLong("12345", 5L), "toLong(String, long) 1 failed");
1719 assertEquals(5L, NumberUtils.toLong("1234.5", 5L), "toLong(String, long) 2 failed");
1720 assertEquals(5L, NumberUtils.toLong("", 5L));
1721 assertEquals(5L, NumberUtils.toLong(null, 5L));
1722 }
1723
1724
1725
1726
1727 @Test
1728 public void testToScaledBigDecimalBigDecimal() {
1729 assertEquals(NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(123.456)), BigDecimal.valueOf(123.46),
1730 "toScaledBigDecimal(BigDecimal) 1 failed");
1731
1732 assertEquals(NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.515)), BigDecimal.valueOf(23.52),
1733 "toScaledBigDecimal(BigDecimal) 2 failed");
1734 assertEquals(NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.525)), BigDecimal.valueOf(23.52),
1735 "toScaledBigDecimal(BigDecimal) 3 failed");
1736 assertEquals("2352.00",
1737 NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.525)).multiply(BigDecimal.valueOf(100)).toString(),
1738 "toScaledBigDecimal(BigDecimal) 4 failed");
1739 assertEquals(NumberUtils.toScaledBigDecimal((BigDecimal) null), BigDecimal.ZERO,
1740 "toScaledBigDecimal(BigDecimal) 5 failed");
1741 }
1742
1743
1744
1745
1746 @Test
1747 public void testToScaledBigDecimalBigDecimalIRM() {
1748 assertEquals(NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(123.456), 1, RoundingMode.CEILING),
1749 BigDecimal.valueOf(123.5), "toScaledBigDecimal(BigDecimal, int, RoundingMode) 1 failed");
1750 assertEquals(NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.5159), 3, RoundingMode.FLOOR),
1751 BigDecimal.valueOf(23.515), "toScaledBigDecimal(BigDecimal, int, RoundingMode) 2 failed");
1752 assertEquals(NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.525), 2, RoundingMode.HALF_UP),
1753 BigDecimal.valueOf(23.53), "toScaledBigDecimal(BigDecimal, int, RoundingMode) 3 failed");
1754 assertEquals("23521.0000",
1755 NumberUtils.toScaledBigDecimal(BigDecimal.valueOf(23.521), 4, RoundingMode.HALF_EVEN)
1756 .multiply(BigDecimal.valueOf(1000)).toString(),
1757 "toScaledBigDecimal(BigDecimal, int, RoundingMode) 4 failed");
1758 assertEquals(NumberUtils.toScaledBigDecimal((BigDecimal) null, 2, RoundingMode.HALF_UP), BigDecimal.ZERO,
1759 "toScaledBigDecimal(BigDecimal, int, RoundingMode) 5 failed");
1760 }
1761
1762
1763
1764
1765 @Test
1766 public void testToScaledBigDecimalDouble() {
1767 assertEquals(NumberUtils.toScaledBigDecimal(Double.valueOf(123.456d)), BigDecimal.valueOf(123.46),
1768 "toScaledBigDecimal(Double) 1 failed");
1769
1770 assertEquals(NumberUtils.toScaledBigDecimal(Double.valueOf(23.515d)), BigDecimal.valueOf(23.52),
1771 "toScaledBigDecimal(Double) 2 failed");
1772 assertEquals(NumberUtils.toScaledBigDecimal(Double.valueOf(23.525d)), BigDecimal.valueOf(23.52),
1773 "toScaledBigDecimal(Double) 3 failed");
1774 assertEquals("2352.00",
1775 NumberUtils.toScaledBigDecimal(Double.valueOf(23.525d)).multiply(BigDecimal.valueOf(100)).toString(),
1776 "toScaledBigDecimal(Double) 4 failed");
1777 assertEquals(NumberUtils.toScaledBigDecimal((Double) null), BigDecimal.ZERO,
1778 "toScaledBigDecimal(Double) 5 failed");
1779 }
1780
1781
1782
1783
1784 @Test
1785 public void testToScaledBigDecimalDoubleIRM() {
1786 assertEquals(NumberUtils.toScaledBigDecimal(Double.valueOf(123.456d), 1, RoundingMode.CEILING),
1787 BigDecimal.valueOf(123.5), "toScaledBigDecimal(Double, int, RoundingMode) 1 failed");
1788 assertEquals(NumberUtils.toScaledBigDecimal(Double.valueOf(23.5159d), 3, RoundingMode.FLOOR),
1789 BigDecimal.valueOf(23.515), "toScaledBigDecimal(Double, int, RoundingMode) 2 failed");
1790 assertEquals(NumberUtils.toScaledBigDecimal(Double.valueOf(23.525d), 2, RoundingMode.HALF_UP),
1791 BigDecimal.valueOf(23.53), "toScaledBigDecimal(Double, int, RoundingMode) 3 failed");
1792 assertEquals("23521.0000",
1793 NumberUtils.toScaledBigDecimal(Double.valueOf(23.521d), 4, RoundingMode.HALF_EVEN)
1794 .multiply(BigDecimal.valueOf(1000)).toString(),
1795 "toScaledBigDecimal(Double, int, RoundingMode) 4 failed");
1796 assertEquals(NumberUtils.toScaledBigDecimal((Double) null, 2, RoundingMode.HALF_UP), BigDecimal.ZERO,
1797 "toScaledBigDecimal(Double, int, RoundingMode) 5 failed");
1798 }
1799
1800
1801
1802
1803 @Test
1804 public void testToScaledBigDecimalFloat() {
1805 assertEquals(NumberUtils.toScaledBigDecimal(Float.valueOf(123.456f)), BigDecimal.valueOf(123.46),
1806 "toScaledBigDecimal(Float) 1 failed");
1807
1808 assertEquals(NumberUtils.toScaledBigDecimal(Float.valueOf(23.515f)), BigDecimal.valueOf(23.51),
1809 "toScaledBigDecimal(Float) 2 failed");
1810
1811
1812 assertEquals(NumberUtils.toScaledBigDecimal(Float.valueOf(23.525f)), BigDecimal.valueOf(23.52),
1813 "toScaledBigDecimal(Float) 3 failed");
1814 assertEquals("2352.00",
1815 NumberUtils.toScaledBigDecimal(Float.valueOf(23.525f)).multiply(BigDecimal.valueOf(100)).toString(),
1816 "toScaledBigDecimal(Float) 4 failed");
1817 assertEquals(NumberUtils.toScaledBigDecimal((Float) null), BigDecimal.ZERO,
1818 "toScaledBigDecimal(Float) 5 failed");
1819 }
1820
1821
1822
1823
1824 @Test
1825 public void testToScaledBigDecimalFloatIRM() {
1826 assertEquals(NumberUtils.toScaledBigDecimal(Float.valueOf(123.456f), 1, RoundingMode.CEILING),
1827 BigDecimal.valueOf(123.5), "toScaledBigDecimal(Float, int, RoundingMode) 1 failed");
1828 assertEquals(NumberUtils.toScaledBigDecimal(Float.valueOf(23.5159f), 3, RoundingMode.FLOOR),
1829 BigDecimal.valueOf(23.515), "toScaledBigDecimal(Float, int, RoundingMode) 2 failed");
1830
1831 assertEquals(NumberUtils.toScaledBigDecimal(Float.valueOf(23.525f), 2, RoundingMode.HALF_UP),
1832 BigDecimal.valueOf(23.52), "toScaledBigDecimal(Float, int, RoundingMode) 3 failed");
1833 assertEquals("23521.0000", NumberUtils.toScaledBigDecimal(Float.valueOf(23.521f), 4, RoundingMode.HALF_EVEN)
1834 .multiply(BigDecimal.valueOf(1000)).toString(), "toScaledBigDecimal(Float, int, RoundingMode) 4 failed");
1835 assertEquals(NumberUtils.toScaledBigDecimal((Float) null, 2, RoundingMode.HALF_UP), BigDecimal.ZERO,
1836 "toScaledBigDecimal(Float, int, RoundingMode) 5 failed");
1837 }
1838
1839
1840
1841
1842 @Test
1843 public void testToScaledBigDecimalString() {
1844 assertEquals(NumberUtils.toScaledBigDecimal("123.456"), BigDecimal.valueOf(123.46),
1845 "toScaledBigDecimal(String) 1 failed");
1846
1847 assertEquals(NumberUtils.toScaledBigDecimal("23.515"), BigDecimal.valueOf(23.52),
1848 "toScaledBigDecimal(String) 2 failed");
1849 assertEquals(NumberUtils.toScaledBigDecimal("23.525"), BigDecimal.valueOf(23.52),
1850 "toScaledBigDecimal(String) 3 failed");
1851 assertEquals("2352.00", NumberUtils.toScaledBigDecimal("23.525").multiply(BigDecimal.valueOf(100)).toString(),
1852 "toScaledBigDecimal(String) 4 failed");
1853 assertEquals(NumberUtils.toScaledBigDecimal((String) null), BigDecimal.ZERO,
1854 "toScaledBigDecimal(String) 5 failed");
1855 }
1856
1857
1858
1859
1860 @Test
1861 public void testToScaledBigDecimalStringIRM() {
1862 assertEquals(NumberUtils.toScaledBigDecimal("123.456", 1, RoundingMode.CEILING), BigDecimal.valueOf(123.5),
1863 "toScaledBigDecimal(String, int, RoundingMode) 1 failed");
1864 assertEquals(NumberUtils.toScaledBigDecimal("23.5159", 3, RoundingMode.FLOOR), BigDecimal.valueOf(23.515),
1865 "toScaledBigDecimal(String, int, RoundingMode) 2 failed");
1866 assertEquals(NumberUtils.toScaledBigDecimal("23.525", 2, RoundingMode.HALF_UP), BigDecimal.valueOf(23.53),
1867 "toScaledBigDecimal(String, int, RoundingMode) 3 failed");
1868 assertEquals(
1869 "23521.0000", NumberUtils.toScaledBigDecimal("23.521", 4, RoundingMode.HALF_EVEN)
1870 .multiply(BigDecimal.valueOf(1000)).toString(),
1871 "toScaledBigDecimal(String, int, RoundingMode) 4 failed");
1872 assertEquals(NumberUtils.toScaledBigDecimal((String) null, 2, RoundingMode.HALF_UP), BigDecimal.ZERO,
1873 "toScaledBigDecimal(String, int, RoundingMode) 5 failed");
1874 }
1875
1876
1877
1878
1879 @Test
1880 public void testToShortString() {
1881 assertEquals(12345, NumberUtils.toShort("12345"), "toShort(String) 1 failed");
1882 assertEquals(0, NumberUtils.toShort("abc"), "toShort(String) 2 failed");
1883 assertEquals(0, NumberUtils.toShort(""), "toShort(empty) failed");
1884 assertEquals(0, NumberUtils.toShort(null), "toShort(null) failed");
1885 }
1886
1887
1888
1889
1890 @Test
1891 public void testToShortStringI() {
1892 assertEquals(12345, NumberUtils.toShort("12345", (short) 5), "toShort(String, short) 1 failed");
1893 assertEquals(5, NumberUtils.toShort("1234.5", (short) 5), "toShort(String, short) 2 failed");
1894 assertEquals(5, NumberUtils.toShort("", (short) 5));
1895 assertEquals(5, NumberUtils.toShort(null, (short) 5));
1896 }
1897 }