Added some tests around ByteUtils
This commit is contained in:
+4
-2
@@ -65,8 +65,10 @@ public final class IBeaconManufacturerData {
|
||||
public IBeaconManufacturerData(final byte[] data) {
|
||||
mData = data;
|
||||
|
||||
mCompanyIdentidier = ByteUtils.getIntFrom2ByteArray(
|
||||
ByteUtils.invertArray(Arrays.copyOfRange(mData, 0, 2)));
|
||||
final byte[] intArray = Arrays.copyOfRange(mData, 0, 2);
|
||||
ByteUtils.invertArray(intArray);
|
||||
|
||||
mCompanyIdentidier = ByteUtils.getIntFrom2ByteArray(intArray);
|
||||
|
||||
mIBeaconAdvertisment = ByteUtils.getIntFrom2ByteArray(Arrays.copyOfRange(mData, 2, 4));
|
||||
mUUID = calculateUUIDString(Arrays.copyOfRange(mData, 4, 20));
|
||||
|
||||
@@ -36,7 +36,7 @@ public class ByteUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if a byte arry starts with another byte array.
|
||||
* Checks to see if a byte array starts with another byte array.
|
||||
*
|
||||
* @param array the array
|
||||
* @param prefix the prefix
|
||||
@@ -78,7 +78,7 @@ public class ByteUtils {
|
||||
* <p/>
|
||||
* For example, FF will be converted to 255 and not -1.
|
||||
*
|
||||
* @param bite the bite
|
||||
* @param bite the byte
|
||||
* @return the int from byte
|
||||
*/
|
||||
public static int getIntFromByte(final byte bite) {
|
||||
@@ -107,12 +107,11 @@ public class ByteUtils {
|
||||
|
||||
|
||||
/**
|
||||
* Inverts an array
|
||||
* Inverts an byte array in place.
|
||||
*
|
||||
* @param array the array
|
||||
* @return the byte[]
|
||||
*/
|
||||
public static byte[] invertArray(final byte[] array) {
|
||||
public static void invertArray(final byte[] array) {
|
||||
final int size = array.length;
|
||||
byte temp;
|
||||
|
||||
@@ -121,7 +120,5 @@ public class ByteUtils {
|
||||
array[i] = array[size - 1 - i];
|
||||
array[size - 1 - i] = temp;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package uk.co.alt236.bluetoothlelib.util;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class ByteUtilsTest extends TestCase {
|
||||
|
||||
public void testInvertArray() throws Exception {
|
||||
final byte[] original = {1, 2 ,3 ,4};
|
||||
final byte[] out = new byte[original.length];
|
||||
|
||||
System.arraycopy( original, 0, out, 0, original.length);
|
||||
ByteUtils.invertArray(out);
|
||||
|
||||
assertEquals(original[0], out[3]);
|
||||
assertEquals(original[1], out[2]);
|
||||
assertEquals(original[2], out[1]);
|
||||
assertEquals(original[3], out[0]);
|
||||
}
|
||||
|
||||
public void testGetIntFromByte() throws Exception {
|
||||
byte bite = 127;
|
||||
int integer = ByteUtils.getIntFromByte(bite);
|
||||
assertEquals(127, integer);
|
||||
|
||||
bite = -1;
|
||||
integer = ByteUtils.getIntFromByte(bite);
|
||||
assertEquals(255, integer);
|
||||
}
|
||||
|
||||
public void testDoesArrayBeginWith() throws Exception {
|
||||
|
||||
// If the prefix is longer than the array,
|
||||
// we automatically fail
|
||||
byte[] array = new byte[10];
|
||||
byte[] prefix = new byte[array.length * 2];
|
||||
assertFalse(ByteUtils.doesArrayBeginWith(array, prefix));
|
||||
|
||||
array = new byte[]{1, 2, 3};
|
||||
prefix = new byte[]{1, 3};
|
||||
assertFalse(ByteUtils.doesArrayBeginWith(array, prefix));
|
||||
|
||||
array = new byte[10];
|
||||
prefix = new byte[array.length];
|
||||
assertTrue(ByteUtils.doesArrayBeginWith(array, prefix));
|
||||
|
||||
array = new byte[]{1, 2, 3};
|
||||
prefix = new byte[]{1, 2};
|
||||
assertTrue(ByteUtils.doesArrayBeginWith(array, prefix));
|
||||
}
|
||||
|
||||
public void testByteArrayToHexString() throws Exception {
|
||||
assertEquals("[]", ByteUtils.byteArrayToHexString(new byte[0]));
|
||||
|
||||
final byte[] one = {1, 10, 15, 127};
|
||||
assertEquals("[01, 0A, 0F, 7F]", ByteUtils.byteArrayToHexString(one));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user