test_serialization.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // (C) Copyright Andy Tompkins 2007. Permission to copy, use, modify, sell and
  2. // distribute this software is granted provided this copyright notice appears
  3. // in all copies. This software is provided "as is" without express or implied
  4. // warranty, and with no claim as to its suitability for any purpose.
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // https://www.boost.org/LICENSE_1_0.txt)
  8. // Purpose to test serializing uuids with narrow archives
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <sstream>
  11. #include <iostream>
  12. #include <boost/uuid/uuid.hpp>
  13. #include <boost/uuid/uuid_serialize.hpp>
  14. #include <boost/uuid/uuid_io.hpp>
  15. #include <boost/archive/text_oarchive.hpp>
  16. #include <boost/archive/text_iarchive.hpp>
  17. #include <boost/archive/xml_oarchive.hpp>
  18. #include <boost/archive/xml_iarchive.hpp>
  19. #include <boost/archive/binary_oarchive.hpp>
  20. #include <boost/archive/binary_iarchive.hpp>
  21. template <class OArchiveType, class IArchiveType, class OStringStreamType, class IStringStreamType>
  22. void test_archive()
  23. {
  24. using namespace std;
  25. using namespace boost::uuids;
  26. OStringStreamType o_stream;
  27. uuid u1 = {{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}};
  28. uuid u2;
  29. // save
  30. {
  31. OArchiveType oa(o_stream);
  32. oa << BOOST_SERIALIZATION_NVP(u1);
  33. }
  34. //cout << "stream:" << o_stream.str() << "\n\n";
  35. // load
  36. {
  37. IStringStreamType i_stream(o_stream.str());
  38. IArchiveType ia(i_stream);
  39. ia >> BOOST_SERIALIZATION_NVP(u2);
  40. }
  41. BOOST_TEST_EQ(u1, u2);
  42. }
  43. int main( int /* argc */, char* /* argv */[] )
  44. {
  45. using namespace std;
  46. using namespace boost::archive;
  47. test_archive<text_oarchive, text_iarchive, ostringstream, istringstream>();
  48. test_archive<xml_oarchive, xml_iarchive, ostringstream, istringstream>();
  49. test_archive<binary_oarchive, binary_iarchive, ostringstream, istringstream>();
  50. return boost::report_errors();
  51. }