mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
Refactor files to be in correctly nested packages (#6120)
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package com.thealgorithms.matrix;
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
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 InverseOfMatrixTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestCases")
|
||||
void testInvert(double[][] matrix, double[][] expectedInverse) {
|
||||
double[][] result = InverseOfMatrix.invert(matrix);
|
||||
assertMatrixEquals(expectedInverse, result);
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideTestCases() {
|
||||
return Stream.of(Arguments.of(new double[][] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, new double[][] {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}), Arguments.of(new double[][] {{4, 7}, {2, 6}}, new double[][] {{0.6, -0.7}, {-0.2, 0.4}}));
|
||||
}
|
||||
|
||||
private void assertMatrixEquals(double[][] expected, double[][] actual) {
|
||||
for (int i = 0; i < expected.length; i++) {
|
||||
assertArrayEquals(expected[i], actual[i], 1.0E-10, "Row " + i + " is not equal");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.thealgorithms.matrix;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
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;
|
||||
|
||||
public class MatrixTransposeTest {
|
||||
|
||||
private static Stream<Arguments> provideValidMatrixTestCases() {
|
||||
return Stream.of(Arguments.of(new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, new int[][] {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}, "Transpose of square matrix"), Arguments.of(new int[][] {{1, 2}, {3, 4}, {5, 6}}, new int[][] {{1, 3, 5}, {2, 4, 6}}, "Transpose of rectangular matrix"),
|
||||
Arguments.of(new int[][] {{1, 2, 3}}, new int[][] {{1}, {2}, {3}}, "Transpose of single-row matrix"), Arguments.of(new int[][] {{1}, {2}, {3}}, new int[][] {{1, 2, 3}}, "Transpose of single-column matrix"));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> provideInvalidMatrixTestCases() {
|
||||
return Stream.of(Arguments.of(new int[0][0], "Empty matrix should throw IllegalArgumentException"), Arguments.of(null, "Null matrix should throw IllegalArgumentException"));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "Test case {index}: {2}")
|
||||
@MethodSource("provideValidMatrixTestCases")
|
||||
void testValidMatrixTranspose(int[][] input, int[][] expected, String description) {
|
||||
assertArrayEquals(expected, MatrixTranspose.transpose(input), description);
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "Test case {index}: {1}")
|
||||
@MethodSource("provideInvalidMatrixTestCases")
|
||||
void testInvalidMatrixTranspose(int[][] input, String description) {
|
||||
assertThrows(IllegalArgumentException.class, () -> MatrixTranspose.transpose(input), description);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.thealgorithms.matrix;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class MedianOfMatrixTest {
|
||||
|
||||
@Test
|
||||
public void testMedianWithOddNumberOfElements() {
|
||||
List<List<Integer>> matrix = new ArrayList<>();
|
||||
matrix.add(Arrays.asList(1, 3, 5));
|
||||
matrix.add(Arrays.asList(2, 4, 6));
|
||||
matrix.add(Arrays.asList(7, 8, 9));
|
||||
|
||||
int result = MedianOfMatrix.median(matrix);
|
||||
|
||||
assertEquals(5, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMedianWithEvenNumberOfElements() {
|
||||
List<List<Integer>> matrix = new ArrayList<>();
|
||||
matrix.add(Arrays.asList(2, 4));
|
||||
matrix.add(Arrays.asList(1, 3));
|
||||
|
||||
int result = MedianOfMatrix.median(matrix);
|
||||
|
||||
assertEquals(2, result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
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.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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);
|
||||
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMirrorMatrixEmptyMatrix() {
|
||||
int[][] originalMatrix = {};
|
||||
int[][] expectedMirrorMatrix = {};
|
||||
int[][] mirroredMatrix = MirrorOfMatrix.mirrorMatrix(originalMatrix);
|
||||
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMirrorMatrixSingleElementMatrix() {
|
||||
int[][] originalMatrix = {{42}};
|
||||
int[][] expectedMirrorMatrix = {{42}};
|
||||
int[][] 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);
|
||||
assertArrayEquals(expectedMirrorMatrix, mirroredMatrix);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMirrorMatrixNullInput() {
|
||||
int[][] originalMatrix = null;
|
||||
assertNull(MirrorOfMatrix.mirrorMatrix(originalMatrix));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMirrotMarixThrows() {
|
||||
assertThrows(IllegalArgumentException.class, () -> MirrorOfMatrix.mirrorMatrix(new int[][] {{1}, {2, 3}}));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.thealgorithms.matrix;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
|
||||
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestPrintMatrixInSpiralOrder {
|
||||
@Test
|
||||
public void testOne() {
|
||||
int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
|
||||
var printClass = new PrintAMatrixInSpiralOrder();
|
||||
List<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
|
||||
List<Integer> list = List.of(3, 4, 5, 6, 7, 12, 18, 27, 34, 33, 32, 31, 30, 23, 14, 8, 9, 10, 11, 17, 26, 25, 24, 15, 16);
|
||||
assertIterableEquals(res, list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwo() {
|
||||
int[][] matrix = {{2, 2}};
|
||||
var printClass = new PrintAMatrixInSpiralOrder();
|
||||
List<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
|
||||
List<Integer> list = List.of(2, 2);
|
||||
assertIterableEquals(res, list);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user