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