|
20 | 20 |
|
21 | 21 | import static org.junit.Assert.assertEquals; |
22 | 22 | import static org.junit.Assert.assertTrue; |
| 23 | +import static org.junit.Assert.fail; |
23 | 24 |
|
24 | 25 | import java.math.BigDecimal; |
25 | 26 | import java.math.BigInteger; |
@@ -111,4 +112,57 @@ public void testBigDecimalDifferentScaleAndPrecision() { |
111 | 112 | } |
112 | 113 | } |
113 | 114 | } |
| 115 | + |
| 116 | + /** |
| 117 | + * Test {@link NullableDecimalVector#setBigEndian(int, byte[])} which takes BE layout input and stores in LE layout. |
| 118 | + * Cases to cover: value given in byte array in different lengths in range [1-16] and negative values. |
| 119 | + */ |
| 120 | + @Test |
| 121 | + public void decimalBE2LE() { |
| 122 | + try (NullableDecimalVector decimalVector = TestUtils.newVector(NullableDecimalVector.class, "decimal", new ArrowType.Decimal(21, 2), allocator);) { |
| 123 | + decimalVector.allocateNew(); |
| 124 | + |
| 125 | + BigInteger[] testBigInts = new BigInteger[] { |
| 126 | + new BigInteger("0"), |
| 127 | + new BigInteger("-1"), |
| 128 | + new BigInteger("23"), |
| 129 | + new BigInteger("234234"), |
| 130 | + new BigInteger("-234234234"), |
| 131 | + new BigInteger("234234234234"), |
| 132 | + new BigInteger("-56345345345345"), |
| 133 | + new BigInteger("29823462983462893462934679234653456345"), // converts to 16 byte array |
| 134 | + new BigInteger("-3894572983475982374598324598234346536"), // converts to 16 byte array |
| 135 | + new BigInteger("-345345"), |
| 136 | + new BigInteger("754533") |
| 137 | + }; |
| 138 | + |
| 139 | + int insertionIdx = 0; |
| 140 | + insertionIdx++; // insert a null |
| 141 | + for (BigInteger val : testBigInts) { |
| 142 | + decimalVector.setBigEndian(insertionIdx++, val.toByteArray()); |
| 143 | + } |
| 144 | + insertionIdx++; // insert a null |
| 145 | + // insert a zero length buffer |
| 146 | + decimalVector.setBigEndian(insertionIdx++, new byte[0]); |
| 147 | + |
| 148 | + // Try inserting a buffer larger than 16bytes and expect a failure |
| 149 | + try { |
| 150 | + decimalVector.setBigEndian(insertionIdx, new byte[17]); |
| 151 | + fail("above statement should have failed"); |
| 152 | + } catch (IllegalArgumentException ex) { |
| 153 | + assertTrue(ex.getMessage().equals("Invalid decimal value length. Valid length in [1 - 16], got 17")); |
| 154 | + } |
| 155 | + decimalVector.setValueCount(insertionIdx); |
| 156 | + |
| 157 | + // retrieve values and check if they are correct |
| 158 | + int outputIdx = 0; |
| 159 | + assertTrue(decimalVector.isNull(outputIdx++)); |
| 160 | + for (BigInteger expected : testBigInts) { |
| 161 | + final BigDecimal actual = decimalVector.getObject(outputIdx++); |
| 162 | + assertEquals(expected, actual.unscaledValue()); |
| 163 | + } |
| 164 | + assertTrue(decimalVector.isNull(outputIdx++)); |
| 165 | + assertEquals(BigInteger.valueOf(0), decimalVector.getObject(outputIdx).unscaledValue()); |
| 166 | + } |
| 167 | + } |
114 | 168 | } |
0 commit comments