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