-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_z3_parser.py
More file actions
85 lines (73 loc) · 2.09 KB
/
test_z3_parser.py
File metadata and controls
85 lines (73 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import re
import sre_parse
import pytest
import z3
from engines.z3.parser import regex_to_z3_expr
def compile_to_z3(pattern: str) -> z3.ReRef:
return regex_to_z3_expr(sre_parse.parse(pattern))
def matches(pattern: str, sample: str) -> bool:
s = z3.String('s')
solver = z3.Solver()
solver.add(s == z3.StringVal(sample))
solver.add(z3.InRe(s, compile_to_z3(pattern)))
return solver.check() == z3.sat
@pytest.mark.parametrize("pattern,sample,expected", [
# Literals
("a", "a", True),
("a", "b", False),
("abc", "abc", True),
("abc", "ab", False),
# Alternation
("a|b", "a", True),
("a|b", "b", True),
("a|b", "c", False),
# Quantifiers
("a*", "", True),
("a*", "aaaa", True),
("a+", "", False),
("a+", "a", True),
("a?", "", True),
("a?", "a", True),
("a?", "aa", False),
("a{2,3}", "a", False),
("a{2,3}", "aa", True),
("a{2,3}", "aaa", True),
("a{2,3}", "aaaa", False),
# Any char
(".", "x", True),
(".", "", False),
# Char classes
("[abc]", "a", True),
("[abc]", "d", False),
("[^abc]", "d", True),
("[^abc]", "a", False),
("[a-z]", "m", True),
("[a-z]", "M", False),
# Categories
(r"\d", "5", True),
(r"\d", "a", False),
(r"\D", "a", True),
(r"\D", "5", False),
(r"\w", "_", True),
(r"\w", "-", False),
(r"\W", "-", True),
(r"\s", " ", True),
(r"\s", "x", False),
# Concat + groups
("(ab)+", "abab", True),
("(ab)+", "aba", False),
# Negated literal
("[^a]", "b", True),
("[^a]", "a", False),
])
def test_pattern(pattern, sample, expected):
# Cross-check that Python regex agrees (sanity of the test data itself).
assert bool(re.fullmatch(pattern, sample)) == expected
assert matches(pattern, sample) == expected
def test_empty_regex_raises():
with pytest.raises(ValueError):
regex_to_z3_expr(sre_parse.parse(""))
@pytest.mark.parametrize("pattern", ["^a", "a$", r"\ba", r"\Ba"])
def test_unsupported_anchors(pattern):
with pytest.raises(NotImplementedError):
compile_to_z3(pattern)