Added Tests for bytesToLong
- Tested for Bytes to long - Fixed little endian and big endian being thewrong way around. #story[782]
This commit is contained in:
@@ -64,16 +64,6 @@ public class ByteConverter {
|
||||
public static long bytesToLong(byte[] bytes, ByteOrder byteOrder){
|
||||
byte[] bites = new byte[8];
|
||||
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
|
||||
for (int i = 0; i < 8 - bytes.length; i++) {
|
||||
bites[i] = 0b0;
|
||||
}
|
||||
for (int i = 8 - bytes.length; i < 8; i++) {
|
||||
bites[i] = bytes[i];
|
||||
if (i > 8){//break if over the limit
|
||||
break;
|
||||
}
|
||||
}
|
||||
}else{//if big endian
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
bites[i] = bytes[i];
|
||||
if (i > 8){//break if over hte limit
|
||||
@@ -83,6 +73,16 @@ public class ByteConverter {
|
||||
for (int i = bytes.length; i < 8; i++) {
|
||||
bites[i] = 0b0;
|
||||
}
|
||||
}else{//if big endian
|
||||
for (int i = 0; i < 8 - bytes.length; i++) {
|
||||
bites[i] = 0b0;
|
||||
}
|
||||
for (int i = 8 - bytes.length; i < 8; i++) {
|
||||
bites[i] = bytes[i];
|
||||
if (i > 8){//break if over the limit
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ByteBuffer.wrap(bites).order(byteOrder).getLong();
|
||||
}
|
||||
|
||||
@@ -44,6 +44,35 @@ public class ByteConverterTest {
|
||||
assertTrue(testArrayContents(chopped6, bytes8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLargerIntToByte(){
|
||||
int int1 = 1532158456; //100 in bytes
|
||||
byte[] bytes1 = {(byte)0xF8, (byte)0xE1, 0x52, 0x5B};//this is in little endian
|
||||
assertTrue(testArrayContents(ByteConverter.intToBytes(int1), bytes1));
|
||||
byte[] bytes2 = {0x5B, 0x52, (byte)0xE1, (byte)0xF8};// this is big endian
|
||||
assertTrue(testArrayContents(ByteConverter.intToBytes(int1, ByteConverter.IntegerSize, ByteOrder.BIG_ENDIAN), bytes2));
|
||||
//test chopping
|
||||
byte[] chopped1 = ByteConverter.intToBytes(int1, 3, ByteOrder.LITTLE_ENDIAN);
|
||||
byte[] bytes3 = {(byte)0xF8, (byte)0xE1, 0x52};
|
||||
assertTrue(testArrayContents(chopped1, bytes3));
|
||||
byte[] chopped2 = ByteConverter.intToBytes(int1, 2, ByteOrder.LITTLE_ENDIAN);
|
||||
byte[] bytes4 = {(byte)0xF8, (byte)0xE1};
|
||||
assertTrue(testArrayContents(chopped2, bytes4));
|
||||
byte[] chopped3 = ByteConverter.intToBytes(int1, 1, ByteOrder.LITTLE_ENDIAN);
|
||||
byte[] bytes5 = {(byte)0xF8};
|
||||
assertTrue(testArrayContents(chopped3, bytes5));
|
||||
|
||||
byte[] chopped4 = ByteConverter.intToBytes(int1, 3, ByteOrder.BIG_ENDIAN);
|
||||
byte[] bytes6 = {0x52, (byte)0xE1, (byte)0xF8};
|
||||
assertTrue(testArrayContents(chopped4, bytes6));
|
||||
byte[] chopped5 = ByteConverter.intToBytes(int1, 2, ByteOrder.BIG_ENDIAN);
|
||||
byte[] bytes7 = {(byte)0xE1, (byte)0xF8};
|
||||
assertTrue(testArrayContents(chopped5, bytes7));
|
||||
byte[] chopped6 = ByteConverter.intToBytes(int1, 1, ByteOrder.BIG_ENDIAN);
|
||||
byte[] bytes8 = {(byte)0xF8};
|
||||
assertTrue(testArrayContents(chopped6, bytes8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteToInt(){
|
||||
int int1 = 100; //100 in bytes
|
||||
@@ -56,6 +85,47 @@ public class ByteConverterTest {
|
||||
assertTrue(ByteConverter.bytesToInt((byte)100) == int1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteToLong(){
|
||||
long lng1 = 15; //100 in bytes
|
||||
byte[] bytes1 = {15, 0, 0, 0, 0, 0, 0, 0};//this is in little endian
|
||||
assertTrue(ByteConverter.bytesToLong(bytes1) == lng1);
|
||||
assertTrue(ByteConverter.bytesToLong(bytes1, ByteOrder.LITTLE_ENDIAN) == lng1);
|
||||
byte[] bytes2 = {0, 0, 0, 0, 0, 0, 0, 15};// this is big endian
|
||||
assertTrue(ByteConverter.bytesToLong(bytes2, ByteOrder.BIG_ENDIAN) == lng1);
|
||||
//check single bytes to integers
|
||||
assertTrue(ByteConverter.bytesToLong((byte)15) == lng1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntToLong(){/*
|
||||
long lng1 = 15; //100 in bytes
|
||||
byte[] bytes1 = {15, 0, 0, 0, 0, 0, 0, 0};//this is in little endian
|
||||
assertTrue(testArrayContents(ByteConverter.longToBytes(lng1), bytes1));
|
||||
byte[] bytes2 = {0, 0, 0, 0, 0, 0, 0, 15};// this is big endian
|
||||
assertTrue(testArrayContents(ByteConverter.lngToBytes(lng1, ByteConverter.IntegerSize, ByteOrder.BIG_ENDIAN), bytes2));
|
||||
//test chopping
|
||||
byte[] chopped1 = ByteConverter.lngToBytes(lng1, 3, ByteOrder.LITTLE_ENDIAN);
|
||||
byte[] bytes3 = {100, 0, 0};
|
||||
assertTrue(testArrayContents(chopped1, bytes3));
|
||||
byte[] chopped2 = ByteConverter.lngToBytes(lng1, 2, ByteOrder.LITTLE_ENDIAN);
|
||||
byte[] bytes4 = {100, 0};
|
||||
assertTrue(testArrayContents(chopped2, bytes4));
|
||||
byte[] chopped3 = ByteConverter.lngToBytes(lng1, 1, ByteOrder.LITTLE_ENDIAN);
|
||||
byte[] bytes5 = {100};
|
||||
assertTrue(testArrayContents(chopped3, bytes5));
|
||||
|
||||
byte[] chopped4 = ByteConverter.lngToBytes(lng1, 3, ByteOrder.BIG_ENDIAN);
|
||||
byte[] bytes6 = {0, 0, 100};
|
||||
assertTrue(testArrayContents(chopped4, bytes6));
|
||||
byte[] chopped5 = ByteConverter.lngToBytes(lng1, 2, ByteOrder.BIG_ENDIAN);
|
||||
byte[] bytes7 = {0, 100};
|
||||
assertTrue(testArrayContents(chopped5, bytes7));
|
||||
byte[] chopped6 = ByteConverter.lngToBytes(lng1, 1, ByteOrder.BIG_ENDIAN);
|
||||
byte[] bytes8 = {100};
|
||||
assertTrue(testArrayContents(chopped6, bytes8));*/
|
||||
}
|
||||
|
||||
public boolean testArrayContents(byte[] bytes1, byte[] bytes2){
|
||||
if (bytes1.length != bytes2.length){
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user