Boost.Test failing to compare std::basic_string<char8_t> strings

Hi Boost.Users, I’m using Boost.Test from Boost 1.72.0 and I’m getting a failure when I perform the following test: auto string_1 = u8”value”s; auto string_2 = u8”value”s; BOOST_TEST(string_1 == string_2); However, the problem goes away when I change the test line to: BOOST_TEST((string_1 == string_2)); What is the reason why Boost.Test is failing without the double parentheses? Kind regards, Leo

On Sun, 8 Mar 2020 at 01:06, Leo Carreon via Boost-users <boost-users@lists.boost.org> wrote:
Hi Boost.Users,
I’m using Boost.Test from Boost 1.72.0 and I’m getting a failure when I perform the following test:
auto string_1 = u8”value”s; auto string_2 = u8”value”s; BOOST_TEST(string_1 == string_2);
If you are getting a failure, tell what is the failure (crystal spheres not always work ;)) I suspect you are hitting two issues: 1. std::u8string is not "eligible for string comparison" https://www.boost.org/libs/test/doc/html/boost_test/testing_tools/extended_c... 2. reporting of char8_t falls into reporting of user-defined type https://www.boost.org/libs/test/doc/html/boost_test/test_output/test_tools_s... A possible hack to work around these two could be namespace std { std::ostream& boost_test_print_type(std::ostream& os, char8_t const& c) { // convert c return os; } } BOOST_TEST(string_1 == string_2, boost::test_tools::per_element());
However, the problem goes away when I change the test line to:
BOOST_TEST((string_1 == string_2));
What is the reason why Boost.Test is failing without the double parentheses?
It disables (de)constructing of the expression from macro input, see https://www.boost.org/libs/test/doc/html/boost_test/testing_tools/internal_d... https://www.boost.org/libs/test/doc/html/boost_test/testing_tools/boost_test... Best regards, -- Mateusz Loskot, http://mateusz.loskot.net

Hi Mateusz, Here is the information you requested. When the following test is performed using Boost.Test: auto string_1 = u8"sample"s; // variable initialized with std::u8string literal auto string_2 = u8"sample"sv; // variable initialized with std::u8string_view literal BOOST_TEST(string_1 == string_2); The compilation fails with an error message similar to this: In file included from /usr/include/c++/9/string:55, from /usr/include/c++/9/bits/locale_classes.h:40, from /usr/include/c++/9/bits/ios_base.h:41, from /usr/include/c++/9/ios:42, from /usr/include/c++/9/istream:38, from /usr/include/c++/9/sstream:38, from src/test_description.cpp:9: /usr/include/c++/9/bits/basic_string.h: In instantiation of ‘class std::__cxx11::basic_string<const char8_t, std::char_traits<const char8_t>, std::allocator<const char8_t> >’: /usr/local/include/boost/test/tools/cstring_comparison_op.hpp:75:1: required from ‘static bool boost::test_tools::assertion::op::EQ<Lhs, Rhs, typename boost::enable_if_c<(boost::unit_test::is_cstring_comparable<Lhs>::value && boost::unit_test::is_cstring_comparable<Rhs>::value)>::type>::eval(const Lhs&, const Rhs&) [with Lhs = std::__cxx11::basic_string<char8_t>; Rhs = std::basic_string_view<char8_t>]’ /usr/local/include/boost/test/tools/assertion.hpp:354:24: required from ‘boost::test_tools::assertion::binary_expr<Lhs, Rhs, OP>::result_type boost::test_tools::assertion::binary_expr<Lhs, Rhs, OP>::value() const [with LExpr = boost::test_tools::assertion::value_expr<std::__cxx11::basic_string<char8_t>
; Rhs = const std::basic_string_view<char8_t>&; OP = boost::test_tools::assertion::op::EQ<std::__cxx11::basic_string<char8_t>, std::basic_string_view<char8_t>, void>; boost::test_tools::assertion::binary_expr<Lhs, Rhs, OP>::result_type = boost::test_tools::assertion_result]’ /usr/local/include/boost/test/tools/assertion.hpp:363:42: required from ‘boost::test_tools::assertion_result boost::test_tools::assertion::binary_expr<Lhs, Rhs, OP>::evaluate(bool) const [with LExpr = boost::test_tools::assertion::value_expr<std::__cxx11::basic_string<char8_t> ; Rhs = const std::basic_string_view<char8_t>&; OP = boost::test_tools::assertion::op::EQ<std::__cxx11::basic_string<char8_t>, std::basic_string_view<char8_t>, void>]’ src/test_description.cpp:50:2: required from here
Is there a possible workaround other than doing this: BOOST_TEST((string_1 == string_2)); Note that I have provided the following functions to allow the printing of std::u8string, std::u8string_view and char8_t*: std::ostream& std::operator<<(std::ostream&, std::u8string_view); std::ostream& std::operator<<(std::ostream&, const char8_t*); To allow the printing of the compared values. BTW, Boost.Test seems to be happy with the following test: auto string_1 = u8"sample"s; // this creates a std::u8string variable auto string_2 = u8"sample"; // this creates a const char8_t* variable BOOST_TEST(string_1 == string_2); -----Original Message----- From: Mateusz Loskot via Boost-users Sent: Sunday, March 8, 2020 11:51 AM To: boost-users@lists.boost.org Cc: Mateusz Loskot Subject: Re: [Boost-users] Boost.Test failing to compare std::basic_string strings On Sun, 8 Mar 2020 at 01:06, Leo Carreon via Boost-users <boost-users@lists.boost.org> wrote:
Hi Boost.Users,
I’m using Boost.Test from Boost 1.72.0 and I’m getting a failure when I perform the following test:
auto string_1 = u8”value”s; auto string_2 = u8”value”s; BOOST_TEST(string_1 == string_2);
If you are getting a failure, tell what is the failure (crystal spheres not always work ;)) I suspect you are hitting two issues: 1. std::u8string is not "eligible for string comparison" https://www.boost.org/libs/test/doc/html/boost_test/testing_tools/extended_c... 2. reporting of char8_t falls into reporting of user-defined type https://www.boost.org/libs/test/doc/html/boost_test/test_output/test_tools_s... A possible hack to work around these two could be namespace std { std::ostream& boost_test_print_type(std::ostream& os, char8_t const& c) { // convert c return os; } } BOOST_TEST(string_1 == string_2, boost::test_tools::per_element());
However, the problem goes away when I change the test line to:
BOOST_TEST((string_1 == string_2));
What is the reason why Boost.Test is failing without the double parentheses?
It disables (de)constructing of the expression from macro input, see https://www.boost.org/libs/test/doc/html/boost_test/testing_tools/internal_d... https://www.boost.org/libs/test/doc/html/boost_test/testing_tools/boost_test... Best regards, -- Mateusz Loskot, http://mateusz.loskot.net _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org https://lists.boost.org/mailman/listinfo.cgi/boost-users

On Mon, 9 Mar 2020 at 04:59, Leo Carreon via Boost-users <boost-users@lists.boost.org> wrote:
[...] Is there a possible workaround other than doing this:
BOOST_TEST((string_1 == string_2));
Note that I have provided the following functions to allow the printing of std::u8string, std::u8string_view and char8_t*:
std::ostream& std::operator<<(std::ostream&, std::u8string_view); std::ostream& std::operator<<(std::ostream&, const char8_t*);
To allow the printing of the compared values.
BTW, Boost.Test seems to be happy with the following test:
auto string_1 = u8"sample"s; // this creates a std::u8string variable auto string_2 = u8"sample"; // this creates a const char8_t* variable BOOST_TEST(string_1 == string_2);
Sorry, I don't know. There is this BOOST_TEST_STRING_VIEW macro, but it remains undocumented as far as I can see, which may be auto-defined or user is expected to define it. Perhaps, it will help. Best regards, -- Mateusz Loskot, http://mateusz.loskot.net
participants (2)
-
Leo Carreon
-
Mateusz Loskot