- Integers cutting and chopping now works - Integer to bytes word - bytes to integer also work #story[782]main
parent
be9f02997e
commit
df871b1242
@ -0,0 +1,71 @@
|
||||
package seng302.Networking;
|
||||
|
||||
import org.junit.Test;
|
||||
import seng302.Networking.Utils.ByteConverter;
|
||||
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 25/04/17.
|
||||
*/
|
||||
public class ByteConverterTest {
|
||||
|
||||
@Test
|
||||
public void testIntToByte(){
|
||||
int int1 = 100; //100 in bytes
|
||||
byte[] bytes1 = {100, 0, 0, 0};//this is in little endian
|
||||
assertTrue(testArrayContents(ByteConverter.intToBytes(int1), bytes1));
|
||||
byte[] bytes2 = {0, 0, 0, 100};// 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 = {100, 0, 0};
|
||||
assertTrue(testArrayContents(chopped1, bytes3));
|
||||
byte[] chopped2 = ByteConverter.intToBytes(int1, 2, ByteOrder.LITTLE_ENDIAN);
|
||||
byte[] bytes4 = {100, 0};
|
||||
assertTrue(testArrayContents(chopped2, bytes4));
|
||||
byte[] chopped3 = ByteConverter.intToBytes(int1, 1, ByteOrder.LITTLE_ENDIAN);
|
||||
byte[] bytes5 = {100};
|
||||
assertTrue(testArrayContents(chopped3, bytes5));
|
||||
|
||||
byte[] chopped4 = ByteConverter.intToBytes(int1, 3, ByteOrder.BIG_ENDIAN);
|
||||
byte[] bytes6 = {0, 0, 100};
|
||||
assertTrue(testArrayContents(chopped4, bytes6));
|
||||
byte[] chopped5 = ByteConverter.intToBytes(int1, 2, ByteOrder.BIG_ENDIAN);
|
||||
byte[] bytes7 = {0, 100};
|
||||
assertTrue(testArrayContents(chopped5, bytes7));
|
||||
byte[] chopped6 = ByteConverter.intToBytes(int1, 1, ByteOrder.BIG_ENDIAN);
|
||||
byte[] bytes8 = {100};
|
||||
assertTrue(testArrayContents(chopped6, bytes8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteToInt(){
|
||||
int int1 = 100; //100 in bytes
|
||||
byte[] bytes1 = {100, 0, 0, 0};//this is in little endian
|
||||
assertTrue(ByteConverter.bytesToInt(bytes1) == int1);
|
||||
assertTrue(ByteConverter.bytesToInt(bytes1, ByteOrder.LITTLE_ENDIAN) == int1);
|
||||
byte[] bytes2 = {0, 0, 0, 100};// this is big endian
|
||||
assertTrue(ByteConverter.bytesToInt(bytes2, ByteOrder.BIG_ENDIAN) == int1);
|
||||
//check single bytes to integers
|
||||
assertTrue(ByteConverter.bytesToInt((byte)100) == int1);
|
||||
}
|
||||
|
||||
public boolean testArrayContents(byte[] bytes1, byte[] bytes2){
|
||||
if (bytes1.length != bytes2.length){
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < bytes1.length; i++){
|
||||
if (bytes1[i] != bytes2[i]){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue