HBASE-4648 Bytes.toBigDecimal() doesn't use offset

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1189351 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
larsh 2011-10-26 18:05:02 +00:00
parent 770cc7d5d2
commit 9efd27b733
3 changed files with 42 additions and 14 deletions

View File

@ -58,6 +58,7 @@ Release 0.92.0 - Unreleased
(Akash Ashok) (Akash Ashok)
HBASE-4503 Purge deprecated HBaseClusterTestCase HBASE-4503 Purge deprecated HBaseClusterTestCase
HBASE-4374 Up default regions size from 256M to 1G HBASE-4374 Up default regions size from 256M to 1G
HBASE-4648 Bytes.toBigDecimal() doesn't use offset (Brian Keller via Lars H)
BUG FIXES BUG FIXES
HBASE-3280 YouAreDeadException being swallowed in HRS getMaster HBASE-3280 YouAreDeadException being swallowed in HRS getMaster

View File

@ -814,17 +814,6 @@ public class Bytes {
return toBigDecimal(bytes, 0, bytes.length); return toBigDecimal(bytes, 0, bytes.length);
} }
/**
* Converts a byte array to a BigDecimal value
*
* @param bytes
* @param offset
* @return the char value
*/
public static BigDecimal toBigDecimal(byte[] bytes, int offset) {
return toBigDecimal(bytes, offset, bytes.length);
}
/** /**
* Converts a byte array to a BigDecimal value * Converts a byte array to a BigDecimal value
* *
@ -839,9 +828,9 @@ public class Bytes {
return null; return null;
} }
int scale = toInt(bytes, 0); int scale = toInt(bytes, offset);
byte[] tcBytes = new byte[length - SIZEOF_INT]; byte[] tcBytes = new byte[length - SIZEOF_INT];
System.arraycopy(bytes, SIZEOF_INT, tcBytes, 0, length - SIZEOF_INT); System.arraycopy(bytes, offset + SIZEOF_INT, tcBytes, 0, length - SIZEOF_INT);
return new BigDecimal(new BigInteger(tcBytes), scale); return new BigDecimal(new BigInteger(tcBytes), scale);
} }

View File

@ -24,6 +24,7 @@ import java.io.ByteArrayOutputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays; import java.util.Arrays;
import junit.framework.TestCase; import junit.framework.TestCase;
@ -107,11 +108,25 @@ public class TestBytes extends TestCase {
} }
} }
public void testToInt() throws Exception {
int [] ints = {-1, 123, Integer.MIN_VALUE, Integer.MAX_VALUE};
for (int i = 0; i < ints.length; i++) {
byte [] b = Bytes.toBytes(ints[i]);
assertEquals(ints[i], Bytes.toInt(b));
byte [] b2 = bytesWithOffset(b);
assertEquals(ints[i], Bytes.toInt(b2, 1));
assertEquals(ints[i], Bytes.toInt(b2, 1, Bytes.SIZEOF_INT));
}
}
public void testToLong() throws Exception { public void testToLong() throws Exception {
long [] longs = {-1l, 123l, 122232323232l}; long [] longs = {-1l, 123l, Long.MIN_VALUE, Long.MAX_VALUE};
for (int i = 0; i < longs.length; i++) { for (int i = 0; i < longs.length; i++) {
byte [] b = Bytes.toBytes(longs[i]); byte [] b = Bytes.toBytes(longs[i]);
assertEquals(longs[i], Bytes.toLong(b)); assertEquals(longs[i], Bytes.toLong(b));
byte [] b2 = bytesWithOffset(b);
assertEquals(longs[i], Bytes.toLong(b2, 1));
assertEquals(longs[i], Bytes.toLong(b2, 1, Bytes.SIZEOF_LONG));
} }
} }
@ -120,6 +135,8 @@ public class TestBytes extends TestCase {
for (int i = 0; i < floats.length; i++) { for (int i = 0; i < floats.length; i++) {
byte [] b = Bytes.toBytes(floats[i]); byte [] b = Bytes.toBytes(floats[i]);
assertEquals(floats[i], Bytes.toFloat(b)); assertEquals(floats[i], Bytes.toFloat(b));
byte [] b2 = bytesWithOffset(b);
assertEquals(floats[i], Bytes.toFloat(b2, 1));
} }
} }
@ -128,9 +145,30 @@ public class TestBytes extends TestCase {
for (int i = 0; i < doubles.length; i++) { for (int i = 0; i < doubles.length; i++) {
byte [] b = Bytes.toBytes(doubles[i]); byte [] b = Bytes.toBytes(doubles[i]);
assertEquals(doubles[i], Bytes.toDouble(b)); assertEquals(doubles[i], Bytes.toDouble(b));
byte [] b2 = bytesWithOffset(b);
assertEquals(doubles[i], Bytes.toDouble(b2, 1));
} }
} }
public void testToBigDecimal() throws Exception {
BigDecimal [] decimals = {new BigDecimal("-1"), new BigDecimal("123.123"),
new BigDecimal("123123123123")};
for (int i = 0; i < decimals.length; i++) {
byte [] b = Bytes.toBytes(decimals[i]);
assertEquals(decimals[i], Bytes.toBigDecimal(b));
byte [] b2 = bytesWithOffset(b);
assertEquals(decimals[i], Bytes.toBigDecimal(b2, 1, b.length));
}
}
private byte [] bytesWithOffset(byte [] src) {
// add one byte in front to test offset
byte [] result = new byte[src.length + 1];
result[0] = (byte) 0xAA;
System.arraycopy(src, 0, result, 1, src.length);
return result;
}
public void testBinarySearch() throws Exception { public void testBinarySearch() throws Exception {
byte [][] arr = { byte [][] arr = {
{1}, {1},