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 // See gcd_test.dart first. This file contains only the tests that need Bigint | |
11 // or would fail in dart2js compatibility mode. | |
12 | |
13 class BigintTest { | |
14 // 8 random primes less within [2^60, 2^64] | |
15 final int p1 = 6714601027348841563; | |
16 final int p2 = 13464639003769154407; | |
17 final int p3 = 9519493673324441563; | |
18 final int p4 = 7064784879742017229; | |
19 final int p5 = 18364232533526122157; | |
20 final int p6 = 2099437422495963203; | |
21 final int p7 = 10166792634765954647; | |
22 final int p8 = 2745073355742392083; | |
23 | |
24 void testGcdWithBigints() { | |
25 Expect.equals(pow(2, 63)*3, gcd(pow(2, 64)*3*5, pow(2, 63)*3*7)); | |
26 // 595056260442243647 is the first prime after 2**64 / 31. | |
27 Expect.equals(595056260442243647, | |
28 gcd(31*595056260442243647, 37*595056260442243647)); | |
29 } | |
30 | |
31 void testGcdextWithBigints() { | |
32 Expect.listEquals([pow(2, 63)*3, -2, 3], | |
33 gcdext(pow(2, 64)*3*5, pow(2, 63)*3*7)); | |
34 // 595056260442243647 is the first prime after 2**64 / 31. | |
35 Expect.listEquals([595056260442243647, 6, -5], | |
36 gcdext(31*595056260442243647, 37*595056260442243647)); | |
37 Expect.listEquals([1, 970881267037344823, -970881267037344822], | |
38 gcdext(73786976294838206473, 73786976294838206549)); | |
39 } | |
40 | |
41 void testInvertWithBigints() { | |
42 // 9223372036854775837 is the first prime after 2^63. | |
43 Expect.equals(2093705452366034115, invert(1000, 9223372036854775837)); | |
44 Expect.equals(970547769322117497, invert(1000000, 9223372036854775837)); | |
45 | |
46 Expect.equals(796993873408264695, invert(p1, p2)); | |
47 Expect.equals(2302612976619580647501352961102487476, invert(p3*p4, p5*p6)); | |
48 | |
49 Expect.throws(() => invert(p1 * p2, p2 * p3), | |
50 (e) => e is IntegerDivisionByZeroException); | |
51 } | |
52 | |
53 void testLcmWithBigints() { | |
54 Expect.equals(pow(2, 64)*3*5*7, lcm(pow(2, 64)*3*5, pow(2,63)*3*7)); | |
55 // 595056260442243647 is the first prime after 2**64 / 31. | |
56 Expect.equals(31*37*595056260442243647, | |
57 lcm(31*595056260442243647, 37*595056260442243647)); | |
58 | |
59 Expect.equals(p1 * p2, lcm(p1, p2)); | |
60 Expect.equals(p1 * p2 * p3, lcm(p1 * p2, p2 * p3)); | |
61 Expect.equals(p4 * p5, lcm(p4 * p5, p4)); | |
62 } | |
63 | |
64 void testPowmodWithBigints() { | |
65 // A modulus value greater than 94906265 can result in an intermediate step | |
66 // evaluating to a bigint (base * base). | |
67 // 9079837958533 is the first prime after 2**48 / 31. | |
68 Expect.equals(1073741824, powmod(pow(2, 30), 1, 9079837958533)); | |
69 Expect.equals(9079822119301, powmod(pow(2, 30), 2, 9079837958533)); | |
70 Expect.equals(8370475851674, powmod(pow(2, 30), 3, 9079837958533)); | |
71 Expect.equals(5725645469433, powmod(pow(2, 30), 4, 9079837958533)); | |
72 | |
73 // bigint base | |
74 Expect.equals(10435682577172878912, powmod(p1, 31, p2)); | |
75 Expect.equals(2171334335785523204, powmod(p1 * p2, 5, p3)); | |
76 Expect.equals(2075559997960884603, powmod(p1 * 120, 8, p2)); | |
77 | |
78 // bigint exponent | |
79 Expect.equals(236325130834703514, powmod(pow(2, 64), p1, p4)); | |
80 Expect.equals(1733635560285390571, powmod(1000000, p5, p6)); | |
81 | |
82 // bigint modulus | |
83 Expect.equals(4740839599282053976, powmod(p7, p8, p1)); | |
84 Expect.equals(13037487407831899228197227177643459429, | |
85 powmod(p2, p3, p4 * p5)); | |
86 } | |
87 | |
88 testMain() { | |
89 // Source for expected values is Wolfram Alpha (presumably just GMP). | |
90 testGcdWithBigints(); | |
91 testGcdextWithBigints(); | |
92 testInvertWithBigints(); | |
93 testLcmWithBigints(); | |
94 testPowmodWithBigints(); | |
95 } | |
96 } | |
97 | |
98 main() { | |
99 new BigintTest().testMain(); | |
Lasse Reichstein Nielsen
2014/08/26 07:44:26
I miss tests with negative integers.
srawlins
2014/08/29 06:43:25
Eek!!! Good call. I've added them to each test met
| |
100 } | |
OLD | NEW |