mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
update the new coercion test to use actual values and show the parseDouble method is not robust enough for large numbers
This commit is contained in:
parent
bdb1163445
commit
0150639119
1 changed files with 9 additions and 7 deletions
|
@ -2,6 +2,7 @@ package org.json.junit;
|
|||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
@ -2016,13 +2017,14 @@ public class JSONObjectTest {
|
|||
assertTrue("unexpected optNumber value",jo.optNumber("int",BigInteger.ZERO).longValue()==123l);
|
||||
|
||||
// Test type coercion from larger to smaller
|
||||
final BigDecimal largeValue = new BigDecimal("19007199254740993.35481234487103587486413587843213584");
|
||||
assertEquals(largeValue,jo.optBigDecimal("largeNumber", null));
|
||||
assertEquals(largeValue.toBigInteger(),jo.optBigInteger("largeNumber", null));
|
||||
assertEquals(largeValue.doubleValue(), jo.optDouble("largeNumber"), 0.0d);
|
||||
assertEquals(largeValue.floatValue(), jo.optFloat("largeNumber"), 0.0f);
|
||||
assertEquals(largeValue.longValue(), jo.optLong("largeNumber"));
|
||||
assertEquals(largeValue.intValue(), jo.optInt("largeNumber"));
|
||||
assertEquals(new BigInteger("19007199254740993"), jo.optBigInteger("largeNumber",null));
|
||||
assertEquals(1.9007199254740992E16, jo.optDouble("largeNumber"),0.0);
|
||||
assertEquals(1.90071995E16f, jo.optFloat("largeNumber"),0.0f);
|
||||
assertEquals(19007199254740993l, jo.optLong("largeNumber"));
|
||||
assertEquals(1874919425, jo.optInt("largeNumber"));
|
||||
|
||||
// the integer portion of the actual value is larger than a double can hold.
|
||||
assertNotEquals((long)Double.parseDouble("19007199254740993.35481234487103587486413587843213584"), jo.optLong("largeNumber"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue