[ci skip] Add test of short int to see if there's any obvious places for overflow (none are obvious, but no guarantees they still aren't there). Print basic information about the test to console so that failures are easier to track down.

This commit is contained in:
Nick Thompson
2018-02-10 13:56:11 -06:00
parent 8c415f77b1
commit b3966428c4
4 changed files with 11 additions and 4 deletions

View File

@ -14,6 +14,7 @@ namespace boost { namespace integer {
// From "The Joy of Factoring", Algorithm 2.7.
// Should the tuple be a named tuple? Is that possible?
// Solves mx + ny = gcd(m,n). Returns tuple with (gcd(m,n), x, y).
// Is this the natural ordering?, or must people simply have to read the docs?
template<class Z>
std::tuple<Z, Z, Z> extended_euclidean(Z m, Z n)
{

View File

@ -13,11 +13,11 @@
namespace boost { namespace integer {
// From "The Joy of Factoring", Algorithm 2.7.
// The name is a bit verbose. Here's some others names I've found for this function:
// Here's some others names I've found for this function:
// PowerMod[a, -1, m] (Mathematica)
// mpz_invert (gmplib)
// modinv (some dude on stackoverflow)
// Would modular_inverse be sometimes mistaken as the modular *additive* inverse?
// Would mod_inverse be sometimes mistaken as the modular *additive* inverse?
template<class Z>
boost::optional<Z> mod_inverse(Z a, Z modulus)
{

View File

@ -17,6 +17,7 @@ using boost::integer::gcd;
template<class Z>
void test_extended_euclidean()
{
std::cout << "Testing the extended Euclidean algorithm on type " << boost::typeindex::type_id<Z>().pretty_name() << "\n";
Z max_arg = 1000;
for (Z m = 1; m < max_arg; ++m)
{
@ -34,6 +35,7 @@ void test_extended_euclidean()
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>();

View File

@ -4,19 +4,21 @@
* 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 modular_multiplicative_inverse_test
#define BOOST_TEST_MODULE mod_inverse_test
#include <boost/test/included/unit_test.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/integer/common_factor.hpp>
#include <boost/integer/mod_inverse.hpp>
using boost::multiprecision::int128_t;
using boost::multiprecision::int256_t;
using boost::integer::mod_inverse;
using boost::integer::gcd;
template<class Z>
void test_mod_inverse()
{
std::cout << "Testing the modular multiplicative inverse on type " << boost::typeindex::type_id<Z>().pretty_name() << "\n";
Z max_arg = 1000;
for (Z modulus = 2; modulus < max_arg; ++modulus)
{
@ -39,10 +41,12 @@ void test_mod_inverse()
}
}
BOOST_AUTO_TEST_CASE(extended_euclidean_test)
BOOST_AUTO_TEST_CASE(mod_inverse_test)
{
test_mod_inverse<short int>();
test_mod_inverse<int>();
test_mod_inverse<long>();
test_mod_inverse<long long>();
test_mod_inverse<int128_t>();
test_mod_inverse<int256_t>();
}