regression_adapt_adt.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. // Copyright (c) 2011 Colin Rundel
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/mpl/print.hpp>
  7. #include <boost/config/warning_disable.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/fusion/include/adapt_adt.hpp>
  10. #include <boost/spirit/include/karma.hpp>
  11. #include <boost/spirit/include/support_adapt_adt_attributes.hpp>
  12. #include "test.hpp"
  13. ///////////////////////////////////////////////////////////////////////////////
  14. class data1
  15. {
  16. private:
  17. int width_;
  18. int height_;
  19. public:
  20. data1()
  21. : width_(400), height_(400)
  22. {}
  23. data1(int width, int height)
  24. : width_(width), height_(height)
  25. {}
  26. int width() const { return width_;}
  27. int height() const { return height_;}
  28. void set_width(int width) { width_ = width;}
  29. void set_height(int height) { height_ = height;}
  30. };
  31. BOOST_FUSION_ADAPT_ADT(
  32. data1,
  33. (int, int, obj.width(), obj.set_width(val))
  34. (int, int, obj.height(), obj.set_height(val))
  35. )
  36. ///////////////////////////////////////////////////////////////////////////////
  37. class data2
  38. {
  39. private:
  40. std::string data_;
  41. public:
  42. data2()
  43. : data_("test")
  44. {}
  45. data2(std::string const& data)
  46. : data_(data)
  47. {}
  48. std::string const& data() const { return data_;}
  49. void set_data(std::string const& data) { data_ = data;}
  50. };
  51. BOOST_FUSION_ADAPT_ADT(
  52. data2,
  53. (std::string, std::string const&, obj.data(), obj.set_data(val))
  54. )
  55. ///////////////////////////////////////////////////////////////////////////////
  56. class data3
  57. {
  58. private:
  59. double data_;
  60. public:
  61. data3(double data = 0.0)
  62. : data_(data)
  63. {}
  64. double data() const { return data_;}
  65. void set_data(double data) { data_ = data;}
  66. };
  67. BOOST_FUSION_ADAPT_ADT(
  68. data3,
  69. (double, double, obj.data(), obj.set_data(val))
  70. )
  71. ///////////////////////////////////////////////////////////////////////////////
  72. class data4
  73. {
  74. public:
  75. boost::optional<int> a_;
  76. boost::optional<double> b_;
  77. boost::optional<std::string> c_;
  78. boost::optional<int> const& a() const { return a_; }
  79. boost::optional<double> const& b() const { return b_; }
  80. boost::optional<std::string> const& c() const { return c_; }
  81. };
  82. BOOST_FUSION_ADAPT_ADT(
  83. data4,
  84. (boost::optional<int>, boost::optional<int> const&, obj.a(), /**/)
  85. (boost::optional<double>, boost::optional<double> const&, obj.b(), /**/)
  86. (boost::optional<std::string>, boost::optional<std::string> const&, obj.c(), /**/)
  87. )
  88. ///////////////////////////////////////////////////////////////////////////////
  89. int main ()
  90. {
  91. using spirit_test::test;
  92. {
  93. using boost::spirit::karma::int_;
  94. data1 b(800, 600);
  95. BOOST_TEST(test("width: 800\nheight: 600\n",
  96. "width: " << int_ << "\n" << "height: " << int_ << "\n", b));
  97. }
  98. {
  99. using boost::spirit::karma::char_;
  100. using boost::spirit::karma::string;
  101. data2 d("test");
  102. BOOST_TEST(test("data: test\n", "data: " << +char_ << "\n", d));
  103. BOOST_TEST(test("data: test\n", "data: " << string << "\n", d));
  104. }
  105. {
  106. using boost::spirit::karma::double_;
  107. BOOST_TEST(test("x=0.0\n", "x=" << double_ << "\n", data3(0)));
  108. BOOST_TEST(test("x=1.1\n", "x=" << double_ << "\n", data3(1.1)));
  109. BOOST_TEST(test("x=1.0e10\n", "x=" << double_ << "\n", data3(1e10)));
  110. #if defined(_MSC_VER) && _MSC_VER < 1900
  111. # pragma warning(push)
  112. # pragma warning(disable: 4127) // conditional expression is constant
  113. #endif
  114. BOOST_TEST(test("x=inf\n", "x=" << double_ << "\n",
  115. data3(std::numeric_limits<double>::infinity())));
  116. if (std::numeric_limits<double>::has_quiet_NaN) {
  117. BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n",
  118. data3(std::numeric_limits<double>::quiet_NaN())));
  119. }
  120. if (std::numeric_limits<double>::has_signaling_NaN) {
  121. BOOST_TEST(test("x=nan\n", "x=" << double_ << "\n",
  122. data3(std::numeric_limits<double>::signaling_NaN())));
  123. }
  124. #if defined(_MSC_VER) && _MSC_VER < 1900
  125. # pragma warning(pop)
  126. #endif
  127. }
  128. {
  129. using boost::spirit::karma::double_;
  130. using boost::spirit::karma::int_;
  131. using boost::spirit::karma::string;
  132. data4 d;
  133. d.b_ = 10;
  134. BOOST_TEST(test(
  135. "Testing: b: 10.0\n",
  136. "Testing: " << -("a: " << int_ << "\n")
  137. << -("b: " << double_ << "\n")
  138. << -("c: " << string << "\n"), d));
  139. }
  140. return boost::report_errors();
  141. }