Guest User

Untitled

a guest
Feb 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. package MyGeometryLib;
  2.  
  3. import org.junit.jupiter.api.Test;
  4.  
  5. import java.util.Random;
  6.  
  7. import static org.junit.jupiter.api.Assertions.*;
  8.  
  9. class Vector2DTest {
  10. private final Random random = new Random();
  11. private final double DELTA = 1e-15;
  12.  
  13. @Test
  14. void dimension() {
  15. Vector vector = new Vector2D(random.nextDouble(), random.nextDouble());
  16. assertEquals(3, vector.dimension(), DELTA);
  17. }
  18.  
  19. @Test
  20. void getComponent() {
  21. for (int i = 0; i < 100; i++) {
  22. double expectedX = random.nextDouble();
  23. double expectedY = random.nextDouble();
  24. Vector vector = new Vector2D(expectedX, expectedY);
  25.  
  26. double[] expectedCoord = new double[] {expectedX, expectedY};
  27. double[] actualCoord = new double[] {vector.getComponent(0), vector.getComponent(1)};
  28.  
  29. assertArrayEquals(expectedCoord, actualCoord, DELTA);
  30. }
  31. }
  32.  
  33. @Test
  34. void scalar() {
  35. for (int i = 0; i < 100; i++) {
  36. double vectorX = random.nextDouble();
  37. double vectorY = random.nextDouble();
  38.  
  39. double anVectorX = random.nextDouble();
  40. double anVectorY = random.nextDouble();
  41.  
  42. Vector2D vector = new Vector2D(vectorX, vectorY);
  43. Vector2D anotherVector = new Vector2D(anVectorX, anVectorY);
  44. double expectedScalar = vectorX*anVectorX + vectorY*anVectorY;
  45.  
  46. assertEquals(expectedScalar, vector.scalar(anotherVector), DELTA);
  47. }
  48. }
  49.  
  50. @Test
  51. void len() {
  52. for (int i = 0; i < 100; i++) {
  53. double x = random.nextDouble();
  54. double y = random.nextDouble();
  55.  
  56. Vector vector = new Vector2D(x, y);
  57. double expectedLength = Math.sqrt(x*x+y*y);
  58.  
  59. assertEquals(expectedLength, vector.len(), DELTA);
  60. }
  61.  
  62. assertEquals(0, new Vector2D(0 , 0).len(), DELTA);
  63. }
  64.  
  65. @Test
  66. void multiply() {
  67. double factor = random.nextDouble();
  68. double x = random.nextDouble();
  69. double y = random.nextDouble();
  70.  
  71. Vector vector = new Vector2D(x, y);
  72. Vector vectorMul = new Vector2D(factor*x, factor*y);
  73.  
  74. assertTrue(vector.multiply(factor).equals(vectorMul));
  75. }
  76.  
  77. @Test
  78. void add() {
  79. for (int i = 0; i < 100; i++) {
  80. double vectorX = random.nextDouble();
  81. double vectorY = random.nextDouble();
  82.  
  83. double anVectorX = random.nextDouble();
  84. double anVectorY = random.nextDouble();
  85.  
  86. Vector vector = new Vector2D(vectorX, vectorY);
  87. Vector anotherVector = new Vector2D(anVectorX, anVectorY);
  88. Vector vectorAdd = new Vector2D(vectorX+anVectorX, vectorY+anVectorY);
  89.  
  90. assertTrue(vector.add(anotherVector).equals(vectorAdd));
  91. }
  92. }
  93.  
  94. @Test
  95. void sub() {
  96. for (int i = 0; i < 100; i++) {
  97. double vectorX = random.nextDouble();
  98. double vectorY = random.nextDouble();
  99.  
  100. double anVectorX = random.nextDouble();
  101. double anVectorY = random.nextDouble();
  102.  
  103. Vector vector = new Vector2D(vectorX, vectorY);
  104. Vector anotherVector = new Vector2D(anVectorX, anVectorY);
  105. Vector vectorSub = new Vector2D(vectorX-anVectorX, vectorY-anVectorY);
  106.  
  107. assertTrue(vector.sub(anotherVector).equals(vectorSub));
  108. }
  109. }
  110. }
Add Comment
Please, Sign In to add comment