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