tribool_io_test.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright Douglas Gregor 2002-2004. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/logic/tribool.hpp>
  6. #include <boost/logic/tribool_io.hpp>
  7. #include <boost/test/minimal.hpp>
  8. #include <sstream>
  9. #include <string>
  10. #include <iostream>
  11. #include <ios> // for std::boolalpha
  12. #ifndef BOOST_NO_STD_LOCALE
  13. # include <locale>
  14. #endif
  15. int test_main(int, char*[])
  16. {
  17. using namespace boost::logic;
  18. tribool x;
  19. #if !defined(BOOST_NO_CWCHAR) && !defined(BOOST_NO_STD_WSTRING)
  20. std::wostringstream wout;
  21. wout << std::boolalpha << tribool(false);
  22. BOOST_CHECK(wout.str() == L"false");
  23. wout.str(std::wstring());
  24. wout << std::boolalpha << tribool(true);
  25. BOOST_CHECK(wout.str() == L"true");
  26. wout.str(std::wstring());
  27. wout << std::boolalpha << tribool(indeterminate);
  28. BOOST_CHECK(wout.str() == L"indeterminate");
  29. #endif
  30. // Check tribool output
  31. std::ostringstream out;
  32. // Output false (noboolalpha)
  33. out.str(std::string());
  34. x = false;
  35. out << x;
  36. std::cout << "Output false (noboolalpha): " << out.str() << std::endl;
  37. BOOST_CHECK(out.str() == "0");
  38. // Output true (noboolalpha)
  39. out.str(std::string());
  40. x = true;
  41. out << x;
  42. std::cout << "Output true (noboolalpha): " << out.str() << std::endl;
  43. BOOST_CHECK(out.str() == "1");
  44. // Output indeterminate (noboolalpha)
  45. out.str(std::string());
  46. x = indeterminate;
  47. out << x;
  48. std::cout << "Output indeterminate (noboolalpha): " << out.str()
  49. << std::endl;
  50. BOOST_CHECK(out.str() == "2");
  51. // Output indeterminate (noboolalpha)
  52. out.str(std::string());
  53. out << indeterminate;
  54. std::cout << "Output indeterminate (noboolalpha): " << out.str()
  55. << std::endl;
  56. BOOST_CHECK(out.str() == "2");
  57. #ifndef BOOST_NO_STD_LOCALE
  58. const std::numpunct<char>& punct =
  59. BOOST_USE_FACET(std::numpunct<char>, out.getloc());
  60. // Output false (boolalpha)
  61. out.str(std::string());
  62. x = false;
  63. out << std::boolalpha << x;
  64. std::cout << "Output false (boolalpha): " << out.str() << std::endl;
  65. BOOST_CHECK(out.str() == punct.falsename());
  66. // Output true (boolalpha)
  67. out.str(std::string());
  68. x = true;
  69. out << std::boolalpha << x;
  70. std::cout << "Output true (boolalpha): " << out.str() << std::endl;
  71. BOOST_CHECK(out.str() == punct.truename());
  72. // Output indeterminate (boolalpha - default name)
  73. out.str(std::string());
  74. x = indeterminate;
  75. out << std::boolalpha << x;
  76. std::cout << "Output indeterminate (boolalpha - default name): " << out.str()
  77. << std::endl;
  78. BOOST_CHECK(out.str() == "indeterminate");
  79. // Output indeterminate (boolalpha - default name)
  80. out.str(std::string());
  81. out << std::boolalpha << indeterminate;
  82. std::cout << "Output indeterminate (boolalpha - default name): " << out.str()
  83. << std::endl;
  84. BOOST_CHECK(out.str() == "indeterminate");
  85. # if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
  86. // No template constructors, so we can't build the test locale
  87. # else
  88. // Give indeterminate a new name, and output it via boolalpha
  89. std::locale global;
  90. std::locale test_locale(global, new indeterminate_name<char>("maybe"));
  91. out.imbue(test_locale);
  92. out.str(std::string());
  93. out << std::boolalpha << x;
  94. std::cout << "Output indeterminate (boolalpha - \"maybe\"): " << out.str()
  95. << std::endl;
  96. BOOST_CHECK(out.str() == "maybe");
  97. # endif
  98. #endif // ! BOOST_NO_STD_LOCALE
  99. // Checking tribool input
  100. // Input false (noboolalpha)
  101. {
  102. std::istringstream in("0");
  103. std::cout << "Input \"0\" (checks for false)" << std::endl;
  104. in >> x;
  105. BOOST_CHECK(x == false);
  106. }
  107. // Input true (noboolalpha)
  108. {
  109. std::istringstream in("1");
  110. std::cout << "Input \"1\" (checks for true)" << std::endl;
  111. in >> x;
  112. BOOST_CHECK(x == true);
  113. }
  114. // Input false (noboolalpha)
  115. {
  116. std::istringstream in("2");
  117. std::cout << "Input \"2\" (checks for indeterminate)" << std::endl;
  118. in >> x;
  119. BOOST_CHECK(indeterminate(x));
  120. }
  121. // Input bad number (noboolalpha)
  122. {
  123. std::istringstream in("3");
  124. std::cout << "Input \"3\" (checks for failure)" << std::endl;
  125. BOOST_CHECK(!(in >> x));
  126. }
  127. // Input false (boolalpha)
  128. {
  129. std::istringstream in("false");
  130. std::cout << "Input \"false\" (checks for false)" << std::endl;
  131. in >> std::boolalpha >> x;
  132. BOOST_CHECK(x == false);
  133. }
  134. // Input true (boolalpha)
  135. {
  136. std::istringstream in("true");
  137. std::cout << "Input \"true\" (checks for true)" << std::endl;
  138. in >> std::boolalpha >> x;
  139. BOOST_CHECK(x == true);
  140. }
  141. // Input indeterminate (boolalpha)
  142. {
  143. std::istringstream in("indeterminate");
  144. std::cout << "Input \"indeterminate\" (checks for indeterminate)"
  145. << std::endl;
  146. in >> std::boolalpha >> x;
  147. BOOST_CHECK(indeterminate(x));
  148. }
  149. // Input bad string (boolalpha)
  150. {
  151. std::istringstream in("bad");
  152. std::cout << "Input \"bad\" (checks for failure)"
  153. << std::endl;
  154. BOOST_CHECK(!(in >> std::boolalpha >> x));
  155. }
  156. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
  157. // No template constructors, so we can't build the test locale
  158. #elif !defined(BOOST_NO_STD_LOCALE)
  159. // Input indeterminate named "maybe" (boolalpha)
  160. {
  161. std::istringstream in("maybe");
  162. in.imbue(test_locale);
  163. std::cout << "Input \"maybe\" (checks for indeterminate, uses locales)"
  164. << std::endl;
  165. in >> std::boolalpha >> x;
  166. BOOST_CHECK(indeterminate(x));
  167. }
  168. // Input indeterminate named "true_or_false" (boolalpha)
  169. {
  170. std::locale my_locale(global,
  171. new indeterminate_name<char>("true_or_false"));
  172. std::istringstream in("true_or_false");
  173. in.imbue(my_locale);
  174. std::cout << "Input \"true_or_false\" (checks for indeterminate)"
  175. << std::endl;
  176. in >> std::boolalpha >> x;
  177. BOOST_CHECK(indeterminate(x));
  178. }
  179. #endif
  180. return 0;
  181. }