|
| 1 | +package com.thealgorithms.stacks; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import java.util.stream.Stream; |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.Arguments; |
| 9 | +import org.junit.jupiter.params.provider.MethodSource; |
| 10 | + |
| 11 | +class StockSpanProblemTest { |
| 12 | + |
| 13 | + @ParameterizedTest |
| 14 | + @MethodSource("validTestCases") |
| 15 | + void testCalculateSpan(int[] prices, int[] expectedSpans) { |
| 16 | + assertArrayEquals(expectedSpans, StockSpanProblem.calculateSpan(prices)); |
| 17 | + } |
| 18 | + |
| 19 | + private static Stream<Arguments> validTestCases() { |
| 20 | + return Stream.of(Arguments.of(new int[] {10, 4, 5, 90, 120, 80}, new int[] {1, 1, 2, 4, 5, 1}), Arguments.of(new int[] {100, 50, 60, 70, 80, 90}, new int[] {1, 1, 2, 3, 4, 5}), Arguments.of(new int[] {5, 4, 3, 2, 1}, new int[] {1, 1, 1, 1, 1}), |
| 21 | + Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {1, 2, 3, 4, 5}), Arguments.of(new int[] {10, 20, 30, 40, 50}, new int[] {1, 2, 3, 4, 5}), Arguments.of(new int[] {100, 80, 60, 70, 60, 75, 85}, new int[] {1, 1, 1, 2, 1, 4, 6}), |
| 22 | + Arguments.of(new int[] {7, 7, 7, 7}, new int[] {1, 2, 3, 4}), Arguments.of(new int[] {}, new int[] {}), Arguments.of(new int[] {42}, new int[] {1})); |
| 23 | + } |
| 24 | + |
| 25 | + @ParameterizedTest |
| 26 | + @MethodSource("invalidTestCases") |
| 27 | + void testCalculateSpanInvalidInput(int[] prices) { |
| 28 | + assertThrows(IllegalArgumentException.class, () -> StockSpanProblem.calculateSpan(prices)); |
| 29 | + } |
| 30 | + |
| 31 | + private static Stream<Arguments> invalidTestCases() { |
| 32 | + return Stream.of(Arguments.of((int[]) null)); |
| 33 | + } |
| 34 | +} |
0 commit comments