Skip to content

test: LargestRectangle #5360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.thealgorithms.stacks;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class LargestRectangleTest {

@Test
void testLargestRectangleHistogramWithTypicalCases() {
// Typical case with mixed heights
int[] heights = {2, 1, 5, 6, 2, 3};
String expected = "10";
String result = LargestRectangle.largestRectanglehistogram(heights);
assertEquals(expected, result);

// Another typical case with increasing heights
heights = new int[] {2, 4};
expected = "4";
result = LargestRectangle.largestRectanglehistogram(heights);
assertEquals(expected, result);

// Case with multiple bars of the same height
heights = new int[] {4, 4, 4, 4};
expected = "16";
result = LargestRectangle.largestRectanglehistogram(heights);
assertEquals(expected, result);
}

@Test
void testLargestRectangleHistogramWithEdgeCases() {
// Edge case with an empty array
int[] heights = {};
String expected = "0";
String result = LargestRectangle.largestRectanglehistogram(heights);
assertEquals(expected, result);

// Edge case with a single bar
heights = new int[] {5};
expected = "5";
result = LargestRectangle.largestRectanglehistogram(heights);
assertEquals(expected, result);

// Edge case with all bars of height 0
heights = new int[] {0, 0, 0};
expected = "0";
result = LargestRectangle.largestRectanglehistogram(heights);
assertEquals(expected, result);
}

@Test
void testLargestRectangleHistogramWithLargeInput() {
// Large input case
int[] heights = new int[10000];
for (int i = 0; i < heights.length; i++) {
heights[i] = 1;
}
String expected = "10000";
String result = LargestRectangle.largestRectanglehistogram(heights);
assertEquals(expected, result);
}

@Test
void testLargestRectangleHistogramWithComplexCases() {
// Complex case with a mix of heights
int[] heights = {6, 2, 5, 4, 5, 1, 6};
String expected = "12";
String result = LargestRectangle.largestRectanglehistogram(heights);
assertEquals(expected, result);

// Case with a peak in the middle
heights = new int[] {2, 1, 5, 6, 2, 3, 1};
expected = "10";
result = LargestRectangle.largestRectanglehistogram(heights);
assertEquals(expected, result);
}
}