Add LongestValidParenthesesTest (#3612)

This commit is contained in:
laks-mi1099 2022-10-23 00:51:54 +05:30 committed by GitHub
parent bf03d16303
commit 8d4b048cb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 15 deletions

View File

@ -43,19 +43,4 @@ public class LongestValidParentheses {
return max; return max;
} }
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
String str = sc.nextLine();
if ("quit".equals(str)) {
break;
}
System.out.println("Len is: " + getLongestValidParentheses(str));
}
sc.close();
}
} }

View File

@ -0,0 +1,51 @@
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);
}
}