a*p % m may overflow, do not perform naive multiplication in unit tests or undefined behavior may result. [CI SKIP]

This commit is contained in:
Nick Thompson
2018-10-26 11:19:43 -06:00
parent 3f1603938c
commit 2d463f3ee7
4 changed files with 35 additions and 34 deletions

View File

@ -11,6 +11,7 @@
#include <boost/integer/extended_euclidean.hpp>
using boost::multiprecision::int128_t;
using boost::multiprecision::int256_t;
using boost::integer::extended_euclidean;
using boost::integer::gcd;
@ -18,26 +19,29 @@ template<class Z>
void test_extended_euclidean()
{
std::cout << "Testing the extended Euclidean algorithm on type " << boost::typeindex::type_id<Z>().pretty_name() << "\n";
// Stress test:
//Z max_arg = std::numeric_limits<Z>::max();
Z max_arg = 500;
for (Z m = 1; m < max_arg; ++m)
for (Z m = max_arg; m > 0; --m)
{
for (Z n = 1; n < max_arg; ++n)
for (Z n = m; n > 0; --n)
{
boost::integer::euclidean_result_t u = extended_euclidean(m, n);
Z gcdmn = gcd(m, n);
Z x = u.x;
Z y = u.y;
int256_t gcdmn = gcd(m, n);
int256_t x = u.x;
int256_t y = u.y;
BOOST_CHECK_EQUAL(u.gcd, gcdmn);
BOOST_CHECK_EQUAL(m*x + n*y, gcdmn);
}
}
}
BOOST_AUTO_TEST_CASE(extended_euclidean_test)
{
test_extended_euclidean<short int>();
test_extended_euclidean<int>();
test_extended_euclidean<long>();
test_extended_euclidean<long long>();
test_extended_euclidean<int16_t>();
test_extended_euclidean<int32_t>();
test_extended_euclidean<int64_t>();
test_extended_euclidean<int128_t>();
}