From 58b7d9479070c41656c730a45625adcfcaf27b14 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 20 Jul 2008 01:43:15 +0000 Subject: Added utilities for encoding and decoding BigDecimals. --- .../com/amazon/carbonado/raw/TestKeyEncoding.java | 199 +++++++++++++++++++++ 1 file changed, 199 insertions(+) (limited to 'src') diff --git a/src/test/java/com/amazon/carbonado/raw/TestKeyEncoding.java b/src/test/java/com/amazon/carbonado/raw/TestKeyEncoding.java index 3f6485f..bbbb1c3 100644 --- a/src/test/java/com/amazon/carbonado/raw/TestKeyEncoding.java +++ b/src/test/java/com/amazon/carbonado/raw/TestKeyEncoding.java @@ -18,6 +18,7 @@ package com.amazon.carbonado.raw; +import java.math.BigDecimal; import java.math.BigInteger; import java.util.Random; @@ -444,6 +445,204 @@ public class TestKeyEncoding extends TestCase { } } + public void test_BigDecimal() throws Exception { + BigDecimal[] ref = new BigDecimal[1]; + + { + BigDecimal d1 = new BigDecimal("123"); + BigDecimal d2 = new BigDecimal("123.0"); + + assertTrue(d1.compareTo(d2) == 0); + assertFalse(d1.equals(d2)); + + byte[] b1 = new byte[KeyEncoder.calculateEncodedLength(d1)]; + KeyEncoder.encode(d1, b1, 0); + + byte[] b2 = new byte[KeyEncoder.calculateEncodedLength(d2)]; + KeyEncoder.encode(d2, b2, 0); + + assertTrue(TestDataEncoding.byteArrayCompare(b1, b2) < 0); + + ref[0] = BigDecimal.ZERO; + KeyDecoder.decode(b1, 0, ref); + assertEquals(d1, ref[0]); + + ref[0] = BigDecimal.ZERO; + KeyDecoder.decode(b2, 0, ref); + assertEquals(d2, ref[0]); + } + + { + BigDecimal d1 = new BigDecimal("-123.0"); + BigDecimal d2 = new BigDecimal("-123"); + + assertTrue(d1.compareTo(d2) == 0); + assertFalse(d1.equals(d2)); + + byte[] b1 = new byte[KeyEncoder.calculateEncodedLength(d1)]; + KeyEncoder.encode(d1, b1, 0); + + byte[] b2 = new byte[KeyEncoder.calculateEncodedLength(d2)]; + KeyEncoder.encode(d2, b2, 0); + + assertTrue(TestDataEncoding.byteArrayCompare(b1, b2) < 0); + + ref[0] = BigDecimal.ZERO; + KeyDecoder.decode(b1, 0, ref); + assertEquals(d1, ref[0]); + + ref[0] = BigDecimal.ZERO; + KeyDecoder.decode(b2, 0, ref); + assertEquals(d2, ref[0]); + } + + byte[] bytes = new byte[5 + 1 + 100 + 5]; + + BigDecimal lastValue = null; + byte[] lastBytes = null; + for (int i=0; i 0); + + ref[0] = BigDecimal.ZERO; + KeyDecoder.decodeDesc(b1, 0, ref); + assertEquals(d1, ref[0]); + + ref[0] = BigDecimal.ZERO; + KeyDecoder.decodeDesc(b2, 0, ref); + assertEquals(d2, ref[0]); + } + + { + BigDecimal d1 = new BigDecimal("-123.0"); + BigDecimal d2 = new BigDecimal("-123"); + + assertTrue(d1.compareTo(d2) == 0); + assertFalse(d1.equals(d2)); + + byte[] b1 = new byte[KeyEncoder.calculateEncodedLength(d1)]; + KeyEncoder.encodeDesc(d1, b1, 0); + + byte[] b2 = new byte[KeyEncoder.calculateEncodedLength(d2)]; + KeyEncoder.encodeDesc(d2, b2, 0); + + assertTrue(TestDataEncoding.byteArrayCompare(b1, b2) > 0); + + ref[0] = BigDecimal.ZERO; + KeyDecoder.decodeDesc(b1, 0, ref); + assertEquals(d1, ref[0]); + + ref[0] = BigDecimal.ZERO; + KeyDecoder.decodeDesc(b2, 0, ref); + assertEquals(d2, ref[0]); + } + + byte[] bytes = new byte[5 + 1 + 100 + 5]; + + BigDecimal lastValue = null; + byte[] lastBytes = null; + for (int i=0; i