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