| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 | 6 |
| 7 import "dart:math" show pow; | 7 import "dart:math" show pow; |
| 8 | 8 |
| 9 var smallNumber = 1234567890; // is 31-bit integer. | 9 var smallNumber = 1234567890; // is 31-bit integer. |
| 10 var mediumNumber = 1234567890123456; // is 53-bit integer | 10 var mediumNumber = 1234567890123456; // is 53-bit integer |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 if (value == other) return; | 134 if (value == other) return; |
| 135 testFunc(other, value); | 135 testFunc(other, value); |
| 136 testFunc(other, -value); | 136 testFunc(other, -value); |
| 137 testFunc(-other, value); | 137 testFunc(-other, value); |
| 138 testFunc(-other, -value); | 138 testFunc(-other, -value); |
| 139 } | 139 } |
| 140 | 140 |
| 141 // Test that gcd of value and other (non-negative) is expectedResult. | 141 // Test that gcd of value and other (non-negative) is expectedResult. |
| 142 // Tests all combinations of positive and negative values and order of | 142 // Tests all combinations of positive and negative values and order of |
| 143 // operands, so use positive values and order is not important. | 143 // operands, so use positive values and order is not important. |
| 144 test(value, other, [expectedResult]) { | 144 test(value, other, expectedResult) { |
| 145 assert(value % expectedResult == 0); // Check for bug in test. | 145 // Check for bug in test. |
| 146 assert(other % expectedResult == 0); | 146 assert(expectedResult == 0 || value % expectedResult == 0); |
| 147 assert(expectedResult == 0 || other % expectedResult == 0); |
| 147 callCombos(value, other, (a, b) { | 148 callCombos(value, other, (a, b) { |
| 148 var result = a.gcd(b); | 149 var result = a.gcd(b); |
| 149 /// Check that the result is a divisor. | 150 /// Check that the result is a divisor. |
| 150 Expect.equals(0, a % result, "$result | $a"); | 151 Expect.equals(0, result == 0 ? a : a % result, "$result | $a"); |
| 151 Expect.equals(0, b % result, "$result | $b"); | 152 Expect.equals(0, result == 0 ? b : b % result, "$result | $b"); |
| 152 // Check for bug in test. If assert fails, the expected value is too low, | 153 // Check for bug in test. If assert fails, the expected value is too low, |
| 153 // and the gcd call has found a greater common divisor. | 154 // and the gcd call has found a greater common divisor. |
| 154 assert(result >= expectedResult); | 155 assert(result >= expectedResult); |
| 155 Expect.equals(expectedResult, result, "$a.gcd($b)"); | 156 Expect.equals(expectedResult, result, "$a.gcd($b)"); |
| 156 }); | 157 }); |
| 157 } | 158 } |
| 158 | 159 |
| 159 // Test that gcd of value and other (non-negative) throws. | 160 // Test that gcd of value and other (non-negative) throws. |
| 160 testThrows(value, other) { | 161 testThrows(value, other) { |
| 161 callCombos(value, other, (a, b) { | 162 callCombos(value, other, (a, b) { |
| 162 Expect.throws(() => a.gcd(b), null, "$a.gcd($b)"); | 163 Expect.throws(() => a.gcd(b), null, "$a.gcd($b)"); |
| 163 }); | 164 }); |
| 164 } | 165 } |
| 165 | 166 |
| 166 // Throws if either operand is zero, and if both operands are zero. | 167 testThrows(2.5, 5); // Not a method on double. |
| 167 testThrows(0, 1000); | 168 testThrows(5, 2.5); // Not accepting non-int arguments. |
| 168 testThrows(0, 0); | |
| 169 | 169 |
| 170 // Format: | 170 // Format: |
| 171 // test(value1, value2, expectedResult); | 171 // test(value1, value2, expectedResult); |
| 172 test(1, 1, 1); // both are 1 | 172 test(1, 1, 1); // both are 1 |
| 173 test(1, 2, 1); // one is 1 | 173 test(1, 2, 1); // one is 1 |
| 174 test(3, 5, 1); // coprime. | 174 test(3, 5, 1); // coprime. |
| 175 test(37, 37, 37); // Same larger prime. | 175 test(37, 37, 37); // Same larger prime. |
| 176 | 176 |
| 177 test(9999, 7272, 909); // Larger numbers | 177 test(9999, 7272, 909); // Larger numbers |
| 178 | 178 |
| 179 test(0, 1000, 1000); // One operand is zero. |
| 180 test(0, 0, 0); // Both operands are zero. |
| 181 |
| 179 // Multiplying both operands by a number multiplies result by same number. | 182 // Multiplying both operands by a number multiplies result by same number. |
| 180 test(693, 609, 21); | 183 test(693, 609, 21); |
| 181 test(693 << 5, 609 << 5, 21 << 5); | 184 test(693 << 5, 609 << 5, 21 << 5); |
| 182 test(693 * 937, 609 * 937, 21 * 937); | 185 test(693 * 937, 609 * 937, 21 * 937); |
| 183 test(693 * pow(2, 32), 609 * pow(2, 32), 21 * pow(2, 32)); | 186 test(693 * pow(2, 32), 609 * pow(2, 32), 21 * pow(2, 32)); |
| 184 test(693 * pow(2, 52), 609 * pow(2, 52), 21 * pow(2, 52)); | 187 test(693 * pow(2, 52), 609 * pow(2, 52), 21 * pow(2, 52)); |
| 185 test(693 * pow(2, 53), 609 * pow(2, 53), 21 * pow(2, 53)); // Regression. | 188 test(693 * pow(2, 53), 609 * pow(2, 53), 21 * pow(2, 53)); // Regression. |
| 186 test(693 * pow(2, 99), 609 * pow(2, 99), 21 * pow(2, 99)); | 189 test(693 * pow(2, 99), 609 * pow(2, 99), 21 * pow(2, 99)); |
| 187 | 190 |
| 188 test(1234567890, 19, 1); | 191 test(1234567890, 19, 1); |
| 189 test(1234567890, 1000000001, 1); | 192 test(1234567890, 1000000001, 1); |
| 190 test(19, 1000000001, 19); | 193 test(19, 1000000001, 19); |
| 191 | 194 |
| 192 test(0x3FFFFFFF, 0x3FFFFFFF, 0x3FFFFFFF); | 195 test(0x3FFFFFFF, 0x3FFFFFFF, 0x3FFFFFFF); |
| 193 test(0x3FFFFFFF, 0x40000000, 1); | 196 test(0x3FFFFFFF, 0x40000000, 1); |
| 194 | 197 |
| 195 test(pow(2, 54), pow(2, 53), pow(2, 53)); | 198 test(pow(2, 54), pow(2, 53), pow(2, 53)); |
| 196 | 199 |
| 197 test((pow(2, 52) - 1) * pow(2, 14), | 200 test((pow(2, 52) - 1) * pow(2, 14), |
| 198 (pow(2, 26) - 1) * pow(2, 22), | 201 (pow(2, 26) - 1) * pow(2, 22), |
| 199 (pow(2, 26) - 1) * pow(2, 14)); | 202 (pow(2, 26) - 1) * pow(2, 14)); |
| 200 } | 203 } |
| 201 | 204 |
| 202 main() { | 205 main() { |
| 203 testModPow(); /// modPow: ok | 206 testModPow(); /// modPow: ok |
| 204 testModInverse(); | 207 testModInverse(); |
| 205 testGcd(); | 208 testGcd(); |
| 206 } | 209 } |
| 207 | 210 |
| OLD | NEW |