|
| 1 | +package com.thealgorithms.others; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +class PrintAMatrixInSpiralOrderTest { |
| 11 | + |
| 12 | + private final PrintAMatrixInSpiralOrder spiralPrinter = new PrintAMatrixInSpiralOrder(); |
| 13 | + |
| 14 | + @Test |
| 15 | + void testSquareMatrix() { |
| 16 | + int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; |
| 17 | + List<Integer> expected = Arrays.asList(1, 2, 3, 6, 9, 8, 7, 4, 5); |
| 18 | + assertEquals(expected, spiralPrinter.print(matrix, 3, 3)); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + void testRectangularMatrixMoreRows() { |
| 23 | + int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; |
| 24 | + List<Integer> expected = Arrays.asList(1, 2, 3, 6, 9, 12, 11, 10, 7, 4, 5, 8); |
| 25 | + assertEquals(expected, spiralPrinter.print(matrix, 4, 3)); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void testRectangularMatrixMoreCols() { |
| 30 | + int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; |
| 31 | + List<Integer> expected = Arrays.asList(1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7); |
| 32 | + assertEquals(expected, spiralPrinter.print(matrix, 3, 4)); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void testSingleRow() { |
| 37 | + int[][] matrix = {{1, 2, 3, 4}}; |
| 38 | + List<Integer> expected = Arrays.asList(1, 2, 3, 4); |
| 39 | + assertEquals(expected, spiralPrinter.print(matrix, 1, 4)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testSingleColumn() { |
| 44 | + int[][] matrix = {{1}, {2}, {3}}; |
| 45 | + List<Integer> expected = Arrays.asList(1, 2, 3); |
| 46 | + assertEquals(expected, spiralPrinter.print(matrix, 3, 1)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testEmptyMatrix() { |
| 51 | + int[][] matrix = new int[0][0]; |
| 52 | + List<Integer> expected = Collections.emptyList(); |
| 53 | + assertEquals(expected, spiralPrinter.print(matrix, 0, 0)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testOneElementMatrix() { |
| 58 | + int[][] matrix = {{42}}; |
| 59 | + List<Integer> expected = Collections.singletonList(42); |
| 60 | + assertEquals(expected, spiralPrinter.print(matrix, 1, 1)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testMatrixWithNegativeNumbers() { |
| 65 | + int[][] matrix = {{-1, -2}, {-3, -4}}; |
| 66 | + List<Integer> expected = Arrays.asList(-1, -2, -4, -3); |
| 67 | + assertEquals(expected, spiralPrinter.print(matrix, 2, 2)); |
| 68 | + } |
| 69 | +} |
0 commit comments