1#!/usr/bin/env python3 2 3# Automatically formatted with yapf (https://github.com/google/yapf) 4 5import subprocess 6import unittest 7 8 9def getFinalPasses(run): 10 stdout = run.stdout.decode() 11 stdout = stdout[:stdout.rfind('\n')] 12 stdout = stdout[stdout.rfind('\n') + 1:] 13 return stdout 14 15 16class Test(unittest.TestCase): 17 def test_0(self): 18 """Test all passes are removed except those required to crash. Verify 19 that PM structure is intact.""" 20 run_args = [ 21 './utils/reduce_pipeline.py', 22 '--opt-binary=./utils/reduce_pipeline_test/fake_opt.py', 23 '--input=/dev/null', '--passes=a,b,c,A(d,B(e,f),g),h,i', 24 '-crash-seq=b,d,f' 25 ] 26 run = subprocess.run(run_args, 27 stdout=subprocess.PIPE, 28 stderr=subprocess.PIPE) 29 self.assertEqual(run.returncode, 0) 30 self.assertEqual(getFinalPasses(run), '-passes="b,A(d,B(f))"') 31 32 def test_1(self): 33 """Test all passes are removed except those required to crash. The 34 required passes in this case are the first and last in that order 35 (a bit of a corner-case for the reduction algorithm).""" 36 run_args = [ 37 './utils/reduce_pipeline.py', 38 '--opt-binary=./utils/reduce_pipeline_test/fake_opt.py', 39 '--input=/dev/null', '--passes=a,b,c,A(d,B(e,f),g),h,i', 40 '-crash-seq=a,i' 41 ] 42 run = subprocess.run(run_args, 43 stdout=subprocess.PIPE, 44 stderr=subprocess.PIPE) 45 self.assertEqual(run.returncode, 0) 46 self.assertEqual(getFinalPasses(run), '-passes="a,i"') 47 48 def test_2_0(self): 49 """Test expansion of EXPAND_a_to_f (expands into 'a,b,c,d,e,f').""" 50 run_args = [ 51 './utils/reduce_pipeline.py', 52 '--opt-binary=./utils/reduce_pipeline_test/fake_opt.py', 53 '--input=/dev/null', '--passes=EXPAND_a_to_f', '-crash-seq=b,e' 54 ] 55 run = subprocess.run(run_args, 56 stdout=subprocess.PIPE, 57 stderr=subprocess.PIPE) 58 self.assertEqual(run.returncode, 0) 59 self.assertEqual(getFinalPasses(run), '-passes="b,e"') 60 61 def test_2_1(self): 62 """Test EXPAND_a_to_f and the '--dont-expand-passes' option.""" 63 run_args = [ 64 './utils/reduce_pipeline.py', 65 '--opt-binary=./utils/reduce_pipeline_test/fake_opt.py', 66 '--input=/dev/null', '--passes=EXPAND_a_to_f', 67 '-crash-seq=EXPAND_a_to_f', '--dont-expand-passes' 68 ] 69 run = subprocess.run(run_args, 70 stdout=subprocess.PIPE, 71 stderr=subprocess.PIPE) 72 self.assertEqual(run.returncode, 0) 73 self.assertEqual(getFinalPasses(run), '-passes="EXPAND_a_to_f"') 74 75 def test_3(self): 76 """Test that empty pass-managers get removed by default.""" 77 run_args = [ 78 './utils/reduce_pipeline.py', 79 '--opt-binary=./utils/reduce_pipeline_test/fake_opt.py', 80 '--input=/dev/null', '--passes=a,b,c,A(d,B(e,f),g),h,i', 81 '-crash-seq=b,d,h' 82 ] 83 run = subprocess.run(run_args, 84 stdout=subprocess.PIPE, 85 stderr=subprocess.PIPE) 86 self.assertEqual(run.returncode, 0) 87 self.assertEqual(getFinalPasses(run), '-passes="b,A(d),h"') 88 89 def test_4(self): 90 """Test the '--dont-remove-empty-pm' option.""" 91 run_args = [ 92 './utils/reduce_pipeline.py', 93 '--opt-binary=./utils/reduce_pipeline_test/fake_opt.py', 94 '--input=/dev/null', '--passes=a,b,c,A(d,B(e,f),g),h,i', 95 '-crash-seq=b,d,h', '--dont-remove-empty-pm' 96 ] 97 run = subprocess.run(run_args, 98 stdout=subprocess.PIPE, 99 stderr=subprocess.PIPE) 100 self.assertEqual(run.returncode, 0) 101 self.assertEqual(getFinalPasses(run), '-passes="b,A(d,B()),h"') 102 103 104unittest.main() 105exit(0) 106