- Adds compatibility tests between string and std::string_view

- Fixes #290 ("boost::hash<container::string> doesn't match boost::hash<std::string_view>")
This commit is contained in:
Ion Gaztañaga
2024-09-30 16:00:12 +02:00
parent 18a86594b6
commit bf029b6a52
2 changed files with 114 additions and 54 deletions

View File

@@ -16,6 +16,7 @@ project
<library>/boost/iterator//boost_iterator <library>/boost/iterator//boost_iterator
<library>/boost/utility//boost_utility <library>/boost/utility//boost_utility
<library>/boost/tuple//boost_tuple <library>/boost/tuple//boost_tuple
<library>/boost/container_hash//boost_container_hash
<link>shared:<define>BOOST_CONTAINER_DYN_LINK=1 <link>shared:<define>BOOST_CONTAINER_DYN_LINK=1
<toolset>gcc-cygwin:<link>static <toolset>gcc-cygwin:<link>static

View File

@@ -13,38 +13,46 @@
#include <boost/core/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
#if defined(__cpp_lib_string_view) && (__cpp_lib_string_view >= 201606L)
#define BOOST_CONTAINER_TEST_HAS_STD_STRING_VIEW
#include <string_view>
#endif
template<class StringViewType>
void conversion_test() void conversion_test()
{ {
#ifndef BOOST_CONTAINER_TEMPLATED_CONVERSION_OPERATOR_BROKEN #ifndef BOOST_CONTAINER_TEMPLATED_CONVERSION_OPERATOR_BROKEN
{ {
const boost::container::string s = "some text"; const boost::container::string s = "some text";
boost::string_view sv(s); StringViewType sv(s);
BOOST_TEST(s.data() == sv.data() && s.size() == sv.size()); BOOST_TEST(s.data() == sv.data() && s.size() == sv.size());
boost::string_view sv2; StringViewType sv2;
sv2 = s; sv2 = s;
BOOST_TEST(s.data() == sv2.data() && s.size() == sv2.size()); BOOST_TEST(s.data() == sv2.data() && s.size() == sv2.size());
const boost::string_view csv(s); const StringViewType csv(s);
BOOST_TEST(s.data() == sv.data() && s.size() == csv.size()); BOOST_TEST(s.data() == sv.data() && s.size() == csv.size());
} }
#endif #endif
} }
template<class StringViewType>
void to_view_test() void to_view_test()
{ {
const boost::container::string s = "some text"; const boost::container::string s = "some text";
boost::string_view sv(s.to_view<boost::string_view>()); StringViewType sv(s.to_view<StringViewType>());
BOOST_TEST(s.data() == sv.data() && s.size() == sv.size()); BOOST_TEST(s.data() == sv.data() && s.size() == sv.size());
boost::string_view sv2; StringViewType sv2;
sv2 = s.to_view<boost::string_view>(); sv2 = s.to_view<StringViewType>();
BOOST_TEST(s.data() == sv2.data() && s.size() == sv2.size()); BOOST_TEST(s.data() == sv2.data() && s.size() == sv2.size());
const boost::string_view csv(s.to_view<boost::string_view>()); const StringViewType csv(s.to_view<StringViewType>());
BOOST_TEST(s.data() == csv.data() && s.size() == csv.size()); BOOST_TEST(s.data() == csv.data() && s.size() == csv.size());
} }
template<class StringViewType>
void equal_test() void equal_test()
{ {
const boost::string_view sv = "same text"; const StringViewType sv = "same text";
const boost::string_view svd = "different text"; const StringViewType svd = "different text";
const boost::container::string s = "same text"; const boost::container::string s = "same text";
BOOST_TEST(sv == s); BOOST_TEST(sv == s);
BOOST_TEST(s == sv); BOOST_TEST(s == sv);
@@ -52,10 +60,11 @@ void equal_test()
BOOST_TEST(!(s == svd)); BOOST_TEST(!(s == svd));
} }
template<class StringViewType>
void unequal_test() void unequal_test()
{ {
const boost::string_view sv = "same text"; const StringViewType sv = "same text";
const boost::string_view svd = "different text"; const StringViewType svd = "different text";
const boost::container::string s = "same text"; const boost::container::string s = "same text";
BOOST_TEST(!(sv != s)); BOOST_TEST(!(sv != s));
BOOST_TEST(!(s != sv)); BOOST_TEST(!(s != sv));
@@ -63,9 +72,10 @@ void unequal_test()
BOOST_TEST(s != svd); BOOST_TEST(s != svd);
} }
template<class StringViewType>
void less_test() void less_test()
{ {
boost::string_view sv = "0123456"; StringViewType sv = "0123456";
boost::container::string s = "0123459"; boost::container::string s = "0123459";
BOOST_TEST(sv < s); BOOST_TEST(sv < s);
BOOST_TEST(!(s < sv)); BOOST_TEST(!(s < sv));
@@ -80,9 +90,10 @@ void less_test()
BOOST_TEST(!(s < sv)); BOOST_TEST(!(s < sv));
} }
template<class StringViewType>
void greater_test() void greater_test()
{ {
boost::string_view sv = "0123459"; StringViewType sv = "0123459";
boost::container::string s = "0123456"; boost::container::string s = "0123456";
BOOST_TEST(sv > s); BOOST_TEST(sv > s);
BOOST_TEST(!(s > sv)); BOOST_TEST(!(s > sv));
@@ -97,9 +108,10 @@ void greater_test()
BOOST_TEST(!(s > sv)); BOOST_TEST(!(s > sv));
} }
template<class StringViewType>
void less_equal_test() void less_equal_test()
{ {
boost::string_view sv = "0123456"; StringViewType sv = "0123456";
boost::container::string s = "0123459"; boost::container::string s = "0123459";
BOOST_TEST(sv <= s); BOOST_TEST(sv <= s);
BOOST_TEST(!(s <= sv)); BOOST_TEST(!(s <= sv));
@@ -114,9 +126,10 @@ void less_equal_test()
BOOST_TEST(s <= sv); BOOST_TEST(s <= sv);
} }
template<class StringViewType>
void greater_equal_test() void greater_equal_test()
{ {
boost::string_view sv = "0123459"; StringViewType sv = "0123459";
boost::container::string s = "0123456"; boost::container::string s = "0123456";
BOOST_TEST(sv >= s); BOOST_TEST(sv >= s);
BOOST_TEST(!(s >= sv)); BOOST_TEST(!(s >= sv));
@@ -131,58 +144,65 @@ void greater_equal_test()
BOOST_TEST(s >= sv); BOOST_TEST(s >= sv);
} }
template<class StringViewType>
void constructor_test() void constructor_test()
{ {
boost::string_view sv = "0123459"; StringViewType sv = "0123459";
boost::container::string s(sv); boost::container::string s(sv);
BOOST_TEST(sv == s); BOOST_TEST(sv == s);
boost::container::string s2(sv, s.get_allocator()); boost::container::string s2(sv, s.get_allocator());
BOOST_TEST(sv == s); BOOST_TEST(sv == s);
} }
template<class StringViewType>
void assignment_test() void assignment_test()
{ {
boost::string_view sv = "0123459"; StringViewType sv = "0123459";
boost::container::string s; boost::container::string s;
s = sv; s = sv;
BOOST_TEST(sv == s); BOOST_TEST(sv == s);
} }
template<class StringViewType>
void assign_test() void assign_test()
{ {
boost::string_view sv = "0123459"; StringViewType sv = "0123459";
boost::container::string s; boost::container::string s;
s.assign(sv); s.assign(sv);
BOOST_TEST(sv == s); BOOST_TEST(sv == s);
} }
template<class StringViewType>
void plus_equal_test() void plus_equal_test()
{ {
boost::string_view sv = "23459"; StringViewType sv = "23459";
boost::container::string s("01"); boost::container::string s("01");
s += sv; s += sv;
BOOST_TEST(s == "0123459"); BOOST_TEST(s == "0123459");
} }
template<class StringViewType>
void append_test() void append_test()
{ {
boost::string_view sv = "23459"; StringViewType sv = "23459";
boost::container::string s("01"); boost::container::string s("01");
s.append(sv); s.append(sv);
BOOST_TEST(s == "0123459"); BOOST_TEST(s == "0123459");
} }
template<class StringViewType>
void insert_test() void insert_test()
{ {
boost::string_view sv = "34"; StringViewType sv = "34";
boost::container::string s("01259"); boost::container::string s("01259");
s.insert(3u, sv); s.insert(3u, sv);
BOOST_TEST(s == "0123459"); BOOST_TEST(s == "0123459");
} }
template<class StringViewType>
void replace_test() void replace_test()
{ {
boost::string_view sv = "5678"; StringViewType sv = "5678";
boost::container::string s("01259"); boost::container::string s("01259");
s.replace(2u, 2u, sv); s.replace(2u, 2u, sv);
BOOST_TEST(s == "0156789"); BOOST_TEST(s == "0156789");
@@ -192,83 +212,122 @@ void replace_test()
BOOST_TEST(s == "0155678"); BOOST_TEST(s == "0155678");
} }
template<class StringViewType>
void find_test() void find_test()
{ {
const boost::string_view sv = "25"; const StringViewType sv = "25";
boost::container::string s("0125925123"); boost::container::string s("0125925123");
BOOST_TEST(s.find(sv,4) == 5); BOOST_TEST(s.find(sv,4) == 5);
} }
template<class StringViewType>
void rfind_test() void rfind_test()
{ {
const boost::string_view sv = "25"; const StringViewType sv = "25";
boost::container::string s("0125925123"); boost::container::string s("0125925123");
BOOST_TEST(s.rfind(sv,4) == 2); BOOST_TEST(s.rfind(sv,4) == 2);
} }
template<class StringViewType>
void find_first_of_test() void find_first_of_test()
{ {
const boost::string_view sv = "52"; const StringViewType sv = "52";
boost::container::string s("0125925123"); boost::container::string s("0125925123");
BOOST_TEST(s.find_first_of(sv,4) == 5); BOOST_TEST(s.find_first_of(sv,4) == 5);
} }
template<class StringViewType>
void find_last_of_test() void find_last_of_test()
{ {
const boost::string_view sv = "52"; const StringViewType sv = "52";
boost::container::string s("520125925123"); boost::container::string s("520125925123");
BOOST_TEST(s.find_last_of(sv,6) == 5); BOOST_TEST(s.find_last_of(sv,6) == 5);
} }
template<class StringViewType>
void find_first_not_of_test() void find_first_not_of_test()
{ {
const boost::string_view sv = "52"; const StringViewType sv = "52";
boost::container::string s("0125925123"); boost::container::string s("0125925123");
BOOST_TEST(s.find_first_not_of(sv,2) == 4); BOOST_TEST(s.find_first_not_of(sv,2) == 4);
} }
template<class StringViewType>
void find_last_not_of_test() void find_last_not_of_test()
{ {
const boost::string_view sv = "52"; const StringViewType sv = "52";
boost::container::string s("0125925123"); boost::container::string s("0125925123");
BOOST_TEST(s.find_last_not_of(sv,6) == 4); BOOST_TEST(s.find_last_not_of(sv,6) == 4);
} }
template<class StringViewType>
void compare_test() void compare_test()
{ {
const boost::string_view sv = "52"; const StringViewType sv = "52";
boost::container::string s("0125925123"); boost::container::string s("0125925123");
BOOST_TEST(s.compare(sv) < 0); BOOST_TEST(s.compare(sv) < 0);
BOOST_TEST(s.compare(boost::string_view("0125925123")) == 0); BOOST_TEST(s.compare(StringViewType("0125925123")) == 0);
BOOST_TEST(s.compare(2u, s.size() - 2u, boost::string_view("25925123")) == 0); BOOST_TEST(s.compare(2u, s.size() - 2u, StringViewType("25925123")) == 0);
boost::string_view sv2("5212592512389"); StringViewType sv2("5212592512389");
BOOST_TEST(s.compare(2u, s.size() - 2u, sv2, 3, sv2.size()-5u) == 0); BOOST_TEST(s.compare(2u, s.size() - 2u, sv2, 3, sv2.size()-5u) == 0);
} }
#if BOOST_CXX_VERSION >= 201103L
#include <boost/container_hash/hash.hpp>
template<class StringViewType>
void hash_test()
{
const boost::container::string s1 = "0125925123";
const boost::container::string s2 = "25925123";
BOOST_TEST(boost::hash_value(s1) == boost::hash_value(StringViewType(s1)));
BOOST_TEST(boost::hash_value(s2) == boost::hash_value(StringViewType(s2)));
}
#else
template<class StringViewType>
void hash_test()
{}
#endif
template<class StringViewType>
void test_all()
{
conversion_test<StringViewType>();
to_view_test<StringViewType>();
equal_test<StringViewType>();
unequal_test<StringViewType>();
less_test<StringViewType>();
greater_test<StringViewType>();
less_equal_test<StringViewType>();
greater_equal_test<StringViewType>();
constructor_test<StringViewType>();
assignment_test<StringViewType>();
assign_test<StringViewType>();
plus_equal_test<StringViewType>();
append_test<StringViewType>();
insert_test<StringViewType>();
replace_test<StringViewType>();
find_test<StringViewType>();
rfind_test<StringViewType>();
find_first_of_test<StringViewType>();
find_last_of_test<StringViewType>();
find_first_not_of_test<StringViewType>();
find_last_not_of_test<StringViewType>();
compare_test<StringViewType>();
hash_test<StringViewType>();
}
int main() int main()
{ {
conversion_test(); test_all<boost::string_view>();
to_view_test(); #ifdef BOOST_CONTAINER_TEST_HAS_STD_STRING_VIEW
equal_test(); test_all<std::string_view>();
unequal_test(); #endif
less_test();
greater_test();
less_equal_test();
greater_equal_test();
constructor_test();
assignment_test();
assign_test();
plus_equal_test();
append_test();
insert_test();
replace_test();
find_test();
rfind_test();
find_first_of_test();
find_last_of_test();
find_first_not_of_test();
find_last_not_of_test();
compare_test();
return boost::report_errors(); return boost::report_errors();
} }