|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 4 |
|
5 | | -import org.junit.jupiter.api.Test; |
| 5 | +import java.util.stream.Stream; |
| 6 | +import org.junit.jupiter.params.ParameterizedTest; |
| 7 | +import org.junit.jupiter.params.provider.Arguments; |
| 8 | +import org.junit.jupiter.params.provider.MethodSource; |
6 | 9 |
|
7 | 10 | public class LargestRectangleTest { |
8 | 11 |
|
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); |
| 12 | + @ParameterizedTest(name = "Histogram: {0} → Expected area: {1}") |
| 13 | + @MethodSource("histogramProvider") |
| 14 | + void testLargestRectangleHistogram(int[] heights, String expected) { |
| 15 | + assertEquals(expected, LargestRectangle.largestRectangleHistogram(heights)); |
28 | 16 | } |
29 | 17 |
|
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); |
| 18 | + static Stream<Arguments> histogramProvider() { |
| 19 | + return Stream.of(Arguments.of(new int[] {2, 1, 5, 6, 2, 3}, "10"), Arguments.of(new int[] {2, 4}, "4"), Arguments.of(new int[] {4, 4, 4, 4}, "16"), Arguments.of(new int[] {}, "0"), Arguments.of(new int[] {5}, "5"), Arguments.of(new int[] {0, 0, 0}, "0"), |
| 20 | + Arguments.of(new int[] {6, 2, 5, 4, 5, 1, 6}, "12"), Arguments.of(new int[] {2, 1, 5, 6, 2, 3, 1}, "10"), Arguments.of(createLargeArray(10000, 1), "10000")); |
49 | 21 | } |
50 | 22 |
|
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); |
| 23 | + private static int[] createLargeArray(int size, int value) { |
| 24 | + int[] arr = new int[size]; |
| 25 | + java.util.Arrays.fill(arr, value); |
| 26 | + return arr; |
76 | 27 | } |
77 | 28 | } |
0 commit comments