test: LongestValidParenthesesTest (#5416)

* test: LongestValidParenthesesTest

* checkstyle: fix formatting

---------

Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
Alex Klymenko 2024-08-28 20:56:58 +02:00 committed by GitHub
parent 7d1847f51c
commit 203d544668
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 51 deletions

View File

@ -1,51 +0,0 @@
package com.thealgorithms.dynamicprogramming;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class LongestValidParenthesesTest {
LongestValidParentheses longestValidParentheses = new LongestValidParentheses();
@Test
void shouldReturnZeroWhenSingleOpeningParenthesisIsGiven() {
String input = "(";
int validLength = longestValidParentheses.getLongestValidParentheses(input);
assertEquals(0, validLength);
}
@Test
void shouldReturnZeroWhenSingleClosingParenthesisIsGiven() {
String input = ")";
int validLength = longestValidParentheses.getLongestValidParentheses(input);
assertEquals(0, validLength);
}
@Test
void shouldReturnZeroWhenNullStringIsGiven() {
String input = "";
int validLength = longestValidParentheses.getLongestValidParentheses(input);
assertEquals(0, validLength);
}
@Test
void shouldReturnTwoWhenTwoBalancedParenthesesAreGiven() {
String input = "()";
int validLength = longestValidParentheses.getLongestValidParentheses(input);
assertEquals(2, validLength);
}
@Test
void shouldReturnLengthWhenInputStringIsValid() {
String input = "()((()))";
int validLength = longestValidParentheses.getLongestValidParentheses(input);
assertEquals(8, validLength);
}
@Test
void shouldReturnValidLengthWhenInputStringIsGiven() {
String input = "((()((())))";
int validLength = longestValidParentheses.getLongestValidParentheses(input);
assertEquals(10, validLength);
}
}

View File

@ -0,0 +1,22 @@
package com.thealgorithms.dynamicprogramming;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public class LongestValidParenthesesTest {
private static Stream<Arguments> provideTestCases() {
return Stream.of(Arguments.of("", 0), Arguments.of("(", 0), Arguments.of(")", 0), Arguments.of("()", 2), Arguments.of("(())", 4), Arguments.of("()()", 4), Arguments.of(")(", 0), Arguments.of("(()", 2), Arguments.of("())(", 2), Arguments.of("(()())", 6), Arguments.of("(((())))", 8),
Arguments.of("(()))(()", 4), Arguments.of("()()()(", 6), Arguments.of("(()())()(", 8), Arguments.of("((((((", 0), Arguments.of("))))))", 0), Arguments.of("(()())(", 6), Arguments.of("))()(", 2), Arguments.of("()((()))", 8), Arguments.of("((()((())))", 10));
}
@ParameterizedTest
@MethodSource("provideTestCases")
public void testLongestValidParentheses(String input, int expected) {
assertEquals(expected, LongestValidParentheses.getLongestValidParentheses(input));
}
}