Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / lib / python2.4 / test / test_regex.py
CommitLineData
920dae64
AT
1from test.test_support import verbose, sortdict
2import warnings
3warnings.filterwarnings("ignore", "the regex module is deprecated",
4 DeprecationWarning, __name__)
5import regex
6from regex_syntax import *
7
8re = 'a+b+c+'
9print 'no match:', regex.match(re, 'hello aaaabcccc world')
10print 'successful search:', regex.search(re, 'hello aaaabcccc world')
11try:
12 cre = regex.compile('\(' + re)
13except regex.error:
14 print 'caught expected exception'
15else:
16 print 'expected regex.error not raised'
17
18print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb')
19prev = regex.set_syntax(RE_SYNTAX_AWK)
20print 'successful awk syntax:', regex.search('(a+)|(b+)', 'cdb')
21regex.set_syntax(prev)
22print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb')
23
24re = '\(<one>[0-9]+\) *\(<two>[0-9]+\)'
25print 'matching with group names and compile()'
26cre = regex.compile(re)
27print cre.match('801 999')
28try:
29 print cre.group('one')
30except regex.error:
31 print 'caught expected exception'
32else:
33 print 'expected regex.error not raised'
34
35print 'matching with group names and symcomp()'
36cre = regex.symcomp(re)
37print cre.match('801 999')
38print cre.group(0)
39print cre.group('one')
40print cre.group(1, 2)
41print cre.group('one', 'two')
42print 'realpat:', cre.realpat
43print 'groupindex:', sortdict(cre.groupindex)
44
45re = 'world'
46cre = regex.compile(re)
47print 'not case folded search:', cre.search('HELLO WORLD')
48cre = regex.compile(re, regex.casefold)
49print 'case folded search:', cre.search('HELLO WORLD')
50
51print '__members__:', cre.__members__
52print 'regs:', cre.regs
53print 'last:', cre.last
54print 'translate:', len(cre.translate)
55print 'givenpat:', cre.givenpat
56
57print 'match with pos:', cre.match('hello world', 7)
58print 'search with pos:', cre.search('hello world there world', 7)
59print 'bogus group:', cre.group(0, 1, 3)
60try:
61 print 'no name:', cre.group('one')
62except regex.error:
63 print 'caught expected exception'
64else:
65 print 'expected regex.error not raised'
66
67from regex_tests import *
68if verbose: print 'Running regex_tests test suite'
69
70for t in tests:
71 pattern=s=outcome=repl=expected=None
72 if len(t)==5:
73 pattern, s, outcome, repl, expected = t
74 elif len(t)==3:
75 pattern, s, outcome = t
76 else:
77 raise ValueError, ('Test tuples should have 3 or 5 fields',t)
78
79 try:
80 obj=regex.compile(pattern)
81 except regex.error:
82 if outcome==SYNTAX_ERROR: pass # Expected a syntax error
83 else:
84 # Regex syntax errors aren't yet reported, so for
85 # the official test suite they'll be quietly ignored.
86 pass
87 #print '=== Syntax error:', t
88 else:
89 try:
90 result=obj.search(s)
91 except regex.error, msg:
92 print '=== Unexpected exception', t, repr(msg)
93 if outcome==SYNTAX_ERROR:
94 # This should have been a syntax error; forget it.
95 pass
96 elif outcome==FAIL:
97 if result==-1: pass # No match, as expected
98 else: print '=== Succeeded incorrectly', t
99 elif outcome==SUCCEED:
100 if result!=-1:
101 # Matched, as expected, so now we compute the
102 # result string and compare it to our expected result.
103 start, end = obj.regs[0]
104 found=s[start:end]
105 groups=obj.group(1,2,3,4,5,6,7,8,9,10)
106 vardict=vars()
107 for i in range(len(groups)):
108 vardict['g'+str(i+1)]=str(groups[i])
109 repl=eval(repl)
110 if repl!=expected:
111 print '=== grouping error', t, repr(repl)+' should be '+repr(expected)
112 else:
113 print '=== Failed incorrectly', t