|
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; |
@@ -190,4 +191,57 @@ public void testBigDecimalReadWrite() { |
190 | 191 | assertEquals(decimal8, decimalVector.getObject(7)); |
191 | 192 | } |
192 | 193 | } |
| 194 | + |
| 195 | + /** |
| 196 | + * Test {@link DecimalVector#setBigEndian(int, byte[])} which takes BE layout input and stores in LE layout. |
| 197 | + * Cases to cover: value given in byte array in different lengths in range [1-16] and negative values. |
| 198 | + */ |
| 199 | + @Test |
| 200 | + public void decimalBE2LE() { |
| 201 | + try (DecimalVector decimalVector = TestUtils.newVector(DecimalVector.class, "decimal", new ArrowType.Decimal(21, 2), allocator);) { |
| 202 | + decimalVector.allocateNew(); |
| 203 | + |
| 204 | + BigInteger[] testBigInts = new BigInteger[] { |
| 205 | + new BigInteger("0"), |
| 206 | + new BigInteger("-1"), |
| 207 | + new BigInteger("23"), |
| 208 | + new BigInteger("234234"), |
| 209 | + new BigInteger("-234234234"), |
| 210 | + new BigInteger("234234234234"), |
| 211 | + new BigInteger("-56345345345345"), |
| 212 | + new BigInteger("29823462983462893462934679234653456345"), // converts to 16 byte array |
| 213 | + new BigInteger("-3894572983475982374598324598234346536"), // converts to 16 byte array |
| 214 | + new BigInteger("-345345"), |
| 215 | + new BigInteger("754533") |
| 216 | + }; |
| 217 | + |
| 218 | + int insertionIdx = 0; |
| 219 | + insertionIdx++; // insert a null |
| 220 | + for (BigInteger val : testBigInts) { |
| 221 | + decimalVector.setBigEndian(insertionIdx++, val.toByteArray()); |
| 222 | + } |
| 223 | + insertionIdx++; // insert a null |
| 224 | + // insert a zero length buffer |
| 225 | + decimalVector.setBigEndian(insertionIdx++, new byte[0]); |
| 226 | + |
| 227 | + // Try inserting a buffer larger than 16bytes and expect a failure |
| 228 | + try { |
| 229 | + decimalVector.setBigEndian(insertionIdx, new byte[17]); |
| 230 | + fail("above statement should have failed"); |
| 231 | + } catch (IllegalArgumentException ex) { |
| 232 | + assertTrue(ex.getMessage().equals("Invalid decimal value length. Valid length in [1 - 16], got 17")); |
| 233 | + } |
| 234 | + decimalVector.setValueCount(insertionIdx); |
| 235 | + |
| 236 | + // retrieve values and check if they are correct |
| 237 | + int outputIdx = 0; |
| 238 | + assertTrue(decimalVector.isNull(outputIdx++)); |
| 239 | + for (BigInteger expected : testBigInts) { |
| 240 | + final BigDecimal actual = decimalVector.getObject(outputIdx++); |
| 241 | + assertEquals(expected, actual.unscaledValue()); |
| 242 | + } |
| 243 | + assertTrue(decimalVector.isNull(outputIdx++)); |
| 244 | + assertEquals(BigInteger.valueOf(0), decimalVector.getObject(outputIdx).unscaledValue()); |
| 245 | + } |
| 246 | + } |
193 | 247 | } |
0 commit comments