refactor: NextSmallerElement (#5412)

* refactor: NextSmallerElement

* checkstyle: fix formatting

* checkstyle: fix formatting

* checkstyle: remove redundant new line

---------

Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
Alex Klymenko
2024-08-27 11:31:47 +02:00
committed by GitHub
parent 0c8616e332
commit af7c425010
2 changed files with 62 additions and 46 deletions

View File

@ -0,0 +1,29 @@
package com.thealgorithms.stacks;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class NextSmallerElementTest {
@ParameterizedTest
@MethodSource("provideTestCases")
void testFindNextSmallerElements(int[] input, int[] expected) {
assertArrayEquals(expected, NextSmallerElement.findNextSmallerElements(input));
}
static Stream<Arguments> provideTestCases() {
return Stream.of(Arguments.of(new int[] {2, 7, 3, 5, 4, 6, 8}, new int[] {-1, 2, 2, 3, 3, 4, 6}), Arguments.of(new int[] {5}, new int[] {-1}), Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {-1, 1, 2, 3, 4}), Arguments.of(new int[] {5, 4, 3, 2, 1}, new int[] {-1, -1, -1, -1, -1}),
Arguments.of(new int[] {4, 5, 2, 25}, new int[] {-1, 4, -1, 2}), Arguments.of(new int[] {}, new int[] {}));
}
@Test
void testFindNextSmallerElementsExceptions() {
assertThrows(IllegalArgumentException.class, () -> NextSmallerElement.findNextSmallerElements(null));
}
}