Skip to content

Commit

Permalink
Tests for std::pmr::strings (boostorg#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
schorsch1976 committed Oct 14, 2022
1 parent 3f322d4 commit a97328d
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -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 : : : <cxxstd>17 ]

# [ test-bsl-run test_z ]

# should fail compilation
Expand Down
149 changes: 149 additions & 0 deletions test/test_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#include <boost/config.hpp>
#include <boost/serialization/string.hpp>

#include <boost/archive/archive_exception.hpp>

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>

#include <sstream>
#include <algorithm>

// -----------------------------------
#include <iostream>
#include <thread>

#include "test_tools.hpp"

// Test with some unicode characters
template <typename T>
struct TestTraits
{
};
template <>
struct TestTraits<char>
{
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<wchar_t>
{
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 <typename String>
struct Data
{
template<class Archive>
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<typename String::value_type>::DefaultValue;
int test = 1;
};

template <typename Archive, typename T>
std::string ToString(const Data<T>& data)
{
std::ostringstream oss(std::ios::binary);

{
Archive oa(oss);
oa << BOOST_SERIALIZATION_NVP(data);
}

return oss.str();
}

template <typename Archive, typename T>
Data<T> FromString(std::string& s)
{
Data<T> data;
std::istringstream iss(s, std::ios::binary);
{
Archive ia(iss);
ia >> BOOST_SERIALIZATION_NVP(data);
}
return data;
}

template <typename String, typename OArchive, typename IArchive>
int PerformTest()
{
try
{
Data<String> d1;
d1.value = TestTraits<typename String::value_type>::TestValue;
d1.test = 1;

auto s1 = ToString<OArchive, String>(d1);
auto r1 = FromString<IArchive, String>(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<std::string, boost::archive::xml_oarchive, boost::archive::xml_iarchive>();
result += PerformTest<std::pmr::string, boost::archive::xml_oarchive, boost::archive::xml_iarchive>();

result += PerformTest<std::string, boost::archive::text_oarchive, boost::archive::text_iarchive>();
result += PerformTest<std::pmr::string, boost::archive::text_oarchive, boost::archive::text_iarchive>();

result += PerformTest<std::string, boost::archive::binary_oarchive, boost::archive::binary_iarchive>();
result += PerformTest<std::pmr::string, boost::archive::binary_oarchive, boost::archive::binary_iarchive>();

#ifndef BOOST_NO_STD_WSTRING
result += PerformTest<std::wstring, boost::archive::text_oarchive, boost::archive::text_iarchive>();
result += PerformTest<std::pmr::wstring,boost::archive::text_oarchive, boost::archive::text_iarchive>();

result += PerformTest<std::wstring, boost::archive::binary_oarchive, boost::archive::binary_iarchive>();
result += PerformTest<std::pmr::wstring,boost::archive::binary_oarchive, boost::archive::binary_iarchive>();

// fails but why?
//result += PerformTest<std::wstring, boost::archive::xml_oarchive, boost::archive::xml_iarchive>();
//result += PerformTest<std::pmr::wstring, boost::archive::xml_oarchive, boost::archive::xml_iarchive>();
#endif // BOOST_NO_STD_WSTRING

return result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

0 comments on commit a97328d

Please sign in to comment.