Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MyGeometryLib;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- import static org.junit.jupiter.api.Assertions.*;
- class Vector3DTest {
- private final Random random = new Random();
- private final double DELTA = 1e-15;
- @Test
- void dimension() {
- Vector vector = new Vector3D(random.nextDouble(), random.nextDouble(), random.nextDouble());
- assertEquals(3, vector.dimension(), DELTA);
- }
- @Test
- void getComponent() {
- for (int i = 0; i < 100; i++) {
- double expectedX = random.nextDouble();
- double expectedY = random.nextDouble();
- double expectedZ = random.nextDouble();
- Vector vector = new Vector3D(expectedX, expectedY, expectedZ);
- double[] expectedCoord = new double[] {expectedX, expectedY, expectedZ};
- double[] actualCoord = new double[] {vector.getComponent(0), vector.getComponent(1), vector.getComponent(2)};
- assertArrayEquals(expectedCoord, actualCoord, DELTA);
- }
- }
- @Test
- void scalar() {
- for (int i = 0; i < 100; i++) {
- double vectorX = random.nextDouble();
- double vectorY = random.nextDouble();
- double vectorZ = random.nextDouble();
- double anVectorX = random.nextDouble();
- double anVectorY = random.nextDouble();
- double anVectorZ = random.nextDouble();
- Vector vector = new Vector3D(vectorX, vectorY, vectorZ);
- Vector anotherVector = new Vector3D(anVectorX, anVectorY, anVectorZ);
- double expectedScalar = vectorX*anVectorX + vectorY*anVectorY + vectorZ*anVectorZ;
- assertEquals(expectedScalar, vector.scalar(anotherVector), DELTA);
- }
- }
- @Test
- void len() {
- for (int i = 0; i < 100; i++) {
- double x = random.nextDouble();
- double y = random.nextDouble();
- double z = random.nextDouble();
- Vector vector = new Vector3D(x, y ,z);
- double expectedLength = Math.sqrt(x*x+y*y+z*z);
- assertEquals(expectedLength, vector.len(), DELTA);
- }
- assertEquals(0, new Vector3D(0, 0, 0).len(), DELTA);
- }
- @Test
- void multiply() {
- for (int i = 0; i < 100; i++) {
- double factor = random.nextDouble();
- double x = random.nextDouble();
- double y = random.nextDouble();
- double z = random.nextDouble();
- Vector vector = new Vector3D(x, y, z);
- Vector vectorMul = new Vector3D(factor*x, factor*y, factor*z);
- assertTrue(vector.multiply(factor).equals(vectorMul));
- }
- }
- @Test
- void add() {
- for (int i = 0; i < 100; i++) {
- double vectorX = random.nextDouble();
- double vectorY = random.nextDouble();
- double vectorZ = random.nextDouble();
- double anVectorX = random.nextDouble();
- double anVectorY = random.nextDouble();
- double anVectorZ = random.nextDouble();
- Vector vector = new Vector3D(vectorX, vectorY, vectorZ);
- Vector anotherVector = new Vector3D(anVectorX, anVectorY, anVectorZ);
- Vector vectorAdd = new Vector3D(vectorX+anVectorX, vectorY+anVectorY, vectorZ+anVectorZ);
- assertTrue(vector.add(anotherVector).equals(vectorAdd));
- }
- }
- @Test
- void sub() {
- for (int i = 0; i < 100; i++) {
- double vectorX = random.nextDouble();
- double vectorY = random.nextDouble();
- double vectorZ = random.nextDouble();
- double anVectorX = random.nextDouble();
- double anVectorY = random.nextDouble();
- double anVectorZ = random.nextDouble();
- Vector vector = new Vector3D(vectorX, vectorY, vectorZ);
- Vector anotherVector = new Vector3D(anVectorX, anVectorY, anVectorZ);
- Vector vectorSub = new Vector3D(vectorX-anVectorX, vectorY-anVectorY, vectorZ-anVectorZ);
- assertTrue(vector.sub(anotherVector).equals(vectorSub));
- }
- }
- }
Add Comment
Please, Sign In to add comment