mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
* testing: improve test coverage ValidParenthesesTest * style: fix formatting for checkstyle * style: fix formatting for checkstyle * style: fix import --------- Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
34 lines
1.5 KiB
Java
34 lines
1.5 KiB
Java
package com.thealgorithms.strings;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
import org.junit.jupiter.params.provider.CsvSource;
|
|
|
|
public class ValidParenthesesTest {
|
|
|
|
@ParameterizedTest(name = "Input: \"{0}\" → Expected: {1}")
|
|
@CsvSource({"'()', true", "'()[]{}', true", "'(]', false", "'{[]}', true", "'([{}])', true", "'([)]', false", "'', true", "'(', false", "')', false", "'{{{{}}}}', true", "'[({})]', true", "'[(])', false", "'[', false", "']', false", "'()()()()', true", "'(()', false", "'())', false",
|
|
"'{[()()]()}', true"})
|
|
void
|
|
testIsValid(String input, boolean expected) {
|
|
assertEquals(expected, ValidParentheses.isValid(input));
|
|
}
|
|
|
|
@Test
|
|
void testNullInputThrows() {
|
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> ValidParentheses.isValid(null));
|
|
assertEquals("Input string cannot be null", ex.getMessage());
|
|
}
|
|
|
|
@ParameterizedTest(name = "Input: \"{0}\" → throws IllegalArgumentException")
|
|
@CsvSource({"'a'", "'()a'", "'[123]'", "'{hello}'", "'( )'", "'\t'", "'\n'", "'@#$%'"})
|
|
void testInvalidCharactersThrow(String input) {
|
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> ValidParentheses.isValid(input));
|
|
assertTrue(ex.getMessage().startsWith("Unexpected character"));
|
|
}
|
|
}
|