1# Copyright (C) 2014 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15all_tests = [ 16 ({'@INT@': 'int', '@SUFFIX@':''}, 17 [('CheckDiv', 'idiv_by_pow2_', [2**i for i in range(31)]), 18 ('CheckDiv', 'idiv_by_small_', [i for i in range(3, 16) if i not in (4, 8)]), 19 ('CheckRem', 'irem_by_pow2_', [2**i for i in range(31)])]), 20 ({'@INT@': 'long', '@SUFFIX@': 'l'}, 21 [('CheckDiv', 'ldiv_by_pow2_', [2**i for i in range(63)]), 22 ('CheckDiv', 'ldiv_by_small_', [i for i in range(3, 16) if i not in (4, 8)]), 23 ('CheckRem', 'lrem_by_pow2_', [2**i for i in range(63)])]) 24] 25 26def subst_vars(variables, text): 27 '''Substitute variables in text.''' 28 for key, value in variables.iteritems(): 29 text = text.replace(str(key), str(value)) 30 return text 31 32# Generate all the function bodies (in decls) and all the function calls (in calls). 33decls, calls = '', {} 34for default_vars, tests in all_tests: 35 local_vars = default_vars.copy() 36 int_type = local_vars['@INT@'] 37 for checker, name, values in tests: 38 local_vars['@CHECKER@'] = checker 39 for i, value in enumerate(values): 40 local_vars['@NAME@'] = name + str(i) 41 local_vars['@VALUE@'] = value 42 local_vars['@OP@'] = '/' if 'div' in name else '%' 43 44 # Function body. 45 decls += subst_vars(local_vars, ''' 46 public static @INT@ @NAME@(@INT@ x) {return x @OP@ @VALUE@@SUFFIX@;}''') 47 48 # Function call and test. 49 calls[int_type] = calls.get(int_type, '') + subst_vars(local_vars, ''' 50 @INT@@CHECKER@("@NAME@", @NAME@(x), x, @VALUE@@SUFFIX@);''') 51 52# Generate the checkers. 53checkers = '' 54local_vars = {} 55for int_type in ('int', 'long'): 56 local_vars['@INT@'] = int_type 57 for op, op_name in (('/', 'Div'), ('%', 'Rem')): 58 local_vars['@OP@'] = op 59 local_vars['@OP_NAME@'] = op_name 60 checkers += subst_vars(local_vars, ''' 61 public static void @INT@Check@OP_NAME@(String desc, @INT@ result, @INT@ dividend, @INT@ divisor) { 62 @INT@ correct_result = dividend @OP@ divisor; 63 if (result != correct_result) { 64 reportError(desc + "(" + dividend + ") == " + result + 65 " should be " + correct_result); 66 } 67 }''') 68 69 70code = \ 71'''/* 72 * Copyright (C) 2014 The Android Open Source Project 73 * 74 * Licensed under the Apache License, Version 2.0 (the "License"); 75 * you may not use this file except in compliance with the License. 76 * You may obtain a copy of the License at 77 * 78 * http://www.apache.org/licenses/LICENSE-2.0 79 * 80 * Unless required by applicable law or agreed to in writing, software 81 * distributed under the License is distributed on an "AS IS" BASIS, 82 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 83 * See the License for the specific language governing permissions and 84 * limitations under the License. 85 */ 86 87public class Main { 88 public static int num_errors = 0; 89 90 public static void reportError(String message) { 91 if (num_errors == 10) { 92 System.out.println("Omitting other error messages..."); 93 } else if (num_errors < 10) { 94 System.out.println(message); 95 } 96 num_errors += 1; 97 } 98%s 99%s 100 101 public static void intCheckAll(int x) {%s 102 } 103 104 public static void longCheckAll(long x) {%s 105 } 106 107 public static void main(String[] args) { 108 int i; 109 long l; 110 111 System.out.println("Begin"); 112 113 System.out.println("Int: checking some equally spaced dividends..."); 114 for (i = -1000; i < 1000; i += 300) { 115 intCheckAll(i); 116 intCheckAll(-i); 117 } 118 119 System.out.println("Int: checking small dividends..."); 120 for (i = 1; i < 100; i += 1) { 121 intCheckAll(i); 122 intCheckAll(-i); 123 } 124 125 System.out.println("Int: checking big dividends..."); 126 for (i = 0; i < 100; i += 1) { 127 intCheckAll(Integer.MAX_VALUE - i); 128 intCheckAll(Integer.MIN_VALUE + i); 129 } 130 131 System.out.println("Long: checking some equally spaced dividends..."); 132 for (l = 0l; l < 1000000000000l; l += 300000000000l) { 133 longCheckAll(l); 134 longCheckAll(-l); 135 } 136 137 System.out.println("Long: checking small dividends..."); 138 for (l = 1l; l < 100l; l += 1l) { 139 longCheckAll(l); 140 longCheckAll(-l); 141 } 142 143 System.out.println("Long: checking big dividends..."); 144 for (l = 0l; l < 100l; l += 1l) { 145 longCheckAll(Long.MAX_VALUE - l); 146 longCheckAll(Long.MIN_VALUE + l); 147 } 148 149 System.out.println("End"); 150 } 151} 152''' % (checkers, decls, calls['int'], calls['long']) 153 154with open('src/Main.java', 'w') as f: 155 f.write(code) 156