Make changes suggested by reviewer.

This commit is contained in:
Nick Thompson
2018-10-29 13:10:02 -06:00
parent 9312962a68
commit a7da90a79e
4 changed files with 12 additions and 14 deletions

View File

@ -22,15 +22,13 @@ struct euclidean_result_t {
Z y; Z y;
}; };
using std::numeric_limits;
template<class Z> template<class Z>
euclidean_result_t<typename std::enable_if<numeric_limits< Z >::is_signed, Z>::type> euclidean_result_t<typename std::enable_if<std::numeric_limits< Z >::is_signed, Z>::type>
extended_euclidean(Z m, Z n) extended_euclidean(Z m, Z n)
{ {
if (m < 1 || n < 1) if (m < 1 || n < 1)
{ {
BOOST_THROW_EXCEPTION(std::domain_error("Arguments must be strictly positive.\n")); BOOST_THROW_EXCEPTION(std::domain_error("Arguments must be strictly positive."));
} }
bool swapped = false; bool swapped = false;

View File

@ -25,7 +25,7 @@ boost::optional<Z> mod_inverse(Z a, Z modulus)
{ {
if (modulus < 2) if (modulus < 2)
{ {
BOOST_THROW_EXCEPTION(std::domain_error("Modulus must be > 1.\n")); BOOST_THROW_EXCEPTION(std::domain_error("Modulus must be > 1."));
} }
// make sure a < modulus: // make sure a < modulus:
a = a % modulus; a = a % modulus;

View File

@ -11,7 +11,7 @@
#endif #endif
#ifndef DISABLE_MP_TESTS #ifndef DISABLE_MP_TESTS
#include <cassert> #include <boost/core/lightweight_test.hpp>
#include <boost/multiprecision/cpp_int.hpp> #include <boost/multiprecision/cpp_int.hpp>
#include <boost/integer/common_factor.hpp> #include <boost/integer/common_factor.hpp>
#include <boost/integer/extended_euclidean.hpp> #include <boost/integer/extended_euclidean.hpp>
@ -35,8 +35,8 @@ void test_extended_euclidean()
int256_t gcdmn = gcd(m, n); int256_t gcdmn = gcd(m, n);
int256_t x = u.x; int256_t x = u.x;
int256_t y = u.y; int256_t y = u.y;
assert(u.gcd == gcdmn); BOOST_TEST_EQ(u.gcd, gcdmn);
assert(m*x + n*y == gcdmn); BOOST_TEST_EQ(m*x + n*y, gcdmn);
} }
} }
} }
@ -50,7 +50,7 @@ int main()
test_extended_euclidean<int64_t>(); test_extended_euclidean<int64_t>();
test_extended_euclidean<int128_t>(); test_extended_euclidean<int128_t>();
return 0; return boost::report_errors();;
} }
#else #else
int main() int main()

View File

@ -11,7 +11,7 @@
#endif #endif
#ifndef DISABLE_MP_TESTS #ifndef DISABLE_MP_TESTS
#include <cassert> #include <boost/core/lightweight_test.hpp>
#include <boost/multiprecision/cpp_int.hpp> #include <boost/multiprecision/cpp_int.hpp>
#include <boost/integer/common_factor.hpp> #include <boost/integer/common_factor.hpp>
#include <boost/integer/mod_inverse.hpp> #include <boost/integer/mod_inverse.hpp>
@ -39,17 +39,17 @@ void test_mod_inverse()
// Should fail if gcd(a, mod) != 1: // Should fail if gcd(a, mod) != 1:
if (gcdam > 1) if (gcdam > 1)
{ {
assert(!inv_a); BOOST_TEST(!inv_a);
} }
else else
{ {
assert(inv_a.value() > 0); BOOST_TEST(inv_a.value() > 0);
// Cast to a bigger type so the multiplication won't overflow. // Cast to a bigger type so the multiplication won't overflow.
int256_t a_inv = inv_a.value(); int256_t a_inv = inv_a.value();
int256_t big_a = a; int256_t big_a = a;
int256_t m = modulus; int256_t m = modulus;
int256_t outta_be_one = (a_inv*big_a) % m; int256_t outta_be_one = (a_inv*big_a) % m;
assert(outta_be_one == 1); BOOST_TEST_EQ(outta_be_one, 1);
} }
} }
} }
@ -62,7 +62,7 @@ int main()
test_mod_inverse<int64_t>(); test_mod_inverse<int64_t>();
test_mod_inverse<int128_t>(); test_mod_inverse<int128_t>();
return 0; return boost::report_errors();
} }
#else #else
int main() int main()