forked from stleary/JSON-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stleary#653 - review comments updated.
- Loading branch information
1 parent
d5277b1
commit 0cdc38a
Showing
4 changed files
with
236 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/test/java/org/json/junit/JSONObjectDecimalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"))); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.