From f5000eea111eebb0c152faa86a1b6e486960d41c Mon Sep 17 00:00:00 2001 From: hba56 Date: Sun, 23 Apr 2017 13:29:46 +1200 Subject: [PATCH] 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] --- .../MessageDecoders/CourseWindDecoder.java | 31 +++++++--- .../Networking/RaceVisionByteEncoder.java | 11 ++-- .../CourseWindDecoderTest.java | 60 +++++++++++++++++++ 3 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 src/test/java/seng302/Networking/MessageDecoders/CourseWindDecoderTest.java diff --git a/src/main/java/seng302/Networking/MessageDecoders/CourseWindDecoder.java b/src/main/java/seng302/Networking/MessageDecoders/CourseWindDecoder.java index 12163aa8..dad8523f 100644 --- a/src/main/java/seng302/Networking/MessageDecoders/CourseWindDecoder.java +++ b/src/main/java/seng302/Networking/MessageDecoders/CourseWindDecoder.java @@ -14,7 +14,7 @@ public class CourseWindDecoder { byte messageVersionNumber; byte byteWindID; byte loopCount; - ArrayList loopMessages; + ArrayList 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 getLoopMessages() { + return loopMessages; + } } diff --git a/src/main/java/seng302/Networking/RaceVisionByteEncoder.java b/src/main/java/seng302/Networking/RaceVisionByteEncoder.java index d75111e4..aec05dbe 100644 --- a/src/main/java/seng302/Networking/RaceVisionByteEncoder.java +++ b/src/main/java/seng302/Networking/RaceVisionByteEncoder.java @@ -231,14 +231,13 @@ public class RaceVisionByteEncoder { return result.array(); } - public byte[] courseWind(char windID, CourseWind[] courseWinds){ + public byte[] courseWind(byte windID, ArrayList 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)); diff --git a/src/test/java/seng302/Networking/MessageDecoders/CourseWindDecoderTest.java b/src/test/java/seng302/Networking/MessageDecoders/CourseWindDecoderTest.java new file mode 100644 index 00000000..e6556144 --- /dev/null +++ b/src/test/java/seng302/Networking/MessageDecoders/CourseWindDecoderTest.java @@ -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 testCourseWinds = new ArrayList(); + testCourseWinds.add(testCourseWind1); + testCourseWinds.add(testCourseWind2); + + + RaceVisionByteEncoder raceVisionByteEncoder = new RaceVisionByteEncoder(); + + byte[] testEncodedCourseWind = raceVisionByteEncoder.courseWind((byte) 1, testCourseWinds); + + CourseWindDecoder testDecoder = new CourseWindDecoder(testEncodedCourseWind); + + ArrayList 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()); + + } +}