test_binary.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_simple_class.cpp
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // should pass compilation and execution
  8. #include <cstdlib> // for rand(), NULL, size_t
  9. #include <fstream>
  10. #include <boost/config.hpp>
  11. #include <cstdio> // remove
  12. #if defined(BOOST_NO_STDC_NAMESPACE)
  13. namespace std{
  14. using ::rand;
  15. using ::remove;
  16. }
  17. #endif
  18. #include "test_tools.hpp"
  19. #include <boost/serialization/nvp.hpp>
  20. #include <boost/serialization/binary_object.hpp>
  21. class A {
  22. friend class boost::serialization::access;
  23. char data[150];
  24. // note: from an aesthetic perspective, I would much prefer to have this
  25. // defined out of line. Unfortunately, this trips a bug in the VC 6.0
  26. // compiler. So hold our nose and put it her to permit running of tests.
  27. template<class Archive>
  28. void serialize(Archive & ar, const unsigned int /* file_version */){
  29. ar & boost::serialization::make_nvp(
  30. "data",
  31. boost::serialization::make_binary_object(data, sizeof(data))
  32. );
  33. }
  34. public:
  35. A();
  36. bool operator==(const A & rhs) const;
  37. };
  38. A::A(){
  39. int i = sizeof(data);
  40. while(i-- > 0)
  41. data[i] = static_cast<char>(0xff & std::rand());
  42. }
  43. bool A::operator==(const A & rhs) const {
  44. int i = sizeof(data);
  45. while(i-- > 0)
  46. if(data[i] != rhs.data[i])
  47. return false;
  48. return true;
  49. }
  50. int test_main( int /* argc */, char* /* argv */[] )
  51. {
  52. const char * testfile = boost::archive::tmpnam(NULL);
  53. BOOST_REQUIRE(NULL != testfile);
  54. const A a;
  55. char s1[] = "a";
  56. char s2[] = "ab";
  57. char s3[] = "abc";
  58. char s4[] = "abcd";
  59. const int i = 12345;
  60. A a1;
  61. char s1_1[10];
  62. char s1_2[10];
  63. char s1_3[10];
  64. char s1_4[10];
  65. int i1 = 34790;
  66. std::memset(s1_1, '\0', sizeof(s1_1));
  67. std::memset(s1_2, '\0', sizeof(s1_2));
  68. std::memset(s1_3, '\0', sizeof(s1_3));
  69. std::memset(s1_4, '\0', sizeof(s1_4));
  70. {
  71. test_ostream os(testfile, TEST_STREAM_FLAGS);
  72. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  73. oa << boost::serialization::make_nvp(
  74. "s1",
  75. boost::serialization::make_binary_object(
  76. s1,
  77. sizeof(s1)
  78. )
  79. );
  80. oa << boost::serialization::make_nvp(
  81. "s2",
  82. boost::serialization::make_binary_object(
  83. s2,
  84. sizeof(s2)
  85. )
  86. );
  87. oa << boost::serialization::make_nvp(
  88. "s3",
  89. boost::serialization::make_binary_object(
  90. s3,
  91. sizeof(s3)
  92. )
  93. );
  94. oa << boost::serialization::make_nvp(
  95. "s4",
  96. boost::serialization::make_binary_object(
  97. s4,
  98. sizeof(s4)
  99. )
  100. );
  101. oa << BOOST_SERIALIZATION_NVP(a);
  102. // note: add a little bit on the end of the archive to detect
  103. // failure of text mode binary.
  104. oa << BOOST_SERIALIZATION_NVP(i);
  105. }
  106. {
  107. test_istream is(testfile, TEST_STREAM_FLAGS);
  108. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  109. ia >> boost::serialization::make_nvp(
  110. "s1",
  111. boost::serialization::make_binary_object(
  112. s1_1,
  113. sizeof(s1)
  114. )
  115. );
  116. ia >> boost::serialization::make_nvp(
  117. "s2",
  118. boost::serialization::make_binary_object(
  119. s1_2,
  120. sizeof(s2)
  121. )
  122. );
  123. ia >> boost::serialization::make_nvp(
  124. "s3",
  125. boost::serialization::make_binary_object(
  126. s1_3,
  127. sizeof(s3)
  128. )
  129. );
  130. ia >> boost::serialization::make_nvp(
  131. "s4",
  132. boost::serialization::make_binary_object(
  133. s1_4,
  134. sizeof(s4)
  135. )
  136. );
  137. ia >> BOOST_SERIALIZATION_NVP(a1);
  138. // note: add a little bit on the end of the archive to detect
  139. // failure of text mode binary.
  140. ia >> BOOST_SERIALIZATION_NVP(i1);
  141. }
  142. BOOST_CHECK(0 == std::strcmp(s1, s1_1));
  143. BOOST_CHECK(0 == std::strcmp(s2, s1_2));
  144. BOOST_CHECK(0 == std::strcmp(s3, s1_3));
  145. BOOST_CHECK(0 == std::strcmp(s4, s1_4));
  146. BOOST_CHECK(a == a1);
  147. BOOST_CHECK(i == i1);
  148. std::remove(testfile);
  149. return EXIT_SUCCESS;
  150. }
  151. // EOF