diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestValidParentheses.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestValidParentheses.java index b2c0bedb..a2a5427e 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestValidParentheses.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestValidParentheses.java @@ -43,19 +43,4 @@ public class LongestValidParentheses { 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(); - } } diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest b/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest new file mode 100644 index 00000000..b2b28ffd --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest @@ -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); + } +}