File: test_native_integer.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (101 lines) | stat: -rw-r--r-- 2,894 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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::domain_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::domain_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<std::int16_t, boost::int8_t>();
   test<std::int32_t, std::int16_t>();
   test<std::int64_t, std::int32_t>();
   test<std::uint16_t, boost::uint8_t>();
   test<std::uint32_t, std::uint16_t>();
   test<std::uint64_t, std::uint32_t>();

   return boost::report_errors();
}