extended_euclidean_test.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * (C) Copyright Nick Thompson 2018.
  3. * Use, modification and distribution are subject to the
  4. * Boost Software License, Version 1.0. (See accompanying file
  5. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include "multiprecision_config.hpp"
  8. #ifndef DISABLE_MP_TESTS
  9. #include <boost/integer/extended_euclidean.hpp>
  10. #include <boost/cstdint.hpp>
  11. #include <boost/core/lightweight_test.hpp>
  12. #include <boost/multiprecision/cpp_int.hpp>
  13. #include <boost/integer/common_factor.hpp>
  14. using boost::multiprecision::int128_t;
  15. using boost::multiprecision::int256_t;
  16. using boost::integer::extended_euclidean;
  17. using boost::integer::gcd;
  18. template<class Z>
  19. void test_extended_euclidean()
  20. {
  21. // Stress test:
  22. //Z max_arg = std::numeric_limits<Z>::max();
  23. Z max_arg = 500;
  24. for (Z m = max_arg; m > 0; --m)
  25. {
  26. for (Z n = max_arg; n > 0; --n)
  27. {
  28. boost::integer::euclidean_result_t<Z> u = extended_euclidean(m, n);
  29. int256_t gcdmn = gcd(m, n);
  30. int256_t x = u.x;
  31. int256_t y = u.y;
  32. BOOST_TEST_EQ(u.gcd, gcdmn);
  33. BOOST_TEST_EQ(m*x + n*y, gcdmn);
  34. }
  35. }
  36. }
  37. int main()
  38. {
  39. test_extended_euclidean<boost::int16_t>();
  40. test_extended_euclidean<boost::int32_t>();
  41. test_extended_euclidean<boost::int64_t>();
  42. test_extended_euclidean<int128_t>();
  43. return boost::report_errors();;
  44. }
  45. #else
  46. int main()
  47. {
  48. return 0;
  49. }
  50. #endif