diff options
| author | Brian S. O'Neill <bronee@gmail.com> | 2008-07-18 04:39:37 +0000 |
|---|---|---|
| committer | Brian S. O'Neill <bronee@gmail.com> | 2008-07-18 04:39:37 +0000 |
| commit | a16429d7c7c1c8b3e322870fd0517dc24a0df451 (patch) | |
| tree | 065fbbccce9d51f7235e9b5c131b8979ee36cb08 /src/test/java | |
| parent | 470900c8bea7b3f9d13624433aa9da0f906c23a3 (diff) | |
Added utilities for encoding and decoding BigIntegers.
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/com/amazon/carbonado/raw/TestDataEncoding.java | 24 | ||||
| -rw-r--r-- | src/test/java/com/amazon/carbonado/raw/TestKeyEncoding.java | 66 |
2 files changed, 90 insertions, 0 deletions
diff --git a/src/test/java/com/amazon/carbonado/raw/TestDataEncoding.java b/src/test/java/com/amazon/carbonado/raw/TestDataEncoding.java index bff5820..b08049f 100644 --- a/src/test/java/com/amazon/carbonado/raw/TestDataEncoding.java +++ b/src/test/java/com/amazon/carbonado/raw/TestDataEncoding.java @@ -18,6 +18,8 @@ package com.amazon.carbonado.raw;
+import java.math.BigInteger;
+
import java.util.Random;
import junit.framework.TestCase;
@@ -463,6 +465,28 @@ public class TestDataEncoding extends TestCase { }
}
+ public void test_BigInteger() throws Exception {
+ byte[] bytes = new byte[101];
+
+ assertEquals(1, DataEncoder.encode((BigInteger) null, bytes, 0));
+ BigInteger[] ref = new BigInteger[1];
+ ref[0] = BigInteger.ONE;
+ assertEquals(1, DataDecoder.decode(bytes, 0, ref));
+ assertEquals(null, ref[0]);
+
+ for (int i=0; i<SHORT_TEST; i++) {
+ int len = mRandom.nextInt(100 * 8);
+ BigInteger value = new BigInteger(len, mRandom);
+ if (mRandom.nextBoolean()) {
+ value = value.negate();
+ }
+ int amt = DataEncoder.calculateEncodedLength(value);
+ assertEquals(amt, DataEncoder.encode(value, bytes, 0));
+ assertEquals(amt, DataDecoder.decode(bytes, 0, ref));
+ assertEquals(value, ref[0]);
+ }
+ }
+
public void test_String() throws Exception {
String[] ref = new String[1];
for (int i=0; i<SHORT_TEST; i++) {
diff --git a/src/test/java/com/amazon/carbonado/raw/TestKeyEncoding.java b/src/test/java/com/amazon/carbonado/raw/TestKeyEncoding.java index 3dba00e..6214813 100644 --- a/src/test/java/com/amazon/carbonado/raw/TestKeyEncoding.java +++ b/src/test/java/com/amazon/carbonado/raw/TestKeyEncoding.java @@ -18,6 +18,8 @@ package com.amazon.carbonado.raw;
+import java.math.BigInteger;
+
import java.util.Random;
import junit.framework.TestCase;
@@ -378,6 +380,70 @@ public class TestKeyEncoding extends TestCase { }
}
+ public void test_BigInteger() throws Exception {
+ byte[] bytes = new byte[205];
+ BigInteger[] ref = new BigInteger[1];
+
+ BigInteger lastValue = null;
+ byte[] lastBytes = null;
+ for (int i=0; i<LONG_TEST; i++) {
+ BigInteger value;
+ if (mRandom.nextInt(10) == 1) {
+ value = null;
+ } else {
+ int len = mRandom.nextInt(200 * 8);
+ value = new BigInteger(len, mRandom);
+ if (mRandom.nextBoolean()) {
+ value = value.negate();
+ }
+ }
+
+ int amt = KeyEncoder.calculateEncodedLength(value);
+ assertEquals(amt, KeyEncoder.encode(value, bytes, 0));
+ assertEquals(amt, KeyDecoder.decode(bytes, 0, ref));
+ assertEquals(value, ref[0]);
+
+ if (lastBytes != null) {
+ int sgn = TestDataEncoding.compare(value, lastValue);
+ assertEquals(sgn, TestDataEncoding.byteArrayCompare(bytes, lastBytes));
+ }
+ lastValue = value;
+ lastBytes = bytes.clone();
+ }
+ }
+
+ public void test_BigIntegerDesc() throws Exception {
+ byte[] bytes = new byte[205];
+ BigInteger[] ref = new BigInteger[1];
+
+ BigInteger lastValue = null;
+ byte[] lastBytes = null;
+ for (int i=0; i<LONG_TEST; i++) {
+ BigInteger value;
+ if (mRandom.nextInt(10) == 1) {
+ value = null;
+ } else {
+ int len = mRandom.nextInt(200 * 8);
+ value = new BigInteger(len, mRandom);
+ if (mRandom.nextBoolean()) {
+ value = value.negate();
+ }
+ }
+
+ int amt = KeyEncoder.calculateEncodedLength(value);
+ assertEquals(amt, KeyEncoder.encodeDesc(value, bytes, 0));
+ assertEquals(amt, KeyDecoder.decodeDesc(bytes, 0, ref));
+ assertEquals(value, ref[0]);
+
+ if (lastBytes != null) {
+ int sgn = -TestDataEncoding.compare(value, lastValue);
+ assertEquals(sgn, TestDataEncoding.byteArrayCompare(bytes, lastBytes));
+ }
+ lastValue = value;
+ lastBytes = bytes.clone();
+ }
+ }
+
public void test_String() throws Exception {
String lastValue = null;
byte[] lastBytes = null;
|
