PR1_Pflicht_3/VectorTest.java

120 lines
4.1 KiB
Java

// Dieser Test ist eine ausführliche Testung der Vektorklasse
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.*;
public class VectorTest {
// Testteil für die überprüfung der Scalarenmultiplikation
@Test
void testScalarMultiplicationVariants() {
int[] vector1 = { 1, -2, 3 };
Vector.scalarMultiplication(vector1, 2);
assertArrayEquals(new int[] { 2, -4, 6 }, vector1);
int[] vector2 = { 7, 8, 15 };
Vector.scalarMultiplication(vector2, -1);
assertArrayEquals(new int[] { -7, -8, -15 }, vector2);
int[] vector3 = { 0, 0, 0 };
Vector.scalarMultiplication(vector3, 5);
assertArrayEquals(new int[] { 0, 0, 0 }, vector3);
int[] vector4 = {};
Vector.scalarMultiplication(vector4, 2);
assertArrayEquals(new int[] {}, vector4);
int[] vector5 = null;
Vector.scalarMultiplication(vector5, 5);
assertArrayEquals(null, vector5);
}
// Test für den Kreuzprodukt in 2D
@Test
void testVectorProduct2DVariants() {
int ergebnis1 = -2;
System.out.println("Erwartet: " + ergebnis1);
Vector.vectorProduct(new int[] { 3, 4 }, new int[] { 5, 6 });
int ergebnis2 = -1;
System.out.println("Erwartet: " + ergebnis2);
Vector.vectorProduct(new int[] { 0, 1 }, new int[] { 1, 0 });
}
// Test für den Kreuzprodukt in 3D
@Test
void testVectorProduct3DVariants() {
int[] ergebnis1 = { -3, 6, -3 };
System.out.println("Erwartet: [" + ergebnis1[0] + ", " + ergebnis1[1] + ", " + ergebnis1[2] + "]");
Vector.vectorProduct(new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 });
int[] ergebnis2 = { -1, 0, 0 };
System.out.println("Erwartet: [" + ergebnis2[0] + ", " + ergebnis2[1] + ", " + ergebnis2[2] + "]");
Vector.vectorProduct(new int[] { 0, 0, 1 }, new int[] { 0, 1, 0 });
}
// Test für den Kreuzprodukt bei unterschiedlichen Dimensionen
@Test
void testVectorProductInvalidDimensions() {
System.out.println("Erwartet: illegale Dimension");
Vector.vectorProduct(new int[] { 1, 0 }, new int[] { 0, 1, 0 });
}
// Test für den Vektorbetrag
@Test
void testVectorLengthVariants() {
assertEquals(5.0, Vector.vectorLength(new int[] { 3, 4 }), 0.001);
assertEquals(3.0, Vector.vectorLength(new int[] { 1, 2, 2 }), 0.001);
assertEquals(0.0, Vector.vectorLength(new int[] { 0, 0, 0 }), 0.001);
}
// Test für die scalare Matrixmultiplikation
@Test
void testMatrixMultiplicationVariants() {
int[][] matrix = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
int[][] expected = {
{ 2, 4, 6 },
{ 8, 10, 12 },
{ 14, 16, 18 }
};
assertArrayEquals(expected, Vector.matrixMultiplication(matrix, 2));
int[][] zeroMatrix = {
{ 1, -2, -6 },
{ -3, 4, 5 },
{ -70, 12, -8 }
};
int[][] expectedZero = {
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 }
};
assertArrayEquals(expectedZero, Vector.matrixMultiplication(zeroMatrix, 0));
}
// Test von fehlerhafte Matrixen für die scalare Matrixmultiplikation
@Test
void testMatrixMultiplicationInvalidCases() {
int[][] non3DSquare = {
{ 1, 2, 3 },
{ 4, 5, 6 }
};
assertNull(Vector.matrixMultiplication(non3DSquare, 3));
int[][] non3DSquare1 = {
{ 1, 2, 3 },
{ 3, 4, 5 },
{ 6, 7 }
};
assertNull(Vector.matrixMultiplication(non3DSquare1, 3));
int[][] non3DSquare2 = {
{ 1, 1, 1 },
{ 2, 2, 2 },
{ 3, 3, 3 },
{ 4, 4, 4 }
};
assertNull(Vector.matrixMultiplication(non3DSquare2, 3));
int[][] empty = new int[0][0];
assertNull(Vector.matrixMultiplication(empty, 2));
}
}