Guest User

Untitled

a guest
Feb 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 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 Vector3DTest {
  10. private final Random random = new Random();
  11. private final double DELTA = 1e-15;
  12.  
  13. @Test
  14. void dimension() {
  15. Vector vector = new Vector3D(random.nextDouble(), 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. double expectedZ = random.nextDouble();
  25. Vector vector = new Vector3D(expectedX, expectedY, expectedZ);
  26.  
  27. double[] expectedCoord = new double[] {expectedX, expectedY, expectedZ};
  28. double[] actualCoord = new double[] {vector.getComponent(0), vector.getComponent(1), vector.getComponent(2)};
  29.  
  30. assertArrayEquals(expectedCoord, actualCoord, DELTA);
  31. }
  32. }
  33.  
  34. @Test
  35. void scalar() {
  36. for (int i = 0; i < 100; i++) {
  37. double vectorX = random.nextDouble();
  38. double vectorY = random.nextDouble();
  39. double vectorZ = random.nextDouble();
  40.  
  41. double anVectorX = random.nextDouble();
  42. double anVectorY = random.nextDouble();
  43. double anVectorZ = random.nextDouble();
  44.  
  45. Vector vector = new Vector3D(vectorX, vectorY, vectorZ);
  46. Vector anotherVector = new Vector3D(anVectorX, anVectorY, anVectorZ);
  47. double expectedScalar = vectorX*anVectorX + vectorY*anVectorY + vectorZ*anVectorZ;
  48.  
  49. assertEquals(expectedScalar, vector.scalar(anotherVector), DELTA);
  50. }
  51. }
  52.  
  53. @Test
  54. void len() {
  55. for (int i = 0; i < 100; i++) {
  56. double x = random.nextDouble();
  57. double y = random.nextDouble();
  58. double z = random.nextDouble();
  59.  
  60. Vector vector = new Vector3D(x, y ,z);
  61. double expectedLength = Math.sqrt(x*x+y*y+z*z);
  62.  
  63. assertEquals(expectedLength, vector.len(), DELTA);
  64. }
  65.  
  66. assertEquals(0, new Vector3D(0, 0, 0).len(), DELTA);
  67. }
  68.  
  69. @Test
  70. void multiply() {
  71. for (int i = 0; i < 100; i++) {
  72. double factor = random.nextDouble();
  73. double x = random.nextDouble();
  74. double y = random.nextDouble();
  75. double z = random.nextDouble();
  76.  
  77. Vector vector = new Vector3D(x, y, z);
  78. Vector vectorMul = new Vector3D(factor*x, factor*y, factor*z);
  79.  
  80. assertTrue(vector.multiply(factor).equals(vectorMul));
  81. }
  82. }
  83.  
  84. @Test
  85. void add() {
  86. for (int i = 0; i < 100; i++) {
  87. double vectorX = random.nextDouble();
  88. double vectorY = random.nextDouble();
  89. double vectorZ = random.nextDouble();
  90.  
  91. double anVectorX = random.nextDouble();
  92. double anVectorY = random.nextDouble();
  93. double anVectorZ = random.nextDouble();
  94.  
  95. Vector vector = new Vector3D(vectorX, vectorY, vectorZ);
  96. Vector anotherVector = new Vector3D(anVectorX, anVectorY, anVectorZ);
  97. Vector vectorAdd = new Vector3D(vectorX+anVectorX, vectorY+anVectorY, vectorZ+anVectorZ);
  98.  
  99. assertTrue(vector.add(anotherVector).equals(vectorAdd));
  100. }
  101. }
  102.  
  103. @Test
  104. void sub() {
  105. for (int i = 0; i < 100; i++) {
  106. double vectorX = random.nextDouble();
  107. double vectorY = random.nextDouble();
  108. double vectorZ = random.nextDouble();
  109.  
  110. double anVectorX = random.nextDouble();
  111. double anVectorY = random.nextDouble();
  112. double anVectorZ = random.nextDouble();
  113.  
  114. Vector vector = new Vector3D(vectorX, vectorY, vectorZ);
  115. Vector anotherVector = new Vector3D(anVectorX, anVectorY, anVectorZ);
  116. Vector vectorSub = new Vector3D(vectorX-anVectorX, vectorY-anVectorY, vectorZ-anVectorZ);
  117.  
  118. assertTrue(vector.sub(anotherVector).equals(vectorSub));
  119. }
  120. }
  121. }
Add Comment
Please, Sign In to add comment