Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package seng302.model;
- import org.junit.Assert;
- import org.junit.Test;
- import seng302.DataAccessObjects.PolarDataAccessObject;
- import java.io.IOException;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.List;
- public class RetrievingPolarFromPolarFileTest {
- Boat testBoat = new Boat("Test", "TEST", 1337);
- Race testRace = new Race();
- Polar testPolar;
- /**
- * Given a true wind speed, this method should return a polar with the closest true wind speed value.
- * The polar file had 7 polars, with true wind speeds ranging from 4 to 30. This method tests given
- * the true wind speed values of 1 to 30, in increments of 0.25, it will return the correct closest polar.
- */
- @Test
- public void closestPolarTest() throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
- testRace.setWindDirection(15);
- for(int i = 1; i < 31 * 4; i += 1){
- Double testTrueWindSpeed = i * 0.25;
- //Used to access a private method
- Method getClosestPolarMethod = Boat.class.getDeclaredMethod("getClosestPolar", Double.class);
- getClosestPolarMethod.setAccessible(true);
- testPolar = (Polar) getClosestPolarMethod.invoke(testBoat, testTrueWindSpeed);
- if(testTrueWindSpeed <= 6){
- Assert.assertEquals(testPolar.getTrueWindSpeed(), 4.0, 0.0);
- } else if (testTrueWindSpeed <= 10) {
- Assert.assertEquals(testPolar.getTrueWindSpeed(), 8.0, 0.0);
- } else if (testTrueWindSpeed <= 14){
- Assert.assertEquals(testPolar.getTrueWindSpeed(), 12.0, 0.0);
- } else if (testTrueWindSpeed <= 18){
- Assert.assertEquals(testPolar.getTrueWindSpeed(), 16.0, 0.0);
- } else if (testTrueWindSpeed <= 22.5){
- Assert.assertEquals(testPolar.getTrueWindSpeed(), 20.0, 0.0);
- } else if(testTrueWindSpeed <= 27.5){
- Assert.assertEquals(testPolar.getTrueWindSpeed(), 25.0, 0.0);
- } else {
- Assert.assertEquals(testPolar.getTrueWindSpeed(), 30.0, 0.0);
- }
- }
- }
- /**
- * One step in our algorithm which calculate the estimated boat speed from the acc polar file uses this method.
- * We are testing if the index returned is the correct one.
- * Our test cases attempts every wind angle from 0 to 180, which is the range of the given values in the polar file.
- * Several helper methods below are used to reduce code duplication.
- * @throws NoSuchMethodException
- * @throws InvocationTargetException
- * @throws IllegalAccessException
- * @throws IOException
- */
- @Test
- public void getClosestBoatSpeedIndexTest() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException {
- Boat testBoat = new Boat("Test", "TEST", 1337);
- Method getClosestBoatSpeedIndexMethod = Boat.class.getDeclaredMethod("getClosestBoatSpeedIndex", Polar.class, Double.class);
- getClosestBoatSpeedIndexMethod.setAccessible(true);
- PolarDataAccessObject polarParser = new AccPolarParser();
- List<Polar> allPolars = polarParser.readFile("/accPolarFile.txt");
- for (int i = 0; i < 181; i++) {
- for (Polar polar : allPolars) {
- Double trueWindAngle = (double) i * 1.0;
- int index = (int) getClosestBoatSpeedIndexMethod.invoke(testBoat, polar, trueWindAngle);
- if (trueWindAngle <= (0 + 30) / 2.0) {
- Assert.assertEquals(0, index);
- } else if (checkUpWindIndex1(trueWindAngle, polar.getTrueWindSpeed())){
- Assert.assertEquals(1, index);
- } else if (checkUpWindIndex2(trueWindAngle, polar.getTrueWindSpeed())){
- Assert.assertEquals(2, index);
- } else if (trueWindAngle <= (60 + 75) / 2.0) {
- Assert.assertEquals(3, index);
- } else if (trueWindAngle <= (75 + 90) / 2.0) {
- Assert.assertEquals(4, index);
- } else if (trueWindAngle <= (90 + 115) / 2.0) {
- Assert.assertEquals(5, index);
- } else if (trueWindAngle <= (115 + 145) / 2.0) {
- Assert.assertEquals(6, index);
- } else if (checkDownWindIndex7(trueWindAngle, polar.getTrueWindSpeed())) {
- Assert.assertEquals(7, index);
- } else if (checkDownWindIndex8(trueWindAngle, polar.getTrueWindSpeed())) {
- Assert.assertEquals(8, index);
- } else {
- Assert.assertEquals(9, index);
- }
- }
- }
- }
- /**
- * Differentiates between the trueWindSpeeds for any polar as this is the only
- * part of the polar file that has variation across polars.
- * @param trueWindAngle Double true wind angle
- * @param trueWindSpeed Double true wind speed
- * @return A boolean indicating if it needs to be tested
- */
- private boolean checkUpWindIndex1(Double trueWindAngle, Double trueWindSpeed){
- if(trueWindSpeed == 4){
- if (trueWindAngle <= (30 + 45) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 8 || trueWindSpeed == 12){
- if (trueWindAngle <= (30 + 43) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 16 || trueWindSpeed == 30){
- if (trueWindAngle <= (30 + 42) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 20){
- if (trueWindAngle <= (30 + 41) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 25){
- if (trueWindAngle <= (30 + 40) / 2.0) {
- return true;
- }
- }
- return false;
- }
- /**
- * Differentiates between the trueWindSpeeds for any polar as this is the only
- * part of the polar file that has variation across polars.
- * @param trueWindAngle Double true wind angle
- * @param trueWindSpeed Double true wind speed
- * @return A boolean indicating if it needs to be tested
- */
- private boolean checkUpWindIndex2(Double trueWindAngle, Double trueWindSpeed){
- if(trueWindSpeed == 4){
- if (trueWindAngle <= (45 + 60) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 8 || trueWindSpeed == 12){
- if (trueWindAngle <= (43 + 60) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 16 || trueWindSpeed == 30){
- if (trueWindAngle <= (42 + 60) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 20){
- if (trueWindAngle <= (41 + 60) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 25){
- if (trueWindAngle <= (40 + 60) / 2.0) {
- return true;
- }
- }
- return false;
- }
- /**
- * Differentiates between the trueWindSpeeds for any polar as this is the only
- * part of the polar file that has variation across polars.
- * @param trueWindAngle Double true wind angle
- * @param trueWindSpeed Double true wind speed
- * @return A boolean indicating if it needs to be tested
- */
- private boolean checkDownWindIndex7(Double trueWindAngle, Double trueWindSpeed){
- if(trueWindSpeed == 4){
- if (trueWindAngle <= (145 + 155) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 8 || trueWindSpeed == 12
- || trueWindSpeed == 16 || trueWindSpeed == 20){
- if (trueWindAngle <= (145 + 153) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 25){
- if (trueWindAngle <= (145 + 151) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 30){
- if (trueWindAngle <= (145 + 150) / 2.0) {
- return true;
- }
- }
- return false;
- }
- /**
- * Differentiates between the trueWindSpeeds for any polar as this is the only
- * part of the polar file that has variation across polars.
- * @param trueWindAngle Double true wind angle
- * @param trueWindSpeed Double true wind speed
- * @return A boolean indicating if it needs to be tested
- */
- private boolean checkDownWindIndex8(Double trueWindAngle, Double trueWindSpeed){
- if(trueWindSpeed == 4){
- if (trueWindAngle <= (155 + 175) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 8 || trueWindSpeed == 12
- || trueWindSpeed == 16 || trueWindSpeed == 20){
- if (trueWindAngle <= (153 + 175) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 25){
- if (trueWindAngle <= (151 + 175) / 2.0) {
- return true;
- }
- } else if(trueWindSpeed == 30){
- if (trueWindAngle <= (150 + 175) / 2.0) {
- return true;
- }
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment