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

Unified Diff: tests/lib/math/math_test.dart

Issue 475463005: Implement math.gcd in dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: mint and bigint tests Created 6 years, 4 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 side-by-side diff with in-line comments
Download patch
« sdk/lib/math/math.dart ('K') | « sdk/lib/math/math.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/lib/math/math_test.dart
diff --git a/tests/lib/math/math_test.dart b/tests/lib/math/math_test.dart
index b911e0a5157d1f305cab9c099c878d8068538703..05170033c8f0e1e45ee80a287566852d732a80b7 100644
--- a/tests/lib/math/math_test.dart
+++ b/tests/lib/math/math_test.dart
@@ -146,6 +146,63 @@ class MathTest {
checkVeryClose(LN2, log(2.0));
}
+ static void testGcd() {
+ Expect.equals(7, gcd(0, 7));
+ Expect.equals(5, gcd(5, 0));
+ Expect.equals(0, gcd(0, 0));
+ Expect.equals(1, gcd(5, 7));
+ Expect.equals(6, gcd(12, 18));
+ Expect.equals(6, gcd(18, 12));
+ Expect.equals(15, gcd(45, 105));
+
+ Expect.throws(() => gcd(0, -1), (e) => e is RangeError);
+ Expect.throws(() => gcd(-1, 0), (e) => e is RangeError);
+ Expect.throws(() => gcd(0, null), (e) => e is ArgumentError);
+ Expect.throws(() => gcd(null, 0), (e) => e is ArgumentError);
+
+ // Cover all branches in Binary GCD implementation.
+ // 0 shared powers-of-two factors.
+ Expect.equals(1, gcd(2*2, 7));
+ // 1 shared power-of-two factor.
+ Expect.equals(2, gcd(2*2, 2*7));
+ // >1 shared powers-of-two factors.
+ Expect.equals(8, gcd(2*2*2*3, 2*2*2*5));
+
+ // 0 remaining powers-of-two in a.
+ Expect.equals(6, gcd(2*3, 2*3*3));
+ // 1 remaining power-of-two in a.
+ Expect.equals(6, gcd(2*2*3, 2*3*3));
+ // >1 remaining powers-of-two in a.
+ Expect.equals(6, gcd(2*2*2*2*3, 2*3*3));
+
+ // 0 remaining powers-of-two in b.
+ Expect.equals(6, gcd(2*3, 2*3*3));
+ // 1 remaining power-of-two in b.
+ Expect.equals(6, gcd(2*3, 2*2*3));
+ // >1 remaining powers-of-two in b.
+ Expect.equals(6, gcd(2*3, 2*2*2*3*3));
+
+ // Innermost 'if'
+ // a > b.
+ Expect.equals(6, gcd(2*2*3*5, 2*3));
+ // a == b.
+ Expect.equals(6, gcd(2*3, 2*2*2*3));
+ // a < b.
+ Expect.equals(6, gcd(2*3, 2*2*3*7));
+
+ // do while loop executions.
+ // Executed 1 time.
+ Expect.equals(6, gcd(2*3, 2*2*2*3));
+ // Executed >1 times.
+ Expect.equals(6, gcd(2*3*3, 2*2*3*5));
+
+ // Medium int (mint) arguments.
+ Expect.equals(pow(2, 61), gcd(pow(2, 61)*3, pow(2,62)));
+
+ // Big integer (bigint) arguments.
+ 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,
+ }
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
+
static bool parseIntThrowsFormatException(str) {
try {
int.parse(str);
@@ -245,6 +302,7 @@ class MathTest {
testSqrt();
testLog();
testExp();
+ testGcd();
testParseInt();
}
}
« sdk/lib/math/math.dart ('K') | « sdk/lib/math/math.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698