-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathregex_test.py
executable file
·77 lines (59 loc) · 1.65 KB
/
regex_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
#!/usr/bin/env python
"""
This program generates random text that matches a given regex-pattern.
The pattern is given via sys.argv and the generated text is passed to
the binary 'bin/test_rand' to check if the generated text also matches
the regex-pattern in the C implementation.
The exit-code of the testing program, is used to determine test success.
This script is called by the Makefile when doing 'make test'
"""
import re
import sys
import exrex
from subprocess import call
prog = "./bin/test_rand"
if len(sys.argv) < 2:
print("")
print("usage: %s pattern [nrepeat]" % sys.argv[0])
print(" where [nrepeat] is optional")
print("")
sys.exit(-1)
own_prog = sys.argv[0]
pattern = sys.argv[1]
if len(sys.argv) > 2:
ntests = int(sys.argv[2])
else:
ntests = 10
nfails = 0
repeats = ntests
try:
repeats = int(sys.argv[2])
except:
pass
r = 50
while r < 0:
try:
g = exrex.generate(pattern)
break
except:
pass
sys.stdout.write("%-35s" % (" pattern '%s': " % pattern))
while repeats >= 0:
try:
repeats -= 1
example = exrex.getone(pattern)
#print("%s %s %s" % (prog, pattern, example))
ret = call([prog, "\"%s\"" % pattern, "\"%s\"" % example])
if ret != 0:
escaped = repr(example) # escapes special chars for better printing
print(" FAIL : doesn't match %s as expected [%s]." % (escaped, ", ".join([("0x%02x" % ord(e)) for e in example]) ))
nfails += 1
except:
#import traceback
#print("EXCEPTION!")
#raw_input(traceback.format_exc())
ntests -= 1
repeats += 1
#nfails += 1
sys.stdout.write("%4d/%d tests succeeded \n" % (ntests - nfails, ntests))
#print("")