refactor: optimize ValidParentheses methods and add parameterized tests (#6352)

This commit is contained in:
Oleksandr Klymenko
2025-07-08 09:50:06 +02:00
committed by GitHub
parent fa2ca9db39
commit 6c3049530f
2 changed files with 46 additions and 64 deletions

View File

@@ -1,27 +1,15 @@
package com.thealgorithms.strings;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class ValidParenthesesTest {
@Test
void testOne() {
assertTrue(ValidParentheses.isValid("()"));
assertTrue(ValidParentheses.isValidParentheses("()"));
}
@Test
void testTwo() {
assertTrue(ValidParentheses.isValid("()[]{}"));
assertTrue(ValidParentheses.isValidParentheses("()[]{}"));
}
@Test
void testThree() {
assertFalse(ValidParentheses.isValid("(]"));
assertFalse(ValidParentheses.isValidParentheses("(]"));
@ParameterizedTest(name = "Input: \"{0}\" → Expected: {1}")
@CsvSource({"'()', true", "'()[]{}', true", "'(]', false", "'{[]}', true", "'([{}])', true", "'([)]', false", "'', true", "'(', false", "')', false"})
void testIsValid(String input, boolean expected) {
assertEquals(expected, ValidParentheses.isValid(input));
}
}