Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions src/main/java/com/amazon/ion/impl/IonReaderTextSystemX.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
46 changes: 31 additions & 15 deletions src/test/java/com/amazon/ion/IntTest.java
Original file line number Diff line number Diff line change
@@ -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;


Expand Down Expand Up @@ -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());
Expand All @@ -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) {
Expand All @@ -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());
}

}
27 changes: 11 additions & 16 deletions src/test/java/com/amazon/ion/streaming/ReaderIntegerSizeTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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;
}
Expand Down