File: test_native_integer.cpp

package info (click to toggle)
boost1.62 1.62.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 686,420 kB
  • sloc: cpp: 2,609,004; xml: 972,558; ansic: 53,674; python: 32,437; sh: 8,829; asm: 3,071; cs: 2,121; makefile: 964; perl: 859; yacc: 472; php: 132; ruby: 94; f90: 55; sql: 13; csh: 6
file content (104 lines) | stat: -rw-r--r-- 2,907 bytes parent folder | download | duplicates (3)
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
102
103
104
///////////////////////////////////////////////////////////////
//  Copyright 2012 John Maddock. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_

//
// 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();
}