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