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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
// Copyright 2023 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/config.hpp>
#ifdef BOOST_HAS_INT128
// We need to define these operator<< overloads before
// including boost/core/lightweight_test.hpp, or they
// won't be visible to BOOST_TEST_EQ
// LCOV_EXCL_START
#include <ostream>
static char* mini_to_chars( char (&buffer)[ 64 ], boost::uint128_type v )
{
char* p = buffer + 64;
*--p = '\0';
do
{
*--p = "0123456789"[ v % 10 ];
v /= 10;
}
while ( v != 0 );
return p;
}
std::ostream& operator<<( std::ostream& os, boost::uint128_type v )
{
char buffer[ 64 ];
os << mini_to_chars( buffer, v );
return os;
}
std::ostream& operator<<( std::ostream& os, boost::int128_type v )
{
char buffer[ 64 ];
char* p;
if( v >= 0 )
{
p = mini_to_chars( buffer, static_cast<boost::uint128_type>(v) );
}
else
{
p = mini_to_chars( buffer, -static_cast<boost::uint128_type>(v) );
*--p = '-';
}
os << p;
return os;
}
// LCOV_EXCL_STOP
#endif // #ifdef BOOST_HAS_INT128
#include <boost/charconv/detail/emulated128.hpp>
#include <boost/core/lightweight_test.hpp>
#include <limits>
#include <iostream>
#include <climits>
#include <cstdint>
using boost::charconv::detail::uint128;
using boost::charconv::detail::trivial_uint128;
template <typename T>
void test_relational_operators(T val = (std::numeric_limits<T>::max)())
{
uint128 test_val = UINT64_MAX;
test_val += 1;
BOOST_TEST(test_val > val);
BOOST_TEST(!(test_val < val));
BOOST_TEST(!(test_val == val));
BOOST_TEST(test_val != val);
const uint128 equal_val = val;
BOOST_TEST(!(equal_val > val));
BOOST_TEST(equal_val >= val);
BOOST_TEST(!(equal_val < val));
BOOST_TEST(equal_val <= val);
BOOST_TEST(equal_val == val);
BOOST_TEST(!(equal_val != val));
int negative_val = -100;
BOOST_TEST(test_val > negative_val);
BOOST_TEST(!(test_val < negative_val));
BOOST_TEST(!(test_val == negative_val));
BOOST_TEST(test_val != negative_val);
}
void test_arithmetic_operators()
{
// Only using low word
const auto fixed_val = UINT64_MAX / 2;
uint128 test_val = fixed_val;
BOOST_TEST(test_val / 2 == UINT64_MAX / 4);
BOOST_TEST(test_val + 1 == fixed_val + 1);
test_val++;
BOOST_TEST(test_val == fixed_val + 1);
BOOST_TEST(test_val % fixed_val == 1);
test_val--;
BOOST_TEST(test_val == fixed_val);
BOOST_TEST(test_val % fixed_val == 0);
BOOST_TEST(test_val / fixed_val == 1);
test_val = 2;
std::uint64_t comp_val = 1;
while (test_val < UINT64_MAX)
{
comp_val *= 2;
if(!BOOST_TEST(test_val == comp_val))
{
// LCOV_EXCL_START
std::cerr << "Target: " << comp_val
<< "\ntest_val: " << test_val.low << std::endl;
// LCOV_EXCL_STOP
}
test_val *= 2;
}
// And back down
while (test_val >= 2)
{
test_val /= 2;
if(!BOOST_TEST(test_val == comp_val))
{
// LCOV_EXCL_START
std::cerr << "Target: " << comp_val
<< "\ntest_val: " << test_val.low << std::endl;
// LCOV_EXCL_STOP
}
comp_val /= 2;
}
// Add the high word
uint128 test_high_word = UINT64_MAX;
++test_high_word;
BOOST_TEST(test_high_word.high == 1 && test_high_word.low == 0);
--test_high_word;
#ifdef BOOST_CHARCONV_HAS_INT128
boost::uint128_type reference = UINT64_MAX;
BOOST_TEST(test_high_word == reference);
for (int i = 0; i < 63; ++i)
{
if(!BOOST_TEST(test_high_word == reference))
{
// LCOV_EXCL_START
std::cerr << "i: " << i
<< "\nTarget: " << reference
<< "\ntest_val: " << test_high_word.high << " " << test_high_word.low << std::endl;
// // LCOV_EXCL_STOP
}
test_high_word *= 2;
reference *= 2;
}
while (test_high_word >= 2)
{
BOOST_TEST(test_high_word == reference);
test_high_word /= 2;
reference /= 2;
}
#endif
}
void test_bitwise_operators()
{
#ifdef BOOST_CHARCONV_HAS_INT128
boost::uint128_type ref = UINT64_MAX;
uint128 test_val = UINT64_MAX;
ref <<= 1;
test_val <<= 1;
BOOST_TEST(test_val == ref);
ref >>= 2;
test_val >>= 2;
BOOST_TEST(test_val == ref);
BOOST_TEST((test_val | 1) == (ref | 1));
BOOST_TEST((test_val & 1) == (ref & 1));
BOOST_TEST(~test_val == ~ref);
#endif
}
void test_memcpy()
{
#if defined(BOOST_CHARCONV_HAS_QUADMATH) && defined(BOOST_CHARCONV_HAS_INT128)
__float128 fval = 1e4000Q;
boost::uint128_type ref;
trivial_uint128 cpyval;
std::memcpy(&cpyval, &fval, sizeof(fval));
std::memcpy(&ref, &fval, sizeof(fval));
uint128 test_val = cpyval;
BOOST_TEST(test_val == ref);
#endif
}
void test_limits()
{
BOOST_TEST((std::numeric_limits<uint128>::min)() == 0);
BOOST_TEST((std::numeric_limits<uint128>::max)() == uint128(UINT64_MAX, UINT64_MAX));
BOOST_TEST(std::numeric_limits<uint128>::is_signed == false);
BOOST_TEST(std::numeric_limits<uint128>::is_integer == true);
BOOST_TEST(std::numeric_limits<uint128>::digits == CHAR_BIT * sizeof(std::uint64_t) * 2);
// Max value is 340,282,366,920,938,463,463,374,607,431,768,211,455 (39 digits) so 38 without change
BOOST_TEST(std::numeric_limits<uint128>::digits10 == 38);
}
int main()
{
test_relational_operators<char>();
test_relational_operators<signed char>();
test_relational_operators<short>();
test_relational_operators<int>();
test_relational_operators<long>();
test_relational_operators<long long>();
test_relational_operators<unsigned char>();
test_relational_operators<unsigned short>();
test_relational_operators<unsigned>();
test_relational_operators<unsigned long>();
test_relational_operators<unsigned long long>();
test_arithmetic_operators();
test_bitwise_operators();
test_memcpy();
test_limits();
return boost::report_errors();
}
|