| 1 | // Copyright 2022 Peter Dimov. |
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // https://www.boost.org/LICENSE_1_0.txt |
| 4 | |
| 5 | #include <boost/utility/string_view.hpp> |
| 6 | #include <boost/core/detail/string_view.hpp> |
| 7 | #include <boost/container_hash/hash.hpp> |
| 8 | #include <boost/core/lightweight_test.hpp> |
| 9 | #include <boost/config.hpp> |
| 10 | #include <string> |
| 11 | #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW) |
| 12 | #include <string_view> |
| 13 | #endif |
| 14 | #include <vector> |
| 15 | #include <deque> |
| 16 | #include <list> |
| 17 | #if !defined(BOOST_NO_CXX11_HDR_FORWARD_LIST) |
| 18 | # include <forward_list> |
| 19 | #endif |
| 20 | |
| 21 | // Test whether the hash values of a string and a |
| 22 | // string_view that refers to it match. This is |
| 23 | // important for unordered heterogeneous lookups. |
| 24 | |
| 25 | template<class T> std::size_t hv( T const& t ) |
| 26 | { |
| 27 | return boost::hash<T>()( t ); |
| 28 | } |
| 29 | |
| 30 | int main() |
| 31 | { |
| 32 | std::string s( "Test." ); |
| 33 | std::size_t h0 = hv( t: s ); |
| 34 | |
| 35 | BOOST_TEST_EQ( h0, hv( boost::string_view( s ) ) ); |
| 36 | BOOST_TEST_EQ( h0, hv( boost::core::string_view( s ) ) ); |
| 37 | |
| 38 | #if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW) |
| 39 | BOOST_TEST_EQ( h0, hv( std::string_view( s ) ) ); |
| 40 | #endif |
| 41 | |
| 42 | BOOST_TEST_EQ( h0, hv( std::vector<char>( s.begin(), s.end() ) ) ); |
| 43 | BOOST_TEST_EQ( h0, hv( std::deque<char>( s.begin(), s.end() ) ) ); |
| 44 | BOOST_TEST_EQ( h0, hv( std::list<char>( s.begin(), s.end() ) ) ); |
| 45 | |
| 46 | #if !defined(BOOST_NO_CXX11_HDR_FORWARD_LIST) |
| 47 | BOOST_TEST_EQ( h0, hv( std::forward_list<char>( s.begin(), s.end() ) ) ); |
| 48 | #endif |
| 49 | |
| 50 | return boost::report_errors(); |
| 51 | } |
| 52 | |