test_complex.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_complex.cpp
  3. // (C) Copyright 2005 Matthias Troyer .
  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 <fstream>
  9. #include <cstddef> // NULL
  10. #include <cstdlib> // rand
  11. #include <cstdio> // remove
  12. #include <boost/config.hpp>
  13. #include <boost/detail/workaround.hpp>
  14. #include <boost/math/special_functions/next.hpp>
  15. #if defined(BOOST_NO_STDC_NAMESPACE)
  16. #include <boost/limits.hpp>
  17. namespace std{
  18. using ::rand;
  19. using ::remove;
  20. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(UNDER_CE)
  21. using ::numeric_limits;
  22. #endif
  23. }
  24. #endif
  25. #include "test_tools.hpp"
  26. #include <boost/preprocessor/stringize.hpp>
  27. #include BOOST_PP_STRINGIZE(BOOST_ARCHIVE_TEST)
  28. #include <boost/serialization/complex.hpp>
  29. #include <iostream>
  30. int test_main( int /* argc */, char* /* argv */[] )
  31. {
  32. const char * testfile = boost::archive::tmpnam(NULL);
  33. BOOST_REQUIRE(NULL != testfile);
  34. // test array of objects
  35. std::complex<float> a(
  36. static_cast<float>(std::rand()) / static_cast<float>(std::rand()),
  37. static_cast<float>(std::rand()) / static_cast<float>(std::rand())
  38. );
  39. std::complex<double> b(
  40. static_cast<double>(std::rand()) / static_cast<double>(std::rand()),
  41. static_cast<double>(std::rand()) / static_cast<double>(std::rand())
  42. );
  43. {
  44. test_ostream os(testfile, TEST_STREAM_FLAGS);
  45. test_oarchive oa(os);
  46. oa << boost::serialization::make_nvp("afloatcomplex", a);
  47. oa << boost::serialization::make_nvp("adoublecomplex", b);
  48. }
  49. std::complex<float> a1;
  50. std::complex<double> b1;
  51. {
  52. test_istream is(testfile, TEST_STREAM_FLAGS);
  53. test_iarchive ia(is);
  54. ia >> boost::serialization::make_nvp("afloatcomplex", a1);
  55. ia >> boost::serialization::make_nvp("adoublecomplex", b1);
  56. }
  57. std::cerr << "a.real()-a1a.real() distance = " << std::abs( boost::math::float_distance(a.real(), a1.real())) << std::endl;
  58. BOOST_CHECK(std::abs(boost::math::float_distance(a.real(), a1.real())) < 2);
  59. std::cerr << "a.imag() - a1a.imag() distance = " << std::abs( boost::math::float_distance(a.imag(), a1.imag())) << std::endl;
  60. BOOST_CHECK(std::abs(boost::math::float_distance(a.imag(), a1.imag())) < 2);
  61. std::cerr << "b.real() - b1.real() distance = " << std::abs( boost::math::float_distance(b.real(), b1.real())) << std::endl;
  62. BOOST_CHECK(std::abs(boost::math::float_distance(b.real(), b1.real())) < 2);
  63. std::cerr << "b.imag() - b1.imag() distance = " << std::abs( boost::math::float_distance(b.imag(), b1.imag())) << std::endl;
  64. BOOST_CHECK(std::abs(boost::math::float_distance(b.imag(), b1.imag())) < 2);
  65. std::remove(testfile);
  66. return EXIT_SUCCESS;
  67. }
  68. // EOF