2018-01-28 14:47:14 -06:00
|
|
|
/*
|
|
|
|
* (C) Copyright Nick Thompson 2018.
|
|
|
|
* Use, modification and distribution are subject to the
|
|
|
|
* Boost Software License, Version 1.0. (See accompanying file
|
|
|
|
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#define BOOST_TEST_MODULE extended_euclidean_test
|
|
|
|
#include <boost/test/included/unit_test.hpp>
|
|
|
|
#include <boost/multiprecision/cpp_int.hpp>
|
|
|
|
#include <boost/integer/common_factor.hpp>
|
|
|
|
#include <boost/integer/extended_euclidean.hpp>
|
|
|
|
|
|
|
|
using boost::multiprecision::int128_t;
|
|
|
|
using boost::integer::extended_euclidean;
|
|
|
|
using boost::integer::gcd;
|
|
|
|
|
|
|
|
template<class Z>
|
|
|
|
void test_extended_euclidean()
|
|
|
|
{
|
2018-02-10 13:56:11 -06:00
|
|
|
std::cout << "Testing the extended Euclidean algorithm on type " << boost::typeindex::type_id<Z>().pretty_name() << "\n";
|
2018-10-25 09:38:16 -06:00
|
|
|
Z max_arg = 500;
|
2018-01-28 14:47:14 -06:00
|
|
|
for (Z m = 1; m < max_arg; ++m)
|
|
|
|
{
|
|
|
|
for (Z n = 1; n < max_arg; ++n)
|
|
|
|
{
|
2018-10-25 09:38:16 -06:00
|
|
|
boost::integer::euclidean_result_t u = extended_euclidean(m, n);
|
2018-01-28 14:47:14 -06:00
|
|
|
Z gcdmn = gcd(m, n);
|
2018-10-25 09:38:16 -06:00
|
|
|
Z x = u.x;
|
|
|
|
Z y = u.y;
|
|
|
|
BOOST_CHECK_EQUAL(u.gcd, gcdmn);
|
2018-01-28 14:47:14 -06:00
|
|
|
BOOST_CHECK_EQUAL(m*x + n*y, gcdmn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(extended_euclidean_test)
|
|
|
|
{
|
2018-02-10 13:56:11 -06:00
|
|
|
test_extended_euclidean<short int>();
|
2018-01-28 14:47:14 -06:00
|
|
|
test_extended_euclidean<int>();
|
|
|
|
test_extended_euclidean<long>();
|
2018-02-09 17:19:26 -06:00
|
|
|
test_extended_euclidean<long long>();
|
2018-01-28 14:47:14 -06:00
|
|
|
test_extended_euclidean<int128_t>();
|
|
|
|
}
|