test_variant.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_variant.cpp
  3. // test of non-intrusive serialization of variant types
  4. //
  5. // copyright (c) 2005
  6. // troy d. straszheim <troy@resophonic.com>
  7. // http://www.resophonic.com
  8. //
  9. // Use, modification and distribution is subject to the Boost Software
  10. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. //
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. //
  15. // thanks to Robert Ramey and Peter Dimov.
  16. //
  17. #include <cstddef> // NULL
  18. #include <cstdio> // remove
  19. #include <fstream>
  20. #include <boost/config.hpp>
  21. #include <boost/math/special_functions/next.hpp> // float_distance
  22. #if defined(BOOST_NO_STDC_NAMESPACE)
  23. namespace std{
  24. using ::remove;
  25. }
  26. #endif
  27. #include <boost/type_traits/is_same.hpp>
  28. #include <boost/mpl/eval_if.hpp>
  29. #include <boost/mpl/identity.hpp>
  30. #include <boost/serialization/throw_exception.hpp>
  31. #if defined(_MSC_VER) && (_MSC_VER <= 1020)
  32. # pragma warning (disable : 4786) // too long name, harmless warning
  33. #endif
  34. #include "test_tools.hpp"
  35. #include <boost/archive/archive_exception.hpp>
  36. #include <boost/serialization/nvp.hpp>
  37. #include <boost/serialization/variant.hpp>
  38. #include "A.hpp"
  39. #include "A.ipp"
  40. class are_equal
  41. : public boost::static_visitor<bool>
  42. {
  43. public:
  44. // note extra rigamorole for compilers which don't support
  45. // partial function template ordering - specfically msvc 6.x
  46. struct same {
  47. template<class T, class U>
  48. static bool invoke(const T & t, const U & u){
  49. return t == u;
  50. }
  51. };
  52. struct not_same {
  53. template<class T, class U>
  54. static bool invoke(const T &, const U &){
  55. return false;
  56. }
  57. };
  58. template <class T, class U>
  59. bool operator()( const T & t, const U & u) const
  60. {
  61. typedef typename boost::mpl::eval_if<boost::is_same<T, U>,
  62. boost::mpl::identity<same>,
  63. boost::mpl::identity<not_same>
  64. >::type type;
  65. return type::invoke(t, u);
  66. }
  67. bool operator()( const float & lhs, const float & rhs ) const
  68. {
  69. return std::abs( boost::math::float_distance(lhs, rhs)) < 2;
  70. }
  71. bool operator()( const double & lhs, const double & rhs ) const
  72. {
  73. return std::abs( boost::math::float_distance(lhs, rhs)) < 2;
  74. }
  75. };
  76. template <class T>
  77. void test_type(const T& gets_written){
  78. const char * testfile = boost::archive::tmpnam(NULL);
  79. BOOST_REQUIRE(testfile != NULL);
  80. {
  81. test_ostream os(testfile, TEST_STREAM_FLAGS);
  82. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  83. oa << boost::serialization::make_nvp("written", gets_written);
  84. }
  85. T got_read;
  86. {
  87. test_istream is(testfile, TEST_STREAM_FLAGS);
  88. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  89. ia >> boost::serialization::make_nvp("written", got_read);
  90. }
  91. BOOST_CHECK(boost::apply_visitor(are_equal(), gets_written, got_read));
  92. std::remove(testfile);
  93. }
  94. // this verifies that if you try to read in a variant from a file
  95. // whose "which" is illegal for the one in memory (that is, you're
  96. // reading in to a different variant than you wrote out to) the load()
  97. // operation will throw. One could concievably add checking for
  98. // sequence length as well, but this would add size to the archive for
  99. // dubious benefit.
  100. //
  101. void do_bad_read()
  102. {
  103. // Compiling this test invokes and ICE on msvc 6
  104. // So, we'll just to skip it for this compiler
  105. #if defined(_MSC_VER) && (_MSC_VER <= 1020)
  106. boost::variant<bool, float, int, std::string> big_variant;
  107. big_variant = std::string("adrenochrome");
  108. const char * testfile = boost::archive::tmpnam(NULL);
  109. BOOST_REQUIRE(testfile != NULL);
  110. {
  111. test_ostream os(testfile, TEST_STREAM_FLAGS);
  112. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  113. oa << BOOST_SERIALIZATION_NVP(big_variant);
  114. }
  115. boost::variant<bool, float, int> little_variant;
  116. {
  117. test_istream is(testfile, TEST_STREAM_FLAGS);
  118. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  119. bool exception_invoked = false;
  120. BOOST_TRY {
  121. ia >> BOOST_SERIALIZATION_NVP(little_variant);
  122. } BOOST_CATCH (boost::archive::archive_exception const& e) {
  123. BOOST_CHECK(boost::archive::archive_exception::unsupported_version == e.code);
  124. exception_invoked = true;
  125. }
  126. BOOST_CATCH_END
  127. BOOST_CHECK(exception_invoked);
  128. }
  129. #endif
  130. }
  131. struct H {
  132. int i;
  133. };
  134. namespace boost {
  135. namespace serialization {
  136. template<class Archive>
  137. void serialize(Archive &ar, H & h, const unsigned int /*file_version*/){
  138. ar & boost::serialization::make_nvp("h", h.i);
  139. }
  140. } // namespace serialization
  141. } // namespace boost
  142. inline bool operator==(H const & lhs, H const & rhs) {
  143. return lhs.i == rhs.i;
  144. }
  145. inline bool operator!=(H const & lhs, H const & rhs) {
  146. return !(lhs == rhs);
  147. }
  148. inline bool operator<(H const & lhs, H const & rhs) {
  149. return lhs.i < rhs.i;
  150. }
  151. inline std::size_t hash_value(H const & val) {
  152. return val.i;
  153. }
  154. void test_pointer(){
  155. const char * testfile = boost::archive::tmpnam(NULL);
  156. BOOST_REQUIRE(testfile != NULL);
  157. typedef boost::variant<H, int> variant_t;
  158. H const h = {5};
  159. variant_t v(h);
  160. {
  161. test_ostream os(testfile, TEST_STREAM_FLAGS);
  162. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  163. oa << boost::serialization::make_nvp("written", v);
  164. const H * h_ptr = & boost::strict_get<H const &>(v);
  165. oa << boost::serialization::make_nvp("written", h_ptr);
  166. }
  167. variant_t v2;
  168. {
  169. test_istream is(testfile, TEST_STREAM_FLAGS);
  170. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  171. ia >> boost::serialization::make_nvp("written", v2);
  172. H * h2_ptr;
  173. ia >> boost::serialization::make_nvp("written", h2_ptr);
  174. BOOST_CHECK_EQUAL(h, boost::strict_get<H const>(v2));
  175. BOOST_CHECK_EQUAL(h2_ptr, & boost::strict_get<H const &>(v2));
  176. }
  177. BOOST_CHECK_EQUAL(v, v2);
  178. }
  179. #include <boost/serialization/map.hpp>
  180. #include <boost/serialization/set.hpp>
  181. // test a pointer to an object contained into a variant that is an
  182. // element of a set
  183. void test_variant_set()
  184. {
  185. const char * testfile = boost::archive::tmpnam(NULL);
  186. BOOST_REQUIRE(testfile != NULL);
  187. typedef boost::variant<H, int> variant_t;
  188. typedef std::set<variant_t> uset_t;
  189. uset_t set;
  190. {
  191. test_ostream os(testfile, TEST_STREAM_FLAGS);
  192. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  193. H const h = {5};
  194. variant_t v(h);
  195. set.insert(v);
  196. oa << boost::serialization::make_nvp("written", set);
  197. H const * const h_ptr = boost::strict_get<H const>(&(*set.begin()));
  198. oa << boost::serialization::make_nvp("written", h_ptr);
  199. }
  200. uset_t set2;
  201. {
  202. test_istream is(testfile, TEST_STREAM_FLAGS);
  203. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  204. ia >> boost::serialization::make_nvp("written", set2);
  205. H * h_ptr;
  206. ia >> boost::serialization::make_nvp("written", h_ptr);
  207. const H * h_ptr2 = & boost::strict_get<H const>(*set2.begin());
  208. BOOST_CHECK_EQUAL(h_ptr, h_ptr2);
  209. }
  210. BOOST_CHECK_EQUAL(set, set2);
  211. }
  212. // test a pointer to an object contained into a variant that is an
  213. // element of a map
  214. void test_variant_map()
  215. {
  216. const char * testfile = boost::archive::tmpnam(NULL);
  217. BOOST_REQUIRE(testfile != NULL);
  218. typedef boost::variant<H, int> variant_t;
  219. typedef std::map<int, variant_t> map_t;
  220. map_t map;
  221. {
  222. test_ostream os(testfile, TEST_STREAM_FLAGS);
  223. test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  224. H const h = {5};
  225. variant_t v(h);
  226. map[0] = v;
  227. BOOST_ASSERT(1 == map.size());
  228. oa << boost::serialization::make_nvp("written", map);
  229. H const * const h_ptr = boost::strict_get<H const>(&map[0]);
  230. BOOST_CHECK_EQUAL(h_ptr, boost::strict_get<H const>(&map[0]));
  231. oa << boost::serialization::make_nvp("written", h_ptr);
  232. }
  233. map_t map2;
  234. {
  235. test_istream is(testfile, TEST_STREAM_FLAGS);
  236. test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  237. ia >> boost::serialization::make_nvp("written", map2);
  238. BOOST_ASSERT(1 == map2.size());
  239. H * h_ptr;
  240. ia >> boost::serialization::make_nvp("written", h_ptr);
  241. H const * const h_ptr2 = boost::strict_get<H const>(&map2[0]);
  242. BOOST_CHECK_EQUAL(h_ptr, h_ptr2);
  243. }
  244. BOOST_CHECK_EQUAL(map, map2);
  245. }
  246. int test_main( int /* argc */, char* /* argv */[] )
  247. {
  248. {
  249. boost::variant<bool, int, float, double, A, std::string> v;
  250. v = false;
  251. test_type(v);
  252. v = 1;
  253. test_type(v);
  254. v = (float) 2.3;
  255. test_type(v);
  256. v = (double) 6.4;
  257. test_type(v);
  258. v = std::string("we can't stop here, this is Bat Country");
  259. test_type(v);
  260. v = A();
  261. test_type(v);
  262. }
  263. test_pointer();
  264. test_variant_set();
  265. test_variant_map();
  266. do_bad_read();
  267. return EXIT_SUCCESS;
  268. }
  269. // EOF