-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.py
151 lines (118 loc) · 6.16 KB
/
test.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import unittest
import importlib
import string
import gst
from hypothesis import strategies, settings, given
# TODO test cases for:
# pattern is prefix of text
# pattern is suffix of text
# both
# pattern is longer than text
# varying minmatch length
EDGE_CASES = (
# Causes unterminated loop in gst.cpp match_strings #FIXME
(
"09ggg2cgtrrdd27a2ckjnkjnknokjnnkjnjnnkjnkjnjnjnjnjnjnjnjnjnnkjnnjnmnjnjnjnjnjnjnjnjnhnjnkjnjnjnjnjnonnjnnkjnonkjnkjnjnjnnjnjnjnjnonjnjnnnkjnonkjnkjnjnjnnjnjnnnjnjnonknnnjnjnjnjnnnnnjnjnnkjnjnnknnjnnjnjnnkjnkjnjnjnjnjnjnjnjnjnnkjnkjnjnjnnjnnjnjnnjnhnjnonjnmnjnmnnnjnjnjnnjnjnnnkjnnnjnonnnkjnnjnhnjnmnjnmnnkjnjnjnjnjnjnjnnjnjnonnjnonknnnkjnjnkjnonkjnkjnjnjnnjnjnjnjnonjnjnnnkjnonkjnkjnjnjnnjnjnnnjnjnonknnnjnjnjnjnnnnnjnjnjnjnnnnnnknkjnjnjnonnjnjnonjnjnjnjnnn",
" 09ggg2cgtrrdd27a2ckjnkjnknokjnnkjnjnnjnjnjnknjnnkjnkjnjnjnjnjnjnjnjnjnnkjnnjnmnjnjnjnjnjnjnjnjnhnjnkjnjnjnjnjnonnjnnkjnonkjnkjnjnjnnjnjnjnjnonjnjnnnkjnonkjnkjnjnjnnjnjnnnjnjnonknnnjnjnjnjnnnnnnnnjnjnnnkjnjnnknnjnnjnjnnkjnkjnjnjnjnjnjnjnjnjnnkjnkjnjnjnnjnnnjnnnjnjnnnnjnhnjnonjnmnjnmnnnjnjnjnnjnjnjnkjnnnjnonnnkjnnjnhnjnmnjnmnnkjnjnjnjnjnjnjnnjnjnonnjnonknnnkjnonkjnkjnjnjnnjnjnjnjnonjnjnnnkjnonkjnkjnjnjnnjnjnnnjnjnonknnnjnjnjnjnnnnnjnjnjnjnnnnnnknkjnjnjnonnjnjnonjnjnjnjnnn"
),
)
class TestCase(unittest.TestCase):
def assertCorrectMatchSubstringMapping(self, pattern, text, match):
self.assertIsInstance(match, tuple)
pattern_start, text_start, match_len = match
self.assertEqual(pattern[pattern_start:pattern_start + match_len],
text[text_start:text_start + match_len])
class Test1Simple(TestCase):
def test2_single_full_match(self):
pattern = b"hello"
text = b"how delightful, hello there"
matches = gst.match(pattern, '', text, '', len(pattern))
self.assertIsInstance(matches, list)
self.assertEqual(len(matches), 1)
self.assertCorrectMatchSubstringMapping(pattern, text, matches[0])
pattern_str, text_str = pattern.decode("ascii"), text.decode("ascii")
matches = gst.match(pattern_str, '', text_str, '', len(pattern_str))
self.assertCorrectMatchSubstringMapping(pattern, text, matches[0])
def test3_single_partial_match(self):
pattern = b"hello"
text = b"we are in helsinki now"
match_len = 3
matches = gst.match(pattern, '', text, '', match_len)
self.assertIsInstance(matches, list)
self.assertEqual(len(matches), 1)
self.assertCorrectMatchSubstringMapping(pattern, text, matches[0])
pattern_str, text_str = pattern.decode("ascii"), text.decode("ascii")
matches = gst.match(pattern_str, '', text_str, '', match_len)
self.assertIsInstance(matches, list)
self.assertEqual(len(matches), 1)
self.assertCorrectMatchSubstringMapping(pattern, text, matches[0])
def test4_no_match(self):
pattern = b"hello"
text = b"go away, you nuisance"
matches = gst.match(pattern, '', text, '', len(pattern))
self.assertIsInstance(matches, list)
self.assertEqual(len(matches), 0)
pattern_str, text_str = pattern.decode("ascii"), text.decode("ascii")
matches = gst.match(pattern_str, '', text_str, '', len(pattern_str))
self.assertIsInstance(matches, list)
self.assertEqual(len(matches), 0)
class Test2EdgeCases(TestCase):
def test_edge_cases(self):
for a, b in EDGE_CASES:
matches = gst.match(a, '', b, '', 1)
self.assertGreater(len(matches), 0)
@strategies.composite
def tuples_of_text_and_substring(draw, text_min_size=0, text_max_size=100, alphabet=string.printable):
text = draw(strategies.text(
alphabet=alphabet,
min_size=text_min_size,
max_size=text_max_size))
pattern_begin = draw(strategies.integers(
min_value=0,
max_value=len(text)))
pattern_end = draw(strategies.integers(
min_value=pattern_begin,
max_value=len(text)))
pattern = text[pattern_begin:pattern_end]
return text, pattern
@strategies.composite
def text_substring_and_minmatch_length(draw, **kwargs):
text, pattern = draw(tuples_of_text_and_substring(**kwargs))
min_match_length = draw(strategies.integers(
min_value=min(1, len(pattern)),
max_value=min(100, len(pattern))))
return text, pattern, min_match_length
class Test2RandomInputShortStrings(TestCase):
@settings(max_examples=300)
@given(text_and_pattern=tuples_of_text_and_substring())
def test1_full_match(self, text_and_pattern):
text, pattern = text_and_pattern
matches = gst.match(pattern, '', text, '', len(pattern))
for match in matches:
self.assertCorrectMatchSubstringMapping(pattern, text, match)
@settings(max_examples=300)
@given(text_and_pattern=tuples_of_text_and_substring())
def test2_full_match_bytes(self, text_and_pattern):
text, pattern = text_and_pattern
pattern_bytes, text_bytes = pattern.encode("ascii"), text.encode("ascii")
matches = gst.match(pattern_bytes, '', text_bytes, '', len(pattern_bytes))
for match in matches:
self.assertCorrectMatchSubstringMapping(pattern, text, match)
class Test3RandomInputLongStrings(TestCase):
@settings(max_examples=10)
@given(text_and_pattern=tuples_of_text_and_substring(text_min_size=100, text_max_size=2000))
def test1_full_match(self, text_and_pattern):
text, pattern = text_and_pattern
matches = gst.match(pattern, '', text, '', len(pattern))
for match in matches:
self.assertCorrectMatchSubstringMapping(pattern, text, match)
@settings(max_examples=10)
@given(text_and_pattern=tuples_of_text_and_substring(text_min_size=100, text_max_size=2000))
def test2_full_match_bytes(self, text_and_pattern):
text, pattern = text_and_pattern
pattern_bytes, text_bytes = pattern.encode("ascii"), text.encode("ascii")
matches = gst.match(pattern_bytes, '', text_bytes, '', len(pattern_bytes))
for match in matches:
self.assertCorrectMatchSubstringMapping(pattern, text, match)
if __name__ == "__main__":
unittest.main(verbosity=2)