|
| 1 | +package com.thealgorithms.dynamicprogramming; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +/** |
| 10 | + * Unit tests for RegularExpressionMatching algorithm |
| 11 | + * |
| 12 | + * Covers various test cases including: |
| 13 | + * - Basic matching scenarios |
| 14 | + * - '.' wildcard functionality |
| 15 | + * - '*' quantifier functionality |
| 16 | + * - Edge cases and boundary conditions |
| 17 | + * - Invalid input validation |
| 18 | + * |
| 19 | + * @author Your Name (replace with your GitHub username) |
| 20 | + */ |
| 21 | +public class RegularExpressionMatchingTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + void testBasicMatching() { |
| 25 | + // Exact matches |
| 26 | + assertTrue(RegularExpressionMatching.isMatch("abc", "abc")); |
| 27 | + assertFalse(RegularExpressionMatching.isMatch("abc", "abcd")); |
| 28 | + assertFalse(RegularExpressionMatching.isMatch("abcd", "abc")); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void testDotWildcard() { |
| 33 | + // '.' should match any single character |
| 34 | + assertTrue(RegularExpressionMatching.isMatch("abc", "a.c")); |
| 35 | + assertTrue(RegularExpressionMatching.isMatch("axc", "a.c")); |
| 36 | + assertFalse(RegularExpressionMatching.isMatch("abc", "a..")); |
| 37 | + assertTrue(RegularExpressionMatching.isMatch("abc", "...")); |
| 38 | + assertFalse(RegularExpressionMatching.isMatch("ab", "...")); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void testStarQuantifier() { |
| 43 | + // '*' means zero or more of preceding element |
| 44 | + assertTrue(RegularExpressionMatching.isMatch("aa", "a*")); |
| 45 | + assertTrue(RegularExpressionMatching.isMatch("aaa", "a*")); |
| 46 | + assertTrue(RegularExpressionMatching.isMatch("", "a*")); |
| 47 | + assertFalse(RegularExpressionMatching.isMatch("b", "a*")); |
| 48 | + |
| 49 | + // Complex star patterns |
| 50 | + assertTrue(RegularExpressionMatching.isMatch("aab", "c*a*b")); |
| 51 | + assertTrue(RegularExpressionMatching.isMatch("b", "c*a*b")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void testDotStarCombination() { |
| 56 | + // ".*" should match any sequence of characters |
| 57 | + assertTrue(RegularExpressionMatching.isMatch("abc", ".*")); |
| 58 | + assertTrue(RegularExpressionMatching.isMatch("xyz", ".*")); |
| 59 | + assertTrue(RegularExpressionMatching.isMatch("", ".*")); |
| 60 | + assertTrue(RegularExpressionMatching.isMatch("abc123", ".*")); |
| 61 | + |
| 62 | + // More complex combinations |
| 63 | + assertTrue(RegularExpressionMatching.isMatch("abc", "a.*c")); |
| 64 | + assertTrue(RegularExpressionMatching.isMatch("axxxc", "a.*c")); |
| 65 | + assertFalse(RegularExpressionMatching.isMatch("abc", "a.*d")); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void testComplexPatterns() { |
| 70 | + // Mixed patterns |
| 71 | + assertTrue(RegularExpressionMatching.isMatch("mississippi", "mis*is*ip*.")); |
| 72 | + assertTrue(RegularExpressionMatching.isMatch("mississippi", "mis*is*p*.")); |
| 73 | + assertFalse(RegularExpressionMatching.isMatch("mississippi", "mis*is*ip*..")); |
| 74 | + |
| 75 | + // Multiple star operators |
| 76 | + assertTrue(RegularExpressionMatching.isMatch("a", "a*a*a*")); |
| 77 | + assertTrue(RegularExpressionMatching.isMatch("aaa", "a*a*a*")); |
| 78 | + assertTrue(RegularExpressionMatching.isMatch("", "a*b*c*")); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testEdgeCases() { |
| 83 | + // Empty strings |
| 84 | + assertTrue(RegularExpressionMatching.isMatch("", "")); |
| 85 | + assertTrue(RegularExpressionMatching.isMatch("", "a*")); |
| 86 | + assertTrue(RegularExpressionMatching.isMatch("", ".*")); |
| 87 | + assertFalse(RegularExpressionMatching.isMatch("", "a")); |
| 88 | + assertFalse(RegularExpressionMatching.isMatch("", ".")); |
| 89 | + |
| 90 | + // Single character patterns |
| 91 | + assertTrue(RegularExpressionMatching.isMatch("a", "a")); |
| 92 | + assertTrue(RegularExpressionMatching.isMatch("a", ".")); |
| 93 | + assertFalse(RegularExpressionMatching.isMatch("a", "b")); |
| 94 | + assertFalse(RegularExpressionMatching.isMatch("a", "aa")); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testInvalidInputs() { |
| 99 | + // Null inputs |
| 100 | + assertThrows(IllegalArgumentException.class, |
| 101 | + () -> RegularExpressionMatching.isMatch(null, "pattern")); |
| 102 | + assertThrows(IllegalArgumentException.class, |
| 103 | + () -> RegularExpressionMatching.isMatch("string", null)); |
| 104 | + assertThrows(IllegalArgumentException.class, |
| 105 | + () -> RegularExpressionMatching.isMatch(null, null)); |
| 106 | + |
| 107 | + // Invalid patterns |
| 108 | + assertThrows(IllegalArgumentException.class, |
| 109 | + () -> RegularExpressionMatching.isMatch("test", "*abc")); |
| 110 | + assertThrows(IllegalArgumentException.class, |
| 111 | + () -> RegularExpressionMatching.isMatch("test", "a**b")); |
| 112 | + assertThrows(IllegalArgumentException.class, |
| 113 | + () -> RegularExpressionMatching.isMatch("test", "abc@")); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void testIterativeImplementation() { |
| 118 | + // Test that iterative implementation produces same results as recursive |
| 119 | + assertTrue(RegularExpressionMatching.isMatchIterative("aa", "a*")); |
| 120 | + assertTrue(RegularExpressionMatching.isMatchIterative("ab", ".*")); |
| 121 | + assertFalse(RegularExpressionMatching.isMatchIterative("aa", "a")); |
| 122 | + assertTrue(RegularExpressionMatching.isMatchIterative("aab", "c*a*b")); |
| 123 | + |
| 124 | + // Test with same inputs for both implementations |
| 125 | + String[] testStrings = {"", "a", "aa", "ab", "aaa", "aab", "mississippi"}; |
| 126 | + String[] testPatterns = {"", "a", "a*", ".*", "a.b", "c*a*b", "mis*is*p*."}; |
| 127 | + |
| 128 | + for (String s : testStrings) { |
| 129 | + for (String p : testPatterns) { |
| 130 | + if (!p.isEmpty() && p.charAt(0) != '*') { // Skip invalid patterns |
| 131 | + boolean recursiveResult = RegularExpressionMatching.isMatch(s, p); |
| 132 | + boolean iterativeResult = RegularExpressionMatching.isMatchIterative(s, p); |
| 133 | + assertTrue(recursiveResult == iterativeResult, |
| 134 | + String.format("Mismatch for s='%s', p='%s'", s, p)); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void testLeetCodeExamples() { |
| 142 | + // Examples from LeetCode problem description |
| 143 | + assertFalse(RegularExpressionMatching.isMatch("aa", "a")); |
| 144 | + assertTrue(RegularExpressionMatching.isMatch("aa", "a*")); |
| 145 | + assertTrue(RegularExpressionMatching.isMatch("ab", ".*")); |
| 146 | + |
| 147 | + // Additional LeetCode test cases |
| 148 | + assertTrue(RegularExpressionMatching.isMatch("aab", "c*a*b")); |
| 149 | + assertFalse(RegularExpressionMatching.isMatch("mississippi", "mis*is*p*.")); |
| 150 | + } |
| 151 | +} |
0 commit comments