Skip to content

Commit 3398c56

Browse files
authored
test: LargestRectangle (#5360)
1 parent 3ed8561 commit 3398c56

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class LargestRectangleTest {
8+
9+
@Test
10+
void testLargestRectangleHistogramWithTypicalCases() {
11+
// Typical case with mixed heights
12+
int[] heights = {2, 1, 5, 6, 2, 3};
13+
String expected = "10";
14+
String result = LargestRectangle.largestRectanglehistogram(heights);
15+
assertEquals(expected, result);
16+
17+
// Another typical case with increasing heights
18+
heights = new int[] {2, 4};
19+
expected = "4";
20+
result = LargestRectangle.largestRectanglehistogram(heights);
21+
assertEquals(expected, result);
22+
23+
// Case with multiple bars of the same height
24+
heights = new int[] {4, 4, 4, 4};
25+
expected = "16";
26+
result = LargestRectangle.largestRectanglehistogram(heights);
27+
assertEquals(expected, result);
28+
}
29+
30+
@Test
31+
void testLargestRectangleHistogramWithEdgeCases() {
32+
// Edge case with an empty array
33+
int[] heights = {};
34+
String expected = "0";
35+
String result = LargestRectangle.largestRectanglehistogram(heights);
36+
assertEquals(expected, result);
37+
38+
// Edge case with a single bar
39+
heights = new int[] {5};
40+
expected = "5";
41+
result = LargestRectangle.largestRectanglehistogram(heights);
42+
assertEquals(expected, result);
43+
44+
// Edge case with all bars of height 0
45+
heights = new int[] {0, 0, 0};
46+
expected = "0";
47+
result = LargestRectangle.largestRectanglehistogram(heights);
48+
assertEquals(expected, result);
49+
}
50+
51+
@Test
52+
void testLargestRectangleHistogramWithLargeInput() {
53+
// Large input case
54+
int[] heights = new int[10000];
55+
for (int i = 0; i < heights.length; i++) {
56+
heights[i] = 1;
57+
}
58+
String expected = "10000";
59+
String result = LargestRectangle.largestRectanglehistogram(heights);
60+
assertEquals(expected, result);
61+
}
62+
63+
@Test
64+
void testLargestRectangleHistogramWithComplexCases() {
65+
// Complex case with a mix of heights
66+
int[] heights = {6, 2, 5, 4, 5, 1, 6};
67+
String expected = "12";
68+
String result = LargestRectangle.largestRectanglehistogram(heights);
69+
assertEquals(expected, result);
70+
71+
// Case with a peak in the middle
72+
heights = new int[] {2, 1, 5, 6, 2, 3, 1};
73+
expected = "10";
74+
result = LargestRectangle.largestRectanglehistogram(heights);
75+
assertEquals(expected, result);
76+
}
77+
}

0 commit comments

Comments
 (0)