bool.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Hartmut Kaiser
  3. Copyright (c) 2001-2011 Joel de Guzman
  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. =============================================================================*/
  7. #include <boost/config/warning_disable.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/spirit/include/karma_string.hpp>
  10. #include <boost/spirit/include/karma_numeric.hpp>
  11. #include <boost/spirit/include/karma_directive.hpp>
  12. #include <boost/spirit/include/karma_operator.hpp>
  13. #include <boost/spirit/include/karma_phoenix_attributes.hpp>
  14. #include <boost/spirit/include/phoenix_core.hpp>
  15. #include <boost/spirit/include/phoenix_operator.hpp>
  16. #include "test.hpp"
  17. using namespace spirit_test;
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // special bool output policy allowing to spell false as true backwards (eurt)
  20. struct special_bool_policy : boost::spirit::karma::bool_policies<>
  21. {
  22. template <typename CharEncoding, typename Tag
  23. , typename OutputIterator>
  24. static bool generate_false(OutputIterator& sink, bool)
  25. {
  26. // we want to spell the names of true and false backwards
  27. return boost::spirit::karma::string_inserter<CharEncoding, Tag>::
  28. call(sink, "eurt");
  29. }
  30. };
  31. ///////////////////////////////////////////////////////////////////////////////
  32. // special policy allowing to use any type as a boolean
  33. struct test_bool_data
  34. {
  35. explicit test_bool_data(bool b) : b(b) {}
  36. bool b;
  37. // we need to provide (safe) convertibility to bool
  38. private:
  39. struct dummy { void true_() {} };
  40. typedef void (dummy::*safe_bool)();
  41. public:
  42. operator safe_bool () const { return b ? &dummy::true_ : 0; }
  43. };
  44. struct test_bool_policy : boost::spirit::karma::bool_policies<>
  45. {
  46. template <typename Inserter, typename OutputIterator, typename Policies>
  47. static bool
  48. call (OutputIterator& sink, test_bool_data b, Policies const& p)
  49. {
  50. // call the predefined inserter to do the job
  51. return Inserter::call_n(sink, bool(b), p);
  52. }
  53. };
  54. ///////////////////////////////////////////////////////////////////////////////
  55. int main()
  56. {
  57. using boost::spirit::karma::bool_;
  58. using boost::spirit::karma::false_;
  59. using boost::spirit::karma::true_;
  60. using boost::spirit::karma::lit;
  61. using boost::spirit::karma::lower;
  62. using boost::spirit::karma::upper;
  63. // testing plain bool
  64. {
  65. BOOST_TEST(test("false", bool_, false));
  66. BOOST_TEST(test("true", bool_, true));
  67. BOOST_TEST(test("false", false_, false));
  68. BOOST_TEST(test("true", true_, true));
  69. BOOST_TEST(test("false", bool_(false)));
  70. BOOST_TEST(test("true", bool_(true)));
  71. BOOST_TEST(test("false", bool_(false), false));
  72. BOOST_TEST(test("true", bool_(true), true));
  73. BOOST_TEST(!test("", bool_(false), true));
  74. BOOST_TEST(!test("", bool_(true), false));
  75. BOOST_TEST(test("false", lit(false)));
  76. BOOST_TEST(test("true", lit(true)));
  77. }
  78. // test optional attributes
  79. {
  80. boost::optional<bool> optbool;
  81. BOOST_TEST(!test("", bool_, optbool));
  82. BOOST_TEST(test("", -bool_, optbool));
  83. optbool = false;
  84. BOOST_TEST(test("false", bool_, optbool));
  85. BOOST_TEST(test("false", -bool_, optbool));
  86. optbool = true;
  87. BOOST_TEST(test("true", bool_, optbool));
  88. BOOST_TEST(test("true", -bool_, optbool));
  89. }
  90. // we support Phoenix attributes only starting with V2.2
  91. #if SPIRIT_VERSION >= 0x2020
  92. // test Phoenix expression attributes (requires to include
  93. // karma_phoenix_attributes.hpp)
  94. {
  95. namespace phoenix = boost::phoenix;
  96. BOOST_TEST(test("true", bool_, phoenix::val(true)));
  97. bool b = false;
  98. BOOST_TEST(test("false", bool_, phoenix::ref(b)));
  99. BOOST_TEST(test("true", bool_, !phoenix::ref(b)));
  100. }
  101. #endif
  102. {
  103. BOOST_TEST(test("false", lower[bool_], false));
  104. BOOST_TEST(test("true", lower[bool_], true));
  105. BOOST_TEST(test("false", lower[bool_(false)]));
  106. BOOST_TEST(test("true", lower[bool_(true)]));
  107. BOOST_TEST(test("false", lower[bool_(false)], false));
  108. BOOST_TEST(test("true", lower[bool_(true)], true));
  109. BOOST_TEST(!test("", lower[bool_(false)], true));
  110. BOOST_TEST(!test("", lower[bool_(true)], false));
  111. BOOST_TEST(test("false", lower[lit(false)]));
  112. BOOST_TEST(test("true", lower[lit(true)]));
  113. }
  114. {
  115. BOOST_TEST(test("FALSE", upper[bool_], false));
  116. BOOST_TEST(test("TRUE", upper[bool_], true));
  117. BOOST_TEST(test("FALSE", upper[bool_(false)]));
  118. BOOST_TEST(test("TRUE", upper[bool_(true)]));
  119. BOOST_TEST(test("FALSE", upper[bool_(false)], false));
  120. BOOST_TEST(test("TRUE", upper[bool_(true)], true));
  121. BOOST_TEST(!test("", upper[bool_(false)], true));
  122. BOOST_TEST(!test("", upper[bool_(true)], false));
  123. BOOST_TEST(test("FALSE", upper[lit(false)]));
  124. BOOST_TEST(test("TRUE", upper[lit(true)]));
  125. }
  126. {
  127. typedef boost::spirit::karma::bool_generator<bool, special_bool_policy>
  128. backwards_bool_type;
  129. backwards_bool_type const backwards_bool = backwards_bool_type();
  130. BOOST_TEST(test("eurt", backwards_bool, false));
  131. BOOST_TEST(test("true", backwards_bool, true));
  132. BOOST_TEST(test("eurt", backwards_bool(false)));
  133. BOOST_TEST(test("true", backwards_bool(true)));
  134. BOOST_TEST(test("eurt", backwards_bool(false), false));
  135. BOOST_TEST(test("true", backwards_bool(true), true));
  136. BOOST_TEST(!test("", backwards_bool(false), true));
  137. BOOST_TEST(!test("", backwards_bool(true), false));
  138. }
  139. {
  140. typedef boost::spirit::karma::bool_generator<
  141. test_bool_data, test_bool_policy> test_bool_type;
  142. test_bool_type const test_bool = test_bool_type();
  143. test_bool_data const test_false = test_bool_data(false);
  144. test_bool_data const test_true = test_bool_data(true);
  145. BOOST_TEST(test("false", test_bool, test_false));
  146. BOOST_TEST(test("true", test_bool, test_true));
  147. BOOST_TEST(test("false", test_bool(test_false)));
  148. BOOST_TEST(test("true", test_bool(test_true)));
  149. BOOST_TEST(test("false", test_bool(test_false), test_false));
  150. BOOST_TEST(test("true", test_bool(test_true), test_true));
  151. BOOST_TEST(!test("", test_bool(test_false), test_true));
  152. BOOST_TEST(!test("", test_bool(test_true), test_false));
  153. }
  154. return boost::report_errors();
  155. }