From 805c9b28ff44baa7f21c32b5cb16f3fef164d7e8 Mon Sep 17 00:00:00 2001 From: Khushboo Desai Date: Wed, 19 Mar 2025 12:14:00 -0700 Subject: [PATCH 1/6] Fixes `IonReader.getIntegerSize()` calculation --- .../java/com/amazon/ion/impl/IonReaderTextSystemX.java | 5 ++++- src/test/java/com/amazon/ion/IntTest.java | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java b/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java index c187682d53..c3d478ab44 100644 --- a/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java +++ b/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java @@ -191,8 +191,11 @@ private static boolean valueWithinBounds(String value, int len, char[] minImage, private static boolean magnitudeLessThanOrEqualTo(String lhs, int lhsLen, char[] rhs) { assert lhsLen == rhs.length; - for (int i = lhsLen - 1; i >= 0; i--) + for (int i = 0; i < lhsLen; i++) { + if (lhs.charAt(i) < rhs[i]) { + return true; + } if (lhs.charAt(i) > rhs[i]) { return false; diff --git a/src/test/java/com/amazon/ion/IntTest.java b/src/test/java/com/amazon/ion/IntTest.java index 7401a3eaa6..cccab40758 100644 --- a/src/test/java/com/amazon/ion/IntTest.java +++ b/src/test/java/com/amazon/ion/IntTest.java @@ -517,6 +517,11 @@ private void testGetIntegerSizeLongBoundary(long boundaryValue) { IonInt pastBoundaryIon = (IonInt)oneValue(pastBoundary.toString()); assertEquals(IntegerSize.BIG_INTEGER, pastBoundaryIon.getIntegerSize()); assertEquals(pastBoundary, pastBoundaryIon.bigIntegerValue()); + + long withinBoundary = (boundaryValue < 0) ? boundaryValue + 10 : boundaryValue - 10; + IonInt withinBoundaryIon = (IonInt)oneValue(Long.toString(withinBoundary)); + assertEquals(IntegerSize.LONG, withinBoundaryIon.getIntegerSize()); + assertEquals(withinBoundary, withinBoundaryIon.longValue()); } private void testGetIntegerSizeIntBoundary(int boundaryValue) { @@ -529,6 +534,11 @@ private void testGetIntegerSizeIntBoundary(int boundaryValue) { IonInt pastBoundaryIon = (IonInt)oneValue(pastBoundary.toString()); assertEquals(IntegerSize.LONG, pastBoundaryIon.getIntegerSize()); assertEquals(pastBoundary.longValue(), pastBoundaryIon.longValue()); + + int withinBoundary = (boundaryValue < 0) ? boundaryValue + 10 : boundaryValue - 10; + IonInt withinBoundaryIon = (IonInt)oneValue(Integer.toString(withinBoundary)); + assertEquals(IntegerSize.INT, withinBoundaryIon.getIntegerSize()); + assertEquals(withinBoundary, withinBoundaryIon.intValue()); } } From b174fe5090568387adbdb7f10641b8f4e3b9681f Mon Sep 17 00:00:00 2001 From: Khushboo Desai Date: Wed, 19 Mar 2025 12:55:04 -0700 Subject: [PATCH 2/6] adds more tests for boundary check with reader --- src/test/java/com/amazon/ion/IntTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/test/java/com/amazon/ion/IntTest.java b/src/test/java/com/amazon/ion/IntTest.java index cccab40758..c988cf41df 100644 --- a/src/test/java/com/amazon/ion/IntTest.java +++ b/src/test/java/com/amazon/ion/IntTest.java @@ -17,6 +17,8 @@ import java.math.BigDecimal; import java.math.BigInteger; + +import com.amazon.ion.system.IonSystemBuilder; import org.junit.Test; @@ -507,6 +509,23 @@ public void testBigDecimalValue() assertNull(nullValue.bigDecimalValue()); } + @Test + public void testGetIntegerSizeNearBoundariesUsingReader() { + assertIntegerSizeForValue(IntegerSize.INT, Integer.toString(Integer.MAX_VALUE - 10)); + assertIntegerSizeForValue(IntegerSize.INT, Integer.toString(Integer.MIN_VALUE + 10)); + assertIntegerSizeForValue(IntegerSize.LONG, Long.toString(Long.MAX_VALUE - 10)); + assertIntegerSizeForValue(IntegerSize.LONG, Long.toString(Long.MIN_VALUE + 10)); + assertIntegerSizeForValue(IntegerSize.LONG, (BigInteger.valueOf(Long.MAX_VALUE - 10).toString())); + assertIntegerSizeForValue(IntegerSize.LONG, (BigInteger.valueOf(Long.MIN_VALUE + 10).toString())); + } + + private void assertIntegerSizeForValue(IntegerSize expected, String ionData) { + IonReader reader = IonSystemBuilder.standard().build().newReader(ionData); + reader.next(); + IntegerSize size = reader.getIntegerSize(); + assertEquals(expected, size); + } + private void testGetIntegerSizeLongBoundary(long boundaryValue) { BigInteger boundary = BigInteger.valueOf(boundaryValue); IonInt boundaryIon = (IonInt)oneValue(boundary.toString()); From f9efe76e48cdd8f47178926ab25ec5d04f091703 Mon Sep 17 00:00:00 2001 From: Khushboo Desai Date: Wed, 19 Mar 2025 13:59:25 -0700 Subject: [PATCH 3/6] Removed unused import --- src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java b/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java index c3d478ab44..c1fc106038 100644 --- a/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java +++ b/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java @@ -42,7 +42,6 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.util.Date; -import java.lang.Character; /** * This reader calls the {@link IonReaderTextRawX} for low level events. From 21a69347ae41e58ca1ef16a68af609ac1771a182 Mon Sep 17 00:00:00 2001 From: Khushboo Desai Date: Wed, 19 Mar 2025 14:04:24 -0700 Subject: [PATCH 4/6] Adds suggested changes --- src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java | 6 ++++-- src/test/java/com/amazon/ion/IntTest.java | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java b/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java index c1fc106038..3612fecbc6 100644 --- a/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java +++ b/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java @@ -192,10 +192,12 @@ private static boolean magnitudeLessThanOrEqualTo(String lhs, int lhsLen, char[] assert lhsLen == rhs.length; for (int i = 0; i < lhsLen; i++) { - if (lhs.charAt(i) < rhs[i]) { + char lhc = lhs.charAt(i); + char rhc = rhs[i]; + if (lhc < rhc) { return true; } - if (lhs.charAt(i) > rhs[i]) + if (lhc > rhc) { return false; } diff --git a/src/test/java/com/amazon/ion/IntTest.java b/src/test/java/com/amazon/ion/IntTest.java index c988cf41df..cedacfe271 100644 --- a/src/test/java/com/amazon/ion/IntTest.java +++ b/src/test/java/com/amazon/ion/IntTest.java @@ -537,7 +537,7 @@ private void testGetIntegerSizeLongBoundary(long boundaryValue) { assertEquals(IntegerSize.BIG_INTEGER, pastBoundaryIon.getIntegerSize()); assertEquals(pastBoundary, pastBoundaryIon.bigIntegerValue()); - long withinBoundary = (boundaryValue < 0) ? boundaryValue + 10 : boundaryValue - 10; + long withinBoundary = boundaryValue - 10L * Long.signum(boundaryValue); IonInt withinBoundaryIon = (IonInt)oneValue(Long.toString(withinBoundary)); assertEquals(IntegerSize.LONG, withinBoundaryIon.getIntegerSize()); assertEquals(withinBoundary, withinBoundaryIon.longValue()); @@ -554,7 +554,7 @@ private void testGetIntegerSizeIntBoundary(int boundaryValue) { assertEquals(IntegerSize.LONG, pastBoundaryIon.getIntegerSize()); assertEquals(pastBoundary.longValue(), pastBoundaryIon.longValue()); - int withinBoundary = (boundaryValue < 0) ? boundaryValue + 10 : boundaryValue - 10; + int withinBoundary = boundaryValue - 10 * Integer.signum(boundaryValue); IonInt withinBoundaryIon = (IonInt)oneValue(Integer.toString(withinBoundary)); assertEquals(IntegerSize.INT, withinBoundaryIon.getIntegerSize()); assertEquals(withinBoundary, withinBoundaryIon.intValue()); From be483daa0634a4ab27a1e31441526c73205e4b31 Mon Sep 17 00:00:00 2001 From: Khushboo Desai Date: Wed, 19 Mar 2025 15:11:48 -0700 Subject: [PATCH 5/6] adds gradle spotless changes --- .../amazon/ion/impl/IonReaderTextSystemX.java | 17 ++--------------- src/test/java/com/amazon/ion/IntTest.java | 17 ++--------------- 2 files changed, 4 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java b/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java index 3612fecbc6..10b82ef715 100644 --- a/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java +++ b/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java @@ -1,18 +1,5 @@ -/* - * Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 package com.amazon.ion.impl; import static com.amazon.ion.impl._Private_ScalarConversions.getValueTypeName; diff --git a/src/test/java/com/amazon/ion/IntTest.java b/src/test/java/com/amazon/ion/IntTest.java index cedacfe271..d5d218a1ae 100644 --- a/src/test/java/com/amazon/ion/IntTest.java +++ b/src/test/java/com/amazon/ion/IntTest.java @@ -1,18 +1,5 @@ -/* - * Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 package com.amazon.ion; import java.math.BigDecimal; From 196b59e9a3854a526126fb39f70c9665ced0b09b Mon Sep 17 00:00:00 2001 From: Khushboo Desai Date: Wed, 19 Mar 2025 15:39:42 -0700 Subject: [PATCH 6/6] Adds within boundary tests in ReaderIntegerSizeTest --- .../ion/streaming/ReaderIntegerSizeTest.java | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/test/java/com/amazon/ion/streaming/ReaderIntegerSizeTest.java b/src/test/java/com/amazon/ion/streaming/ReaderIntegerSizeTest.java index ed5802b054..958ba4a8a0 100644 --- a/src/test/java/com/amazon/ion/streaming/ReaderIntegerSizeTest.java +++ b/src/test/java/com/amazon/ion/streaming/ReaderIntegerSizeTest.java @@ -1,18 +1,5 @@ -/* - * Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. - */ - +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 package com.amazon.ion.streaming; import com.amazon.ion.IntegerSize; @@ -142,6 +129,9 @@ public void testGetIntegerSizeNegativeIntBoundary() private void testGetIntegerSizeIntBoundary(int boundaryValue, long pastBoundary) { + in.next(); + assertEquals(IntegerSize.INT, in.getIntegerSize()); + assertEquals(boundaryValue + ((boundaryValue < 0) ? 9 : -9), in.intValue()); in.next(); assertEquals(IntegerSize.INT, in.getIntegerSize()); assertEquals(boundaryValue, in.intValue()); @@ -155,6 +145,9 @@ private void testGetIntegerSizeIntBoundary(int boundaryValue, long pastBoundary) private void testGetIntegerSizeLongBoundary(long boundaryValue, BigInteger pastBoundary) { + in.next(); + assertEquals(IntegerSize.LONG, in.getIntegerSize()); + assertEquals(boundaryValue + ((boundaryValue < 0) ? 9 : -9), in.longValue()); in.next(); assertEquals(IntegerSize.LONG, in.getIntegerSize()); assertEquals(boundaryValue, in.longValue()); @@ -169,7 +162,9 @@ private BigInteger loadBoundaries(long boundaryValue, IntRadix intRadix) { BigInteger boundary = BigInteger.valueOf(boundaryValue); BigInteger pastBoundary = (boundaryValue < 0) ? boundary.subtract(BigInteger.ONE) : boundary.add(BigInteger.ONE); - String ionText = intRadix.getString(boundary) + " " + intRadix.getString(pastBoundary); + BigInteger nine = BigInteger.valueOf(9); + BigInteger withinBoundary = (boundaryValue < 0) ? boundary.add(nine) : boundary.subtract(nine); + String ionText = intRadix.getString(withinBoundary) + " " + intRadix.getString(boundary) + " " + intRadix.getString(pastBoundary); read(ionText); return pastBoundary; }