OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 library math_test; |
| 6 import "package:expect/expect.dart"; |
| 7 import 'dart:math'; |
| 8 import 'package:math/math.dart'; |
| 9 |
| 10 void testGcd() { |
| 11 Expect.equals(7, gcd(0, 7)); |
| 12 Expect.equals(5, gcd(5, 0)); |
| 13 Expect.equals(5, gcd(-5, 0)); |
| 14 Expect.equals(0, gcd(0, 0)); |
| 15 Expect.equals(1, gcd(5, 7)); |
| 16 Expect.equals(6, gcd(12, 18)); |
| 17 Expect.equals(6, gcd(12, -18)); |
| 18 Expect.equals(6, gcd(-12, -18)); |
| 19 Expect.equals(6, gcd(18, 12)); |
| 20 Expect.equals(15, gcd(45, 105)); |
| 21 |
| 22 Expect.throws(() => gcd(0, null), (e) => e is ArgumentError); |
| 23 Expect.throws(() => gcd(null, 0), (e) => e is ArgumentError); |
| 24 |
| 25 // Cover all branches in Binary GCD implementation. |
| 26 // 0 shared powers-of-two factors. |
| 27 Expect.equals(1, gcd(2*2, 7)); |
| 28 // 1 shared power-of-two factor. |
| 29 Expect.equals(2, gcd(2*2, 2*7)); |
| 30 // >1 shared powers-of-two factors. |
| 31 Expect.equals(8, gcd(2*2*2*3, 2*2*2*5)); |
| 32 |
| 33 // 0 remaining powers-of-two in a. |
| 34 Expect.equals(6, gcd(2*3, 2*3*3)); |
| 35 // 1 remaining power-of-two in a. |
| 36 Expect.equals(6, gcd(2*2*3, 2*3*3)); |
| 37 // >1 remaining powers-of-two in a. |
| 38 Expect.equals(6, gcd(2*2*2*2*3, 2*3*3)); |
| 39 |
| 40 // 0 remaining powers-of-two in b. |
| 41 Expect.equals(6, gcd(2*3, 2*3*3)); |
| 42 // 1 remaining power-of-two in b. |
| 43 Expect.equals(6, gcd(2*3, 2*2*3)); |
| 44 // >1 remaining powers-of-two in b. |
| 45 Expect.equals(6, gcd(2*3, 2*2*2*3*3)); |
| 46 |
| 47 // Innermost 'if' |
| 48 // a > b. |
| 49 Expect.equals(6, gcd(2*2*3*5, 2*3)); |
| 50 // a == b. |
| 51 Expect.equals(6, gcd(2*3, 2*2*2*3)); |
| 52 // a < b. |
| 53 Expect.equals(6, gcd(2*3, 2*2*3*7)); |
| 54 |
| 55 // do while loop executions. |
| 56 // Executed 1 time. |
| 57 Expect.equals(6, gcd(2*3, 2*2*2*3)); |
| 58 // Executed >1 times. |
| 59 Expect.equals(6, gcd(2*3*3, 2*2*3*5)); |
| 60 |
| 61 // Medium int (mint) arguments. |
| 62 Expect.equals(pow(2, 61), gcd(pow(2, 61)*3, pow(2,62))); |
| 63 // 9079837958533 is the first prime after 2**48 / 31. |
| 64 Expect.equals(9079837958533, |
| 65 gcd(31*9079837958533, 37*9079837958533)); |
| 66 } |
| 67 |
| 68 main() { |
| 69 testGcd(); |
| 70 } |
OLD | NEW |