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