Skip to content

Commit fb2efb5

Browse files
committed
testing: improve test coverage ValidParenthesesTest
1 parent dd1a51b commit fb2efb5

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
package com.thealgorithms.strings;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
46

57
import org.junit.jupiter.params.ParameterizedTest;
68
import org.junit.jupiter.params.provider.CsvSource;
9+
import org.junit.jupiter.api.Test;
710

811
public class ValidParenthesesTest {
912

1013
@ParameterizedTest(name = "Input: \"{0}\" → Expected: {1}")
11-
@CsvSource({"'()', true", "'()[]{}', true", "'(]', false", "'{[]}', true", "'([{}])', true", "'([)]', false", "'', true", "'(', false", "')', false"})
14+
@CsvSource({"'()', true", "'()[]{}', true", "'(]', false", "'{[]}', true", "'([{}])', true", "'([)]', false", "'', true", "'(', false", "')', false", "'{{{{}}}}', true", "'[({})]', true", "'[(])', false", "'[', false", "']', false", "'()()()()', true", "'(()', false", "'())', false", "'{[()()]()}', true"})
1215
void testIsValid(String input, boolean expected) {
1316
assertEquals(expected, ValidParentheses.isValid(input));
1417
}
18+
19+
@Test
20+
void testNullInputThrows() {
21+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> ValidParentheses.isValid(null));
22+
assertEquals("Input string cannot be null", ex.getMessage());
23+
}
24+
25+
@ParameterizedTest(name = "Input: \"{0}\" → throws IllegalArgumentException")
26+
@CsvSource({"'a'", "'()a'", "'[123]'", "'{hello}'", "'( )'", "'\t'", "'\n'", "'@#$%'"})
27+
void testInvalidCharactersThrow(String input) {
28+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> ValidParentheses.isValid(input));
29+
assertTrue(ex.getMessage().startsWith("Unexpected character"));
30+
}
1531
}

0 commit comments

Comments
 (0)