diff --git a/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java b/src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java index c187682d5..10b82ef71 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; @@ -42,7 +29,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. @@ -191,9 +177,14 @@ 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]) + char lhc = lhs.charAt(i); + char rhc = rhs[i]; + if (lhc < rhc) { + return true; + } + 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 7401a3eaa..d5d218a1a 100644 --- a/src/test/java/com/amazon/ion/IntTest.java +++ b/src/test/java/com/amazon/ion/IntTest.java @@ -1,22 +1,11 @@ -/* - * 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; import java.math.BigInteger; + +import com.amazon.ion.system.IonSystemBuilder; import org.junit.Test; @@ -507,6 +496,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()); @@ -517,6 +523,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 - 10L * Long.signum(boundaryValue); + IonInt withinBoundaryIon = (IonInt)oneValue(Long.toString(withinBoundary)); + assertEquals(IntegerSize.LONG, withinBoundaryIon.getIntegerSize()); + assertEquals(withinBoundary, withinBoundaryIon.longValue()); } private void testGetIntegerSizeIntBoundary(int boundaryValue) { @@ -529,6 +540,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 - 10 * Integer.signum(boundaryValue); + IonInt withinBoundaryIon = (IonInt)oneValue(Integer.toString(withinBoundary)); + assertEquals(IntegerSize.INT, withinBoundaryIon.getIntegerSize()); + assertEquals(withinBoundary, withinBoundaryIon.intValue()); } } diff --git a/src/test/java/com/amazon/ion/streaming/ReaderIntegerSizeTest.java b/src/test/java/com/amazon/ion/streaming/ReaderIntegerSizeTest.java index ed5802b05..958ba4a8a 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; }