Wallace543

Untitled

Jul 25th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.26 KB | None | 0 0
  1. package seng302.model;
  2.  
  3. import org.junit.Assert;
  4. import org.junit.Test;
  5. import seng302.DataAccessObjects.PolarDataAccessObject;
  6. import java.io.IOException;
  7. import java.lang.reflect.InvocationTargetException;
  8. import java.lang.reflect.Method;
  9. import java.util.List;
  10.  
  11.  
  12. public class RetrievingPolarFromPolarFileTest {
  13. Boat testBoat = new Boat("Test", "TEST", 1337);
  14. Race testRace = new Race();
  15. Polar testPolar;
  16.  
  17. /**
  18. * Given a true wind speed, this method should return a polar with the closest true wind speed value.
  19. * The polar file had 7 polars, with true wind speeds ranging from 4 to 30. This method tests given
  20. * the true wind speed values of 1 to 30, in increments of 0.25, it will return the correct closest polar.
  21. */
  22. @Test
  23. public void closestPolarTest() throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
  24. testRace.setWindDirection(15);
  25.  
  26. for(int i = 1; i < 31 * 4; i += 1){
  27. Double testTrueWindSpeed = i * 0.25;
  28.  
  29. //Used to access a private method
  30. Method getClosestPolarMethod = Boat.class.getDeclaredMethod("getClosestPolar", Double.class);
  31. getClosestPolarMethod.setAccessible(true);
  32. testPolar = (Polar) getClosestPolarMethod.invoke(testBoat, testTrueWindSpeed);
  33.  
  34. if(testTrueWindSpeed <= 6){
  35. Assert.assertEquals(testPolar.getTrueWindSpeed(), 4.0, 0.0);
  36. } else if (testTrueWindSpeed <= 10) {
  37. Assert.assertEquals(testPolar.getTrueWindSpeed(), 8.0, 0.0);
  38. } else if (testTrueWindSpeed <= 14){
  39. Assert.assertEquals(testPolar.getTrueWindSpeed(), 12.0, 0.0);
  40. } else if (testTrueWindSpeed <= 18){
  41. Assert.assertEquals(testPolar.getTrueWindSpeed(), 16.0, 0.0);
  42. } else if (testTrueWindSpeed <= 22.5){
  43. Assert.assertEquals(testPolar.getTrueWindSpeed(), 20.0, 0.0);
  44. } else if(testTrueWindSpeed <= 27.5){
  45. Assert.assertEquals(testPolar.getTrueWindSpeed(), 25.0, 0.0);
  46. } else {
  47. Assert.assertEquals(testPolar.getTrueWindSpeed(), 30.0, 0.0);
  48. }
  49. }
  50. }
  51.  
  52. /**
  53. * One step in our algorithm which calculate the estimated boat speed from the acc polar file uses this method.
  54. * We are testing if the index returned is the correct one.
  55. * Our test cases attempts every wind angle from 0 to 180, which is the range of the given values in the polar file.
  56. * Several helper methods below are used to reduce code duplication.
  57. * @throws NoSuchMethodException
  58. * @throws InvocationTargetException
  59. * @throws IllegalAccessException
  60. * @throws IOException
  61. */
  62. @Test
  63. public void getClosestBoatSpeedIndexTest() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException {
  64. Boat testBoat = new Boat("Test", "TEST", 1337);
  65. Method getClosestBoatSpeedIndexMethod = Boat.class.getDeclaredMethod("getClosestBoatSpeedIndex", Polar.class, Double.class);
  66. getClosestBoatSpeedIndexMethod.setAccessible(true);
  67.  
  68. PolarDataAccessObject polarParser = new AccPolarParser();
  69. List<Polar> allPolars = polarParser.readFile("/accPolarFile.txt");
  70.  
  71. for (int i = 0; i < 181; i++) {
  72. for (Polar polar : allPolars) {
  73. Double trueWindAngle = (double) i * 1.0;
  74. int index = (int) getClosestBoatSpeedIndexMethod.invoke(testBoat, polar, trueWindAngle);
  75.  
  76. if (trueWindAngle <= (0 + 30) / 2.0) {
  77. Assert.assertEquals(0, index);
  78. } else if (checkUpWindIndex1(trueWindAngle, polar.getTrueWindSpeed())){
  79. Assert.assertEquals(1, index);
  80. } else if (checkUpWindIndex2(trueWindAngle, polar.getTrueWindSpeed())){
  81. Assert.assertEquals(2, index);
  82. } else if (trueWindAngle <= (60 + 75) / 2.0) {
  83. Assert.assertEquals(3, index);
  84. } else if (trueWindAngle <= (75 + 90) / 2.0) {
  85. Assert.assertEquals(4, index);
  86. } else if (trueWindAngle <= (90 + 115) / 2.0) {
  87. Assert.assertEquals(5, index);
  88. } else if (trueWindAngle <= (115 + 145) / 2.0) {
  89. Assert.assertEquals(6, index);
  90. } else if (checkDownWindIndex7(trueWindAngle, polar.getTrueWindSpeed())) {
  91. Assert.assertEquals(7, index);
  92. } else if (checkDownWindIndex8(trueWindAngle, polar.getTrueWindSpeed())) {
  93. Assert.assertEquals(8, index);
  94. } else {
  95. Assert.assertEquals(9, index);
  96. }
  97. }
  98. }
  99. }
  100.  
  101. /**
  102. * Differentiates between the trueWindSpeeds for any polar as this is the only
  103. * part of the polar file that has variation across polars.
  104. * @param trueWindAngle Double true wind angle
  105. * @param trueWindSpeed Double true wind speed
  106. * @return A boolean indicating if it needs to be tested
  107. */
  108. private boolean checkUpWindIndex1(Double trueWindAngle, Double trueWindSpeed){
  109. if(trueWindSpeed == 4){
  110. if (trueWindAngle <= (30 + 45) / 2.0) {
  111. return true;
  112. }
  113. } else if(trueWindSpeed == 8 || trueWindSpeed == 12){
  114. if (trueWindAngle <= (30 + 43) / 2.0) {
  115. return true;
  116. }
  117. } else if(trueWindSpeed == 16 || trueWindSpeed == 30){
  118. if (trueWindAngle <= (30 + 42) / 2.0) {
  119. return true;
  120. }
  121. } else if(trueWindSpeed == 20){
  122. if (trueWindAngle <= (30 + 41) / 2.0) {
  123. return true;
  124. }
  125. } else if(trueWindSpeed == 25){
  126. if (trueWindAngle <= (30 + 40) / 2.0) {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. /**
  133. * Differentiates between the trueWindSpeeds for any polar as this is the only
  134. * part of the polar file that has variation across polars.
  135. * @param trueWindAngle Double true wind angle
  136. * @param trueWindSpeed Double true wind speed
  137. * @return A boolean indicating if it needs to be tested
  138. */
  139. private boolean checkUpWindIndex2(Double trueWindAngle, Double trueWindSpeed){
  140. if(trueWindSpeed == 4){
  141. if (trueWindAngle <= (45 + 60) / 2.0) {
  142. return true;
  143. }
  144. } else if(trueWindSpeed == 8 || trueWindSpeed == 12){
  145. if (trueWindAngle <= (43 + 60) / 2.0) {
  146. return true;
  147. }
  148. } else if(trueWindSpeed == 16 || trueWindSpeed == 30){
  149. if (trueWindAngle <= (42 + 60) / 2.0) {
  150. return true;
  151. }
  152. } else if(trueWindSpeed == 20){
  153. if (trueWindAngle <= (41 + 60) / 2.0) {
  154. return true;
  155. }
  156. } else if(trueWindSpeed == 25){
  157. if (trueWindAngle <= (40 + 60) / 2.0) {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163.  
  164. /**
  165. * Differentiates between the trueWindSpeeds for any polar as this is the only
  166. * part of the polar file that has variation across polars.
  167. * @param trueWindAngle Double true wind angle
  168. * @param trueWindSpeed Double true wind speed
  169. * @return A boolean indicating if it needs to be tested
  170. */
  171. private boolean checkDownWindIndex7(Double trueWindAngle, Double trueWindSpeed){
  172. if(trueWindSpeed == 4){
  173. if (trueWindAngle <= (145 + 155) / 2.0) {
  174. return true;
  175. }
  176. } else if(trueWindSpeed == 8 || trueWindSpeed == 12
  177. || trueWindSpeed == 16 || trueWindSpeed == 20){
  178. if (trueWindAngle <= (145 + 153) / 2.0) {
  179. return true;
  180. }
  181. } else if(trueWindSpeed == 25){
  182. if (trueWindAngle <= (145 + 151) / 2.0) {
  183. return true;
  184. }
  185. } else if(trueWindSpeed == 30){
  186. if (trueWindAngle <= (145 + 150) / 2.0) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192.  
  193. /**
  194. * Differentiates between the trueWindSpeeds for any polar as this is the only
  195. * part of the polar file that has variation across polars.
  196. * @param trueWindAngle Double true wind angle
  197. * @param trueWindSpeed Double true wind speed
  198. * @return A boolean indicating if it needs to be tested
  199. */
  200. private boolean checkDownWindIndex8(Double trueWindAngle, Double trueWindSpeed){
  201. if(trueWindSpeed == 4){
  202. if (trueWindAngle <= (155 + 175) / 2.0) {
  203. return true;
  204. }
  205. } else if(trueWindSpeed == 8 || trueWindSpeed == 12
  206. || trueWindSpeed == 16 || trueWindSpeed == 20){
  207. if (trueWindAngle <= (153 + 175) / 2.0) {
  208. return true;
  209. }
  210. } else if(trueWindSpeed == 25){
  211. if (trueWindAngle <= (151 + 175) / 2.0) {
  212. return true;
  213. }
  214. } else if(trueWindSpeed == 30){
  215. if (trueWindAngle <= (150 + 175) / 2.0) {
  216. return true;
  217. }
  218. }
  219. return false;
  220. }
  221. }
Add Comment
Please, Sign In to add comment