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 Vector2DTest {
- private final Random random = new Random();
- private final double DELTA = 1e-15;
- @Test
- void dimension() {
- Vector vector = new Vector2D(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();
- Vector vector = new Vector2D(expectedX, expectedY);
- double[] expectedCoord = new double[] {expectedX, expectedY};
- double[] actualCoord = new double[] {vector.getComponent(0), vector.getComponent(1)};
- assertArrayEquals(expectedCoord, actualCoord, DELTA);
- }
- }
- @Test
- void scalar() {
- for (int i = 0; i < 100; i++) {
- double vectorX = random.nextDouble();
- double vectorY = random.nextDouble();
- double anVectorX = random.nextDouble();
- double anVectorY = random.nextDouble();
- Vector2D vector = new Vector2D(vectorX, vectorY);
- Vector2D anotherVector = new Vector2D(anVectorX, anVectorY);
- double expectedScalar = vectorX*anVectorX + vectorY*anVectorY;
- assertEquals(expectedScalar, vector.scalar(anotherVector), DELTA);
- }
- }
- @Test
- void len() {
- for (int i = 0; i < 100; i++) {
- double x = random.nextDouble();
- double y = random.nextDouble();
- Vector vector = new Vector2D(x, y);
- double expectedLength = Math.sqrt(x*x+y*y);
- assertEquals(expectedLength, vector.len(), DELTA);
- }
- assertEquals(0, new Vector2D(0 , 0).len(), DELTA);
- }
- @Test
- void multiply() {
- double factor = random.nextDouble();
- double x = random.nextDouble();
- double y = random.nextDouble();
- Vector vector = new Vector2D(x, y);
- Vector vectorMul = new Vector2D(factor*x, factor*y);
- 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 anVectorX = random.nextDouble();
- double anVectorY = random.nextDouble();
- Vector vector = new Vector2D(vectorX, vectorY);
- Vector anotherVector = new Vector2D(anVectorX, anVectorY);
- Vector vectorAdd = new Vector2D(vectorX+anVectorX, vectorY+anVectorY);
- 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 anVectorX = random.nextDouble();
- double anVectorY = random.nextDouble();
- Vector vector = new Vector2D(vectorX, vectorY);
- Vector anotherVector = new Vector2D(anVectorX, anVectorY);
- Vector vectorSub = new Vector2D(vectorX-anVectorX, vectorY-anVectorY);
- assertTrue(vector.sub(anotherVector).equals(vectorSub));
- }
- }
- }
Add Comment
Please, Sign In to add comment