|
| 1 | +package org.json.junit; |
| 2 | + |
| 3 | +import org.json.JSONObject; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.math.BigDecimal; |
| 7 | +import java.math.BigInteger; |
| 8 | + |
| 9 | +import static org.junit.Assert.assertEquals; |
| 10 | + |
| 11 | +public class JSONObjectDecimalTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + public void shouldParseDecimalNumberThatStartsWithDecimalPoint(){ |
| 15 | + JSONObject jsonObject = new JSONObject("{value:0.50}"); |
| 16 | + assertEquals("Float not recognized", 0.5f, jsonObject.getFloat("value"), 0.0f); |
| 17 | + assertEquals("Float not recognized", 0.5f, jsonObject.optFloat("value"), 0.0f); |
| 18 | + assertEquals("Float not recognized", 0.5f, jsonObject.optFloatObject("value"), 0.0f); |
| 19 | + assertEquals("Double not recognized", 0.5d, jsonObject.optDouble("value"), 0.0f); |
| 20 | + assertEquals("Double not recognized", 0.5d, jsonObject.optDoubleObject("value"), 0.0f); |
| 21 | + assertEquals("Double not recognized", 0.5d, jsonObject.getDouble("value"), 0.0f); |
| 22 | + assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); |
| 23 | + assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); |
| 24 | + assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); |
| 25 | + assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); |
| 26 | + assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); |
| 27 | + assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); |
| 28 | + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); |
| 29 | + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); |
| 30 | + assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(.5).compareTo(jsonObject.getBigDecimal("value"))); |
| 31 | + assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + @Test |
| 37 | + public void shouldParseNegativeDecimalNumberThatStartsWithDecimalPoint(){ |
| 38 | + JSONObject jsonObject = new JSONObject("{value:-.50}"); |
| 39 | + assertEquals("Float not recognized", -0.5f, jsonObject.getFloat("value"), 0.0f); |
| 40 | + assertEquals("Float not recognized", -0.5f, jsonObject.optFloat("value"), 0.0f); |
| 41 | + assertEquals("Float not recognized", -0.5f, jsonObject.optFloatObject("value"), 0.0f); |
| 42 | + assertEquals("Double not recognized", -0.5d, jsonObject.optDouble("value"), 0.0f); |
| 43 | + assertEquals("Double not recognized", -0.5d, jsonObject.optDoubleObject("value"), 0.0f); |
| 44 | + assertEquals("Double not recognized", -0.5d, jsonObject.getDouble("value"), 0.0f); |
| 45 | + assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); |
| 46 | + assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); |
| 47 | + assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); |
| 48 | + assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); |
| 49 | + assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); |
| 50 | + assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); |
| 51 | + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); |
| 52 | + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); |
| 53 | + assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-.5).compareTo(jsonObject.getBigDecimal("value"))); |
| 54 | + assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void shouldParseDecimalNumberThatHasZeroBeforeWithDecimalPoint(){ |
| 59 | + JSONObject jsonObject = new JSONObject("{value:00.050}"); |
| 60 | + assertEquals("Float not recognized", 0.05f, jsonObject.getFloat("value"), 0.0f); |
| 61 | + assertEquals("Float not recognized", 0.05f, jsonObject.optFloat("value"), 0.0f); |
| 62 | + assertEquals("Float not recognized", 0.05f, jsonObject.optFloatObject("value"), 0.0f); |
| 63 | + assertEquals("Double not recognized", 0.05d, jsonObject.optDouble("value"), 0.0f); |
| 64 | + assertEquals("Double not recognized", 0.05d, jsonObject.optDoubleObject("value"), 0.0f); |
| 65 | + assertEquals("Double not recognized", 0.05d, jsonObject.getDouble("value"), 0.0f); |
| 66 | + assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); |
| 67 | + assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); |
| 68 | + assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); |
| 69 | + assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); |
| 70 | + assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); |
| 71 | + assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); |
| 72 | + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); |
| 73 | + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); |
| 74 | + assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(.05).compareTo(jsonObject.getBigDecimal("value"))); |
| 75 | + assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void shouldParseNegativeDecimalNumberThatHasZeroBeforeWithDecimalPoint(){ |
| 80 | + JSONObject jsonObject = new JSONObject("{value:-00.050}"); |
| 81 | + assertEquals("Float not recognized", -0.05f, jsonObject.getFloat("value"), 0.0f); |
| 82 | + assertEquals("Float not recognized", -0.05f, jsonObject.optFloat("value"), 0.0f); |
| 83 | + assertEquals("Float not recognized", -0.05f, jsonObject.optFloatObject("value"), 0.0f); |
| 84 | + assertEquals("Double not recognized", -0.05d, jsonObject.optDouble("value"), 0.0f); |
| 85 | + assertEquals("Double not recognized", -0.05d, jsonObject.optDoubleObject("value"), 0.0f); |
| 86 | + assertEquals("Double not recognized", -0.05d, jsonObject.getDouble("value"), 0.0f); |
| 87 | + assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0); |
| 88 | + assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0); |
| 89 | + assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0); |
| 90 | + assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0); |
| 91 | + assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0); |
| 92 | + assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0); |
| 93 | + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0); |
| 94 | + assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0); |
| 95 | + assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-.05).compareTo(jsonObject.getBigDecimal("value"))); |
| 96 | + assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value"))); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | +} |
0 commit comments