test_native_integer.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2012 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. //
  6. // Compare arithmetic results using fixed_int to GMP results.
  7. //
  8. #ifdef _MSC_VER
  9. #define _SCL_SECURE_NO_WARNINGS
  10. #endif
  11. #include <boost/multiprecision/integer.hpp>
  12. #include "test.hpp"
  13. #ifdef BOOST_MSVC
  14. #pragma warning(disable : 4146)
  15. #endif
  16. template <class I, class H>
  17. void test()
  18. {
  19. using namespace boost::multiprecision;
  20. I i(0);
  21. #ifndef BOOST_NO_EXCEPTIONS
  22. BOOST_CHECK_THROW(lsb(i), std::range_error);
  23. #endif
  24. BOOST_CHECK(bit_test(bit_set(i, 0), 0));
  25. BOOST_CHECK_EQUAL(bit_set(i, 0), 1);
  26. BOOST_CHECK_EQUAL(bit_unset(i, 0), 0);
  27. BOOST_CHECK_EQUAL(bit_flip(bit_set(i, 0), 0), 0);
  28. unsigned max_index = (std::numeric_limits<I>::digits) - 1;
  29. BOOST_CHECK(bit_test(bit_set(i, max_index), max_index));
  30. BOOST_CHECK_EQUAL(bit_unset(i, max_index), 0);
  31. BOOST_CHECK_EQUAL(bit_flip(bit_set(i, max_index), max_index), 0);
  32. i = 0;
  33. bit_set(i, max_index);
  34. BOOST_CHECK_EQUAL(lsb(i), max_index);
  35. BOOST_CHECK_EQUAL(msb(i), max_index);
  36. bit_set(i, max_index / 2);
  37. BOOST_CHECK_EQUAL(lsb(i), max_index / 2);
  38. BOOST_CHECK_EQUAL(msb(i), max_index);
  39. #ifndef BOOST_NO_EXCEPTIONS
  40. if (std::numeric_limits<I>::is_signed)
  41. {
  42. i = static_cast<I>(-1);
  43. BOOST_CHECK_THROW(lsb(i), std::range_error);
  44. }
  45. #endif
  46. H mx = (std::numeric_limits<H>::max)();
  47. BOOST_CHECK_EQUAL(multiply(i, mx, mx), static_cast<I>(mx) * static_cast<I>(mx));
  48. BOOST_CHECK_EQUAL(add(i, mx, mx), static_cast<I>(mx) + static_cast<I>(mx));
  49. if (std::numeric_limits<I>::is_signed)
  50. {
  51. BOOST_CHECK_EQUAL(subtract(i, mx, static_cast<H>(-mx)), static_cast<I>(mx) - static_cast<I>(-mx));
  52. BOOST_CHECK_EQUAL(add(i, static_cast<H>(-mx), static_cast<H>(-mx)), static_cast<I>(-mx) + static_cast<I>(-mx));
  53. }
  54. i = (std::numeric_limits<I>::max)();
  55. I j = 12345;
  56. I r, q;
  57. divide_qr(i, j, q, r);
  58. BOOST_CHECK_EQUAL(q, i / j);
  59. BOOST_CHECK_EQUAL(r, i % j);
  60. BOOST_CHECK_EQUAL(integer_modulus(i, j), i % j);
  61. I p = 456;
  62. BOOST_CHECK_EQUAL(powm(i, p, j), pow(cpp_int(i), static_cast<unsigned>(p)) % j);
  63. for (I i = 0; i < (2 < 8) - 1; ++i)
  64. {
  65. I j = i * i;
  66. I s, r;
  67. s = sqrt(j, r);
  68. BOOST_CHECK_EQUAL(s, i);
  69. BOOST_CHECK(r == 0);
  70. j += 3;
  71. s = sqrt(i, r);
  72. BOOST_CHECK_EQUAL(s, i);
  73. BOOST_CHECK(r == 3);
  74. }
  75. }
  76. int main()
  77. {
  78. using namespace boost::multiprecision;
  79. test<boost::int16_t, boost::int8_t>();
  80. test<boost::int32_t, boost::int16_t>();
  81. test<boost::int64_t, boost::int32_t>();
  82. test<boost::uint16_t, boost::uint8_t>();
  83. test<boost::uint32_t, boost::uint16_t>();
  84. test<boost::uint64_t, boost::uint32_t>();
  85. return boost::report_errors();
  86. }