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