Guido van Rossum | 876736c | 1997-06-03 18:07:49 +0000 | [diff] [blame] | 1 | |
| 2 | # Regex test suite and benchmark suite v1.5a2 |
| 3 | # Due to the use of r"aw" strings, this file will |
| 4 | # only work with Python 1.5 or higher. |
| 5 | |
| 6 | # The 3 possible outcomes for each pattern |
| 7 | [SUCCEED, FAIL, SYNTAX_ERROR] = range(3) |
| 8 | |
| 9 | # Benchmark suite (needs expansion) |
| 10 | # |
| 11 | # The benchmark suite does not test correctness, just speed. The |
| 12 | # first element of each tuple is the regex pattern; the second is a |
| 13 | # string to match it against. The benchmarking code will embed the |
| 14 | # second string inside several sizes of padding, to test how regex |
| 15 | # matching performs on large strings. |
| 16 | |
| 17 | benchmarks = [ |
| 18 | ('Python', 'Python'), # Simple text literal |
| 19 | ('.*Python', 'Python'), # Bad text literal |
| 20 | ('.*Python.*', 'Python'), # Worse text literal |
| 21 | ('.*\\(Python\\)', 'Python'), # Bad text literal with grouping |
| 22 | |
| 23 | ('(Python\\|Perl\\|Tcl', 'Perl'), # Alternation |
| 24 | ('\\(Python\\|Perl\\|Tcl\\)', 'Perl'), # Grouped alternation |
| 25 | ('\\(Python\\)\\1', 'PythonPython'), # Backreference |
| 26 | # ('\\([0a-z][a-z]*,\\)+', 'a5,b7,c9,'), # Disable the fastmap optimization |
| 27 | ('\\([a-z][a-z0-9]*,\\)+', 'a5,b7,c9,') # A few sets |
| 28 | ] |
| 29 | |
| 30 | # Test suite (for verifying correctness) |
| 31 | # |
| 32 | # The test suite is a list of 5- or 3-tuples. The 5 parts of a |
| 33 | # complete tuple are: |
| 34 | # element 0: a string containing the pattern |
| 35 | # 1: the string to match against the pattern |
| 36 | # 2: the expected result (SUCCEED, FAIL, SYNTAX_ERROR) |
| 37 | # 3: a string that will be eval()'ed to produce a test string. |
| 38 | # This is an arbitrary Python expression; the available |
| 39 | # variables are "found" (the whole match), and "g1", "g2", ... |
| 40 | # up to "g10" contain the contents of each group, or the |
| 41 | # string 'None' if the group wasn't given a value. |
| 42 | # 4: The expected result of evaluating the expression. |
| 43 | # If the two don't match, an error is reported. |
| 44 | # |
| 45 | # If the regex isn't expected to work, the latter two elements can be omitted. |
| 46 | |
| 47 | tests = [ |
| 48 | ('abc', 'abc', SUCCEED, |
| 49 | 'found', 'abc'), |
| 50 | ('abc', 'xbc', FAIL), |
| 51 | ('abc', 'axc', FAIL), |
| 52 | ('abc', 'abx', FAIL), |
| 53 | ('abc', 'xabcy', SUCCEED, |
| 54 | 'found', 'abc'), |
| 55 | ('abc', 'ababc', SUCCEED, |
| 56 | 'found', 'abc'), |
| 57 | ('ab*c', 'abc', SUCCEED, |
| 58 | 'found', 'abc'), |
| 59 | ('ab*bc', 'abc', SUCCEED, |
| 60 | 'found', 'abc'), |
| 61 | ('ab*bc', 'abbc', SUCCEED, |
| 62 | 'found', 'abbc'), |
| 63 | ('ab*bc', 'abbbbc', SUCCEED, |
| 64 | 'found', 'abbbbc'), |
| 65 | ('ab+bc', 'abbc', SUCCEED, |
| 66 | 'found', 'abbc'), |
| 67 | ('ab+bc', 'abc', FAIL), |
| 68 | ('ab+bc', 'abq', FAIL), |
| 69 | ('ab+bc', 'abbbbc', SUCCEED, |
| 70 | 'found', 'abbbbc'), |
| 71 | ('ab?bc', 'abbc', SUCCEED, |
| 72 | 'found', 'abbc'), |
| 73 | ('ab?bc', 'abc', SUCCEED, |
| 74 | 'found', 'abc'), |
| 75 | ('ab?bc', 'abbbbc', FAIL), |
| 76 | ('ab?c', 'abc', SUCCEED, |
| 77 | 'found', 'abc'), |
| 78 | ('^abc$', 'abc', SUCCEED, |
| 79 | 'found', 'abc'), |
| 80 | ('^abc$', 'abcc', FAIL), |
| 81 | ('^abc', 'abcc', SUCCEED, |
| 82 | 'found', 'abc'), |
| 83 | ('^abc$', 'aabc', FAIL), |
| 84 | ('abc$', 'aabc', SUCCEED, |
| 85 | 'found', 'abc'), |
| 86 | ('^', 'abc', SUCCEED, |
| 87 | 'found+"-"', '-'), |
| 88 | ('$', 'abc', SUCCEED, |
| 89 | 'found+"-"', '-'), |
| 90 | ('a.c', 'abc', SUCCEED, |
| 91 | 'found', 'abc'), |
| 92 | ('a.c', 'axc', SUCCEED, |
| 93 | 'found', 'axc'), |
| 94 | ('a.*c', 'axyzc', SUCCEED, |
| 95 | 'found', 'axyzc'), |
| 96 | ('a.*c', 'axyzd', FAIL), |
| 97 | ('a[bc]d', 'abc', FAIL), |
| 98 | ('a[bc]d', 'abd', SUCCEED, |
| 99 | 'found', 'abd'), |
| 100 | ('a[b-d]e', 'abd', FAIL), |
| 101 | ('a[b-d]e', 'ace', SUCCEED, |
| 102 | 'found', 'ace'), |
| 103 | ('a[b-d]', 'aac', SUCCEED, |
| 104 | 'found', 'ac'), |
| 105 | ('a[-b]', 'a-', SUCCEED, |
| 106 | 'found', 'a-'), |
| 107 | ('a[b-]', 'a-', SUCCEED, |
| 108 | 'found', 'a-'), |
| 109 | ('a[]b', '-', SYNTAX_ERROR), |
| 110 | ('a[', '-', SYNTAX_ERROR), |
| 111 | ('a\\', '-', SYNTAX_ERROR), |
| 112 | ('abc\\)', '-', SYNTAX_ERROR), |
| 113 | ('\\(abc', '-', SYNTAX_ERROR), |
| 114 | ('a]', 'a]', SUCCEED, |
| 115 | 'found', 'a]'), |
| 116 | ('a[]]b', 'a]b', SUCCEED, |
| 117 | 'found', 'a]b'), |
| 118 | ('a[^bc]d', 'aed', SUCCEED, |
| 119 | 'found', 'aed'), |
| 120 | ('a[^bc]d', 'abd', FAIL), |
| 121 | ('a[^-b]c', 'adc', SUCCEED, |
| 122 | 'found', 'adc'), |
| 123 | ('a[^-b]c', 'a-c', FAIL), |
| 124 | ('a[^]b]c', 'a]c', FAIL), |
| 125 | ('a[^]b]c', 'adc', SUCCEED, |
| 126 | 'found', 'adc'), |
| 127 | ('\\ba\\b', 'a-', SUCCEED, |
| 128 | '"-"', '-'), |
| 129 | ('\\ba\\b', '-a', SUCCEED, |
| 130 | '"-"', '-'), |
| 131 | ('\\ba\\b', '-a-', SUCCEED, |
| 132 | '"-"', '-'), |
| 133 | ('\\by\\b', 'xy', FAIL), |
| 134 | ('\\by\\b', 'yz', FAIL), |
| 135 | ('\\by\\b', 'xyz', FAIL), |
| 136 | ('ab\\|cd', 'abc', SUCCEED, |
| 137 | 'found', 'ab'), |
| 138 | ('ab\\|cd', 'abcd', SUCCEED, |
| 139 | 'found', 'ab'), |
| 140 | ('\\(\\)ef', 'def', SUCCEED, |
| 141 | 'found+"-"+g1', 'ef-'), |
| 142 | ('$b', 'b', FAIL), |
| 143 | ('a(b', 'a(b', SUCCEED, |
| 144 | 'found+"-"+g1', 'a(b-None'), |
| 145 | ('a(*b', 'ab', SUCCEED, |
| 146 | 'found', 'ab'), |
| 147 | ('a(*b', 'a((b', SUCCEED, |
| 148 | 'found', 'a((b'), |
| 149 | ('a\\\\b', 'a\\b', SUCCEED, |
| 150 | 'found', 'a\\b'), |
| 151 | ('\\(\\(a\\)\\)', 'abc', SUCCEED, |
| 152 | 'found+"-"+g1+"-"+g2', 'a-a-a'), |
| 153 | ('\\(a\\)b\\(c\\)', 'abc', SUCCEED, |
| 154 | 'found+"-"+g1+"-"+g2', 'abc-a-c'), |
| 155 | ('a+b+c', 'aabbabc', SUCCEED, |
| 156 | 'found', 'abc'), |
| 157 | ('\\(a+\\|b\\)*', 'ab', SUCCEED, |
| 158 | 'found+"-"+g1', 'ab-b'), |
| 159 | ('\\(a+\\|b\\)+', 'ab', SUCCEED, |
| 160 | 'found+"-"+g1', 'ab-b'), |
| 161 | ('\\(a+\\|b\\)?', 'ab', SUCCEED, |
| 162 | 'found+"-"+g1', 'a-a'), |
| 163 | ('\\)\\(', '-', SYNTAX_ERROR), |
| 164 | ('[^ab]*', 'cde', SUCCEED, |
| 165 | 'found', 'cde'), |
| 166 | ('abc', '', FAIL), |
| 167 | ('a*', '', SUCCEED, |
| 168 | 'found', ''), |
| 169 | ('a\\|b\\|c\\|d\\|e', 'e', SUCCEED, |
| 170 | 'found', 'e'), |
| 171 | ('\\(a\\|b\\|c\\|d\\|e\\)f', 'ef', SUCCEED, |
| 172 | 'found+"-"+g1', 'ef-e'), |
| 173 | ('abcd*efg', 'abcdefg', SUCCEED, |
| 174 | 'found', 'abcdefg'), |
| 175 | ('ab*', 'xabyabbbz', SUCCEED, |
| 176 | 'found', 'ab'), |
| 177 | ('ab*', 'xayabbbz', SUCCEED, |
| 178 | 'found', 'a'), |
| 179 | ('\\(ab\\|cd\\)e', 'abcde', SUCCEED, |
| 180 | 'found+"-"+g1', 'cde-cd'), |
| 181 | ('[abhgefdc]ij', 'hij', SUCCEED, |
| 182 | 'found', 'hij'), |
| 183 | ('^\\(ab\\|cd\\)e', 'abcde', FAIL, |
| 184 | 'xg1y', 'xy'), |
| 185 | ('\\(abc\\|\\)ef', 'abcdef', SUCCEED, |
| 186 | 'found+"-"+g1', 'ef-'), |
| 187 | ('\\(a\\|b\\)c*d', 'abcd', SUCCEED, |
| 188 | 'found+"-"+g1', 'bcd-b'), |
| 189 | ('\\(ab\\|ab*\\)bc', 'abc', SUCCEED, |
| 190 | 'found+"-"+g1', 'abc-a'), |
| 191 | ('a\\([bc]*\\)c*', 'abc', SUCCEED, |
| 192 | 'found+"-"+g1', 'abc-bc'), |
| 193 | ('a\\([bc]*\\)\\(c*d\\)', 'abcd', SUCCEED, |
| 194 | 'found+"-"+g1+"-"+g2', 'abcd-bc-d'), |
| 195 | ('a\\([bc]+\\)\\(c*d\\)', 'abcd', SUCCEED, |
| 196 | 'found+"-"+g1+"-"+g2', 'abcd-bc-d'), |
| 197 | ('a\\([bc]*\\)\\(c+d\\)', 'abcd', SUCCEED, |
| 198 | 'found+"-"+g1+"-"+g2', 'abcd-b-cd'), |
| 199 | ('a[bcd]*dcdcde', 'adcdcde', SUCCEED, |
| 200 | 'found', 'adcdcde'), |
| 201 | ('a[bcd]+dcdcde', 'adcdcde', FAIL), |
| 202 | ('\\(ab\\|a\\)b*c', 'abc', SUCCEED, |
| 203 | 'found+"-"+g1', 'abc-ab'), |
| 204 | ('\\(\\(a\\)\\(b\\)c\\)\\(d\\)', 'abcd', SUCCEED, |
| 205 | 'g1+"-"+g2+"-"+g3+"-"+g4', 'abc-a-b-d'), |
| 206 | ('[a-zA-Z_][a-zA-Z0-9_]*', 'alpha', SUCCEED, |
| 207 | 'found', 'alpha'), |
| 208 | ('^a\\(bc+\\|b[eh]\\)g\\|.h$', 'abh', SUCCEED, |
| 209 | 'found+"-"+g1', 'bh-None'), |
| 210 | ('\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'effgz', SUCCEED, |
| 211 | 'found+"-"+g1+"-"+g2', 'effgz-effgz-None'), |
| 212 | ('\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'ij', SUCCEED, |
| 213 | 'found+"-"+g1+"-"+g2', 'ij-ij-j'), |
| 214 | ('\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'effg', FAIL), |
| 215 | ('\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'bcdd', FAIL), |
| 216 | ('\\(bc+d$\\|ef*g.\\|h?i\\(j\\|k\\)\\)', 'reffgz', SUCCEED, |
| 217 | 'found+"-"+g1+"-"+g2', 'effgz-effgz-None'), |
| 218 | ('\\(\\(\\(\\(\\(\\(\\(\\(\\(a\\)\\)\\)\\)\\)\\)\\)\\)\\)', 'a', SUCCEED, |
| 219 | 'found', 'a'), |
| 220 | ('multiple words of text', 'uh-uh', FAIL), |
| 221 | ('multiple words', 'multiple words, yeah', SUCCEED, |
| 222 | 'found', 'multiple words'), |
| 223 | ('\\(.*\\)c\\(.*\\)', 'abcde', SUCCEED, |
| 224 | 'found+"-"+g1+"-"+g2', 'abcde-ab-de'), |
| 225 | ('(\\(.*\\), \\(.*\\))', '(a, b)', SUCCEED, |
| 226 | 'g2+"-"+g1', 'b-a'), |
| 227 | ('[k]', 'ab', FAIL), |
| 228 | ('a[-]?c', 'ac', SUCCEED, |
| 229 | 'found', 'ac'), |
| 230 | ('\\(abc\\)\\1', 'abcabc', SUCCEED, |
| 231 | 'g1', 'abc'), |
| 232 | ('\\([a-c]*\\)\\1', 'abcabc', SUCCEED, |
| 233 | 'g1', 'abc'), |
| 234 | ('^\\(.+\\)?B', 'AB', SUCCEED, |
| 235 | 'g1', 'A'), |
| 236 | ('\\(a+\\).\\1$', 'aaaaa', SUCCEED, |
| 237 | 'found+"-"+g1', 'aaaaa-aa'), |
| 238 | ('^\\(a+\\).\\1$', 'aaaa', FAIL), |
| 239 | ('\\(abc\\)\\1', 'abcabc', SUCCEED, |
| 240 | 'found+"-"+g1', 'abcabc-abc'), |
| 241 | ('\\([a-c]+\\)\\1', 'abcabc', SUCCEED, |
| 242 | 'found+"-"+g1', 'abcabc-abc'), |
| 243 | ('\\(a\\)\\1', 'aa', SUCCEED, |
| 244 | 'found+"-"+g1', 'aa-a'), |
| 245 | ('\\(a+\\)\\1', 'aa', SUCCEED, |
| 246 | 'found+"-"+g1', 'aa-a'), |
| 247 | ('\\(a+\\)+\\1', 'aa', SUCCEED, |
| 248 | 'found+"-"+g1', 'aa-a'), |
| 249 | ('\\(a\\).+\\1', 'aba', SUCCEED, |
| 250 | 'found+"-"+g1', 'aba-a'), |
| 251 | ('\\(a\\)ba*\\1', 'aba', SUCCEED, |
| 252 | 'found+"-"+g1', 'aba-a'), |
| 253 | ('\\(aa\\|a\\)a\\1$', 'aaa', SUCCEED, |
| 254 | 'found+"-"+g1', 'aaa-a'), |
| 255 | ('\\(a\\|aa\\)a\\1$', 'aaa', SUCCEED, |
| 256 | 'found+"-"+g1', 'aaa-a'), |
| 257 | ('\\(a+\\)a\\1$', 'aaa', SUCCEED, |
| 258 | 'found+"-"+g1', 'aaa-a'), |
| 259 | ('\\([abc]*\\)\\1', 'abcabc', SUCCEED, |
| 260 | 'found+"-"+g1', 'abcabc-abc'), |
| 261 | ('\\(a\\)\\(b\\)c\\|ab', 'ab', SUCCEED, |
| 262 | 'found+"-"+g1+"-"+g2', 'ab-None-None'), |
| 263 | ('\\(a\\)+x', 'aaax', SUCCEED, |
| 264 | 'found+"-"+g1', 'aaax-a'), |
| 265 | ('\\([ac]\\)+x', 'aacx', SUCCEED, |
| 266 | 'found+"-"+g1', 'aacx-c'), |
| 267 | ('\\([^/]*/\\)*sub1/', 'd:msgs/tdir/sub1/trial/away.cpp', SUCCEED, |
| 268 | 'found+"-"+g1', 'd:msgs/tdir/sub1/-tdir/'), |
| 269 | ('\\([^.]*\\)\\.\\([^:]*\\):[T ]+\\(.*\\)', 'track1.title:TBlah blah blah', SUCCEED, |
| 270 | 'found+"-"+g1+"-"+g2+"-"+g3', 'track1.title:TBlah blah blah-track1-title-Blah blah blah'), |
| 271 | ('\\([^N]*N\\)+', 'abNNxyzN', SUCCEED, |
| 272 | 'found+"-"+g1', 'abNNxyzN-xyzN'), |
| 273 | ('\\([^N]*N\\)+', 'abNNxyz', SUCCEED, |
| 274 | 'found+"-"+g1', 'abNN-N'), |
| 275 | ('\\([abc]*\\)x', 'abcx', SUCCEED, |
| 276 | 'found+"-"+g1', 'abcx-abc'), |
| 277 | ('\\([abc]*\\)x', 'abc', FAIL), |
| 278 | ('\\([xyz]*\\)x', 'abcx', SUCCEED, |
| 279 | 'found+"-"+g1', 'x-'), |
| 280 | ('\\(a\\)+b\\|aac', 'aac', SUCCEED, |
Guido van Rossum | 95e8053 | 1997-08-13 22:34:14 +0000 | [diff] [blame] | 281 | 'found+"-"+g1', 'aac-None'), |
| 282 | ('\<a', 'a', SUCCEED, 'found', 'a'), |
| 283 | ('\<a', '!', FAIL), |
| 284 | ('a\<b', 'ab', FAIL), |
| 285 | ('a\>', 'ab', FAIL), |
| 286 | ('a\>', 'a!', SUCCEED, 'found', 'a'), |
| 287 | ('a\>', 'a', SUCCEED, 'found', 'a'), |
Guido van Rossum | 876736c | 1997-06-03 18:07:49 +0000 | [diff] [blame] | 288 | ] |
| 289 | |