mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
Create package prime, matrix and games (#6139)
This commit is contained in:
45
src/test/java/com/thealgorithms/matrix/MatrixRankTest.java
Normal file
45
src/test/java/com/thealgorithms/matrix/MatrixRankTest.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package com.thealgorithms.matrix;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
class MatrixRankTest {
|
||||
|
||||
private static Stream<Arguments> validInputStream() {
|
||||
return Stream.of(Arguments.of(3, new double[][] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}), Arguments.of(0, new double[][] {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}), Arguments.of(1, new double[][] {{1}}), Arguments.of(2, new double[][] {{1, 2}, {3, 4}}),
|
||||
Arguments.of(2, new double[][] {{3, -1, 2}, {-3, 1, 2}, {-6, 2, 4}}), Arguments.of(3, new double[][] {{2, 3, 0, 1}, {1, 0, 1, 2}, {-1, 1, 1, -2}, {1, 5, 3, -1}}), Arguments.of(1, new double[][] {{1, 2, 3}, {3, 6, 9}}),
|
||||
Arguments.of(2, new double[][] {{0.25, 0.5, 0.75, 2}, {1.5, 3, 4.5, 6}, {1, 2, 3, 4}}));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> invalidInputStream() {
|
||||
return Stream.of(Arguments.of((Object) new double[][] {{1, 2}, {10}, {100, 200, 300}}), // jagged array
|
||||
Arguments.of((Object) new double[][] {}), // empty matrix
|
||||
Arguments.of((Object) new double[][] {{}, {}}), // empty row
|
||||
Arguments.of((Object) null), // null matrix
|
||||
Arguments.of((Object) new double[][] {{1, 2}, null}) // null row
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("validInputStream")
|
||||
void computeRankTests(int expectedRank, double[][] matrix) {
|
||||
int originalHashCode = Arrays.deepHashCode(matrix);
|
||||
int rank = MatrixRank.computeRank(matrix);
|
||||
int newHashCode = Arrays.deepHashCode(matrix);
|
||||
|
||||
assertEquals(expectedRank, rank);
|
||||
assertEquals(originalHashCode, newHashCode);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("invalidInputStream")
|
||||
void computeRankWithInvalidMatrix(double[][] matrix) {
|
||||
assertThrows(IllegalArgumentException.class, () -> MatrixRank.computeRank(matrix));
|
||||
}
|
||||
}
|
||||
80
src/test/java/com/thealgorithms/matrix/MatrixUtilTest.java
Normal file
80
src/test/java/com/thealgorithms/matrix/MatrixUtilTest.java
Normal file
@@ -0,0 +1,80 @@
|
||||
package com.thealgorithms.matrix;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.thealgorithms.matrix.utils.MatrixUtil;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Objects;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MatrixUtilTest {
|
||||
|
||||
@Test
|
||||
void add() {
|
||||
final BigDecimal[][] matrix1 = {
|
||||
{new BigDecimal(3), BigDecimal.TWO},
|
||||
{BigDecimal.ZERO, BigDecimal.ONE},
|
||||
};
|
||||
|
||||
final BigDecimal[][] matrix2 = {
|
||||
{BigDecimal.ONE, new BigDecimal(3)},
|
||||
{BigDecimal.TWO, BigDecimal.ZERO},
|
||||
};
|
||||
|
||||
final BigDecimal[][] actual = MatrixUtil.add(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!"));
|
||||
|
||||
final BigDecimal[][] expected = {
|
||||
{new BigDecimal(4), new BigDecimal(5)},
|
||||
{BigDecimal.TWO, BigDecimal.ONE},
|
||||
};
|
||||
|
||||
assertTrue(Objects.deepEquals(actual, expected));
|
||||
}
|
||||
@Test
|
||||
void subtract() {
|
||||
final BigDecimal[][] matrix1 = {
|
||||
{BigDecimal.ONE, new BigDecimal(4)},
|
||||
{new BigDecimal(5), new BigDecimal(6)},
|
||||
};
|
||||
|
||||
final BigDecimal[][] matrix2 = {
|
||||
{BigDecimal.TWO, BigDecimal.ZERO},
|
||||
{new BigDecimal(-2), new BigDecimal(-3)},
|
||||
};
|
||||
|
||||
final BigDecimal[][] actual = MatrixUtil.subtract(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!"));
|
||||
|
||||
final BigDecimal[][] expected = {
|
||||
{new BigDecimal(-1), new BigDecimal(4)},
|
||||
{new BigDecimal(7), new BigDecimal(9)},
|
||||
};
|
||||
|
||||
assertTrue(Objects.deepEquals(actual, expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
void multiply() {
|
||||
|
||||
final BigDecimal[][] matrix1 = {
|
||||
{BigDecimal.ONE, BigDecimal.TWO, new BigDecimal(3)},
|
||||
{new BigDecimal(4), new BigDecimal(5), new BigDecimal(6)},
|
||||
{new BigDecimal(7), new BigDecimal(8), new BigDecimal(9)},
|
||||
};
|
||||
|
||||
final BigDecimal[][] matrix2 = {
|
||||
{BigDecimal.ONE, BigDecimal.TWO},
|
||||
{new BigDecimal(3), new BigDecimal(4)},
|
||||
{new BigDecimal(5), new BigDecimal(6)},
|
||||
};
|
||||
|
||||
final BigDecimal[][] actual = MatrixUtil.multiply(matrix1, matrix2).orElseThrow(() -> new AssertionError("Could not compute matrix!"));
|
||||
|
||||
final BigDecimal[][] expected = {
|
||||
{new BigDecimal(22), new BigDecimal(28)},
|
||||
{new BigDecimal(49), new BigDecimal(64)},
|
||||
{new BigDecimal(76), new BigDecimal(100)},
|
||||
};
|
||||
|
||||
assertTrue(Objects.deepEquals(actual, expected));
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.thealgorithms.matrix;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -10,44 +10,44 @@ class MirrorOfMatrixTest {
|
||||
|
||||
@Test
|
||||
void testMirrorMatrixRegularMatrix() {
|
||||
int[][] originalMatrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||
int[][] expectedMirrorMatrix = {{3, 2, 1}, {6, 5, 4}, {9, 8, 7}};
|
||||
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
||||
double[][] originalMatrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||
double[][] expectedMirrorMatrix = {{3, 2, 1}, {6, 5, 4}, {9, 8, 7}};
|
||||
double[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
||||
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMirrorMatrixEmptyMatrix() {
|
||||
int[][] originalMatrix = {};
|
||||
int[][] expectedMirrorMatrix = {};
|
||||
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
||||
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
||||
double[][] originalMatrix = {};
|
||||
Exception e = assertThrows(IllegalArgumentException.class, () -> MirrorOfMatrix.mirrorMatrix(originalMatrix));
|
||||
assertEquals("The input matrix cannot be empty", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMirrorMatrixSingleElementMatrix() {
|
||||
int[][] originalMatrix = {{42}};
|
||||
int[][] expectedMirrorMatrix = {{42}};
|
||||
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
||||
double[][] originalMatrix = {{42}};
|
||||
double[][] expectedMirrorMatrix = {{42}};
|
||||
double[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
||||
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMirrorMatrixMultipleRowsOneColumnMatrix() {
|
||||
int[][] originalMatrix = {{1}, {2}, {3}, {4}};
|
||||
int[][] expectedMirrorMatrix = {{1}, {2}, {3}, {4}};
|
||||
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
||||
double[][] originalMatrix = {{1}, {2}, {3}, {4}};
|
||||
double[][] expectedMirrorMatrix = {{1}, {2}, {3}, {4}};
|
||||
double[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
||||
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMirrorMatrixNullInput() {
|
||||
int[][] originalMatrix = null;
|
||||
assertNull(MirrorOfMatrix.mirrorMatrix(originalMatrix));
|
||||
double[][] originalMatrix = null;
|
||||
Exception e = assertThrows(IllegalArgumentException.class, () -> MirrorOfMatrix.mirrorMatrix(originalMatrix));
|
||||
assertEquals("The input matrix cannot be null", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMirrotMarixThrows() {
|
||||
assertThrows(IllegalArgumentException.class, () -> MirrorOfMatrix.mirrorMatrix(new int[][] {{1}, {2, 3}}));
|
||||
void testMirrorMatrixThrows() {
|
||||
assertThrows(IllegalArgumentException.class, () -> MirrorOfMatrix.mirrorMatrix(new double[][] {{1}, {2, 3}}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user