|
| 1 | +package com.thealgorithms.prefixsum; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +class DifferenceArrayTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + void testStandardRangeUpdate() { |
| 13 | + int[] input = {10, 20, 30, 40, 50}; |
| 14 | + DifferenceArray da = new DifferenceArray(input); |
| 15 | + |
| 16 | + da.update(1, 3, 5); |
| 17 | + |
| 18 | + long[] expected = {10, 25, 35, 45, 50}; |
| 19 | + assertArrayEquals(expected, da.getResultArray()); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + void testMultipleOverlappingUpdates() { |
| 24 | + int[] input = {10, 10, 10, 10, 10}; |
| 25 | + DifferenceArray da = new DifferenceArray(input); |
| 26 | + |
| 27 | + da.update(0, 2, 10); |
| 28 | + da.update(2, 4, 20); |
| 29 | + |
| 30 | + long[] expected = {20, 20, 40, 30, 30}; |
| 31 | + assertArrayEquals(expected, da.getResultArray()); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void testIntegerOverflowSafety() { |
| 36 | + int[] input = {Integer.MAX_VALUE, 100}; |
| 37 | + DifferenceArray da = new DifferenceArray(input); |
| 38 | + |
| 39 | + da.update(0, 0, 100); |
| 40 | + |
| 41 | + long[] result = da.getResultArray(); |
| 42 | + long expectedVal = (long) Integer.MAX_VALUE + 100; |
| 43 | + |
| 44 | + assertEquals(expectedVal, result[0]); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void testFullRangeUpdate() { |
| 49 | + int[] input = {1, 2, 3}; |
| 50 | + DifferenceArray da = new DifferenceArray(input); |
| 51 | + |
| 52 | + da.update(0, 2, 100); |
| 53 | + |
| 54 | + long[] expected = {101, 102, 103}; |
| 55 | + assertArrayEquals(expected, da.getResultArray()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testBoundaryWriteOptimization() { |
| 60 | + int[] input = {5, 5}; |
| 61 | + DifferenceArray da = new DifferenceArray(input); |
| 62 | + |
| 63 | + da.update(1, 1, 5); |
| 64 | + |
| 65 | + long[] expected = {5, 10}; |
| 66 | + |
| 67 | + assertArrayEquals(expected, da.getResultArray()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testLargeMassiveUpdate() { |
| 72 | + int[] input = {0}; |
| 73 | + DifferenceArray da = new DifferenceArray(input); |
| 74 | + |
| 75 | + int iterations = 100000; |
| 76 | + for (int i = 0; i < iterations; i++) { |
| 77 | + da.update(0, 0, 1); |
| 78 | + } |
| 79 | + |
| 80 | + assertEquals(100000L, da.getResultArray()[0]); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void testNullInputThrowsException() { |
| 85 | + assertThrows(IllegalArgumentException.class, () -> new DifferenceArray(null)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testEmptyInputThrowsException() { |
| 90 | + assertThrows(IllegalArgumentException.class, () -> new DifferenceArray(new int[] {})); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testInvalidRangeNegativeIndex() { |
| 95 | + DifferenceArray da = new DifferenceArray(new int[] {1, 2, 3}); |
| 96 | + assertThrows(IllegalArgumentException.class, () -> da.update(-1, 1, 5)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void testInvalidRangeOutOfBounds() { |
| 101 | + DifferenceArray da = new DifferenceArray(new int[] {1, 2, 3}); |
| 102 | + assertThrows(IllegalArgumentException.class, () -> da.update(0, 3, 5)); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testInvalidRangeStartGreaterThanEnd() { |
| 107 | + DifferenceArray da = new DifferenceArray(new int[] {1, 2, 3}); |
| 108 | + assertThrows(IllegalArgumentException.class, () -> da.update(2, 1, 5)); |
| 109 | + } |
| 110 | +} |
0 commit comments