mirror of
https://github.com/boostorg/integer.git
synced 2025-07-29 12:17:13 +02:00
[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:
@ -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)
|
||||
{
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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>();
|
||||
|
@ -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>();
|
||||
}
|
||||
|
Reference in New Issue
Block a user