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

Side by Side Diff: tests/corelib/big_integer_arith_vm_test.dart

Issue 620943002: Add a bigint test with --no_intrinsify. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 2 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 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 // Testing Bigints with and without intrinsics.
6 // VMOptions=
7 // VMOptions=--no_intrinsify
8
9 library big_integer_test;
10 import "package:expect/expect.dart";
11
12 foo() => 1234567890123456789;
13 bar() => 12345678901234567890;
14
15 testSmiOverflow() {
16 var a = 1073741823;
17 var b = 1073741822;
18 Expect.equals(2147483645, a + b);
19 a = -1000000000;
20 b = 1000000001;
21 Expect.equals(-2000000001, a - b);
22 Expect.equals(-1000000001000000000, a * b);
23 }
24
25 testBigintAdd() {
26 // Bigint and Smi.
27 var a = 12345678901234567890;
28 var b = 2;
29 Expect.equals(12345678901234567892, a + b);
30 Expect.equals(12345678901234567892, b + a);
31 // Bigint and Bigint.
32 a = 10000000000000000001;
33 Expect.equals(20000000000000000002, a + a);
34 // Bigint and double.
35 a = 100000000000000000000.0;
36 b = 200000000000000000000;
37 Expect.isTrue((a + b) is double);
38 Expect.equals(300000000000000000000.0, a + b);
39 Expect.isTrue((b + a) is double);
40 Expect.equals(300000000000000000000.0, b + a);
41 }
42
43 testBigintSub() {
44 // Bigint and Smi.
45 var a = 12345678901234567890;
46 var b = 2;
47 Expect.equals(12345678901234567888, a - b);
48 Expect.equals(-12345678901234567888, b - a);
49 // Bigint and Bigint.
50 a = 10000000000000000001;
51 Expect.equals(20000000000000000002, a + a);
52 // Bigint and double.
53 a = 100000000000000000000.0;
54 b = 200000000000000000000;
55 Expect.isTrue((a + b) is double);
56 Expect.equals(-100000000000000000000.0, a - b);
57 Expect.isTrue((b + a) is double);
58 Expect.equals(100000000000000000000.0, b - a);
59 Expect.equals(-1, 0xF00000000 - 0xF00000001);
60 }
61
62 testBigintMul() {
63 // Bigint and Smi.
64 var a = 12345678901234567890;
65 var b = 10;
66 Expect.equals(123456789012345678900, a * b);
67 Expect.equals(123456789012345678900, b * a);
68 // Bigint and Bigint.
69 a = 12345678901234567890;
70 b = 10000000000000000;
71 Expect.equals(123456789012345678900000000000000000, a * b);
72 // Bigint and double.
73 a = 2.0;
74 b = 200000000000000000000;
75 Expect.isTrue((a * b) is double);
76 Expect.equals(400000000000000000000.0, a * b);
77 Expect.isTrue((b * a) is double);
78 Expect.equals(400000000000000000000.0, b * a);
79 }
80
81 testBigintTruncDiv() {
82 var a = 12345678901234567890;
83 var b = 10;
84 // Bigint and Smi.
85 Expect.equals(1234567890123456789, a ~/ b);
86 Expect.equals(0, b ~/ a);
87 Expect.equals(123456789, 123456789012345678 ~/ 1000000000);
88 // Bigint and Bigint.
89 a = 12345678901234567890;
90 b = 10000000000000000;
91 Expect.equals(1234, a ~/ b);
92 // Bigint and double.
93 a = 100000000000000000000.0;
94 b = 200000000000000000000;
95 Expect.equals(0, a ~/ b);
96 Expect.equals(2, b ~/ a);
97 }
98
99 testBigintDiv() {
100 // Bigint and Smi.
101 Expect.equals(1234567890123456789.1, 12345678901234567891 / 10);
102 Expect.equals(0.000000001234, 1234 / 1000000000000);
103 Expect.equals(12345678901234000000.0, 123456789012340000000 / 10);
104 // Bigint and Bigint.
105 var a = 12345670000000000000;
106 var b = 10000000000000000;
107 Expect.equals(1234.567, a / b);
108 // Bigint and double.
109 a = 400000000000000000000.0;
110 b = 200000000000000000000;
111 Expect.equals(2.0, a / b);
112 Expect.equals(0.5, b / a);
113 }
114
115 testBigintModulo() {
116 // Bigint and Smi.
117 var a = 1000000000005;
118 var b = 10;
119 Expect.equals(5, a % b);
120 Expect.equals(10, b % a);
121 // Bigint & Bigint
122 a = 10000000000000000001;
123 b = 10000000000000000000;
124 Expect.equals(1, a % b);
125 Expect.equals(10000000000000000000, b % a);
126 // Bigint & double.
127 a = 10000000100000000.0;
128 b = 10000000000000000;
129 Expect.equals(100000000.0, a % b);
130 Expect.equals(10000000000000000.0, b % a);
131 // Transitioning from Mint to Bigint.
132 var iStart = 4611686018427387900;
133 var prevX = -23 % iStart;
134 for (int i = iStart + 1; i < iStart + 10; i++) {
135 var x = -23 % i;
136 Expect.equals(1, x - prevX);
137 Expect.isTrue(x > 0);
138 prevX = x;
139 }
140 }
141
142 testBigintNegate() {
143 var a = 0xF000000000000000F;
144 var b = ~a; // negate.
145 Expect.equals(-0xF0000000000000010, b);
146 Expect.equals(0, a & b);
147 Expect.equals(-1, a | b);
148 }
149
150 testShiftAmount() {
151 Expect.equals(0, 12 >> 111111111111111111111111111111);
152 Expect.equals(-1, -12 >> 111111111111111111111111111111);
153 bool exceptionCaught = false;
154 try {
155 var a = 1 << 1111111111111111111111111111;
156 } on OutOfMemoryError catch (e) {
157 exceptionCaught = true;
158 }
159 Expect.equals(true, exceptionCaught);
160 }
161
162 main() {
163 Expect.equals(1234567890123456789, foo());
164 Expect.equals(12345678901234567890, bar());
165 testSmiOverflow();
166 testBigintAdd();
167 testBigintSub();
168 testBigintMul();
169 testBigintModulo();
170 testBigintTruncDiv();
171 testBigintDiv();
172 testBigintNegate();
173 testShiftAmount();
174 Expect.equals(12345678901234567890, (12345678901234567890).abs());
175 Expect.equals(12345678901234567890, (-12345678901234567890).abs());
176 var a = 10000000000000000000;
177 var b = 10000000000000000001;
178 Expect.equals(false, a.hashCode == b.hashCode);
179 Expect.equals(true, a.hashCode == (b - 1).hashCode);
180 // TODO(regis): Add a test for modPow once it is public.
181 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698