test.py revision dd6206bb61bf8df2ed6b643abe8a29c48a315685
1#!/usr/bin/python2.4
2#
3# Copyright 2010 Google Inc. All Rights Reserved.
4
5"""RenderScript Compiler Test.
6
7Runs subdirectories of tests for the RenderScript compiler.
8"""
9
10import filecmp
11import glob
12import os
13import shutil
14import subprocess
15import sys
16
17__author__ = 'Android'
18
19
20class Options(object):
21  def __init__(self):
22    return
23  verbose = 0
24  cleanup = 1
25
26
27def CompareFiles(filename):
28  """Compares filename and filename.expect for equality."""
29  actual = filename
30  expect = filename + '.expect'
31
32  if not os.path.isfile(actual):
33    if Options.verbose:
34      print 'Could not find %s' % actual
35    return False
36  if not os.path.isfile(expect):
37    if Options.verbose:
38      print 'Could not find %s' % expect
39    return False
40
41  return filecmp.cmp(actual, expect, False)
42
43
44def ExecTest(dirname):
45  """Executes an llvm-rs-cc test from dirname."""
46  passed = True
47
48  if Options.verbose != 0:
49    print 'Testing %s' % dirname
50
51  os.chdir(dirname)
52  stdout_file = open('stdout.txt', 'w+')
53  stderr_file = open('stderr.txt', 'w+')
54
55  cmd_string = ('../../../../../out/host/linux-x86/bin/llvm-rs-cc '
56                '-o tmp/ -p tmp/ '
57                '-I ../../../../../frameworks/base/libs/rs/scriptc/')
58  base_args = cmd_string.split()
59  rs_files = glob.glob('*.rs')
60  rs_files.sort()
61  args = base_args + rs_files
62
63  if Options.verbose > 1:
64    print 'Executing:',
65    for arg in args:
66      print arg,
67    print
68
69  # Execute the command and check the resulting shell return value.
70  # All tests that are expected to FAIL have directory names that
71  # start with 'F_'. Other tests that are expected to PASS have
72  # directory names that start with 'P_'.
73  ret = subprocess.call(args, stdout=stdout_file, stderr=stderr_file)
74  stdout_file.flush()
75  stderr_file.flush()
76
77  if Options.verbose > 1:
78    stdout_file.seek(0)
79    stderr_file.seek(0)
80    for line in stdout_file:
81      print 'STDOUT>', line,
82    for line in stderr_file:
83      print 'STDERR>', line,
84
85  stdout_file.close()
86  stderr_file.close()
87
88  if dirname[0:2] == 'F_':
89    if ret == 0:
90      passed = False
91      if Options.verbose:
92        print 'Command passed on invalid input'
93  elif dirname[0:2] == 'P_':
94    if ret != 0:
95      passed = False
96      if Options.verbose:
97        print 'Command failed on valid input'
98  else:
99    passed = (ret == 0)
100    if Options.verbose:
101      print 'Test Directory name should start with an F or a P'
102
103  if not CompareFiles('stdout.txt'):
104    passed = False
105    if Options.verbose:
106      print 'stdout is different'
107  if not CompareFiles('stderr.txt'):
108    passed = False
109    if Options.verbose:
110      print 'stderr is different'
111
112  if Options.cleanup:
113    os.remove('stdout.txt')
114    os.remove('stderr.txt')
115    shutil.rmtree('tmp/')
116
117  os.chdir('..')
118  return passed
119
120
121def Usage():
122  print ('Usage: %s [OPTION]... [TESTNAME]...'
123         'RenderScript Compiler Test Harness\n'
124         'Runs TESTNAMEs (all tests by default)\n'
125         'Available Options:\n'
126         '  -h, --help          Help message\n'
127         '  -n, --no-cleanup    Don\'t clean up after running tests\n'
128         '  -v, --verbose       Verbose output\n'
129        ) % (sys.argv[0]),
130  return
131
132
133def main():
134  passed = 0
135  failed = 0
136  files = []
137  failed_tests = []
138
139  for arg in sys.argv[1:]:
140    if arg in ('-h', '--help'):
141      Usage()
142      return 0
143    elif arg in ('-n', '--no-cleanup'):
144      Options.cleanup = 0
145    elif arg in ('-v', '--verbose'):
146      Options.verbose += 1
147    else:
148      # Test list to run
149      if os.path.isdir(arg):
150        files.append(arg)
151      else:
152        print >> sys.stderr, 'Invalid test or option: %s' % arg
153        return 1
154
155  if not files:
156    tmp_files = os.listdir('.')
157    # Only run tests that are known to PASS or FAIL
158    # Disabled tests can be marked D_ and invoked explicitly
159    for f in tmp_files:
160      if os.path.isdir(f) and (f[0:2] == 'F_' or f[0:2] == 'P_'):
161        files.append(f)
162
163  for f in files:
164    if os.path.isdir(f):
165      if ExecTest(f):
166        passed += 1
167      else:
168        failed += 1
169        failed_tests.append(f)
170
171  print 'Tests Passed: %d\n' % passed,
172  print 'Tests Failed: %d\n' % failed,
173  if failed:
174    print 'Failures:',
175    for t in failed_tests:
176      print t,
177
178  return failed != 0
179
180
181if __name__ == '__main__':
182  sys.exit(main())
183