diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index fafe1189a..1c1c08c07 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -159,6 +159,8 @@ if ! $(BOOST_ARCHIVE_LIST) { [ test-bsl-run test_singleton_inherited ] [ test-bsl-run test_singleton_plain ] + [ test-bsl-run test_string : : : 17 ] + # [ test-bsl-run test_z ] # should fail compilation diff --git a/test/test_string.cpp b/test/test_string.cpp new file mode 100644 index 000000000..357cee640 --- /dev/null +++ b/test/test_string.cpp @@ -0,0 +1,149 @@ +#include +#include + +#include + +#include +#include + +#include +#include + +#include +#include + +#include +#include + +// ----------------------------------- +#include +#include + +#include "test_tools.hpp" + +// Test with some unicode characters +template +struct TestTraits +{ +}; +template <> +struct TestTraits +{ + static constexpr char DefaultValue[] = "Default"; + static constexpr char TestValue[] = + u8"Hello boost! The author of boost::serialization is Robert Ramey.\n" + u8"Hallo boost! Der Autor von boost::serialization ist Robert Ramey.\n" + u8"你好 boost! boost::serialization 由 Robert Ramey 编写。"; +}; + +#ifndef BOOST_NO_STD_WSTRING +template <> +struct TestTraits +{ + static constexpr wchar_t DefaultValue[] = L"Default"; + static constexpr wchar_t TestValue[] = + L"Hello boost! The author of boost::serialization is Robert Ramey.\n" + L"Hallo boost! Der Autor von boost::serialization ist Robert Ramey.\n" + L"你好 boost! boost::serialization 由 Robert Ramey 编写。"; +}; +#endif + +template +struct Data +{ + template + void serialize(Archive& ar, const unsigned int /* version */) + { + ar& BOOST_SERIALIZATION_NVP(value); + ar& BOOST_SERIALIZATION_NVP(test); + } + + bool operator==(const Data& rhs) const + { + // compare by value as the capacity is irrelevant + return std::equal(std::begin(value), std::end(value), std::begin(rhs.value)) == true && + test == rhs.test; + } + + String value = TestTraits::DefaultValue; + int test = 1; +}; + +template +std::string ToString(const Data& data) +{ + std::ostringstream oss(std::ios::binary); + + { + Archive oa(oss); + oa << BOOST_SERIALIZATION_NVP(data); + } + + return oss.str(); +} + +template +Data FromString(std::string& s) +{ + Data data; + std::istringstream iss(s, std::ios::binary); + { + Archive ia(iss); + ia >> BOOST_SERIALIZATION_NVP(data); + } + return data; +} + +template +int PerformTest() +{ + try + { + Data d1; + d1.value = TestTraits::TestValue; + d1.test = 1; + + auto s1 = ToString(d1); + auto r1 = FromString(s1); + + BOOST_CHECK(d1 == r1); + // bool test = (d1 == r1); + + return 0; + } + catch (const std::exception& ex) + { + BOOST_ERROR(ex.what()); + } + + return 1; +} + +int test_main( int /* argc */, char* /* argv */[] ) +{ + int result = 0; + + // working + result += PerformTest(); + result += PerformTest(); + + result += PerformTest(); + result += PerformTest(); + + result += PerformTest(); + result += PerformTest(); + +#ifndef BOOST_NO_STD_WSTRING + result += PerformTest(); + result += PerformTest(); + + result += PerformTest(); + result += PerformTest(); + + // fails but why? + //result += PerformTest(); + //result += PerformTest(); +#endif // BOOST_NO_STD_WSTRING + + return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +}