You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.4 KiB

  1. import sys
  2. import os
  3. import re
  4. import subprocess
  5. import difflib
  6. remove_unicode_marker = re.compile(r'u(\'[^\']*\')')
  7. remove_long_marker = re.compile(r'([0-9])L')
  8. remove_hex = re.compile(r'0x[0-9a-fA-F]+')
  9. shorten_floats = re.compile(r'([1-9][0-9]*\.[0-9]{4})[0-9]*')
  10. relaxed = False
  11. def sanitize(lines):
  12. lines = lines.split('\n')
  13. for i in range(len(lines)):
  14. line = lines[i]
  15. if line.startswith(" |"):
  16. line = ""
  17. line = remove_unicode_marker.sub(r'\1', line)
  18. line = remove_long_marker.sub(r'\1', line)
  19. line = remove_hex.sub(r'0', line)
  20. line = shorten_floats.sub(r'\1', line)
  21. line = line.replace('__builtin__', 'builtins')
  22. line = line.replace('example.', '')
  23. line = line.replace('unicode', 'str')
  24. line = line.replace('Example4.EMode', 'EMode')
  25. line = line.replace('example.EMode', 'EMode')
  26. line = line.replace('method of builtins.PyCapsule instance', '')
  27. line = line.strip()
  28. if relaxed:
  29. lower = line.lower()
  30. # The precise pattern of allocations and deallocations is dependent on the compiler
  31. # and optimization level, so we unfortunately can't reliably check it in this kind of test case
  32. if 'constructor' in lower or 'destructor' in lower \
  33. or 'ref' in lower or 'freeing' in lower:
  34. line = ""
  35. lines[i] = line
  36. return '\n'.join(sorted([l for l in lines if l != ""]))
  37. path = os.path.dirname(__file__)
  38. if path != '':
  39. os.chdir(path)
  40. if len(sys.argv) < 2:
  41. print("Syntax: %s [--relaxed] <test name>" % sys.argv[0])
  42. exit(0)
  43. if len(sys.argv) == 3 and sys.argv[1] == '--relaxed':
  44. del sys.argv[1]
  45. relaxed = True
  46. name = sys.argv[1]
  47. output_bytes = subprocess.check_output([sys.executable, name + ".py"],
  48. stderr=subprocess.STDOUT)
  49. output = sanitize(output_bytes.decode('utf-8'))
  50. reference = sanitize(open(name + '.ref', 'r').read())
  51. if 'NumPy missing' in output:
  52. print('Test "%s" could not be run.' % name)
  53. exit(0)
  54. elif output == reference:
  55. print('Test "%s" succeeded.' % name)
  56. exit(0)
  57. else:
  58. print('Test "%s" FAILED!' % name)
  59. print('--- output')
  60. print('+++ reference')
  61. print(''.join(difflib.ndiff(output.splitlines(keepends=True),
  62. reference.splitlines(keepends=True))))
  63. exit(-1)