test_dll_simple.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_dll_simple.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. // invoke header for a custom archive test.
  9. #include <cstddef> // NULL
  10. #include <fstream>
  11. #include <cstdio> // remove
  12. #include <boost/config.hpp>
  13. #if defined(BOOST_NO_STDC_NAMESPACE)
  14. namespace std{
  15. using ::remove;
  16. }
  17. #endif
  18. // for now, only test with simple text and polymorphic archive
  19. #include "test_tools.hpp"
  20. #include "text_archive.hpp"
  21. #include <boost/archive/polymorphic_text_oarchive.hpp>
  22. #include <boost/archive/polymorphic_text_iarchive.hpp>
  23. #define A_IMPORT
  24. #include "A.hpp"
  25. // simple class with text archive compiled in dll
  26. void
  27. test1(){
  28. const char * testfile = boost::archive::tmpnam(NULL);
  29. BOOST_REQUIRE(NULL != testfile);
  30. const A a;
  31. A a1;
  32. {
  33. test_ostream os(testfile, TEST_STREAM_FLAGS);
  34. boost::archive::text_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  35. oa << boost::serialization::make_nvp("a", a);
  36. }
  37. {
  38. test_istream is(testfile, TEST_STREAM_FLAGS);
  39. boost::archive::text_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  40. ia >> boost::serialization::make_nvp("a", a1);
  41. }
  42. BOOST_CHECK_EQUAL(a, a1);
  43. std::remove(testfile);
  44. }
  45. // simple class with polymorphic archive compiled in dll
  46. void
  47. test2(){
  48. const char * testfile = boost::archive::tmpnam(NULL);
  49. BOOST_REQUIRE(NULL != testfile);
  50. const A a;
  51. A a1;
  52. {
  53. test_ostream os(testfile, TEST_STREAM_FLAGS);
  54. boost::archive::polymorphic_text_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  55. boost::archive::polymorphic_oarchive & poa(oa);
  56. poa << boost::serialization::make_nvp("a", a);
  57. }
  58. {
  59. test_istream is(testfile, TEST_STREAM_FLAGS);
  60. boost::archive::polymorphic_text_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  61. boost::archive::polymorphic_iarchive & pia(ia);
  62. pia >> boost::serialization::make_nvp("a", a1);
  63. }
  64. BOOST_CHECK_EQUAL(a, a1);
  65. std::remove(testfile);
  66. }
  67. // simple class pointer with text archive compiled in dll
  68. void
  69. test3(){
  70. const char * testfile = boost::archive::tmpnam(NULL);
  71. BOOST_REQUIRE(NULL != testfile);
  72. const A *a = new A;
  73. A *a1;
  74. {
  75. test_ostream os(testfile, TEST_STREAM_FLAGS);
  76. boost::archive::text_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  77. oa << boost::serialization::make_nvp("a", a);
  78. }
  79. {
  80. test_istream is(testfile, TEST_STREAM_FLAGS);
  81. boost::archive::text_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  82. ia >> boost::serialization::make_nvp("a", a1);
  83. }
  84. BOOST_CHECK_EQUAL(*a, *a1);
  85. std::remove(testfile);
  86. delete a;
  87. }
  88. // simple class pointer with polymorphic archive compiled in dll
  89. void
  90. test4(){
  91. const char * testfile = boost::archive::tmpnam(NULL);
  92. BOOST_REQUIRE(NULL != testfile);
  93. const A *a = new A;
  94. A *a1;
  95. {
  96. test_ostream os(testfile, TEST_STREAM_FLAGS);
  97. boost::archive::polymorphic_text_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  98. boost::archive::polymorphic_oarchive & poa(oa);
  99. poa << boost::serialization::make_nvp("a", a);
  100. }
  101. {
  102. test_istream is(testfile, TEST_STREAM_FLAGS);
  103. boost::archive::polymorphic_text_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  104. boost::archive::polymorphic_iarchive & pia(ia);
  105. pia >> boost::serialization::make_nvp("a", a1);
  106. }
  107. BOOST_CHECK_EQUAL(*a, *a1);
  108. std::remove(testfile);
  109. delete a;
  110. }
  111. #include "B.hpp"
  112. // derived class with base text archive compiled in dll
  113. void
  114. test5(){
  115. const char * testfile = boost::archive::tmpnam(NULL);
  116. BOOST_REQUIRE(NULL != testfile);
  117. const B b;
  118. B b1;
  119. {
  120. test_ostream os(testfile, TEST_STREAM_FLAGS);
  121. boost::archive::text_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  122. oa << boost::serialization::make_nvp("b", b);
  123. }
  124. {
  125. test_istream is(testfile, TEST_STREAM_FLAGS);
  126. boost::archive::text_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  127. ia >> boost::serialization::make_nvp("b", b1);
  128. }
  129. BOOST_CHECK_EQUAL(b, b1);
  130. std::remove(testfile);
  131. }
  132. // derived class with base base compiled with polymorphic archive in dll
  133. void
  134. test6(){
  135. const char * testfile = boost::archive::tmpnam(NULL);
  136. BOOST_REQUIRE(NULL != testfile);
  137. const B b;
  138. B b1;
  139. {
  140. test_ostream os(testfile, TEST_STREAM_FLAGS);
  141. boost::archive::polymorphic_text_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  142. boost::archive::polymorphic_oarchive & poa(oa);
  143. poa << boost::serialization::make_nvp("b", b);
  144. }
  145. {
  146. test_istream is(testfile, TEST_STREAM_FLAGS);
  147. boost::archive::polymorphic_text_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  148. boost::archive::polymorphic_iarchive & pia(ia);
  149. pia >> boost::serialization::make_nvp("b", b1);
  150. }
  151. BOOST_CHECK_EQUAL(b, b1);
  152. std::remove(testfile);
  153. }
  154. // derived class pointer with base text archive compiled in dll
  155. void
  156. test7(){
  157. const char * testfile = boost::archive::tmpnam(NULL);
  158. BOOST_REQUIRE(NULL != testfile);
  159. const B *b = new B;
  160. B *b1;
  161. {
  162. test_ostream os(testfile, TEST_STREAM_FLAGS);
  163. boost::archive::text_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  164. oa << boost::serialization::make_nvp("b", b);
  165. }
  166. {
  167. test_istream is(testfile, TEST_STREAM_FLAGS);
  168. boost::archive::text_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  169. ia >> boost::serialization::make_nvp("b", b1);
  170. }
  171. BOOST_CHECK_EQUAL(*b, *b1);
  172. std::remove(testfile);
  173. delete b;
  174. }
  175. // derived class pointer with base polymorphic archive compiled in dll
  176. void
  177. test8(){
  178. const char * testfile = boost::archive::tmpnam(NULL);
  179. BOOST_REQUIRE(NULL != testfile);
  180. const B *b = new B;
  181. B *b1;
  182. {
  183. test_ostream os(testfile, TEST_STREAM_FLAGS);
  184. boost::archive::polymorphic_text_oarchive oa(os, TEST_ARCHIVE_FLAGS);
  185. boost::archive::polymorphic_oarchive & poa(oa);
  186. poa << boost::serialization::make_nvp("b", b);
  187. }
  188. {
  189. test_istream is(testfile, TEST_STREAM_FLAGS);
  190. boost::archive::polymorphic_text_iarchive ia(is, TEST_ARCHIVE_FLAGS);
  191. boost::archive::polymorphic_iarchive & pia(ia);
  192. pia >> boost::serialization::make_nvp("b", b1);
  193. }
  194. BOOST_CHECK_EQUAL(*b, *b1);
  195. std::remove(testfile);
  196. delete b;
  197. }
  198. int
  199. test_main( int /* argc */, char* /* argv */[] )
  200. {
  201. test1();
  202. test2();
  203. test3();
  204. test4();
  205. test5();
  206. test6();
  207. test7();
  208. test8();
  209. return EXIT_SUCCESS;
  210. }