]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/random/test/test_const_mod.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / random / test / test_const_mod.cpp
1 /* test_const_mod.cpp
2 *
3 * Copyright Steven Watanabe 2011
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 * $Id$
9 *
10 */
11
12 #include <boost/random/detail/const_mod.hpp>
13
14 #include <boost/cstdint.hpp>
15 #include <boost/mpl/vector.hpp>
16
17 #define BOOST_TEST_MAIN
18 #include <boost/test/unit_test.hpp>
19
20 typedef boost::mpl::vector<
21 boost::int8_t,
22 boost::uint8_t
23 > int8_types;
24
25 BOOST_AUTO_TEST_CASE_TEMPLATE(test_mult8, IntType, int8_types) {
26 for(int i = 0; i < 127; ++i) {
27 for(int j = 0; j < 127; ++j) {
28 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 127>::mult(IntType(i), IntType(j))), i * j % 127);
29 }
30 }
31 int modulus = (std::numeric_limits<IntType>::max)() + 1;
32 for(int i = 0; i < modulus; ++i) {
33 for(int j = 0; j < modulus; ++j) {
34 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 0>::mult(IntType(i), IntType(j))), i * j % modulus);
35 }
36 }
37 }
38
39 BOOST_AUTO_TEST_CASE_TEMPLATE(test_add8, IntType, int8_types) {
40 for(int i = 0; i < 127; ++i) {
41 for(int j = 0; j < 127; ++j) {
42 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 127>::add(IntType(i), IntType(j))), (i + j) % 127);
43 }
44 }
45 {
46 const int modulus = boost::integer_traits<IntType>::const_max;
47 for(int i = 0; i < modulus; ++i) {
48 for(int j = 0; j < modulus; ++j) {
49 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, modulus>::add(IntType(i), IntType(j))), (i + j) % modulus);
50 }
51 }
52 }
53 {
54 int modulus = (std::numeric_limits<IntType>::max)() + 1;
55 for(int i = 0; i < modulus; ++i) {
56 for(int j = 0; j < modulus; ++j) {
57 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 0>::add(IntType(i), IntType(j))), (i + j) % modulus);
58 }
59 }
60 }
61 }
62
63 BOOST_AUTO_TEST_CASE_TEMPLATE(test_mult_add8, IntType, int8_types) {
64 for(int i = 0; i < 127; i += 5) {
65 for(int j = 0; j < 127; j += 3) {
66 for(int k = 0; k < 127; k += 3) {
67 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 127>::mult_add(IntType(i), IntType(j), IntType(k))), (i * j + k) % 127);
68 }
69 }
70 }
71 {
72 int modulus = (std::numeric_limits<IntType>::max)() + 1;
73 for(int i = 0; i < modulus; i += 5) {
74 for(int j = 0; j < modulus; j += 3) {
75 for(int k = 0; k < modulus; k += 3) {
76 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 0>::mult_add(IntType(i), IntType(j), IntType(k))), (i * j + k) % modulus);
77 }
78 }
79 }
80 }
81 }
82
83 BOOST_AUTO_TEST_CASE_TEMPLATE(test_invert8, IntType, int8_types) {
84 for(int i = 1; i < 127; ++i) {
85 IntType inverse = boost::random::const_mod<IntType, 127>::invert(IntType(i));
86 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 127>::mult(IntType(i), inverse)), 1);
87 }
88 int modulus = (std::numeric_limits<IntType>::max)() + 1;
89 for(int i = 1; i < modulus; i += 2) {
90 IntType inverse = boost::random::const_mod<IntType, 0>::invert(IntType(i));
91 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 0>::mult(IntType(i), inverse)), 1);
92 }
93 }
94
95 typedef boost::mpl::vector<
96 boost::int32_t,
97 boost::uint32_t
98 > int32_types;
99
100 BOOST_AUTO_TEST_CASE_TEMPLATE(test_mult32, IntType, int32_types) {
101 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(0, 0)), IntType(0));
102 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(0, 2147483562)), IntType(0));
103 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(2147483562, 0)), IntType(0));
104 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(2147483562, 2147483562)), IntType(1));
105 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(1234567890, 1234657890)), IntType(813106682));
106 }
107
108 BOOST_AUTO_TEST_CASE_TEMPLATE(test_add32, IntType, int32_types) {
109 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::add(0, 0)), IntType(0));
110 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::add(0, 2147483562)), IntType(2147483562));
111 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::add(2147483562, 0)), IntType(2147483562));
112 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::add(2147483562, 2147483562)), IntType(2147483561));
113 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::add(1234567890, 1234657890)), IntType(321742217));
114 }
115
116 BOOST_AUTO_TEST_CASE_TEMPLATE(test_mult_add32, IntType, int32_types) {
117 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult_add(0, 0, 0)), IntType(0));
118 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult_add(0, 2147483562, 827364)), IntType(827364));
119 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult_add(2147483562, 0, 827364)), IntType(827364));
120 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult_add(2147483562, 2147483562, 2147483562)), IntType(0));
121 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult_add(1234567890, 1234657890, 1726384759)), IntType(392007878));
122 }
123
124 BOOST_AUTO_TEST_CASE_TEMPLATE(test_invert32, IntType, int32_types) {
125 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::invert(0)), IntType(0));
126 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult_add(0, 0, 0)), IntType(0));
127 IntType inverse;
128 inverse = boost::random::const_mod<IntType, 2147483563>::invert(2147483562);
129 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(2147483562, inverse)), IntType(1));
130 inverse = boost::random::const_mod<IntType, 2147483563>::invert(1234567890);
131 BOOST_CHECK_EQUAL((boost::random::const_mod<IntType, 2147483563>::mult(1234567890, inverse)), IntType(1));
132 }
133
134 #if !defined(BOOST_NO_INT64_T)
135
136 typedef boost::mpl::vector<
137 boost::int64_t,
138 boost::uint64_t
139 > int64_types;
140
141 BOOST_AUTO_TEST_CASE_TEMPLATE(test_mult64, IntType, int64_types) {
142 typedef boost::random::const_mod<IntType, INT64_C(2147483563652738498)> const_mod_type;
143 BOOST_CHECK_EQUAL((const_mod_type::mult(0, 0)), IntType(0));
144 BOOST_CHECK_EQUAL((const_mod_type::mult(0, 2147483562)), IntType(0));
145 BOOST_CHECK_EQUAL((const_mod_type::mult(2147483562, 0)), IntType(0));
146 BOOST_CHECK_EQUAL((const_mod_type::mult(2147483562, 2147483562)), IntType(INT64_C(316718521754730848)));
147 BOOST_CHECK_EQUAL((const_mod_type::mult(1234567890, 1234657890)), IntType(INT64_C(1524268986129152100)));
148 BOOST_CHECK_EQUAL((const_mod_type::mult(INT64_C(1234567890726352938), INT64_C(1234657890736453927))), IntType(INT64_C(88656187017794672)));
149 }
150
151 BOOST_AUTO_TEST_CASE_TEMPLATE(test_add64, IntType, int64_types) {
152 typedef boost::random::const_mod<IntType, INT64_C(2147483563652738498)> const_mod_type;
153 BOOST_CHECK_EQUAL((const_mod_type::add(0, 0)), IntType(0));
154 BOOST_CHECK_EQUAL((const_mod_type::add(0, 2147483562)), IntType(2147483562));
155 BOOST_CHECK_EQUAL((const_mod_type::add(2147483562, 0)), IntType(2147483562));
156 BOOST_CHECK_EQUAL((const_mod_type::add(2147483562, 2147483562)), IntType(4294967124U));
157 BOOST_CHECK_EQUAL((const_mod_type::add(1234567890, 1234657890)), IntType(2469225780U));
158 BOOST_CHECK_EQUAL((const_mod_type::add(INT64_C(1234567890726352938), INT64_C(1234657890736453927))), IntType(INT64_C(321742217810068367)));
159 BOOST_CHECK_EQUAL((const_mod_type::add(INT64_C(2147483563652738490), 8)), IntType(0));
160 }
161
162 BOOST_AUTO_TEST_CASE_TEMPLATE(test_mult_add64, IntType, int64_types) {
163 typedef boost::random::const_mod<IntType, INT64_C(2147483563652738498)> const_mod_type;
164 BOOST_CHECK_EQUAL((const_mod_type::mult_add(0, 0, 0)), IntType(0));
165 BOOST_CHECK_EQUAL((const_mod_type::mult_add(0, 2147483562, 827364)), IntType(827364));
166 BOOST_CHECK_EQUAL((const_mod_type::mult_add(2147483562, 0, 827364)), IntType(827364));
167 BOOST_CHECK_EQUAL((const_mod_type::mult_add(2147483562, 2147483562, 2147483562)), IntType(INT64_C(316718523902214410)));
168 BOOST_CHECK_EQUAL((const_mod_type::mult_add(1234567890, 1234657890, 1726384759)), IntType(INT64_C(1524268987855536859)));
169 BOOST_CHECK_EQUAL((const_mod_type::mult_add(INT64_C(1234567890726352938), INT64_C(1234657890736453927), INT64_C(1726384759726488649))), IntType(INT64_C(1815040946744283321)));
170 }
171
172 BOOST_AUTO_TEST_CASE_TEMPLATE(test_invert64, IntType, int64_types) {
173 typedef boost::random::const_mod<IntType, INT64_C(2147483563652738498)> const_mod_type;
174 BOOST_CHECK_EQUAL((const_mod_type::invert(0)), IntType(0));
175 BOOST_CHECK_EQUAL((const_mod_type::mult_add(0, 0, 0)), IntType(0));
176 IntType inverse;
177 inverse = const_mod_type::invert(INT64_C(7362947769));
178 BOOST_CHECK_EQUAL((const_mod_type::mult(INT64_C(7362947769), inverse)), IntType(1));
179 inverse = const_mod_type::invert(INT64_C(1263142436887493875));
180 BOOST_CHECK_EQUAL((const_mod_type::mult(INT64_C(1263142436887493875), inverse)), IntType(1));
181 }
182
183 #endif