]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Boost integer_log2.hpp test program --------------------------------------// |
2 | ||
3 | // (C) Copyright Andrey Semashev 2021. | |
4 | // Distributed under the Boost Software License, Version 1.0. (See | |
5 | // accompanying file LICENSE_1_0.txt or copy at | |
6 | // https://www.boost.org/LICENSE_1_0.txt) | |
7 | ||
8 | // See https://www.boost.org for most recent version including documentation. | |
9 | ||
10 | #include <boost/config.hpp> | |
11 | #include <boost/cstdint.hpp> | |
12 | #include <boost/integer/integer_log2.hpp> | |
13 | ||
14 | #include <boost/core/lightweight_test.hpp> | |
15 | ||
16 | #include <iostream> | |
17 | ||
18 | #include "multiprecision_config.hpp" | |
19 | ||
20 | #if !defined(DISABLE_MP_TESTS) | |
21 | #include <boost/multiprecision/cpp_int.hpp> | |
22 | #endif | |
23 | ||
24 | // Macros to compact code | |
25 | #define PRIVATE_LB_TEST( v, e ) BOOST_TEST( ::boost::integer_log2((v)) == e ) | |
26 | ||
27 | #define PRIVATE_PRINT_LB( v ) ::std::cout << "boost::integer_log2(" << (v) \ | |
28 | << ") = " << ::boost::integer_log2((v)) << '.' << ::std::endl | |
29 | ||
30 | // Control to check for a compile-time error | |
31 | #ifndef CONTROL_LB_0_TEST | |
32 | #define PRIVATE_LB_0_TEST | |
33 | #else | |
34 | #define PRIVATE_LB_0_TEST PRIVATE_PRINT_LB( 0 ) | |
35 | #endif | |
36 | ||
37 | // Main testing function | |
38 | int main() | |
39 | { | |
40 | std::cout << "Doing tests on integer_log2." << std::endl; | |
41 | ||
42 | PRIVATE_LB_0_TEST; | |
43 | ||
44 | PRIVATE_LB_TEST( (unsigned char)1, 0 ); | |
45 | PRIVATE_LB_TEST( (unsigned char)2, 1 ); | |
46 | ||
47 | PRIVATE_LB_TEST( 1, 0 ); | |
48 | ||
49 | PRIVATE_LB_TEST( 2, 1 ); | |
50 | PRIVATE_LB_TEST( 3, 1 ); | |
51 | ||
52 | PRIVATE_LB_TEST( 4, 2 ); | |
53 | PRIVATE_LB_TEST( 5, 2 ); | |
54 | PRIVATE_LB_TEST( 6, 2 ); | |
55 | PRIVATE_LB_TEST( 7, 2 ); | |
56 | ||
57 | PRIVATE_LB_TEST( 8, 3 ); | |
58 | PRIVATE_LB_TEST( 9, 3 ); | |
59 | PRIVATE_LB_TEST( 10, 3 ); | |
60 | PRIVATE_LB_TEST( 11, 3 ); | |
61 | PRIVATE_LB_TEST( 12, 3 ); | |
62 | PRIVATE_LB_TEST( 13, 3 ); | |
63 | PRIVATE_LB_TEST( 14, 3 ); | |
64 | PRIVATE_LB_TEST( 15, 3 ); | |
65 | ||
66 | PRIVATE_LB_TEST( 16, 4 ); | |
67 | PRIVATE_LB_TEST( 17, 4 ); | |
68 | PRIVATE_LB_TEST( 18, 4 ); | |
69 | PRIVATE_LB_TEST( 19, 4 ); | |
70 | PRIVATE_LB_TEST( 20, 4 ); | |
71 | PRIVATE_LB_TEST( 21, 4 ); | |
72 | PRIVATE_LB_TEST( 22, 4 ); | |
73 | PRIVATE_LB_TEST( 23, 4 ); | |
74 | PRIVATE_LB_TEST( 24, 4 ); | |
75 | PRIVATE_LB_TEST( 25, 4 ); | |
76 | PRIVATE_LB_TEST( 26, 4 ); | |
77 | PRIVATE_LB_TEST( 27, 4 ); | |
78 | PRIVATE_LB_TEST( 28, 4 ); | |
79 | PRIVATE_LB_TEST( 29, 4 ); | |
80 | PRIVATE_LB_TEST( 30, 4 ); | |
81 | PRIVATE_LB_TEST( 31, 4 ); | |
82 | ||
83 | PRIVATE_LB_TEST( 32, 5 ); | |
84 | PRIVATE_LB_TEST( 33, 5 ); | |
85 | PRIVATE_LB_TEST( 34, 5 ); | |
86 | PRIVATE_LB_TEST( 35, 5 ); | |
87 | PRIVATE_LB_TEST( 36, 5 ); | |
88 | PRIVATE_LB_TEST( 37, 5 ); | |
89 | PRIVATE_LB_TEST( 38, 5 ); | |
90 | PRIVATE_LB_TEST( 39, 5 ); | |
91 | PRIVATE_LB_TEST( 40, 5 ); | |
92 | ||
93 | PRIVATE_LB_TEST( 63, 5 ); | |
94 | ||
95 | PRIVATE_LB_TEST( 64, 6 ); | |
96 | PRIVATE_LB_TEST( 65, 6 ); | |
97 | ||
98 | PRIVATE_LB_TEST( 127, 6 ); | |
99 | ||
100 | PRIVATE_LB_TEST( 128, 7 ); | |
101 | PRIVATE_LB_TEST( 129, 7 ); | |
102 | ||
103 | PRIVATE_LB_TEST( 255, 7 ); | |
104 | ||
105 | PRIVATE_LB_TEST( 256, 8 ); | |
106 | PRIVATE_LB_TEST( 257, 8 ); | |
107 | ||
108 | PRIVATE_LB_TEST( 511, 8 ); | |
109 | ||
110 | PRIVATE_LB_TEST( 512, 9 ); | |
111 | PRIVATE_LB_TEST( 513, 9 ); | |
112 | ||
113 | PRIVATE_LB_TEST( 1023, 9 ); | |
114 | ||
115 | PRIVATE_LB_TEST( 1024, 10 ); | |
116 | PRIVATE_LB_TEST( 1025, 10 ); | |
117 | ||
118 | PRIVATE_LB_TEST( 2047, 10 ); | |
119 | ||
120 | PRIVATE_LB_TEST( 2048, 11 ); | |
121 | PRIVATE_LB_TEST( 2049, 11 ); | |
122 | ||
123 | PRIVATE_LB_TEST( 4095, 11 ); | |
124 | ||
125 | PRIVATE_LB_TEST( 4096, 12 ); | |
126 | PRIVATE_LB_TEST( 4097, 12 ); | |
127 | ||
128 | PRIVATE_LB_TEST( 8191, 12 ); | |
129 | ||
130 | PRIVATE_LB_TEST( 8192, 13 ); | |
131 | PRIVATE_LB_TEST( 8193, 13 ); | |
132 | ||
133 | PRIVATE_LB_TEST( 16383, 13 ); | |
134 | ||
135 | PRIVATE_LB_TEST( 16384, 14 ); | |
136 | PRIVATE_LB_TEST( 16385, 14 ); | |
137 | ||
138 | PRIVATE_LB_TEST( 32767, 14 ); | |
139 | ||
140 | PRIVATE_LB_TEST( 32768, 15 ); | |
141 | PRIVATE_LB_TEST( 32769, 15 ); | |
142 | ||
143 | PRIVATE_LB_TEST( 65535, 15 ); | |
144 | ||
145 | PRIVATE_LB_TEST( 65536, 16 ); | |
146 | PRIVATE_LB_TEST( 65537, 16 ); | |
147 | ||
148 | #if defined(UINT32_C) | |
149 | PRIVATE_LB_TEST( UINT32_C(4294967295), 31 ); | |
150 | #endif | |
151 | ||
152 | #if defined(UINT64_C) && !defined(BOOST_NO_INT64_T) | |
153 | PRIVATE_LB_TEST( UINT64_C(4294967296), 32 ); | |
154 | PRIVATE_LB_TEST( UINT64_C(4294967297), 32 ); | |
155 | ||
156 | PRIVATE_LB_TEST( UINT64_C(18446744073709551615), 63 ); | |
157 | #endif | |
158 | ||
159 | #if defined(BOOST_HAS_INT128) | |
160 | PRIVATE_LB_TEST( boost::uint128_type(1u) << 64u, 64 ); | |
161 | PRIVATE_LB_TEST( (boost::uint128_type(1u) << 64u) + 1u, 64 ); | |
162 | ||
163 | PRIVATE_LB_TEST( ~boost::uint128_type(0u), 127 ); | |
164 | #endif | |
165 | ||
166 | #if !defined(DISABLE_MP_TESTS) | |
167 | PRIVATE_LB_TEST( boost::multiprecision::cpp_int(1), 0 ); | |
168 | PRIVATE_LB_TEST( boost::multiprecision::cpp_int(2), 1 ); | |
169 | PRIVATE_LB_TEST( boost::multiprecision::cpp_int(3), 1 ); | |
170 | PRIVATE_LB_TEST( boost::multiprecision::cpp_int(65535), 15 ); | |
171 | PRIVATE_LB_TEST( boost::multiprecision::cpp_int(65536), 16 ); | |
172 | ||
173 | PRIVATE_LB_TEST( boost::multiprecision::int1024_t(1), 0 ); | |
174 | PRIVATE_LB_TEST( boost::multiprecision::int1024_t(2), 1 ); | |
175 | PRIVATE_LB_TEST( boost::multiprecision::int1024_t(3), 1 ); | |
176 | PRIVATE_LB_TEST( boost::multiprecision::int1024_t(65535), 15 ); | |
177 | PRIVATE_LB_TEST( boost::multiprecision::int1024_t(65536), 16 ); | |
178 | ||
179 | PRIVATE_LB_TEST( boost::multiprecision::uint1024_t(1), 0 ); | |
180 | PRIVATE_LB_TEST( boost::multiprecision::uint1024_t(2), 1 ); | |
181 | PRIVATE_LB_TEST( boost::multiprecision::uint1024_t(3), 1 ); | |
182 | PRIVATE_LB_TEST( boost::multiprecision::uint1024_t(65535), 15 ); | |
183 | PRIVATE_LB_TEST( boost::multiprecision::uint1024_t(65536), 16 ); | |
184 | #endif | |
185 | ||
186 | return boost::report_errors(); | |
187 | } |