| 1 | // Copyright 2005-2009 Daniel James. |
|---|---|
| 2 | // Copyright 2021 Peter Dimov. |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // https://www.boost.org/LICENSE_1_0.txt |
| 5 | |
| 6 | #include <boost/container_hash/hash.hpp> |
| 7 | #include <boost/core/lightweight_test.hpp> |
| 8 | #include <boost/core/type_name.hpp> |
| 9 | #include <boost/type_traits/is_signed.hpp> |
| 10 | #include <boost/type_traits/make_unsigned.hpp> |
| 11 | #include <boost/static_assert.hpp> |
| 12 | #include <boost/config.hpp> |
| 13 | |
| 14 | // This test checks that values representable in a signed |
| 15 | // and the corresponding unsigned type hash to the same value |
| 16 | |
| 17 | template<class T> |
| 18 | void signed_unsigned_test() |
| 19 | { |
| 20 | BOOST_STATIC_ASSERT( boost::is_signed<T>::value ); |
| 21 | |
| 22 | typedef typename boost::make_unsigned<T>::type U; |
| 23 | |
| 24 | T x = std::numeric_limits<T>::max(); |
| 25 | |
| 26 | do |
| 27 | { |
| 28 | BOOST_TEST_EQ( boost::hash<T>()( x ), boost::hash<U>()( static_cast<U>( x ) ) ); |
| 29 | x /= 3; |
| 30 | } |
| 31 | while( x > 0 ); |
| 32 | } |
| 33 | |
| 34 | #define TEST(type) std::cerr << "Testing: " #type " (" << boost::core::type_name<type>() << ")\n"; signed_unsigned_test<type>(); |
| 35 | |
| 36 | int main() |
| 37 | { |
| 38 | TEST(signed char) |
| 39 | TEST(short) |
| 40 | TEST(int) |
| 41 | TEST(long) |
| 42 | |
| 43 | #if !defined(BOOST_NO_LONG_LONG) |
| 44 | TEST(boost::long_long_type) |
| 45 | #endif |
| 46 | |
| 47 | #if defined(BOOST_HAS_INT128) |
| 48 | TEST(boost::int128_type) |
| 49 | #endif |
| 50 | |
| 51 | return boost::report_errors(); |
| 52 | } |
| 53 |
