Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: utils/tests/fixnum/src/int64VmTest.dart

Issue 10381133: Add 'fixnum' library to utils/ (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1
Mads Ager (google) 2012/05/15 07:37:39 Add copyright notice. Tests are now using lower_c
2 #library('int64vmtest');
3 #source('../../../fixnum/intx.dart');
4 #source('../../../fixnum/int32.dart');
5 #source('../../../fixnum/int64.dart');
6
7 final int DISCARD = 1;
8
9 int64 randomInt64() {
10 int i = 0;
11 for (int b = 0; b < 64; b++) {
12 double rand;
13 for (int j = 0; j < DISCARD; j++) {
14 rand = Math.random();
15 }
16 i = (i << 1) | ((rand > 0.5) ? 1 : 0);
17 }
18 return new int64.fromInt(i);
19 }
20
21 int randomInt(int n) {
22 double rand;
23 for (int i = 0; i < DISCARD; i++) {
24 rand = Math.random();
25 }
26 return (rand * n).floor().toInt();
27 }
28
29 void main() {
30 int64VMTest test = new int64VMTest();
31 test.doTestBinary(new BinaryOp("%", (a, b) => a % b));
32 test.doTestBinary(new BinaryOp("~/", (a, b) => a ~/ b));
33 test.doTestBinary(new BinaryOp("remainder", (a, b) => a.remainder(b)));
34 test.doTestBinary(new BinaryOp("&", (a, b) => a & b));
35 test.doTestBinary(new BinaryOp("|", (a, b) => a | b));
36 test.doTestBinary(new BinaryOp("^", (a, b) => a ^ b));
37 test.doTestBinary(new BinaryOp("+", (a, b) => a + b));
38 test.doTestBinary(new BinaryOp("-", (a, b) => a - b));
39 test.doTestBinary(new BinaryOp("*", (a, b) => a * b));
40 test.doTestUnary(new UnaryOp("-", (a) => -a));
41 test.doTestUnary(new UnaryOp("~", (a) => ~a));
42 test.doTestShift(new ShiftOp("<<", (a, n) => a << (n & 63)));
43 test.doTestShift(new ShiftOp(">>", (a, n) => a >> (n & 63)));
44 test.doTestBoolean(new BooleanOp("compareTo", (a, b) => a.compareTo(b)));
45 test.doTestBoolean(new BooleanOp("==", (a, b) => a == b));
46 test.doTestBoolean(new BooleanOp("!=", (a, b) => a != b));
47 test.doTestBoolean(new BooleanOp("<", (a, b) => a < b));
48 test.doTestBoolean(new BooleanOp("<=", (a, b) => a <= b));
49 test.doTestBoolean(new BooleanOp(">", (a, b) => a > b));
50 test.doTestBoolean(new BooleanOp(">=", (a, b) => a >= b));
51
52 /*
53 testBase64();
54 testCompare();
55 testGetAsIntArray();
56 testNumberOfLeadingZeros();
57 testRoundTrip();
58 testShru();
59 */
60 }
61
62 class Op {
63 String name;
64 Function op;
65
66 Op(String this.name, Function this.op);
67
68 // Truncate x to a value in the range [-2^63, 2^63 - 1]
69 int trunc64(int x) {
70 int trunc = x & 0xffffffffffffffff;
71 if ((trunc & 0x8000000000000000) != 0) {
72 trunc -= 18446744073709551616; // 2^64
73 }
74 return trunc;
75 }
76 }
77
78 class UnaryOp extends Op {
79 UnaryOp(String name, Function op) : super(name, op);
80 int ref(int val) => trunc64(op(val));
81 int64 test(int64 val) => op(val);
82 }
83
84 class BinaryOp extends Op {
85 BinaryOp(String name, Function op) : super(name, op);
86 int ref(int val0, int val1) => trunc64(op(val0, val1));
87 int64 test(int64 val0, int64 val1) => op(val0, val1);
88 }
89
90 class BooleanOp extends Op {
91 BooleanOp(String name, Function op) : super(name, op);
92 bool ref(int val0, int val1) => op(val0, val1);
93 bool test(int64 val0, int64 val1) => op(val0, val1);
94 }
95
96 class ShiftOp extends Op {
97 ShiftOp(String name, Function op) : super(name, op);
98 int ref(int val0, int shift) => trunc64(op(val0, shift));
99 int64 test(int64 val0, int shift) => op(val0, shift);
100 }
101
102 class int64VMTest {
Mads Ager (google) 2012/05/15 07:37:39 int64Test? If this only works on the VM there shou
103
104 static final int BASE_VALUES = 32;
105 static final int RANDOM_TESTS = 32;
106 List<int64> TEST_VALUES;
107
108 int64VMTest() {
109 Set<int64> testSet = new Set<int64>();
110 for (int i = 0; i < BASE_VALUES; i++) {
111 testSet.add(new int64.fromInt(i));
112 testSet.add(new int64.fromInt(-i));
113
114 testSet.add(int64.MIN_VALUE + i);
115 testSet.add(int64.MAX_VALUE - i);
116
117 testSet.add(new int64.fromInt(i << int64._BITS ~/ 2));
118 testSet.add(new int64.fromInt(i << int64._BITS));
119 testSet.add(new int64.fromInt(i << (3 * int64._BITS) ~/ 2));
120 testSet.add(new int64.fromInt(i << 2 * int64._BITS));
121 testSet.add(new int64.fromInt(i << (5 * int64._BITS) ~/ 2));
122 }
123
124 int64 one = new int64.fromInt(1);
125 int64 three = new int64.fromInt(3);
126 int64 ones = int64.parseHex("1111111111111111");
127 int64 tens = int64.parseHex("1010101010101010");
128 int64 oh_ones = int64.parseHex("0101010101010101");
129 int64 digits = int64.parseHex("123456789ABCDEFF");
130 for (int i = 0; i < 16; i++) {
131 testSet.add(ones * i);
132 testSet.add(~(ones * i));
133 testSet.add(-(ones * i));
134 testSet.add(tens * i);
135 testSet.add(~(tens * i));
136 testSet.add(-(tens * i));
137 testSet.add(oh_ones * i);
138 testSet.add(~(oh_ones * i));
139 testSet.add(-(oh_ones * i));
140 testSet.add(digits * i);
141 testSet.add(~(digits * i));
142 testSet.add(-(digits * i));
143 }
144
145 for (int i = 0; i < 64; i += 4) {
146 testSet.add(one << i);
147 testSet.add(~(one << i));
148 testSet.add(digits >> i);
149 testSet.add(-(digits >> i));
150
151 // Powers of two and nearby numbers
152 testSet.add(one << i);
153 for (int j = 1; j <= 16; j++) {
154 testSet.add((one << i) + j);
155 testSet.add(-((one << i) + j));
156 testSet.add(~((one << i) + j));
157 testSet.add((one << i) - j);
158 testSet.add(-((one << i) - j));
159 testSet.add(~((one << i) - j));
160 testSet.add((three << i) + j);
161 testSet.add(-((three << i) + j));
162 testSet.add(~((three << i) + j));
163 testSet.add((three << i) - j);
164 testSet.add(-((three << i) - j));
165 testSet.add(~((three << i) - j));
166 }
167 }
168
169 for (int a = 0; a < 19; a++) {
170 // Math.pow(10, a)
171 int pow = 1;
172 for (int j = 0; j < a; j++) {
173 pow *= 10;
174 }
175 testSet.add(new int64.fromInt(pow));
176 }
177
178 TEST_VALUES = new List<int64>(testSet.length);
179 int index = 0;
180 for (int64 val in testSet) {
181 TEST_VALUES[index++] = val;
182 }
183
184 print("VALUES.length = $index");
185 }
186
187 void _doTestUnary(UnaryOp op, int64 val) {
188 int ref = op.ref(val.toInt());
189 int64 result64 = op.test(val);
190 int result = result64.toInt();
191 if (ref != result) {
192 Expect.fail("${op.name}: val = $val");
193 }
194 }
195
196 void doTestUnary(UnaryOp op) {
197 print(op.name);
198 for (int i = 0; i < TEST_VALUES.length; i++) {
199 _doTestUnary(op, TEST_VALUES[i]);
200 }
201 for (int i = 0; i < RANDOM_TESTS; i++) {
202 int64 randomLong = randomInt64();
203 _doTestUnary(op, randomLong);
204 }
205 }
206
207 void _doTestBinary(BinaryOp op, int64 val0, int64 val1) {
208 // print("Test val0 = $val0, val1 = $val1");
209 var refException = null;
210 int ref = -1;
211 try {
212 ref = op.ref(val0.toInt(), val1.toInt());
213 } catch (Exception e) {
214 refException = e;
215 }
216 var testException = null;
217 int result = -2;
218 int64 result64;
219 try {
220 int64 val0_save = new int64._copy(val0);
221 int64 val1_save = new int64._copy(val1);
222 result64 = op.test(val0, val1);
223 result = result64.toInt();
224 if (val0 != val0_save) {
225 print("Test altered first argument val0 = $val0, val0_save = $val0_save" );
Mads Ager (google) 2012/05/15 07:37:39 Long lines.
226 }
227 if (val1 != val1_save) {
228 print("Test altered second argument");
229 }
230 } catch (Exception e) {
231 testException = e;
232 }
233 if (testException is IntegerDivisionByZeroException &&
234 refException is IntegerDivisionByZeroException) {
235 } else if (testException != null || refException != null) {
236 Expect.fail("${op.name}: val0 = $val0, val1 = $val1, testException = $test Exception, refException = $refException");
237 return;
238 } else if (ref != result) {
239 if ("%" == op.name && ref < 0) {
240 // print("Dart VM bug: ${op.name}: val0 = $val0, val1 = $val1, ref = $re f, result64 = $result64, result = $result");
241 } else {
242 Expect.fail("${op.name}: val0 = $val0, val1 = $val1, ref = $ref, result6 4 = $result64, result = $result");
243 }
244 }
245 }
246
247 void doTestBinary(BinaryOp op) {
248 print(op.name);
249 for (int i = 0; i < TEST_VALUES.length; i++) {
250 int64 randomLong = randomInt64();
251 _doTestBinary(op, TEST_VALUES[i], randomLong);
252 _doTestBinary(op, randomLong, TEST_VALUES[i]);
253 for (int j = 0; j < TEST_VALUES.length; j++) {
254 _doTestBinary(op, TEST_VALUES[i], TEST_VALUES[j]);
255 }
256 }
257 for (int i = 0; i < RANDOM_TESTS; i++) {
258 int64 longVal0 = randomInt64();
259 int64 longVal1 = randomInt64();
260 if (randomInt(20) == 0) {
261 if (randomInt(2) == 0) {
262 longVal1 = longVal0;
263 } else {
264 longVal1 = -longVal0;
265 }
266 }
267 _doTestBinary(op, longVal0, longVal1);
268 }
269 }
270
271 void _doTestBoolean(BooleanOp op, int64 val0, int64 val1) {
272 bool ref = op.ref(val0.toInt(), val1.toInt());
273 bool result = op.test(val0, val1);
274 if (ref != result) {
275 Expect.fail("${op.name}: val0 = $val0, val1 = $val1");
276 }
277 }
278
279 void doTestBoolean(BooleanOp op) {
280 print(op.name);
281 for (int i = 0; i < TEST_VALUES.length; i++) {
282 int64 randomLong = randomInt64();
283 _doTestBoolean(op, TEST_VALUES[i], randomLong);
284 _doTestBoolean(op, randomLong, TEST_VALUES[i]);
285 for (int j = 0; j < TEST_VALUES.length; j++) {
286 _doTestBoolean(op, TEST_VALUES[i], TEST_VALUES[j]);
287 }
288 }
289 for (int i = 0; i < RANDOM_TESTS; i++) {
290 int64 longVal0 = randomInt64();
291 int64 longVal1 = randomInt64();
292 if (randomInt(20) == 0) {
293 if (randomInt(2) == 0) {
294 longVal1 = longVal0;
295 } else {
296 longVal1 = -longVal0;
297 }
298 }
299 _doTestBoolean(op, longVal0, longVal1);
300 }
301 }
302
303 void _doTestShift(ShiftOp op, int64 val, int shift) {
304 int ref = op.ref(val.toInt(), shift);
305 int64 result64 = op.test(val, shift);
306 int result = result64.toInt();
307 if (ref != result) {
308 Expect.fail("${op.name}: val = $val, shift = $shift");
309 }
310 }
311
312 void doTestShift(ShiftOp op) {
313 print(op.name);
314 for (int i = 0; i < TEST_VALUES.length; i++) {
315 for (int shift = -64; shift <= 64; shift++) {
316 _doTestShift(op, TEST_VALUES[i], shift);
317 }
318 }
319 for (int i = 0; i < RANDOM_TESTS; i++) {
320 int64 randomLong = randomInt64();
321 for (int shift = -64; shift <= 64; shift++) {
322 _doTestShift(op, randomLong, shift);
323 }
324 }
325 }
326 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698