pybind11/example/run_test.py

61 lines
1.9 KiB
Python
Raw Normal View History

2015-10-01 15:34:26 +00:00
import sys
import os
import re
import subprocess
remove_unicode_marker = re.compile(r'u(\'[^\']*\')')
remove_long_marker = re.compile(r'([0-9])L')
2015-10-13 21:58:10 +00:00
remove_hex = re.compile(r'0x[0-9a-fA-F]+')
2015-10-01 15:34:26 +00:00
shorten_floats = re.compile(r'([1-9][0-9]*\.[0-9]{4})[0-9]*')
def sanitize(lines):
lines = lines.split('\n')
for i in range(len(lines)):
line = lines[i]
if line.startswith(" |"):
line = ""
line = remove_unicode_marker.sub(r'\1', line)
line = remove_long_marker.sub(r'\1', line)
line = remove_hex.sub(r'0', line)
2015-10-01 15:34:26 +00:00
line = shorten_floats.sub(r'\1', line)
line = line.replace('__builtin__', 'builtins')
line = line.replace('example.', '')
line = line.replace('method of builtins.PyCapsule instance', '')
line = line.strip()
2015-10-13 21:58:10 +00:00
if sys.platform == 'win32':
lower = line.lower()
# The precise pattern of allocations and deallocations is dependent on the compiler
# and optimization level, so we unfortunately can't reliably check it in this kind of test case
2015-10-18 15:04:24 +00:00
if 'constructor' in lower or 'destructor' in lower \
or 'ref' in lower or 'freeing' in lower:
2015-10-13 21:58:10 +00:00
line = ""
2015-10-01 15:34:26 +00:00
lines[i] = line
lines = '\n'.join(sorted([l for l in lines if l != ""]))
print('==================')
print(lines)
return lines
2015-07-05 18:05:44 +00:00
path = os.path.dirname(__file__)
if path != '':
os.chdir(path)
name = sys.argv[1]
2015-10-18 15:04:24 +00:00
output_bytes = subprocess.check_output([sys.executable, name + ".py"],
stderr=subprocess.STDOUT)
2015-10-01 15:34:26 +00:00
output = sanitize(output_bytes.decode('utf-8'))
reference = sanitize(open(name + '.ref', 'r').read())
2015-07-05 18:05:44 +00:00
2015-10-18 15:04:24 +00:00
if 'NumPy missing' in output:
print('Test "%s" could not be run.' % name)
exit(0)
elif output == reference:
2015-07-05 18:05:44 +00:00
print('Test "%s" succeeded.' % name)
exit(0)
else:
print('Test "%s" FAILED!' % name)
exit(-1)