|
17 | 17 | #include <cassert> |
18 | 18 | #include <climits> |
19 | 19 | #include <cstdint> |
| 20 | +#include <random> |
20 | 21 | #include <type_traits> |
21 | 22 |
|
22 | 23 | #include "test_macros.h" |
@@ -48,6 +49,74 @@ constexpr bool test0(int in1, int in2, int out) |
48 | 49 | return true; |
49 | 50 | } |
50 | 51 |
|
| 52 | +template <typename T> |
| 53 | +T basic_gcd_(T m, T n) { |
| 54 | + return n == 0 ? m : basic_gcd_<T>(n, m % n); |
| 55 | +} |
| 56 | + |
| 57 | +template <typename T> |
| 58 | +T basic_gcd(T m, T n) { |
| 59 | + using Tp = std::make_unsigned_t<T>; |
| 60 | + if (m < 0 && m != std::numeric_limits<T>::min()) |
| 61 | + m = -m; |
| 62 | + if (n < 0 && n != std::numeric_limits<T>::min()) |
| 63 | + n = -n; |
| 64 | + return basic_gcd_(static_cast<Tp>(m), static_cast<Tp>(n)); |
| 65 | +} |
| 66 | + |
| 67 | +template <typename Input> |
| 68 | +void do_fuzzy_tests() { |
| 69 | + std::mt19937 gen(1938); |
| 70 | + std::uniform_int_distribution<Input> distrib; |
| 71 | + |
| 72 | + constexpr int nb_rounds = 10000; |
| 73 | + for (int i = 0; i < nb_rounds; ++i) { |
| 74 | + Input n = distrib(gen); |
| 75 | + Input m = distrib(gen); |
| 76 | + assert(std::gcd(n, m) == basic_gcd(n, m)); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +template <typename Input> |
| 81 | +void do_limit_tests() { |
| 82 | + Input inputs[] = { |
| 83 | + // The behavior of std::gcd is undefined if the absolute value of one of its |
| 84 | + // operand is not representable in the result type. |
| 85 | + std::numeric_limits<Input>::min() + (std::is_signed<Input>::value ? 3 : 0), |
| 86 | + std::numeric_limits<Input>::min() + 1, |
| 87 | + std::numeric_limits<Input>::min() + 2, |
| 88 | + std::numeric_limits<Input>::max(), |
| 89 | + std::numeric_limits<Input>::max() - 1, |
| 90 | + std::numeric_limits<Input>::max() - 2, |
| 91 | + 0, |
| 92 | + 1, |
| 93 | + 2, |
| 94 | + 3, |
| 95 | + 4, |
| 96 | + 5, |
| 97 | + 6, |
| 98 | + 7, |
| 99 | + 8, |
| 100 | + 9, |
| 101 | + 10, |
| 102 | + (Input)-1, |
| 103 | + (Input)-2, |
| 104 | + (Input)-3, |
| 105 | + (Input)-4, |
| 106 | + (Input)-5, |
| 107 | + (Input)-6, |
| 108 | + (Input)-7, |
| 109 | + (Input)-8, |
| 110 | + (Input)-9, |
| 111 | + (Input)-10, |
| 112 | + }; |
| 113 | + |
| 114 | + for (auto n : inputs) { |
| 115 | + for (auto m : inputs) { |
| 116 | + assert(std::gcd(n, m) == basic_gcd(n, m)); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
51 | 120 |
|
52 | 121 | template <typename Input1, typename Input2 = Input1> |
53 | 122 | constexpr bool do_test(int = 0) |
@@ -143,5 +212,23 @@ int main(int argc, char**) |
143 | 212 | assert(res == 2); |
144 | 213 | } |
145 | 214 |
|
146 | | - return 0; |
| 215 | + do_fuzzy_tests<std::int8_t>(); |
| 216 | + do_fuzzy_tests<std::int16_t>(); |
| 217 | + do_fuzzy_tests<std::int32_t>(); |
| 218 | + do_fuzzy_tests<std::int64_t>(); |
| 219 | + do_fuzzy_tests<std::uint8_t>(); |
| 220 | + do_fuzzy_tests<std::uint16_t>(); |
| 221 | + do_fuzzy_tests<std::uint32_t>(); |
| 222 | + do_fuzzy_tests<std::uint64_t>(); |
| 223 | + |
| 224 | + do_limit_tests<std::int8_t>(); |
| 225 | + do_limit_tests<std::int16_t>(); |
| 226 | + do_limit_tests<std::int32_t>(); |
| 227 | + do_limit_tests<std::int64_t>(); |
| 228 | + do_limit_tests<std::uint8_t>(); |
| 229 | + do_limit_tests<std::uint16_t>(); |
| 230 | + do_limit_tests<std::uint32_t>(); |
| 231 | + do_limit_tests<std::uint64_t>(); |
| 232 | + |
| 233 | + return 0; |
147 | 234 | } |
0 commit comments