mirror of
https://github.com/fankes/JSON-java-compat.git
synced 2025-09-07 19:14:15 +08:00
#653 - review comments updated.
This commit is contained in:
100
src/test/java/org/json/junit/JSONObjectDecimalTest.java
Normal file
100
src/test/java/org/json/junit/JSONObjectDecimalTest.java
Normal file
@@ -0,0 +1,100 @@
|
||||
package org.json.junit;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class JSONObjectDecimalTest {
|
||||
|
||||
@Test
|
||||
public void shouldParseDecimalNumberThatStartsWithDecimalPoint(){
|
||||
JSONObject jsonObject = new JSONObject("{value:0.50}");
|
||||
assertEquals("Float not recognized", 0.5f, jsonObject.getFloat("value"), 0.0f);
|
||||
assertEquals("Float not recognized", 0.5f, jsonObject.optFloat("value"), 0.0f);
|
||||
assertEquals("Float not recognized", 0.5f, jsonObject.optFloatObject("value"), 0.0f);
|
||||
assertEquals("Double not recognized", 0.5d, jsonObject.optDouble("value"), 0.0f);
|
||||
assertEquals("Double not recognized", 0.5d, jsonObject.optDoubleObject("value"), 0.0f);
|
||||
assertEquals("Double not recognized", 0.5d, jsonObject.getDouble("value"), 0.0f);
|
||||
assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0);
|
||||
assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0);
|
||||
assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0);
|
||||
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0);
|
||||
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0);
|
||||
assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(.5).compareTo(jsonObject.getBigDecimal("value")));
|
||||
assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value")));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldParseNegativeDecimalNumberThatStartsWithDecimalPoint(){
|
||||
JSONObject jsonObject = new JSONObject("{value:-.50}");
|
||||
assertEquals("Float not recognized", -0.5f, jsonObject.getFloat("value"), 0.0f);
|
||||
assertEquals("Float not recognized", -0.5f, jsonObject.optFloat("value"), 0.0f);
|
||||
assertEquals("Float not recognized", -0.5f, jsonObject.optFloatObject("value"), 0.0f);
|
||||
assertEquals("Double not recognized", -0.5d, jsonObject.optDouble("value"), 0.0f);
|
||||
assertEquals("Double not recognized", -0.5d, jsonObject.optDoubleObject("value"), 0.0f);
|
||||
assertEquals("Double not recognized", -0.5d, jsonObject.getDouble("value"), 0.0f);
|
||||
assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0);
|
||||
assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0);
|
||||
assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0);
|
||||
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0);
|
||||
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0);
|
||||
assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-.5).compareTo(jsonObject.getBigDecimal("value")));
|
||||
assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseDecimalNumberThatHasZeroBeforeWithDecimalPoint(){
|
||||
JSONObject jsonObject = new JSONObject("{value:00.050}");
|
||||
assertEquals("Float not recognized", 0.05f, jsonObject.getFloat("value"), 0.0f);
|
||||
assertEquals("Float not recognized", 0.05f, jsonObject.optFloat("value"), 0.0f);
|
||||
assertEquals("Float not recognized", 0.05f, jsonObject.optFloatObject("value"), 0.0f);
|
||||
assertEquals("Double not recognized", 0.05d, jsonObject.optDouble("value"), 0.0f);
|
||||
assertEquals("Double not recognized", 0.05d, jsonObject.optDoubleObject("value"), 0.0f);
|
||||
assertEquals("Double not recognized", 0.05d, jsonObject.getDouble("value"), 0.0f);
|
||||
assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0);
|
||||
assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0);
|
||||
assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0);
|
||||
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0);
|
||||
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0);
|
||||
assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(.05).compareTo(jsonObject.getBigDecimal("value")));
|
||||
assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseNegativeDecimalNumberThatHasZeroBeforeWithDecimalPoint(){
|
||||
JSONObject jsonObject = new JSONObject("{value:-00.050}");
|
||||
assertEquals("Float not recognized", -0.05f, jsonObject.getFloat("value"), 0.0f);
|
||||
assertEquals("Float not recognized", -0.05f, jsonObject.optFloat("value"), 0.0f);
|
||||
assertEquals("Float not recognized", -0.05f, jsonObject.optFloatObject("value"), 0.0f);
|
||||
assertEquals("Double not recognized", -0.05d, jsonObject.optDouble("value"), 0.0f);
|
||||
assertEquals("Double not recognized", -0.05d, jsonObject.optDoubleObject("value"), 0.0f);
|
||||
assertEquals("Double not recognized", -0.05d, jsonObject.getDouble("value"), 0.0f);
|
||||
assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0);
|
||||
assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0);
|
||||
assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0);
|
||||
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0);
|
||||
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0);
|
||||
assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-.05).compareTo(jsonObject.getBigDecimal("value")));
|
||||
assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value")));
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -4,6 +4,7 @@ package org.json.junit;
|
||||
Public Domain.
|
||||
*/
|
||||
|
||||
import static java.lang.Double.NaN;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
@@ -782,7 +783,7 @@ public class JSONObjectTest {
|
||||
jsonObject.accumulate("myArray", -23.45e7);
|
||||
// include an unsupported object for coverage
|
||||
try {
|
||||
jsonObject.accumulate("myArray", Double.NaN);
|
||||
jsonObject.accumulate("myArray", NaN);
|
||||
fail("Expected exception");
|
||||
} catch (JSONException ignored) {}
|
||||
|
||||
@@ -814,7 +815,7 @@ public class JSONObjectTest {
|
||||
jsonObject.append("myArray", -23.45e7);
|
||||
// include an unsupported object for coverage
|
||||
try {
|
||||
jsonObject.append("myArray", Double.NaN);
|
||||
jsonObject.append("myArray", NaN);
|
||||
fail("Expected exception");
|
||||
} catch (JSONException ignored) {}
|
||||
|
||||
@@ -839,7 +840,7 @@ public class JSONObjectTest {
|
||||
public void jsonObjectDoubleToString() {
|
||||
String [] expectedStrs = {"1", "1", "-23.4", "-2.345E68", "null", "null" };
|
||||
Double [] doubles = { 1.0, 00001.00000, -23.4, -23.45e67,
|
||||
Double.NaN, Double.NEGATIVE_INFINITY };
|
||||
NaN, Double.NEGATIVE_INFINITY };
|
||||
for (int i = 0; i < expectedStrs.length; ++i) {
|
||||
String actualStr = JSONObject.doubleToString(doubles[i]);
|
||||
assertTrue("value expected ["+expectedStrs[i]+
|
||||
@@ -894,11 +895,11 @@ public class JSONObjectTest {
|
||||
assertTrue("opt doubleKey should be double",
|
||||
jsonObject.optDouble("doubleKey") == -23.45e7);
|
||||
assertTrue("opt doubleKey with Default should be double",
|
||||
jsonObject.optDouble("doubleStrKey", Double.NaN) == 1);
|
||||
jsonObject.optDouble("doubleStrKey", NaN) == 1);
|
||||
assertTrue("opt doubleKey should be Double",
|
||||
Double.valueOf(-23.45e7).equals(jsonObject.optDoubleObject("doubleKey")));
|
||||
assertTrue("opt doubleKey with Default should be Double",
|
||||
Double.valueOf(1).equals(jsonObject.optDoubleObject("doubleStrKey", Double.NaN)));
|
||||
Double.valueOf(1).equals(jsonObject.optDoubleObject("doubleStrKey", NaN)));
|
||||
assertTrue("opt negZeroKey should be a Double",
|
||||
jsonObject.opt("negZeroKey") instanceof Double);
|
||||
assertTrue("get negZeroKey should be a Double",
|
||||
@@ -1071,9 +1072,14 @@ public class JSONObjectTest {
|
||||
"\"hexFloat\":0x1.0P-1074,"+
|
||||
"\"floatIdentifier\":0.1f,"+
|
||||
"\"doubleIdentifier\":0.1d,"+
|
||||
"\"doubleIdentifierWithMultipleLeadingZerosBeforeDecimal\":0000000.1d,"+
|
||||
"\"negativeDoubleIdentifierWithMultipleLeadingZerosBeforeDecimal\":-0000000.1d,"+
|
||||
"\"doubleIdentifierWithMultipleLeadingZerosAfterDecimal\":0000000.0001d,"+
|
||||
"\"negativeDoubleIdentifierWithMultipleLeadingZerosAfterDecimal\":-0000000.0001d,"+
|
||||
"\"integerWithLeadingZeros\":000900,"+
|
||||
"\"integerWithAllZeros\":00000,"+
|
||||
"\"compositeWithLeadingZeros\":00800.90d"+
|
||||
"\"compositeWithLeadingZeros\":00800.90d,"+
|
||||
"\"decimalPositiveWithoutNumberBeforeDecimalPoint\":.90,"+
|
||||
"}";
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
Object obj;
|
||||
@@ -1083,7 +1089,7 @@ public class JSONObjectTest {
|
||||
assertTrue("hexNumber currently evaluates to string",
|
||||
obj.equals("-0x123"));
|
||||
assertTrue( "tooManyZeros currently evaluates to string",
|
||||
jsonObject.get( "tooManyZeros" ).equals("00"));
|
||||
jsonObject.get( "tooManyZeros" ).equals(0));
|
||||
obj = jsonObject.get("negativeInfinite");
|
||||
assertTrue( "negativeInfinite currently evaluates to string",
|
||||
obj.equals("-Infinity"));
|
||||
@@ -1109,6 +1115,16 @@ public class JSONObjectTest {
|
||||
jsonObject.get("floatIdentifier").equals(Double.valueOf(0.1)));
|
||||
assertTrue("doubleIdentifier currently evaluates to double 0.1",
|
||||
jsonObject.get("doubleIdentifier").equals(Double.valueOf(0.1)));
|
||||
assertTrue("doubleIdentifierWithMultipleLeadingZerosBeforeDecimal currently evaluates to double 0.1",
|
||||
jsonObject.get("doubleIdentifierWithMultipleLeadingZerosBeforeDecimal").equals(Double.valueOf(0.1)));
|
||||
assertTrue("negativeDoubleIdentifierWithMultipleLeadingZerosBeforeDecimal currently evaluates to double -0.1",
|
||||
jsonObject.get("negativeDoubleIdentifierWithMultipleLeadingZerosBeforeDecimal").equals(Double.valueOf(-0.1)));
|
||||
assertTrue("doubleIdentifierWithMultipleLeadingZerosAfterDecimal currently evaluates to double 0.0001",
|
||||
jsonObject.get("doubleIdentifierWithMultipleLeadingZerosAfterDecimal").equals(Double.valueOf(0.0001)));
|
||||
assertTrue("doubleIdentifierWithMultipleLeadingZerosAfterDecimal currently evaluates to double 0.0001",
|
||||
jsonObject.get("doubleIdentifierWithMultipleLeadingZerosAfterDecimal").equals(Double.valueOf(0.0001)));
|
||||
assertTrue("negativeDoubleIdentifierWithMultipleLeadingZerosAfterDecimal currently evaluates to double -0.0001",
|
||||
jsonObject.get("negativeDoubleIdentifierWithMultipleLeadingZerosAfterDecimal").equals(Double.valueOf(-0.0001)));
|
||||
assertTrue("Integer does not evaluate to 900",
|
||||
jsonObject.get("integerWithLeadingZeros").equals(900));
|
||||
assertTrue("Integer does not evaluate to 900",
|
||||
@@ -1116,7 +1132,7 @@ public class JSONObjectTest {
|
||||
assertTrue("Integer does not evaluate to 900",
|
||||
jsonObject.optInt("integerWithLeadingZeros")==900);
|
||||
assertTrue("Integer does not evaluate to 0",
|
||||
jsonObject.get("integerWithAllZeros").equals("00000"));
|
||||
jsonObject.get("integerWithAllZeros").equals(0));
|
||||
assertTrue("Integer does not evaluate to 0",
|
||||
jsonObject.getInt("integerWithAllZeros")==0);
|
||||
assertTrue("Integer does not evaluate to 0",
|
||||
@@ -1131,6 +1147,21 @@ public class JSONObjectTest {
|
||||
jsonObject.getLong("compositeWithLeadingZeros")==800);
|
||||
assertTrue("Long does not evaluate to 800.90",
|
||||
jsonObject.optLong("compositeWithLeadingZeros")==800);
|
||||
assertEquals("Get long of decimalPositiveWithoutNumberBeforeDecimalPoint does not match",
|
||||
0.9d,jsonObject.getDouble("decimalPositiveWithoutNumberBeforeDecimalPoint"), 0.0d);
|
||||
assertEquals("Get long of decimalPositiveWithoutNumberBeforeDecimalPoint does not match",
|
||||
0.9d,jsonObject.optDouble("decimalPositiveWithoutNumberBeforeDecimalPoint"), 0.0d);
|
||||
assertEquals("Get long of decimalPositiveWithoutNumberBeforeDecimalPoint does not match",
|
||||
0.0d,jsonObject.optLong("decimalPositiveWithoutNumberBeforeDecimalPoint"), 0.0d);
|
||||
|
||||
assertEquals("Get long of doubleIdentifierWithMultipleLeadingZerosAfterDecimal does not match",
|
||||
0.0001d,jsonObject.getDouble("doubleIdentifierWithMultipleLeadingZerosAfterDecimal"), 0.0d);
|
||||
assertEquals("Get long of doubleIdentifierWithMultipleLeadingZerosAfterDecimal does not match",
|
||||
0.0001d,jsonObject.optDouble("doubleIdentifierWithMultipleLeadingZerosAfterDecimal"), 0.0d);
|
||||
assertEquals("Get long of doubleIdentifierWithMultipleLeadingZerosAfterDecimal does not match",
|
||||
0.0d, jsonObject.getLong("doubleIdentifierWithMultipleLeadingZerosAfterDecimal") , 0.0d);
|
||||
assertEquals("Get long of doubleIdentifierWithMultipleLeadingZerosAfterDecimal does not match",
|
||||
0.0d,jsonObject.optLong("doubleIdentifierWithMultipleLeadingZerosAfterDecimal"), 0.0d);
|
||||
Util.checkJSONObjectMaps(jsonObject);
|
||||
}
|
||||
|
||||
@@ -2361,7 +2392,7 @@ public class JSONObjectTest {
|
||||
}
|
||||
try {
|
||||
// test validity of invalid double
|
||||
JSONObject.testValidity(Double.NaN);
|
||||
JSONObject.testValidity(NaN);
|
||||
fail("Expected an exception");
|
||||
} catch (JSONException e) {
|
||||
assertTrue("", true);
|
||||
|
55
src/test/java/org/json/junit/JsonNumberZeroTest.java
Normal file
55
src/test/java/org/json/junit/JsonNumberZeroTest.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package org.json.junit;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class JsonNumberZeroTest {
|
||||
|
||||
@Test
|
||||
public void shouldParseNegativeZeroValueWithMultipleZeroDigit(){
|
||||
JSONObject jsonObject = new JSONObject("{value:-0000}");
|
||||
assertEquals("Float not recognized", -0f, jsonObject.getFloat("value"), 0.0f);
|
||||
assertEquals("Float not recognized", -0f, jsonObject.optFloat("value"), 0.0f);
|
||||
assertEquals("Float not recognized", -0f, jsonObject.optFloatObject("value"), 0.0f);
|
||||
assertEquals("Double not recognized", -0d, jsonObject.optDouble("value"), 0.0f);
|
||||
assertEquals("Double not recognized", -0.0d, jsonObject.optDoubleObject("value"), 0.0f);
|
||||
assertEquals("Double not recognized", -0.0d, jsonObject.getDouble("value"), 0.0f);
|
||||
assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0);
|
||||
assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0);
|
||||
assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0);
|
||||
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0);
|
||||
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0);
|
||||
assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-0).compareTo(jsonObject.getBigDecimal("value")));
|
||||
assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseZeroValueWithMultipleZeroDigit(){
|
||||
JSONObject jsonObject = new JSONObject("{value:0000}");
|
||||
assertEquals("Float not recognized", 0f, jsonObject.getFloat("value"), 0.0f);
|
||||
assertEquals("Float not recognized", 0f, jsonObject.optFloat("value"), 0.0f);
|
||||
assertEquals("Float not recognized", 0f, jsonObject.optFloatObject("value"), 0.0f);
|
||||
assertEquals("Double not recognized", 0d, jsonObject.optDouble("value"), 0.0f);
|
||||
assertEquals("Double not recognized", 0.0d, jsonObject.optDoubleObject("value"), 0.0f);
|
||||
assertEquals("Double not recognized", 0.0d, jsonObject.getDouble("value"), 0.0f);
|
||||
assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0);
|
||||
assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0);
|
||||
assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0);
|
||||
assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0);
|
||||
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0);
|
||||
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0);
|
||||
assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-0).compareTo(jsonObject.getBigDecimal("value")));
|
||||
assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value")));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user