test.py revision 6b201eb3306b9609a991728a52ce948974bd4aed
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                '-I ../../../../../external/clang/lib/Headers/')
59  base_args = cmd_string.split()
60  rs_files = glob.glob('*.rs')
61  rs_files.sort()
62  args = base_args + rs_files
63
64  if Options.verbose > 1:
65    print 'Executing:',
66    for arg in args:
67      print arg,
68    print
69
70  # Execute the command and check the resulting shell return value.
71  # All tests that are expected to FAIL have directory names that
72  # start with 'F_'. Other tests that are expected to PASS have
73  # directory names that start with 'P_'.
74  ret = 0
75  try:
76    ret = subprocess.call(args, stdout=stdout_file, stderr=stderr_file)
77  except:
78    passed = False
79
80  stdout_file.flush()
81  stderr_file.flush()
82
83  if Options.verbose > 1:
84    stdout_file.seek(0)
85    stderr_file.seek(0)
86    for line in stdout_file:
87      print 'STDOUT>', line,
88    for line in stderr_file:
89      print 'STDERR>', line,
90
91  stdout_file.close()
92  stderr_file.close()
93
94  if dirname[0:2] == 'F_':
95    if ret == 0:
96      passed = False
97      if Options.verbose:
98        print 'Command passed on invalid input'
99  elif dirname[0:2] == 'P_':
100    if ret != 0:
101      passed = False
102      if Options.verbose:
103        print 'Command failed on valid input'
104  else:
105    passed = (ret == 0)
106    if Options.verbose:
107      print 'Test Directory name should start with an F or a P'
108
109  if not CompareFiles('stdout.txt'):
110    passed = False
111    if Options.verbose:
112      print 'stdout is different'
113  if not CompareFiles('stderr.txt'):
114    passed = False
115    if Options.verbose:
116      print 'stderr is different'
117
118  if Options.cleanup:
119    try:
120      os.remove('stdout.txt')
121      os.remove('stderr.txt')
122      shutil.rmtree('tmp/')
123    except:
124      pass
125
126  os.chdir('..')
127  return passed
128
129
130def Usage():
131  print ('Usage: %s [OPTION]... [TESTNAME]...'
132         'RenderScript Compiler Test Harness\n'
133         'Runs TESTNAMEs (all tests by default)\n'
134         'Available Options:\n'
135         '  -h, --help          Help message\n'
136         '  -n, --no-cleanup    Don\'t clean up after running tests\n'
137         '  -v, --verbose       Verbose output\n'
138        ) % (sys.argv[0]),
139  return
140
141
142def main():
143  passed = 0
144  failed = 0
145  files = []
146  failed_tests = []
147
148  for arg in sys.argv[1:]:
149    if arg in ('-h', '--help'):
150      Usage()
151      return 0
152    elif arg in ('-n', '--no-cleanup'):
153      Options.cleanup = 0
154    elif arg in ('-v', '--verbose'):
155      Options.verbose += 1
156    else:
157      # Test list to run
158      if os.path.isdir(arg):
159        files.append(arg)
160      else:
161        print >> sys.stderr, 'Invalid test or option: %s' % arg
162        return 1
163
164  if not files:
165    tmp_files = os.listdir('.')
166    # Only run tests that are known to PASS or FAIL
167    # Disabled tests can be marked D_ and invoked explicitly
168    for f in tmp_files:
169      if os.path.isdir(f) and (f[0:2] == 'F_' or f[0:2] == 'P_'):
170        files.append(f)
171
172  for f in files:
173    if os.path.isdir(f):
174      if ExecTest(f):
175        passed += 1
176      else:
177        failed += 1
178        failed_tests.append(f)
179
180  print 'Tests Passed: %d\n' % passed,
181  print 'Tests Failed: %d\n' % failed,
182  if failed:
183    print 'Failures:',
184    for t in failed_tests:
185      print t,
186
187  return failed != 0
188
189
190if __name__ == '__main__':
191  sys.exit(main())
192