tribool_test.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright Douglas Gregor 2002-2003. 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/config.hpp>
  6. #include <boost/logic/tribool.hpp>
  7. #include <boost/test/minimal.hpp>
  8. #include <iostream>
  9. int test_main(int, char*[])
  10. {
  11. using namespace boost::logic;
  12. tribool x; // false
  13. tribool y(true); // true
  14. tribool z(indeterminate); // indeterminate
  15. #if defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS )
  16. // c++03 allows for implicit conversion to bool
  17. // c++11 uses an explicit conversion operator so this would not compile
  18. // and that is tested in the compile-fail/implicit.cpp file
  19. // so we check the conversion to ensure it is sane
  20. bool bx = x;
  21. BOOST_CHECK(bx == false);
  22. bool by = y;
  23. BOOST_CHECK(by == true);
  24. bool bz = z;
  25. BOOST_CHECK(bz == false);
  26. #endif
  27. BOOST_CHECK(!x);
  28. BOOST_CHECK(x == false);
  29. BOOST_CHECK(false == x);
  30. BOOST_CHECK(x != true);
  31. BOOST_CHECK(true != x);
  32. BOOST_CHECK(indeterminate(x == indeterminate));
  33. BOOST_CHECK(indeterminate(indeterminate == x));
  34. BOOST_CHECK(indeterminate(x != indeterminate));
  35. BOOST_CHECK(indeterminate(indeterminate != x));
  36. BOOST_CHECK(x == x);
  37. BOOST_CHECK(!(x != x));
  38. BOOST_CHECK(!(x && true));
  39. BOOST_CHECK(!(true && x));
  40. BOOST_CHECK(x || true);
  41. BOOST_CHECK(true || x);
  42. BOOST_CHECK(y);
  43. BOOST_CHECK(y == true);
  44. BOOST_CHECK(true == y);
  45. BOOST_CHECK(y != false);
  46. BOOST_CHECK(false != y);
  47. BOOST_CHECK(indeterminate(y == indeterminate));
  48. BOOST_CHECK(indeterminate(indeterminate == y));
  49. BOOST_CHECK(indeterminate(y != indeterminate));
  50. BOOST_CHECK(indeterminate(indeterminate != y));
  51. BOOST_CHECK(y == y);
  52. BOOST_CHECK(!(y != y));
  53. BOOST_CHECK(indeterminate(z || !z));
  54. BOOST_CHECK(indeterminate(z == true));
  55. BOOST_CHECK(indeterminate(true == z));
  56. BOOST_CHECK(indeterminate(z == false));
  57. BOOST_CHECK(indeterminate(false == z));
  58. BOOST_CHECK(indeterminate(z == indeterminate));
  59. BOOST_CHECK(indeterminate(indeterminate == z));
  60. BOOST_CHECK(indeterminate(z != indeterminate));
  61. BOOST_CHECK(indeterminate(indeterminate != z));
  62. BOOST_CHECK(indeterminate(z == z));
  63. BOOST_CHECK(indeterminate(z != z));
  64. BOOST_CHECK(!(x == y));
  65. BOOST_CHECK(x != y);
  66. BOOST_CHECK(indeterminate(x == z));
  67. BOOST_CHECK(indeterminate(x != z));
  68. BOOST_CHECK(indeterminate(y == z));
  69. BOOST_CHECK(indeterminate(y != z));
  70. BOOST_CHECK(!(x && y));
  71. BOOST_CHECK(x || y);
  72. BOOST_CHECK(!(x && z));
  73. BOOST_CHECK(indeterminate(y && z));
  74. BOOST_CHECK(indeterminate(z && z));
  75. BOOST_CHECK(indeterminate(z || z));
  76. BOOST_CHECK(indeterminate(x || z));
  77. BOOST_CHECK(y || z);
  78. BOOST_CHECK(indeterminate(y && indeterminate));
  79. BOOST_CHECK(indeterminate(indeterminate && y));
  80. BOOST_CHECK(!(x && indeterminate));
  81. BOOST_CHECK(!(indeterminate && x));
  82. BOOST_CHECK(indeterminate || y);
  83. BOOST_CHECK(y || indeterminate);
  84. BOOST_CHECK(indeterminate(x || indeterminate));
  85. BOOST_CHECK(indeterminate(indeterminate || x));
  86. // Test the if (z) ... else (!z) ... else ... idiom
  87. if (z) {
  88. BOOST_CHECK(false);
  89. }
  90. else if (!z) {
  91. BOOST_CHECK(false);
  92. }
  93. else {
  94. BOOST_CHECK(true);
  95. }
  96. z = true;
  97. if (z) {
  98. BOOST_CHECK(true);
  99. }
  100. else if (!z) {
  101. BOOST_CHECK(false);
  102. }
  103. else {
  104. BOOST_CHECK(false);
  105. }
  106. z = false;
  107. if (z) {
  108. BOOST_CHECK(false);
  109. }
  110. else if (!z) {
  111. BOOST_CHECK(true);
  112. }
  113. else {
  114. BOOST_CHECK(false);
  115. }
  116. #if !defined(BOOST_NO_CXX11_CONSTEXPR)
  117. constexpr bool res_ors = indeterminate(false || tribool(false) || false || indeterminate); // true
  118. BOOST_CHECK(res_ors);
  119. char array_ors[res_ors ? 2 : 3];
  120. BOOST_CHECK(sizeof(array_ors) / sizeof(char) == 2);
  121. constexpr bool res_ands = !indeterminate(!(true && tribool(true) && true && indeterminate)); // false
  122. BOOST_CHECK(!res_ands);
  123. char array_ands[res_ands ? 2 : 3];
  124. BOOST_CHECK(sizeof(array_ands) / sizeof(char) == 3);
  125. constexpr bool res_safe_bool = static_cast<bool>( tribool(true) );
  126. BOOST_STATIC_ASSERT(res_safe_bool);
  127. // gcc 4.6 chokes on the xxx assignment
  128. # if !BOOST_WORKAROUND(BOOST_GCC, < 40700)
  129. constexpr tribool xxx = (tribool(true) || tribool(indeterminate));
  130. BOOST_STATIC_ASSERT(xxx);
  131. # endif
  132. #endif
  133. std::cout << "no errors detected\n";
  134. return 0;
  135. }