|
| 1 | +package com.thealgorithms.prefixsum; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.DisplayName; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +class PrefixSum2DTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + @DisplayName("Test basic 3x3 square matrix") |
| 13 | + void testStandardSquare() { |
| 14 | + int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; |
| 15 | + PrefixSum2D ps = new PrefixSum2D(matrix); |
| 16 | + |
| 17 | + // Sum of top-left 2x2: {1,2, 4,5} -> 12 |
| 18 | + assertEquals(12L, ps.sumRegion(0, 0, 1, 1)); |
| 19 | + // Sum of bottom-right 2x2: {5,6, 8,9} -> 28 |
| 20 | + assertEquals(28L, ps.sumRegion(1, 1, 2, 2)); |
| 21 | + // Full matrix -> 45 |
| 22 | + assertEquals(45L, ps.sumRegion(0, 0, 2, 2)); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + @DisplayName("Test rectangular matrix (more cols than rows)") |
| 27 | + void testRectangularWide() { |
| 28 | + int[][] matrix = {{1, 1, 1, 1}, {2, 2, 2, 2}}; |
| 29 | + PrefixSum2D ps = new PrefixSum2D(matrix); |
| 30 | + |
| 31 | + // Sum of first 3 columns of both rows -> (1*3) + (2*3) = 9 |
| 32 | + assertEquals(9L, ps.sumRegion(0, 0, 1, 2)); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + @DisplayName("Test rectangular matrix (more rows than cols)") |
| 37 | + void testRectangularTall() { |
| 38 | + int[][] matrix = {{1}, {2}, {3}, {4}}; |
| 39 | + PrefixSum2D ps = new PrefixSum2D(matrix); |
| 40 | + |
| 41 | + // Sum of middle two elements -> 2+3 = 5 |
| 42 | + assertEquals(5L, ps.sumRegion(1, 0, 2, 0)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + @DisplayName("Test single element matrix") |
| 47 | + void testSingleElement() { |
| 48 | + int[][] matrix = {{100}}; |
| 49 | + PrefixSum2D ps = new PrefixSum2D(matrix); |
| 50 | + |
| 51 | + assertEquals(100L, ps.sumRegion(0, 0, 0, 0)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @DisplayName("Test large numbers for overflow (Integer -> Long)") |
| 56 | + void testLargeNumbers() { |
| 57 | + // 2 billion. Two of these sum to > MAX_INT |
| 58 | + int val = 2_000_000_000; |
| 59 | + int[][] matrix = {{val, val}, {val, val}}; |
| 60 | + PrefixSum2D ps = new PrefixSum2D(matrix); |
| 61 | + |
| 62 | + // 4 * 2B = 8 Billion |
| 63 | + assertEquals(8_000_000_000L, ps.sumRegion(0, 0, 1, 1)); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + @DisplayName("Test invalid inputs") |
| 68 | + void testInvalidInputs() { |
| 69 | + assertThrows(IllegalArgumentException.class, () -> new PrefixSum2D(null)); |
| 70 | + assertThrows(IllegalArgumentException.class, () -> new PrefixSum2D(new int[][] {})); // empty |
| 71 | + assertThrows(IllegalArgumentException.class, () -> new PrefixSum2D(new int[][] {{}})); // empty row |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @DisplayName("Test invalid query ranges") |
| 76 | + void testInvalidRanges() { |
| 77 | + int[][] matrix = {{1, 2}, {3, 4}}; |
| 78 | + PrefixSum2D ps = new PrefixSum2D(matrix); |
| 79 | + |
| 80 | + // Negative indices |
| 81 | + assertThrows(IndexOutOfBoundsException.class, () -> ps.sumRegion(-1, 0, 0, 0)); |
| 82 | + assertThrows(IndexOutOfBoundsException.class, () -> ps.sumRegion(0, -1, 0, 0)); |
| 83 | + |
| 84 | + // Out of bounds |
| 85 | + assertThrows(IndexOutOfBoundsException.class, () -> ps.sumRegion(0, 0, 2, 0)); // row2 too big |
| 86 | + assertThrows(IndexOutOfBoundsException.class, () -> ps.sumRegion(0, 0, 0, 2)); // col2 too big |
| 87 | + |
| 88 | + // Inverted ranges (start > end) |
| 89 | + assertThrows(IndexOutOfBoundsException.class, () -> ps.sumRegion(1, 0, 0, 0)); // row1 > row2 |
| 90 | + assertThrows(IndexOutOfBoundsException.class, () -> ps.sumRegion(0, 1, 0, 0)); // col1 > col2 |
| 91 | + } |
| 92 | +} |
0 commit comments