From 3b8b0a681c349f12ea83b490927c0da219a7550d Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Tue, 2 Oct 2018 12:38:17 -0400 Subject: [PATCH 1/2] Update test cases to verify performance change and verify opt/getBigDecimal match --- .../java/org/json/junit/JSONObjectTest.java | 95 +++++++++++++++++-- 1 file changed, 86 insertions(+), 9 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index e3b9529..63385a5 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; +import java.util.regex.Pattern; import org.json.CDL; import org.json.JSONArray; @@ -60,7 +61,13 @@ import com.jayway.jsonpath.JsonPath; * otherwise be impossible. */ public class JSONObjectTest { - + + /** + * Regular Expression Pattern that matches JSON Numbers. This is primarily used for + * output to guarantee that we are always writing valid JSON. + */ + static final Pattern NUMBER_PATTERN = Pattern.compile("-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+-]?\\d+)?"); + /** * Tests that the similar method is working as expected. */ @@ -87,6 +94,67 @@ public class JSONObjectTest { assertTrue("Should eval to true", obj1.similar(obj3)); } + + @Test + public void timeNumberParsing() { + // test data to use + final String[] testData = new String[] { + null, + "", + "100", + "-100", + "abc123", + "012345", + "100.5e199", + "-100.5e199", + "DEADBEEF", + "0xDEADBEEF", + "1234567890.1234567890", + "-1234567890.1234567890", + "adloghakuidghauiehgauioehgdkjfb nsruoh aeu noerty384 nkljfgh " + + "395h tdfn kdz8yt3 4hkls gn.ey85 4hzfhnz.o8y5a84 onvklt " + + "yh389thub nkz8y49lihv al4itlaithknty8hnbl" + // long (in length) number sequences with invalid data at the end of the + // string offer very poor performance for the REGEX. + ,"123467890123467890123467890123467890123467890123467890123467" + + "8901234678901234678901234678901234678901234678901234678" + + "9012346789012346789012346789012346789012346789012346789" + + "0a" + }; + final int testDataLength = testData.length; + final int iterations = 1000000; + + // 10 million iterations 1,000,000 * 10 + long startTime = System.nanoTime(); + for(int i = 0; i < iterations; i++) { + for(int j = 0; j < testDataLength; j++) { + try { + BigDecimal v1 = new BigDecimal(testData[j]); + v1.signum(); + } catch(Exception ignore) { + //do nothing + } + } + } + final long elapsedNano1 = System.nanoTime() - startTime; + System.out.println("new BigDecimal(testData[]) : " + elapsedNano1 / 1000000 + " ms"); + + startTime = System.nanoTime(); + for(int i = 0; i < iterations; i++) { + for(int j = 0; j < testDataLength; j++) { + try { + boolean v2 = NUMBER_PATTERN.matcher(testData[j]).matches(); + assert v2 == !!v2; + } catch(Exception ignore) { + //do nothing + } + } + } + final long elapsedNano2 = System.nanoTime() - startTime; + System.out.println("NUMBER_PATTERN.matcher(testData[]).matches() : " + elapsedNano2 / 1000000 + " ms"); + // don't assert normally as the testing is machine dependent. + // assertTrue("Expected Pattern matching to be faster than BigDecimal constructor",elapsedNano2 Date: Thu, 4 Oct 2018 16:02:50 -0400 Subject: [PATCH 2/2] update expected exception text in tests to match unified number getters --- .../java/org/json/junit/JSONArrayTest.java | 32 +++---- .../java/org/json/junit/JSONObjectTest.java | 94 +++++++++---------- 2 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 845f4e7..3b70446 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -331,57 +331,57 @@ public class JSONArrayTest { jsonArray.getBoolean(4); assertTrue("expected getBoolean to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[4] is not a boolean.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[4] is not a boolean.",e.getMessage()); } try { jsonArray.get(-1); assertTrue("expected get to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[-1] not found.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[-1] not found.",e.getMessage()); } try { jsonArray.getDouble(4); assertTrue("expected getDouble to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[4] is not a number.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[4] is not a number.",e.getMessage()); } try { jsonArray.getInt(4); assertTrue("expected getInt to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[4] is not a number.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[4] is not a number.",e.getMessage()); } try { jsonArray.getJSONArray(4); assertTrue("expected getJSONArray to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[4] is not a JSONArray.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[4] is not a JSONArray.",e.getMessage()); } try { jsonArray.getJSONObject(4); assertTrue("expected getJSONObject to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[4] is not a JSONObject.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[4] is not a JSONObject.",e.getMessage()); } try { jsonArray.getLong(4); assertTrue("expected getLong to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[4] is not a number.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[4] is not a number.",e.getMessage()); } try { jsonArray.getString(5); assertTrue("expected getString to fail", false); } catch (JSONException e) { - assertTrue("Expected an exception message", - "JSONArray[5] not a string.".equals(e.getMessage())); + assertEquals("Expected an exception message", + "JSONArray[5] not a string.",e.getMessage()); } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 63385a5..c6cb580 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -1013,128 +1013,128 @@ public class JSONObjectTest { jsonObject.getBoolean("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("expecting an exception message", - "JSONObject[\"nonKey\"] not found.".equals(e.getMessage())); + assertEquals("expecting an exception message", + "JSONObject[\"nonKey\"] not found.", e.getMessage()); } try { jsonObject.getBoolean("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a Boolean.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a Boolean.", + e.getMessage()); } try { jsonObject.getString("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getString("trueKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"trueKey\"] not a string.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"trueKey\"] not a string.", + e.getMessage()); } try { jsonObject.getDouble("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getDouble("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a number.", + e.getMessage()); } try { jsonObject.getFloat("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getFloat("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a number.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a number.", + e.getMessage()); } try { jsonObject.getInt("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getInt("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not an int.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a number.", + e.getMessage()); } try { jsonObject.getLong("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getLong("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a long.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a number.", + e.getMessage()); } try { jsonObject.getJSONArray("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getJSONArray("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a JSONArray.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a JSONArray.", + e.getMessage()); } try { jsonObject.getJSONObject("nonKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"nonKey\"] not found.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"nonKey\"] not found.", + e.getMessage()); } try { jsonObject.getJSONObject("stringKey"); fail("Expected an exception"); } catch (JSONException e) { - assertTrue("Expecting an exception message", - "JSONObject[\"stringKey\"] is not a JSONObject.". - equals(e.getMessage())); + assertEquals("Expecting an exception message", + "JSONObject[\"stringKey\"] is not a JSONObject.", + e.getMessage()); } }