refactor: Enhance docs, add tests in PrintMatrixInSpiralOrder (#6636)

* refactor: Enhance docs, add tests in `PrintMatrixInSpiralOrder`

* Fix error in BloomFilter

* Fix

* Fix

* Fix
This commit is contained in:
Hardik Pawar
2025-10-23 01:12:11 +05:30
committed by GitHub
parent 89303690f2
commit f66da5e5ee
6 changed files with 135 additions and 121 deletions

View File

@@ -0,0 +1,83 @@
package com.thealgorithms.matrix;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
class PrintAMatrixInSpiralOrderTest {
private final PrintAMatrixInSpiralOrder spiralPrinter = new PrintAMatrixInSpiralOrder();
@Test
void testSquareMatrix() {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
List<Integer> expected = Arrays.asList(1, 2, 3, 6, 9, 8, 7, 4, 5);
assertEquals(expected, spiralPrinter.print(matrix, 3, 3));
}
@Test
void testRectangularMatrixMoreRows() {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
List<Integer> expected = Arrays.asList(1, 2, 3, 6, 9, 12, 11, 10, 7, 4, 5, 8);
assertEquals(expected, spiralPrinter.print(matrix, 4, 3));
}
@Test
void testRectangularMatrixMoreCols() {
int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
List<Integer> expected = Arrays.asList(1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7);
assertEquals(expected, spiralPrinter.print(matrix, 3, 4));
}
@Test
void testSingleRow() {
int[][] matrix = {{1, 2, 3, 4}};
List<Integer> expected = Arrays.asList(1, 2, 3, 4);
assertEquals(expected, spiralPrinter.print(matrix, 1, 4));
}
@Test
void testSingleColumn() {
int[][] matrix = {{1}, {2}, {3}};
List<Integer> expected = Arrays.asList(1, 2, 3);
assertEquals(expected, spiralPrinter.print(matrix, 3, 1));
}
@Test
void testEmptyMatrix() {
int[][] matrix = new int[0][0];
List<Integer> expected = Collections.emptyList();
assertEquals(expected, spiralPrinter.print(matrix, 0, 0));
}
@Test
void testOneElementMatrix() {
int[][] matrix = {{42}};
List<Integer> expected = Collections.singletonList(42);
assertEquals(expected, spiralPrinter.print(matrix, 1, 1));
}
@Test
void testMatrixWithNegativeNumbers() {
int[][] matrix = {{-1, -2}, {-3, -4}};
List<Integer> expected = Arrays.asList(-1, -2, -4, -3);
assertEquals(expected, spiralPrinter.print(matrix, 2, 2));
}
@Test
void testLargeSquareMatrix() {
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}};
List<Integer> expected = Arrays.asList(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);
assertEquals(expected, spiralPrinter.print(matrix, 5, 5));
}
@Test
void testSingleRowWithTwoElements() {
int[][] matrix = {{2, 2}};
List<Integer> expected = Arrays.asList(2, 2);
assertEquals(expected, spiralPrinter.print(matrix, 1, 2));
}
}

View File

@@ -1,26 +0,0 @@
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);
}
}

View File

@@ -1,26 +0,0 @@
package com.thealgorithms.others;
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);
}
}