added decoder test for courseWindDecoder

-added test for coureswind decoder
-changed windID in raceVisionEncoder to byte as char is 2 bytes and can only transport 1

#story[778, 782]
main
hba56 9 years ago
parent c9a968a5fd
commit f5000eea11

@ -14,7 +14,7 @@ public class CourseWindDecoder {
byte messageVersionNumber;
byte byteWindID;
byte loopCount;
ArrayList<CourseWind> loopMessages;
ArrayList<CourseWind> loopMessages = new ArrayList();
public CourseWindDecoder(byte[] encodedCourseWind) {
final int lengthInBytesOfMessages = 20;
@ -22,11 +22,12 @@ public class CourseWindDecoder {
messageVersionNumber = encodedCourseWind[0];
byteWindID = encodedCourseWind[1];
loopCount = encodedCourseWind[2];
byte[] loopMessagesBytes = Arrays.copyOfRange(encodedCourseWind, 3, lengthInBytesOfMessages*loopCount);
byte[] loopMessagesBytes = Arrays.copyOfRange(encodedCourseWind, 3, lengthInBytesOfMessages*loopCount+3);
int messageLoopIndex = 0;
for (int i=0; i < loopCount; i++) {
byte[] messageBytes = Arrays.copyOfRange(loopMessagesBytes, messageLoopIndex, messageLoopIndex+20);
ArrayList test = new ArrayList();
byte[] windId = Arrays.copyOfRange(messageBytes, 0, 1);
byte[] time = Arrays.copyOfRange(messageBytes, 1, 7);
byte[] raceID = Arrays.copyOfRange(messageBytes, 7, 11);
@ -36,16 +37,28 @@ public class CourseWindDecoder {
byte[] bestDownwindAngle = Arrays.copyOfRange(messageBytes, 17, 19);
byte[] flags = Arrays.copyOfRange(messageBytes, 19, 20);
CourseWind message = new CourseWind(bytesToInt(windId), bytesToLong(time),
bytesToInt(raceID), bytesToInt(windDirection),
bytesToInt(windSpeed), bytesToInt(bestUpwindAngle),
bytesToInt(bestDownwindAngle), bytesToInt(flags));
CourseWind message = new CourseWind(windId[0], bytesToLong(time),
bytesToInt(raceID), twoByteToInt(windDirection),
twoByteToInt(windSpeed), twoByteToInt(bestUpwindAngle),
twoByteToInt(bestDownwindAngle), flags[0]);
loopMessages.add(message);
messageLoopIndex += 20;
}
}
private int twoByteToInt(byte[] bytesInt){
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.order(ByteOrder.BIG_ENDIAN);
byteBuffer.put((byte)0);
byteBuffer.put((byte)0);
byteBuffer.put(bytesInt);
int num = byteBuffer.getInt(0);
return num;
}
private int bytesToInt(byte[] bytesInt){
ByteBuffer wrapped = ByteBuffer.wrap(bytesInt);
int num = wrapped.getInt();
@ -66,4 +79,8 @@ public class CourseWindDecoder {
long longVal = byteBuffer.getLong(0);
return longVal;
}
public ArrayList<CourseWind> getLoopMessages() {
return loopMessages;
}
}

@ -231,14 +231,13 @@ public class RaceVisionByteEncoder {
return result.array();
}
public byte[] courseWind(char windID, CourseWind[] courseWinds){
public byte[] courseWind(byte windID, ArrayList<CourseWind> courseWinds){
int messageVersionNumber = 0b1;
char byteWindID = windID;
byte[] loopcount = convert(courseWinds.length, 1);
ByteBuffer result = ByteBuffer.allocate(3 + 20 * courseWinds.length);
byte byteWindID = windID;
byte[] loopcount = convert(courseWinds.size(), 1);
ByteBuffer result = ByteBuffer.allocate(3 + 20 * courseWinds.size());
result.put(convert(messageVersionNumber, 1));
result.putChar(byteWindID);
result.put(byteWindID);
result.put(loopcount);
for (CourseWind wind: courseWinds){
result.put(convert(wind.getID(), 1));

@ -0,0 +1,60 @@
package seng302.Networking.MessageDecoders;
import org.junit.Assert;
import org.junit.Test;
import seng302.Networking.CourseWind;
import seng302.Networking.RaceVisionByteEncoder;
import java.util.ArrayList;
import java.util.List;
/**
* Created by hba56 on 23/04/17.
*/
public class CourseWindDecoderTest {
@Test
public void getByteArrayTest(){
long time = System.currentTimeMillis();
CourseWind testCourseWind1 = new CourseWind(1, time, 2,
3, 4, 5,
7, 6);
long time2 = System.currentTimeMillis();
CourseWind testCourseWind2 = new CourseWind(2, time2, 2,
3, 4, 5,
7, 6);
ArrayList<CourseWind> testCourseWinds = new ArrayList<CourseWind>();
testCourseWinds.add(testCourseWind1);
testCourseWinds.add(testCourseWind2);
RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder();
byte[] testEncodedCourseWind = raceVisionByteEncoder.courseWind((byte) 1, testCourseWinds);
CourseWindDecoder testDecoder = new CourseWindDecoder(testEncodedCourseWind);
ArrayList<CourseWind> testDecodedCourseWinds = testDecoder.getLoopMessages();
Assert.assertEquals(testCourseWinds.get(0).getID(), testDecodedCourseWinds.get(0).getID());
Assert.assertEquals(testCourseWinds.get(0).getTime(), testDecodedCourseWinds.get(0).getTime());
Assert.assertEquals(testCourseWinds.get(0).getRaceID(), testDecodedCourseWinds.get(0).getRaceID());
Assert.assertEquals(testCourseWinds.get(0).getWindDirection(), testDecodedCourseWinds.get(0).getWindDirection());
Assert.assertEquals(testCourseWinds.get(0).getWindSpeed(), testDecodedCourseWinds.get(0).getWindSpeed());
Assert.assertEquals(testCourseWinds.get(0).getBestUpwindAngle(), testDecodedCourseWinds.get(0).getBestUpwindAngle());
Assert.assertEquals(testCourseWinds.get(0).getBestDownwindAngle(), testDecodedCourseWinds.get(0).getBestDownwindAngle());
Assert.assertEquals(testCourseWinds.get(0).getFlags(), testDecodedCourseWinds.get(0).getFlags());
Assert.assertEquals(testCourseWinds.get(1).getID(), testDecodedCourseWinds.get(1).getID());
Assert.assertEquals(testCourseWinds.get(1).getTime(), testDecodedCourseWinds.get(1).getTime());
Assert.assertEquals(testCourseWinds.get(1).getRaceID(), testDecodedCourseWinds.get(1).getRaceID());
Assert.assertEquals(testCourseWinds.get(1).getWindDirection(), testDecodedCourseWinds.get(1).getWindDirection());
Assert.assertEquals(testCourseWinds.get(1).getWindSpeed(), testDecodedCourseWinds.get(1).getWindSpeed());
Assert.assertEquals(testCourseWinds.get(1).getBestUpwindAngle(), testDecodedCourseWinds.get(1).getBestUpwindAngle());
Assert.assertEquals(testCourseWinds.get(1).getBestDownwindAngle(), testDecodedCourseWinds.get(1).getBestDownwindAngle());
Assert.assertEquals(testCourseWinds.get(1).getFlags(), testDecodedCourseWinds.get(1).getFlags());
}
}
Loading…
Cancel
Save