diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/multiprecision/test/test_native_integer.cpp | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/multiprecision/test/test_native_integer.cpp')
-rw-r--r-- | src/boost/libs/multiprecision/test/test_native_integer.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/boost/libs/multiprecision/test/test_native_integer.cpp b/src/boost/libs/multiprecision/test/test_native_integer.cpp new file mode 100644 index 00000000..0b2fe0cf --- /dev/null +++ b/src/boost/libs/multiprecision/test/test_native_integer.cpp @@ -0,0 +1,101 @@ +/////////////////////////////////////////////////////////////// +// Copyright 2012 John Maddock. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt + +// +// Compare arithmetic results using fixed_int to GMP results. +// + +#ifdef _MSC_VER +#define _SCL_SECURE_NO_WARNINGS +#endif + +#include <boost/multiprecision/integer.hpp> +#include "test.hpp" + +#ifdef BOOST_MSVC +#pragma warning(disable : 4146) +#endif + +template <class I, class H> +void test() +{ + using namespace boost::multiprecision; + + I i(0); + +#ifndef BOOST_NO_EXCEPTIONS + BOOST_CHECK_THROW(lsb(i), std::range_error); +#endif + BOOST_CHECK(bit_test(bit_set(i, 0), 0)); + BOOST_CHECK_EQUAL(bit_set(i, 0), 1); + BOOST_CHECK_EQUAL(bit_unset(i, 0), 0); + BOOST_CHECK_EQUAL(bit_flip(bit_set(i, 0), 0), 0); + + unsigned max_index = (std::numeric_limits<I>::digits) - 1; + BOOST_CHECK(bit_test(bit_set(i, max_index), max_index)); + BOOST_CHECK_EQUAL(bit_unset(i, max_index), 0); + BOOST_CHECK_EQUAL(bit_flip(bit_set(i, max_index), max_index), 0); + i = 0; + bit_set(i, max_index); + BOOST_CHECK_EQUAL(lsb(i), max_index); + BOOST_CHECK_EQUAL(msb(i), max_index); + bit_set(i, max_index / 2); + BOOST_CHECK_EQUAL(lsb(i), max_index / 2); + BOOST_CHECK_EQUAL(msb(i), max_index); + +#ifndef BOOST_NO_EXCEPTIONS + if (std::numeric_limits<I>::is_signed) + { + i = static_cast<I>(-1); + BOOST_CHECK_THROW(lsb(i), std::range_error); + } +#endif + H mx = (std::numeric_limits<H>::max)(); + + BOOST_CHECK_EQUAL(multiply(i, mx, mx), static_cast<I>(mx) * static_cast<I>(mx)); + BOOST_CHECK_EQUAL(add(i, mx, mx), static_cast<I>(mx) + static_cast<I>(mx)); + if (std::numeric_limits<I>::is_signed) + { + BOOST_CHECK_EQUAL(subtract(i, mx, static_cast<H>(-mx)), static_cast<I>(mx) - static_cast<I>(-mx)); + BOOST_CHECK_EQUAL(add(i, static_cast<H>(-mx), static_cast<H>(-mx)), static_cast<I>(-mx) + static_cast<I>(-mx)); + } + + i = (std::numeric_limits<I>::max)(); + I j = 12345; + I r, q; + divide_qr(i, j, q, r); + BOOST_CHECK_EQUAL(q, i / j); + BOOST_CHECK_EQUAL(r, i % j); + BOOST_CHECK_EQUAL(integer_modulus(i, j), i % j); + I p = 456; + BOOST_CHECK_EQUAL(powm(i, p, j), pow(cpp_int(i), static_cast<unsigned>(p)) % j); + + for (I i = 0; i < (2 < 8) - 1; ++i) + { + I j = i * i; + I s, r; + s = sqrt(j, r); + BOOST_CHECK_EQUAL(s, i); + BOOST_CHECK(r == 0); + j += 3; + s = sqrt(i, r); + BOOST_CHECK_EQUAL(s, i); + BOOST_CHECK(r == 3); + } +} + +int main() +{ + using namespace boost::multiprecision; + + test<boost::int16_t, boost::int8_t>(); + test<boost::int32_t, boost::int16_t>(); + test<boost::int64_t, boost::int32_t>(); + test<boost::uint16_t, boost::uint8_t>(); + test<boost::uint32_t, boost::uint16_t>(); + test<boost::uint64_t, boost::uint32_t>(); + + return boost::report_errors(); +} |