compiler/glsl/tests: Make tests python3 safe
[mesa.git] / src / compiler / glsl / tests / optimization_test.py
1 #!/usr/bin/env python
2 # encoding=utf-8
3 # Copyright © 2018 Intel Corporation
4
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22
23 """Script to generate and run glsl optimization tests."""
24
25 from __future__ import print_function
26 import argparse
27 import difflib
28 import subprocess
29 import sys
30
31 import sexps
32 import lower_jump_cases
33
34
35 def arg_parser():
36 parser = argparse.ArgumentParser()
37 parser.add_argument(
38 '--test-runner',
39 required=True,
40 help='The glsl_test binary.')
41 return parser.parse_args()
42
43
44 def compare(actual, expected):
45 """Compare the s-expresions and return a diff if they are different."""
46 actual = sexps.sort_decls(sexps.parse_sexp(actual))
47 expected = sexps.sort_decls(sexps.parse_sexp(expected))
48
49 if actual == expected:
50 return None
51
52 actual = sexps.sexp_to_string(actual)
53 expected = sexps.sexp_to_string(expected)
54
55 return difflib.unified_diff(expected.splitlines(), actual.splitlines())
56
57
58 def main():
59 """Generate each test and report pass or fail."""
60 args = arg_parser()
61
62 total = 0
63 passes = 0
64
65 for gen in lower_jump_cases.CASES:
66 for name, opt, source, expected in gen():
67 total += 1
68 print('{}: '.format(name), end='')
69 proc = subprocess.Popen(
70 [args.test_runner, 'optpass', '--quiet', '--input-ir', opt],
71 stdout=subprocess.PIPE,
72 stderr=subprocess.PIPE,
73 stdin=subprocess.PIPE)
74 out, err = proc.communicate(source.encode('utf-8'))
75 out = out.decode('utf-8')
76 err = err.decode('utf-8')
77 if err:
78 print('FAIL')
79 print('Unexpected output on stderr: {}'.format(err),
80 file=sys.stdout)
81 continue
82
83 result = compare(out, expected)
84 if result is not None:
85 print('FAIL')
86 for l in result:
87 print(l, file=sys.stderr)
88 else:
89 print('PASS')
90 passes += 1
91
92 print('{}/{} tests returned correct results'.format(passes, total))
93 exit(0 if passes == total else 1)
94
95
96 if __name__ == '__main__':
97 main()