Files
troupe-standup/wizard-work/beam/test_regex_match.py
T

70 lines
1.6 KiB
Python

import itertools
from regex_match import is_match, is_match_dp, match
CASES = [
("", "", True),
("", "a*", True),
("", ".", False),
("a", "a", True),
("a", ".", True),
("ab", ".*", True),
("aab", "c*a*b", True),
("mississippi", "mis*is*p*.", False),
("mississippi", "mis*is*ip*.", True),
("aa", "a", False),
("aa", "a*", True),
("ab", ".*c", False),
("aaa", "a*a", True),
("aaa", "ab*a*c*a", True),
("a", "ab*a", False),
("", "a*b*c*", True),
("abc", "a.c", True),
("abc", "a..", True),
("abcd", "d*", False),
]
def test_cases_recursive():
for s, p, exp in CASES:
assert is_match(s, p) is exp, (s, p, exp)
def test_cases_dp():
for s, p, exp in CASES:
assert is_match_dp(s, p) is exp, (s, p, exp)
def test_cross_check_random():
import random
rng = random.Random(42)
alphabet = "ab."
for _ in range(2000):
n = rng.randint(0, 6)
s = "".join(rng.choice("ab") for _ in range(n))
# build pattern with possible stars
m = rng.randint(0, 6)
toks = []
for _ in range(m):
c = rng.choice("ab.")
if rng.random() < 0.4:
toks.append(c + "*")
else:
toks.append(c)
p = "".join(toks)
r1 = is_match(s, p)
r2 = is_match_dp(s, p)
assert r1 == r2, (s, p, r1, r2)
def test_type_errors():
for f in (is_match, is_match_dp):
try:
f(123, "a")
assert False
except TypeError:
pass
def test_match_alias():
assert match("aa", "a*") is True