blob: d0967c6e2eb06612724b867b591d72888bf96f57 [file] [log] [blame]
Wenzel Jakob6d6fd092015-10-01 17:34:26 +02001import sys
2import os
3import re
4import subprocess
5
6remove_unicode_marker = re.compile(r'u(\'[^\']*\')')
7remove_long_marker = re.compile(r'([0-9])L')
Wenzel Jakob607654f2015-10-13 23:58:10 +02008remove_hex = re.compile(r'0x[0-9a-fA-F]+')
Wenzel Jakob6d6fd092015-10-01 17:34:26 +02009shorten_floats = re.compile(r'([1-9][0-9]*\.[0-9]{4})[0-9]*')
10
Wenzel Jakob80c24512016-02-20 20:53:10 +010011relaxed = False
Wenzel Jakob6d6fd092015-10-01 17:34:26 +020012
13def sanitize(lines):
14 lines = lines.split('\n')
15 for i in range(len(lines)):
16 line = lines[i]
17 if line.startswith(" |"):
18 line = ""
19 line = remove_unicode_marker.sub(r'\1', line)
20 line = remove_long_marker.sub(r'\1', line)
Wenzel Jakob3b806d42015-10-11 16:29:35 +020021 line = remove_hex.sub(r'0', line)
Wenzel Jakob6d6fd092015-10-01 17:34:26 +020022 line = shorten_floats.sub(r'\1', line)
23 line = line.replace('__builtin__', 'builtins')
24 line = line.replace('example.', '')
Wenzel Jakob27e8e102016-01-17 22:36:37 +010025 line = line.replace('unicode', 'str')
Wenzel Jakob678d7872016-01-17 22:36:41 +010026 line = line.replace('Example4.EMode', 'EMode')
27 line = line.replace('example.EMode', 'EMode')
Wenzel Jakob6d6fd092015-10-01 17:34:26 +020028 line = line.replace('method of builtins.PyCapsule instance', '')
29 line = line.strip()
Wenzel Jakob80c24512016-02-20 20:53:10 +010030 if relaxed:
Wenzel Jakob607654f2015-10-13 23:58:10 +020031 lower = line.lower()
Wenzel Jakob76269b72015-12-13 11:33:41 +010032 # The precise pattern of allocations and deallocations is dependent on the compiler
33 # and optimization level, so we unfortunately can't reliably check it in this kind of test case
Wenzel Jakobfab881c2015-10-18 17:04:24 +020034 if 'constructor' in lower or 'destructor' in lower \
Wenzel Jakob76269b72015-12-13 11:33:41 +010035 or 'ref' in lower or 'freeing' in lower:
Wenzel Jakob607654f2015-10-13 23:58:10 +020036 line = ""
Wenzel Jakob6d6fd092015-10-01 17:34:26 +020037 lines[i] = line
38
39 lines = '\n'.join(sorted([l for l in lines if l != ""]))
40
41 print('==================')
42 print(lines)
43 return lines
Wenzel Jakob38bd7112015-07-05 20:05:44 +020044
45path = os.path.dirname(__file__)
46if path != '':
47 os.chdir(path)
48
Wenzel Jakob80c24512016-02-20 20:53:10 +010049if len(sys.argv) < 2:
Wenzel Jakob57b52792016-02-20 21:19:30 +010050 print("Syntax: %s [--relaxed] <test name>" % sys.argv[0])
Wenzel Jakob80c24512016-02-20 20:53:10 +010051 exit(0)
52
Wenzel Jakob57b52792016-02-20 21:19:30 +010053if len(sys.argv) == 3 and sys.argv[1] == '--relaxed':
54 del sys.argv[1]
Wenzel Jakob80c24512016-02-20 20:53:10 +010055 relaxed = True
56
Wenzel Jakob38bd7112015-07-05 20:05:44 +020057name = sys.argv[1]
Wenzel Jakobfab881c2015-10-18 17:04:24 +020058output_bytes = subprocess.check_output([sys.executable, name + ".py"],
59 stderr=subprocess.STDOUT)
60
Wenzel Jakob6d6fd092015-10-01 17:34:26 +020061output = sanitize(output_bytes.decode('utf-8'))
62reference = sanitize(open(name + '.ref', 'r').read())
Wenzel Jakob38bd7112015-07-05 20:05:44 +020063
Wenzel Jakobfab881c2015-10-18 17:04:24 +020064if 'NumPy missing' in output:
65 print('Test "%s" could not be run.' % name)
66 exit(0)
67elif output == reference:
Wenzel Jakob38bd7112015-07-05 20:05:44 +020068 print('Test "%s" succeeded.' % name)
69 exit(0)
70else:
71 print('Test "%s" FAILED!' % name)
72 exit(-1)