1+ package com .thealgorithms .prefixsum ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertThrows ;
5+
6+ import org .junit .jupiter .api .Test ;
7+
8+ class DifferenceArrayTest {
9+
10+ @ Test
11+ void testStandardRangeUpdate () {
12+ int [] input = {10 , 20 , 30 , 40 , 50 };
13+ DifferenceArray da = new DifferenceArray (input );
14+
15+ // Update range [1, 3] by adding 5 -> {10, 25, 35, 45, 50}
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+ // Add 10 to [0, 2] -> {20, 20, 20, 10, 10}
28+ da .update (0 , 2 , 10 );
29+ // Add 20 to [2, 4] -> {20, 20, 40, 30, 30}
30+ // Index 2 overlap: 10 + 10 + 20 = 40
31+ da .update (2 , 4 , 20 );
32+
33+ long [] expected = {20 , 20 , 40 , 30 , 30 };
34+ assertArrayEquals (expected , da .getResultArray ());
35+ }
36+
37+ @ Test
38+ void testIntegerOverflowSafety () {
39+ // Using Integer.MAX_VALUE to ensure long[] handles the overflow correctly
40+ int [] input = {Integer .MAX_VALUE , 100 };
41+ DifferenceArray da = new DifferenceArray (input );
42+
43+ da .update (0 , 0 , 100 );
44+
45+ long [] result = da .getResultArray ();
46+ long expectedVal = (long ) Integer .MAX_VALUE + 100 ;
47+
48+ assert result [0 ] == expectedVal ;
49+ }
50+
51+ @ Test
52+ void testFullRangeUpdate () {
53+ int [] input = {1 , 2 , 3 };
54+ DifferenceArray da = new DifferenceArray (input );
55+
56+ da .update (0 , 2 , 100 );
57+
58+ long [] expected = {101 , 102 , 103 };
59+ assertArrayEquals (expected , da .getResultArray ());
60+ }
61+
62+ @ Test
63+ void testBoundaryWriteOptimization () {
64+ // Verifies that writing to the internal 'n' index (r+1) does not throw exceptions
65+ int [] input = {5 , 5 };
66+ DifferenceArray da = new DifferenceArray (input );
67+
68+ da .update (1 , 1 , 5 ); // r + 1 = 2, which is out of bounds for the input array size, but valid for differenceArray
69+
70+ long [] expected = {5 , 10 };
71+ assertArrayEquals (expected , da .getResultArray ());
72+ }
73+
74+ @ Test
75+ void testNullInputThrowsException () {
76+ assertThrows (IllegalArgumentException .class , () -> new DifferenceArray (null ));
77+ }
78+
79+ @ Test
80+ void testEmptyInputThrowsException () {
81+ assertThrows (IllegalArgumentException .class , () -> new DifferenceArray (new int [] {}));
82+ }
83+
84+ @ Test
85+ void testInvalidRangeNegativeIndex () {
86+ DifferenceArray da = new DifferenceArray (new int [] {1 , 2 , 3 });
87+ assertThrows (IllegalArgumentException .class , () -> da .update (-1 , 1 , 5 ));
88+ }
89+
90+ @ Test
91+ void testInvalidRangeOutOfBounds () {
92+ DifferenceArray da = new DifferenceArray (new int [] {1 , 2 , 3 });
93+ assertThrows (IllegalArgumentException .class , () -> da .update (0 , 3 , 5 ));
94+ }
95+
96+ @ Test
97+ void testInvalidRangeStartGreaterThanEnd () {
98+ DifferenceArray da = new DifferenceArray (new int [] {1 , 2 , 3 });
99+ assertThrows (IllegalArgumentException .class , () -> da .update (2 , 1 , 5 ));
100+ }
101+ }
0 commit comments