test_tools.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #ifndef BOOST_SERIALIZATION_TEST_TOOLS_HPP
  2. #define BOOST_SERIALIZATION_TEST_TOOLS_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. #define BOOST_FILESYSTEM_VERSION 3
  8. #include <boost/filesystem/operations.hpp> // unique_path
  9. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  10. // test_tools.hpp
  11. //
  12. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  13. // Use, modification and distribution is subject to the Boost Software
  14. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. // See http://www.boost.org for updates, documentation, and revision history.
  17. #include <boost/config.hpp>
  18. #ifndef BOOST_NO_EXCEPTION_STD_NAMESPACE
  19. #include <exception>
  20. #endif
  21. #include <boost/detail/no_exceptions_support.hpp>
  22. #if defined(UNDER_CE)
  23. // Windows CE does not supply the tmpnam function in its CRT.
  24. // Substitute a primitive implementation here.
  25. namespace boost {
  26. namespace archive {
  27. const char * tmpnam(char * buffer){
  28. static char ibuffer [512];
  29. if(NULL == buffer)
  30. buffer = ibuffer;
  31. static unsigned short index = 0;
  32. std::sprintf(buffer, "\\tmpfile%05X.tmp", index++);
  33. return buffer;
  34. }
  35. } // archive
  36. } // boost
  37. #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
  38. // win32 has a brain-dead tmpnam implementation.
  39. // which leaves temp files in root directory
  40. // regardless of environmental settings
  41. #include <cstdlib>
  42. #include <cstring>
  43. #if defined(BOOST_NO_STDC_NAMESPACE)
  44. namespace std{
  45. using ::remove;
  46. using ::strcpy;
  47. using ::strcat;
  48. using ::tmpnam;
  49. }
  50. #endif // defined(BOOST_NO_STDC_NAMESPACE)
  51. #include <direct.h>
  52. #include <boost/archive/tmpdir.hpp>
  53. //#if defined(__COMO__)
  54. #define chdir _chdir
  55. //#endif // defined win32
  56. #if defined(NDEBUG) && defined(__BORLANDC__)
  57. #define STRCPY strcpy
  58. #else
  59. #define STRCPY std::strcpy
  60. #endif
  61. namespace boost {
  62. namespace archive {
  63. const char * test_filename(const char * dir = NULL, char *fname = NULL){
  64. static char ibuffer [512];
  65. ibuffer[0] = '\0';
  66. if(NULL == dir){
  67. dir = boost::archive::tmpdir();
  68. }
  69. STRCPY(ibuffer, dir);
  70. std::strcat(ibuffer, "/");
  71. if(NULL == fname){
  72. char old_dir[256];
  73. _getcwd(old_dir, sizeof(old_dir) - 1);
  74. chdir(dir);
  75. // (C) Copyright 2010 Dean Michael Berris. <mikhailberis@gmail.com>
  76. // Instead of using std::tmpnam, we use Boost.Filesystem's unique_path
  77. boost::filesystem::path tmp_path =
  78. boost::filesystem::unique_path("%%%%%");
  79. std::strcat(ibuffer, tmp_path.string().c_str());
  80. chdir(old_dir);
  81. }
  82. else{
  83. std::strcat(ibuffer, fname);
  84. }
  85. return ibuffer;
  86. }
  87. const char * tmpnam(char * buffer){
  88. const char * name = test_filename(NULL, NULL);
  89. if(NULL != buffer){
  90. STRCPY(buffer, name);
  91. }
  92. return name;
  93. }
  94. } // archive
  95. } // boost
  96. #else // defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
  97. #if defined(__hpux)
  98. // (C) Copyright 2006 Boris Gubenko.
  99. // HP-UX has a restriction that for multi-thread applications, (i.e.
  100. // the ones compiled -mt) if argument to tmpnam is a NULL pointer, then,
  101. // citing the tmpnam(3S) manpage, "the operation is not performed and a
  102. // NULL pointer is returned". tempnam does not have this restriction, so,
  103. // let's use tempnam instead.
  104. #define tmpnam(X) tempnam(NULL,X)
  105. namespace boost {
  106. namespace archive {
  107. using ::tmpnam;
  108. } // archive
  109. } // boost
  110. #else // defined(__hpux)
  111. // (C) Copyright 2010 Dean Michael Berris.
  112. // Instead of using the potentially dangrous tempnam function that's part
  113. // of the C standard library, on Unix/Linux we use the more portable and
  114. // "safe" unique_path function provided in the Boost.Filesystem library.
  115. #include <boost/archive/tmpdir.hpp>
  116. namespace boost {
  117. namespace archive {
  118. char const * tmpnam(char * buffer) {
  119. static char name[512] = {0};
  120. if (name[0] == 0) {
  121. boost::filesystem::path tempdir(tmpdir());
  122. boost::filesystem::path tempfilename =
  123. boost::filesystem::unique_path("serialization-%%%%");
  124. boost::filesystem::path temp = tempdir / tempfilename;
  125. std::strcat(name, temp.string().c_str());
  126. }
  127. if (buffer != 0) std::strcpy(buffer, name);
  128. return name;
  129. }
  130. } // archive
  131. } // boost
  132. #endif // defined(__hpux)
  133. #endif // defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
  134. #include <boost/core/lightweight_test.hpp>
  135. #define BOOST_CHECK( P ) \
  136. BOOST_TEST( (P) )
  137. #define BOOST_REQUIRE( P ) \
  138. BOOST_TEST( (P) )
  139. #define BOOST_CHECK_MESSAGE( P, M ) \
  140. ((P)? (void)0 : ::boost::detail::error_impl( (M) , __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
  141. #define BOOST_REQUIRE_MESSAGE( P, M ) \
  142. BOOST_CHECK_MESSAGE( (P), (M) )
  143. #define BOOST_CHECK_EQUAL( A , B ) \
  144. BOOST_TEST( (A) == (B) )
  145. namespace boost { namespace detail {
  146. inline void msg_impl(char const * msg, char const * file, int line, char const * function)
  147. {
  148. std::cerr << file << "(" << line << "): " << msg << " in function '" << function << "'" << std::endl;
  149. }
  150. } } // boost::detail
  151. #define BOOST_WARN_MESSAGE( P, M ) \
  152. ((P)? (void)0 : ::boost::detail::msg_impl( (M) , __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
  153. #define BOOST_MESSAGE( M ) \
  154. BOOST_WARN_MESSAGE( true , (M) )
  155. #define BOOST_CHECKPOINT( M ) \
  156. BOOST_WARN_MESSAGE( true , (M) )
  157. //#define BOOST_TEST_DONT_PRINT_LOG_VALUE( T )
  158. #define BOOST_FAIL( M ) BOOST_REQUIRE_MESSAGE( false, (M) )
  159. #define EXIT_SUCCESS 0
  160. int test_main(int argc, char * argv[]);
  161. #include <boost/serialization/singleton.hpp>
  162. int
  163. main(int argc, char * argv[]){
  164. boost::serialization::get_singleton_module().lock();
  165. int retval = 1;
  166. BOOST_TRY{
  167. retval = test_main(argc, argv);
  168. }
  169. #ifndef BOOST_NO_EXCEPTION_STD_NAMESPACE
  170. BOOST_CATCH(const std::exception & e){
  171. BOOST_ERROR(e.what());
  172. }
  173. #endif
  174. BOOST_CATCH(...){
  175. BOOST_ERROR("failed with uncaught exception:");
  176. }
  177. BOOST_CATCH_END
  178. boost::serialization::get_singleton_module().unlock();
  179. int error_count = boost::report_errors();
  180. if(error_count > 0)
  181. retval = error_count;
  182. return retval;
  183. }
  184. // the following is to ensure that when one of the libraries changes
  185. // BJAM rebuilds and relinks the test.
  186. /*
  187. #include "text_archive.hpp"
  188. #include "text_warchive.hpp"
  189. #include "binary_archive.hpp"
  190. #include "xml_archive.hpp"
  191. #include "xml_warchive.hpp"
  192. #include "polymorphic_text_archive.hpp"
  193. #include "polymorphic_text_warchive.hpp"
  194. #include "polymorphic_binary_archive.hpp"
  195. #include "polymorphic_xml_archive.hpp"
  196. #include "polymorphic_xml_warchive.hpp"
  197. */
  198. /////////////////////////////////////////////
  199. // invoke header for a custom archive test.
  200. /////////////////////////////////////////////
  201. // invoke header for a custom archive test.
  202. #if ! defined(BOOST_ARCHIVE_TEST)
  203. #define BOOST_ARCHIVE_TEST text_archive.hpp
  204. #endif
  205. #include <boost/preprocessor/stringize.hpp>
  206. #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
  207. #ifndef TEST_STREAM_FLAGS
  208. #define TEST_STREAM_FLAGS (std::ios_base::openmode)0
  209. #endif
  210. #ifndef TEST_ARCHIVE_FLAGS
  211. #define TEST_ARCHIVE_FLAGS 0
  212. #endif
  213. #ifndef TEST_DIRECTORY
  214. #define TEST_DIRECTORY
  215. #else
  216. #define __x__ TEST_DIRECTORY
  217. #undef TEST_DIRECTORY
  218. #define TEST_DIRECTORY BOOST_PP_STRINGIZE(__x__)
  219. #endif
  220. #endif // BOOST_SERIALIZATION_TEST_TOOLS_HPP