Skip to content

Commit 246d680

Browse files
Create RegularExpressionMatchingTest.java
1 parent 5b239c2 commit 246d680

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
* <p>
13+
* For more information about regular expression matching:
14+
* @see <a href="https://en.wikipedia.org/wiki/Regular_expression">Regular Expression</a>
15+
* @see <a href="https://leetcode.com/problems/regular-expression-matching/">LeetCode Problem 10</a>
16+
*/
17+
class RegularExpressionMatchingTest {
18+
19+
@Test
20+
void testBasicMatching() {
21+
assertTrue(RegularExpressionMatching.isMatch("abc", "abc"));
22+
assertFalse(RegularExpressionMatching.isMatch("abc", "abcd"));
23+
assertFalse(RegularExpressionMatching.isMatch("abcd", "abc"));
24+
}
25+
26+
@Test
27+
void testDotWildcard() {
28+
assertTrue(RegularExpressionMatching.isMatch("abc", "a.c"));
29+
assertTrue(RegularExpressionMatching.isMatch("axc", "a.c"));
30+
assertFalse(RegularExpressionMatching.isMatch("abc", "a.."));
31+
assertTrue(RegularExpressionMatching.isMatch("abc", "..."));
32+
assertFalse(RegularExpressionMatching.isMatch("ab", "..."));
33+
}
34+
35+
@Test
36+
void testStarQuantifier() {
37+
assertTrue(RegularExpressionMatching.isMatch("aa", "a*"));
38+
assertTrue(RegularExpressionMatching.isMatch("aaa", "a*"));
39+
assertTrue(RegularExpressionMatching.isMatch("", "a*"));
40+
assertFalse(RegularExpressionMatching.isMatch("b", "a*"));
41+
assertTrue(RegularExpressionMatching.isMatch("aab", "c*a*b"));
42+
assertTrue(RegularExpressionMatching.isMatch("b", "c*a*b"));
43+
}
44+
45+
@Test
46+
void testDotStarCombination() {
47+
assertTrue(RegularExpressionMatching.isMatch("abc", ".*"));
48+
assertTrue(RegularExpressionMatching.isMatch("xyz", ".*"));
49+
assertTrue(RegularExpressionMatching.isMatch("", ".*"));
50+
assertTrue(RegularExpressionMatching.isMatch("abc123", ".*"));
51+
assertTrue(RegularExpressionMatching.isMatch("abc", "a.*c"));
52+
assertTrue(RegularExpressionMatching.isMatch("axxxc", "a.*c"));
53+
assertFalse(RegularExpressionMatching.isMatch("abc", "a.*d"));
54+
}
55+
56+
@Test
57+
void testComplexPatterns() {
58+
assertTrue(RegularExpressionMatching.isMatch("mississippi", "mis*is*ip*."));
59+
assertTrue(RegularExpressionMatching.isMatch("mississippi", "mis*is*p*."));
60+
assertFalse(RegularExpressionMatching.isMatch("mississippi", "mis*is*ip*.."));
61+
assertTrue(RegularExpressionMatching.isMatch("a", "a*a*a*"));
62+
assertTrue(RegularExpressionMatching.isMatch("aaa", "a*a*a*"));
63+
assertTrue(RegularExpressionMatching.isMatch("", "a*b*c*"));
64+
}
65+
66+
@Test
67+
void testEdgeCases() {
68+
assertTrue(RegularExpressionMatching.isMatch("", ""));
69+
assertTrue(RegularExpressionMatching.isMatch("", "a*"));
70+
assertTrue(RegularExpressionMatching.isMatch("", ".*"));
71+
assertFalse(RegularExpressionMatching.isMatch("", "a"));
72+
assertFalse(RegularExpressionMatching.isMatch("", "."));
73+
assertTrue(RegularExpressionMatching.isMatch("a", "a"));
74+
assertTrue(RegularExpressionMatching.isMatch("a", "."));
75+
assertFalse(RegularExpressionMatching.isMatch("a", "b"));
76+
assertFalse(RegularExpressionMatching.isMatch("a", "aa"));
77+
}
78+
79+
@Test
80+
void testInvalidInputs() {
81+
assertThrows(IllegalArgumentException.class,
82+
() -> RegularExpressionMatching.isMatch(null, "pattern"));
83+
assertThrows(IllegalArgumentException.class,
84+
() -> RegularExpressionMatching.isMatch("string", null));
85+
assertThrows(IllegalArgumentException.class,
86+
() -> RegularExpressionMatching.isMatch(null, null));
87+
assertThrows(IllegalArgumentException.class,
88+
() -> RegularExpressionMatching.isMatch("test", "*abc"));
89+
assertThrows(IllegalArgumentException.class,
90+
() -> RegularExpressionMatching.isMatch("test", "a**b"));
91+
}
92+
93+
@Test
94+
void testIterativeImplementation() {
95+
assertTrue(RegularExpressionMatching.isMatchIterative("aa", "a*"));
96+
assertTrue(RegularExpressionMatching.isMatchIterative("ab", ".*"));
97+
assertFalse(RegularExpressionMatching.isMatchIterative("aa", "a"));
98+
assertTrue(RegularExpressionMatching.isMatchIterative("aab", "c*a*b"));
99+
100+
String[] testStrings = {"", "a", "aa", "ab", "aaa", "aab"};
101+
String[] testPatterns = {"", "a", "a*", ".*", "a.b", "c*a*b"};
102+
103+
for (String string : testStrings) {
104+
for (String patternString : testPatterns) {
105+
if (!patternString.isEmpty() && patternString.charAt(0) != '*') {
106+
boolean recursiveResult = RegularExpressionMatching.isMatch(string, patternString);
107+
boolean iterativeResult = RegularExpressionMatching.isMatchIterative(string, patternString);
108+
assertTrue(recursiveResult == iterativeResult);
109+
}
110+
}
111+
}
112+
}
113+
114+
@Test
115+
void testLeetCodeExamples() {
116+
assertFalse(RegularExpressionMatching.isMatch("aa", "a"));
117+
assertTrue(RegularExpressionMatching.isMatch("aa", "a*"));
118+
assertTrue(RegularExpressionMatching.isMatch("ab", ".*"));
119+
assertTrue(RegularExpressionMatching.isMatch("aab", "c*a*b"));
120+
assertFalse(RegularExpressionMatching.isMatch("mississippi", "mis*is*p*."));
121+
}
122+
}

0 commit comments

Comments
 (0)