OLD | NEW |
---|---|
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library math_test; | 5 library math_test; |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 import 'dart:math'; | 7 import 'dart:math'; |
8 | 8 |
9 class MathTest { | 9 class MathTest { |
10 static void testConstants() { | 10 static void testConstants() { |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 } | 139 } |
140 | 140 |
141 static void testLog() { | 141 static void testLog() { |
142 // Even though E is imprecise, it is good enough to get really close to 1. | 142 // Even though E is imprecise, it is good enough to get really close to 1. |
143 // We still provide an epsilon. | 143 // We still provide an epsilon. |
144 checkClose(1.0, log(E), 1e-16); | 144 checkClose(1.0, log(E), 1e-16); |
145 checkVeryClose(LN10, log(10.0)); | 145 checkVeryClose(LN10, log(10.0)); |
146 checkVeryClose(LN2, log(2.0)); | 146 checkVeryClose(LN2, log(2.0)); |
147 } | 147 } |
148 | 148 |
149 static void testGcd() { | |
150 Expect.equals(7, gcd(0, 7)); | |
151 Expect.equals(5, gcd(5, 0)); | |
152 Expect.equals(0, gcd(0, 0)); | |
153 Expect.equals(1, gcd(5, 7)); | |
154 Expect.equals(6, gcd(12, 18)); | |
155 Expect.equals(6, gcd(18, 12)); | |
156 Expect.equals(15, gcd(45, 105)); | |
157 | |
158 Expect.throws(() => gcd(0, -1), (e) => e is RangeError); | |
159 Expect.throws(() => gcd(-1, 0), (e) => e is RangeError); | |
160 Expect.throws(() => gcd(0, null), (e) => e is ArgumentError); | |
161 Expect.throws(() => gcd(null, 0), (e) => e is ArgumentError); | |
162 | |
163 // Cover all branches in Binary GCD implementation. | |
164 // 0 shared powers-of-two factors. | |
165 Expect.equals(1, gcd(2*2, 7)); | |
166 // 1 shared power-of-two factor. | |
167 Expect.equals(2, gcd(2*2, 2*7)); | |
168 // >1 shared powers-of-two factors. | |
169 Expect.equals(8, gcd(2*2*2*3, 2*2*2*5)); | |
170 | |
171 // 0 remaining powers-of-two in a. | |
172 Expect.equals(6, gcd(2*3, 2*3*3)); | |
173 // 1 remaining power-of-two in a. | |
174 Expect.equals(6, gcd(2*2*3, 2*3*3)); | |
175 // >1 remaining powers-of-two in a. | |
176 Expect.equals(6, gcd(2*2*2*2*3, 2*3*3)); | |
177 | |
178 // 0 remaining powers-of-two in b. | |
179 Expect.equals(6, gcd(2*3, 2*3*3)); | |
180 // 1 remaining power-of-two in b. | |
181 Expect.equals(6, gcd(2*3, 2*2*3)); | |
182 // >1 remaining powers-of-two in b. | |
183 Expect.equals(6, gcd(2*3, 2*2*2*3*3)); | |
184 | |
185 // Innermost 'if' | |
186 // a > b. | |
187 Expect.equals(6, gcd(2*2*3*5, 2*3)); | |
188 // a == b. | |
189 Expect.equals(6, gcd(2*3, 2*2*2*3)); | |
190 // a < b. | |
191 Expect.equals(6, gcd(2*3, 2*2*3*7)); | |
192 | |
193 // do while loop executions. | |
194 // Executed 1 time. | |
195 Expect.equals(6, gcd(2*3, 2*2*2*3)); | |
196 // Executed >1 times. | |
197 Expect.equals(6, gcd(2*3*3, 2*2*3*5)); | |
198 | |
199 // Medium int (mint) arguments. | |
200 Expect.equals(pow(2, 61), gcd(pow(2, 61)*3, pow(2,62))); | |
201 | |
202 // Big integer (bigint) arguments. | |
203 Expect.equals(pow(2, 63)*3, gcd(pow(2, 64)*3*5, pow(2,63)*3*7)); | |
Lasse Reichstein Nielsen
2014/08/19 08:31:31
This is very quickly reduced by dividing by 2 63 t
srawlins
2014/08/20 01:06:27
Good idea. Done and split into bigint tests since,
| |
204 } | |
Lasse Reichstein Nielsen
2014/08/19 08:31:31
Do these tests succeed on dart2js?
If so, you nee
srawlins
2014/08/20 01:06:27
Sorry, that test file is expected to raise a Runti
| |
205 | |
149 static bool parseIntThrowsFormatException(str) { | 206 static bool parseIntThrowsFormatException(str) { |
150 try { | 207 try { |
151 int.parse(str); | 208 int.parse(str); |
152 return false; | 209 return false; |
153 } on FormatException catch (e) { | 210 } on FormatException catch (e) { |
154 return true; | 211 return true; |
155 } | 212 } |
156 } | 213 } |
157 | 214 |
158 static void testParseInt() { | 215 static void testParseInt() { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 testSin(); | 295 testSin(); |
239 testCos(); | 296 testCos(); |
240 testTan(); | 297 testTan(); |
241 testAsin(); | 298 testAsin(); |
242 testAcos(); | 299 testAcos(); |
243 testAtan(); | 300 testAtan(); |
244 testAtan2(); | 301 testAtan2(); |
245 testSqrt(); | 302 testSqrt(); |
246 testLog(); | 303 testLog(); |
247 testExp(); | 304 testExp(); |
305 testGcd(); | |
248 testParseInt(); | 306 testParseInt(); |
249 } | 307 } |
250 } | 308 } |
251 | 309 |
252 main() { | 310 main() { |
253 MathTest.testMain(); | 311 MathTest.testMain(); |
254 } | 312 } |
OLD | NEW |