Added Knots <-> MMperSec conversions to AC35UnitConverter.

Removed redundant/unused conversions.
Documented all of the conversions, and renamed them to pack/unpackX, to match the API spec.
Updated/added some tests in AC35UnitConverterTest.
RaceStatus now contains a Bearing instead of a packed int bearing.
RaceStatus now contains wind speed in knots, instead of MMperSec packed.
This means that only RaceStatus decoder/encoder need to care about the bits-over-wire packed values.
issue #35 #36
#story[1095]
This commit is contained in:
fjc40
2017-08-06 14:57:51 +12:00
parent a0f98eadaa
commit ff262a6227
9 changed files with 177 additions and 83 deletions
@@ -9,6 +9,7 @@ import network.Messages.Enums.RaceTypeEnum;
import network.Messages.RaceStatus;
import org.junit.Assert;
import org.junit.Test;
import shared.model.Bearing;
import java.util.ArrayList;
import java.util.Iterator;
@@ -67,8 +68,8 @@ public class RaceStatusDecoderTest {
int raceID = 585;
RaceStatusEnum raceStatus = RaceStatusEnum.STARTED;
long raceStartTime = time - (1000 * 31);
int windDirection = 2341;
int windSpeed = 10201;
Bearing windDirection = Bearing.fromDegrees(185.34);
double windSpeedKnots = 14.52;
RaceTypeEnum raceType = RaceTypeEnum.MATCH_RACE;
List<BoatStatus> boatStatuses = new ArrayList<>(2);
boatStatuses.add(boatStatus1);
@@ -81,7 +82,7 @@ public class RaceStatusDecoderTest {
raceStatus,
raceStartTime,
windDirection,
windSpeed,
windSpeedKnots,
raceType,
boatStatuses );
@@ -110,8 +111,8 @@ public class RaceStatusDecoderTest {
Assert.assertEquals(original.getRaceID(), decoded.getRaceID());
Assert.assertEquals(original.getRaceStatus(), decoded.getRaceStatus());
Assert.assertEquals(original.getExpectedStartTime(), decoded.getExpectedStartTime());
Assert.assertEquals(original.getWindDirection(), decoded.getWindDirection());
Assert.assertEquals(original.getWindSpeed(), decoded.getWindSpeed());
Assert.assertEquals(original.getWindDirection().degrees(), decoded.getWindDirection().degrees(), 0.01);
Assert.assertEquals(original.getWindSpeed(), decoded.getWindSpeed(), 0.01);
//Compare all BoatStatuses
Iterator<BoatStatus> originalIterator = original.getBoatStatuses().iterator();
@@ -2,42 +2,92 @@ package network.Utils;
import org.junit.Test;
import static network.Utils.AC35UnitConverter.convertGPS;
import static network.Utils.AC35UnitConverter.convertGPSToInt;
import static network.Utils.AC35UnitConverter.convertHeading;
import static network.Utils.AC35UnitConverter.convertTrueWindAngle;
import static network.Utils.AC35UnitConverter.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Created by fwy13 on 4/05/17.
* Tests the pack/unpack and conversion functions in {@link AC35UnitConverter}.
*/
public class AC35UnitConverterTest {
/**
* Tests if gps coordinates can be unpacked.
*/
@Test
public void testConvertGPS(){
assertTrue(convertGPS(0) == 0);
assertTrue(convertGPS(Integer.MAX_VALUE) == (double)Integer.MAX_VALUE * 180.0 / Math.pow(2, 31));
public void testUnpackGPS(){
assertTrue(unpackGPS(0) == 0);
assertTrue(unpackGPS(Integer.MAX_VALUE) == (double)Integer.MAX_VALUE * 180.0 / Math.pow(2, 31));
}
/**
* Tests if gps coodinates can be packed.
*/
@Test
public void testConvertGPSToInt(){
assertTrue(convertGPSToInt(0) == 0);
assertTrue(convertGPSToInt(180) == (int)2147483648.0);
public void testPackGPS(){
assertTrue(packGPS(0) == 0);
assertTrue(packGPS(180) == (int)2147483648.0);
}
/**
* Tests if headings/bearings can be unpacked.
*/
@Test
public void testConvertHeading(){
assertTrue(convertHeading(0) == 0);
assertTrue(convertHeading(65536) == 360.0);
public void testUnpackHeading(){
assertTrue(unpackHeading(0) == 0);
assertTrue(unpackHeading(65536) == 360.0);
}
/**
* Tests if headings/bearings can be packed.
*/
@Test
public void testPackHeading(){
assertTrue(packHeading(0) == 0);
assertTrue(packHeading(360) == 65536);
}
/**
* Tests if true wind angles (azimuths) can be unpacked.
*/
@Test
public void testConvertTrueWindAngle(){
assertTrue(convertTrueWindAngle(0) == 0);
assertTrue(convertTrueWindAngle(32768) == 180.0);
public void testUnpackTrueWindAngle(){
assertEquals(unpackTrueWindAngle((short)0), 0, 0.001);
assertEquals(unpackTrueWindAngle((short)32767), 180.0, 0.01);
}
/**
* Tests if true wind angles (azimuths) can be packed.
*/
@Test
public void testPackTrueWindAngle(){
assertTrue(packTrueWindAngle(0) == (short)0);
assertTrue(packTrueWindAngle(180.0) == (short)32768);
}
/**
* Tests if millimeters per second can be unpacked to knots.
*/
@Test
public void testUnpackMMperSecToKnots(){
assertEquals(unpackMMperSecToKnots(0), 0d, 0.001);
assertEquals(unpackMMperSecToKnots(7331), 14.25, 0.01);
}
/**
* Tests if knots can be packed into millimeters per second.
*/
@Test
public void testPackKnotsToMMperSec(){
assertEquals(packKnotsToMMperSec(0), 0, 1);
assertEquals(packKnotsToMMperSec(7.44), 3828, 1);
}
}