1+ package stack .next_greater_element ;
2+
3+ import org .junit .jupiter .api .DisplayName ;
4+ import org .junit .jupiter .api .Test ;
5+
6+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
7+
8+ /**
9+ * Test suite for the {@link MonotonicStack} class.
10+ */
11+ class MonotonicStackTest {
12+
13+ @ Test
14+ @ DisplayName ("Should find the next greater element in a general case array" )
15+ void findNextGreaterElement_generalCase () {
16+ // Arrange
17+ int [] array = {2 , 1 , 5 , 6 , 2 , 3 };
18+ int [] expected = {5 , 5 , 6 , -1 , 3 , -1 };
19+ MonotonicStack monotonicStack = new MonotonicStack (array );
20+
21+ // Act
22+ monotonicStack .findNextGreaterElement ();
23+
24+ // Assert
25+ assertArrayEquals (expected , monotonicStack .answer , "Should correctly find next greater elements" );
26+ }
27+
28+ @ Test
29+ @ DisplayName ("Should return -1 for all elements in a strictly decreasing array" )
30+ void findNextGreaterElement_strictlyDecreasingArray () {
31+ // Arrange
32+ int [] array = {5 , 4 , 3 , 2 , 1 };
33+ int [] expected = {-1 , -1 , -1 , -1 , -1 };
34+ MonotonicStack monotonicStack = new MonotonicStack (array );
35+
36+ // Act
37+ monotonicStack .findNextGreaterElement ();
38+
39+ // Assert
40+ assertArrayEquals (expected , monotonicStack .answer , "All elements should have -1 as next greater" );
41+ }
42+
43+ @ Test
44+ @ DisplayName ("Should find the correct next greater element in a strictly increasing array" )
45+ void findNextGreaterElement_strictlyIncreasingArray () {
46+ // Arrange
47+ int [] array = {1 , 2 , 3 , 4 , 5 };
48+ int [] expected = {2 , 3 , 4 , 5 , -1 };
49+ MonotonicStack monotonicStack = new MonotonicStack (array );
50+
51+ // Act
52+ monotonicStack .findNextGreaterElement ();
53+
54+ // Assert
55+ assertArrayEquals (expected , monotonicStack .answer , "Each element's next greater should be its right neighbor" );
56+ }
57+
58+ @ Test
59+ @ DisplayName ("Should handle an array with duplicate values" )
60+ void findNextGreaterElement_withDuplicates () {
61+ // Arrange
62+ int [] array = {4 , 5 , 2 , 2 , 5 };
63+ int [] expected = {5 , -1 , 5 , 5 , -1 };
64+ MonotonicStack monotonicStack = new MonotonicStack (array );
65+
66+ // Act
67+ monotonicStack .findNextGreaterElement ();
68+
69+ // Assert
70+ assertArrayEquals (expected , monotonicStack .answer , "Should handle duplicates correctly" );
71+ }
72+
73+ @ Test
74+ @ DisplayName ("Should handle an array with duplicate values" )
75+ void findNextGreaterElement_withDuplicates_multiple () {
76+ // Arrange
77+ int [] array = {4 , 12 , 5 , 3 , 1 , 2 , 5 , 3 , 1 , 2 , 4 , 6 };
78+ int [] expected = {12 , -1 , 6 , 5 , 2 , 5 , 6 , 4 , 2 , 4 , 6 , -1 };
79+ MonotonicStack monotonicStack = new MonotonicStack (array );
80+
81+ // Act
82+ monotonicStack .findNextGreaterElement ();
83+
84+ // Assert
85+ assertArrayEquals (expected , monotonicStack .answer , "Should handle duplicates correctly" );
86+ }
87+
88+ @ Test
89+ @ DisplayName ("Should handle an array where all elements are the same" )
90+ void findNextGreaterElement_allSameElements () {
91+ // Arrange
92+ int [] array = {7 , 7 , 7 , 7 };
93+ int [] expected = {-1 , -1 , -1 , -1 };
94+ MonotonicStack monotonicStack = new MonotonicStack (array );
95+
96+ // Act
97+ monotonicStack .findNextGreaterElement ();
98+
99+ // Assert
100+ assertArrayEquals (expected , monotonicStack .answer , "All elements should be -1 when all are identical" );
101+ }
102+
103+ @ Test
104+ @ DisplayName ("Should handle a single-element array" )
105+ void findNextGreaterElement_singleElementArray () {
106+ // Arrange
107+ int [] array = {100 };
108+ int [] expected = {-1 };
109+ MonotonicStack monotonicStack = new MonotonicStack (array );
110+
111+ // Act
112+ monotonicStack .findNextGreaterElement ();
113+
114+ // Assert
115+ assertArrayEquals (expected , monotonicStack .answer , "A single element has no next greater element" );
116+ }
117+
118+ @ Test
119+ @ DisplayName ("Should handle an empty array without errors" )
120+ void findNextGreaterElement_emptyArray () {
121+ // Arrange
122+ int [] array = {};
123+ int [] expected = {};
124+ MonotonicStack monotonicStack = new MonotonicStack (array );
125+
126+ // Act
127+ monotonicStack .findNextGreaterElement ();
128+
129+ // Assert
130+ assertArrayEquals (expected , monotonicStack .answer , "An empty input should result in an empty output" );
131+ }
132+ }
0 commit comments