mirror of
https://github.com/boostorg/logic.git
synced 2025-06-29 14:01:06 +02:00
Compare commits
18 Commits
svn-branch
...
svn-branch
Author | SHA1 | Date | |
---|---|---|---|
32717be609 | |||
31af87e6ae | |||
9c4577cce3 | |||
9e230b17ef | |||
62e9529e09 | |||
6c6cc7dce3 | |||
5aaba307a6 | |||
a582587b22 | |||
246d1ee6df | |||
233cfca75c | |||
44aa483237 | |||
84313760a5 | |||
9df9f3daf6 | |||
24117db3ae | |||
15b34d0bf4 | |||
d1f46c8f98 | |||
23af176127 | |||
196865d37f |
16
doc/Jamfile.v2
Normal file
16
doc/Jamfile.v2
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
project boost-sandbox/utility/doc ;
|
||||||
|
import boostbook ;
|
||||||
|
import doxygen ;
|
||||||
|
|
||||||
|
doxygen reference : [ glob ../../../boost/logic/tribool.hpp ]
|
||||||
|
[ glob ../../../boost/logic/tribool_fwd.hpp ]
|
||||||
|
[ glob ../../../boost/logic/tribool_io.hpp ]
|
||||||
|
;
|
||||||
|
boostbook tribool
|
||||||
|
:
|
||||||
|
tribool.boostbook
|
||||||
|
:
|
||||||
|
<xsl:param>boost.root=../../../..
|
||||||
|
<format>pdf:<xsl:param>boost.url.prefix=http://www.boost.org/doc/libs/release/doc/html
|
||||||
|
<dependency>reference
|
||||||
|
;
|
219
doc/tribool.boostbook
Normal file
219
doc/tribool.boostbook
Normal file
@ -0,0 +1,219 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
|
||||||
|
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
|
||||||
|
<library name="Tribool" dirname="logic" id="tribool"
|
||||||
|
last-revision="$Date: 2007/05/03 03:28:53 $" xmlns:xi="http://www.w3.org/2001/XInclude">
|
||||||
|
<libraryinfo>
|
||||||
|
<author>
|
||||||
|
<firstname>Douglas</firstname>
|
||||||
|
<surname>Gregor</surname>
|
||||||
|
<email>dgregor -at- cs.indiana.edu</email>
|
||||||
|
</author>
|
||||||
|
|
||||||
|
<copyright>
|
||||||
|
<year>2002</year>
|
||||||
|
<year>2003</year>
|
||||||
|
<year>2004</year>
|
||||||
|
<holder>Douglas Gregor</holder>
|
||||||
|
</copyright>
|
||||||
|
|
||||||
|
<legalnotice>
|
||||||
|
<para>Use, modification and distribution is subject to the Boost
|
||||||
|
Software License, Version 1.0. (See accompanying file
|
||||||
|
<filename>LICENSE_1_0.txt</filename> or copy at <ulink
|
||||||
|
url="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</ulink>)</para>
|
||||||
|
</legalnotice>
|
||||||
|
|
||||||
|
<librarypurpose>Three-state boolean type</librarypurpose>
|
||||||
|
<librarycategory name="category:misc"/>
|
||||||
|
</libraryinfo>
|
||||||
|
|
||||||
|
<title>Boost.Tribool</title>
|
||||||
|
|
||||||
|
<section id="tribool.introduction">
|
||||||
|
<title>Introduction</title>
|
||||||
|
|
||||||
|
<para>The 3-state boolean library contains a single class,
|
||||||
|
<code><classname>boost::logic::tribool</classname></code>, along with
|
||||||
|
support functions and operator overloads that implement 3-state
|
||||||
|
boolean logic. </para>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section id="tribool.tutorial">
|
||||||
|
<title>Tutorial</title>
|
||||||
|
|
||||||
|
<using-namespace name="boost::logic"/>
|
||||||
|
|
||||||
|
<section>
|
||||||
|
<title>Basic usage</title>
|
||||||
|
<para> The <code><classname>tribool</classname></code> class acts
|
||||||
|
like the built-in <code>bool</code> type, but for 3-state boolean
|
||||||
|
logic. The three states are <code>true</code>, <code>false</code>,
|
||||||
|
and <code><functionname>indeterminate</functionname></code>, where
|
||||||
|
the first two states are equivalent to those of the C++
|
||||||
|
<code>bool</code> type and the last state represents an unknown
|
||||||
|
boolean value (that may be <code>true</code> or
|
||||||
|
<code>false</code>, we don't know).</para>
|
||||||
|
|
||||||
|
<para> The <code><classname>tribool</classname></code> class
|
||||||
|
supports conversion from <code>bool</code> values and literals
|
||||||
|
along with its own
|
||||||
|
<code><functionname>indeterminate</functionname></code>
|
||||||
|
keyword:</para>
|
||||||
|
|
||||||
|
<programlisting><classname>tribool</classname> b(true);
|
||||||
|
b = false;
|
||||||
|
b = <functionname>indeterminate</functionname>;
|
||||||
|
<classname>tribool</classname> b2(b);</programlisting>
|
||||||
|
|
||||||
|
<para> <code><classname>tribool</classname></code> supports
|
||||||
|
conversions to <code>bool</code> for use in conditional
|
||||||
|
statements. The conversion to <code>bool</code> will be
|
||||||
|
<code>true</code> when the value of the
|
||||||
|
<code><classname>tribool</classname></code> is always true, and
|
||||||
|
<code>false</code> otherwise. Consequently, the following idiom
|
||||||
|
may be used to determine which of the three states a
|
||||||
|
<code><classname>tribool</classname></code> currently
|
||||||
|
holds:</para>
|
||||||
|
|
||||||
|
<programlisting><classname>tribool</classname> b = some_operation();
|
||||||
|
if (b) {
|
||||||
|
// b is true
|
||||||
|
}
|
||||||
|
else if (!b) {
|
||||||
|
// b is false
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// b is indeterminate
|
||||||
|
}</programlisting>
|
||||||
|
|
||||||
|
<para> <code><classname>tribool</classname></code> supports the
|
||||||
|
3-state logic operators <code>!</code> (negation),
|
||||||
|
<code>&&</code> (AND), and <code>||</code> (OR), with
|
||||||
|
<code>bool</code> and <code><classname>tribool</classname></code>
|
||||||
|
values. For instance:</para>
|
||||||
|
|
||||||
|
<programlisting><classname>tribool</classname> x = some_op();
|
||||||
|
<classname>tribool</classname> y = some_other_op();
|
||||||
|
if (x && y) {
|
||||||
|
// both x and y are true
|
||||||
|
}
|
||||||
|
else if (!(x && y)) {
|
||||||
|
// either x or y is false
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// neither x nor y is false, but we don't know that both are true
|
||||||
|
|
||||||
|
if (x || y) {
|
||||||
|
// either x or y is true
|
||||||
|
}
|
||||||
|
}</programlisting>
|
||||||
|
|
||||||
|
<para> Similarly, <code><classname>tribool</classname></code>
|
||||||
|
supports 3-state equality comparisons via the operators
|
||||||
|
<code>==</code> and <code>!=</code>. These operators differ from
|
||||||
|
"normal" equality operators in C++ because they return a
|
||||||
|
<code><classname>tribool</classname></code>, because potentially we
|
||||||
|
might not know the result of a comparison (try to compare
|
||||||
|
<code>true</code> and
|
||||||
|
<code><functionname>indeterminate</functionname></code>). For
|
||||||
|
instance:</para>
|
||||||
|
|
||||||
|
<programlisting><classname>tribool</classname> x(true);
|
||||||
|
<classname>tribool</classname> y(<functionname>indeterminate</functionname>);
|
||||||
|
|
||||||
|
assert(x == x); // okay, x == x returns true
|
||||||
|
assert(x == true); // okay, can compare <classname>tribool</classname>s and bools</programlisting>
|
||||||
|
|
||||||
|
<para> The <code><functionname>indeterminate</functionname></code> keyword (representing the
|
||||||
|
<functionname>indeterminate</functionname> <code><classname>tribool</classname></code> value)
|
||||||
|
doubles as a function to check if the value of a
|
||||||
|
<code><classname>tribool</classname></code> is indeterminate,
|
||||||
|
e.g.,</para>
|
||||||
|
|
||||||
|
<programlisting><classname>tribool</classname> x = try_to_do_something_tricky();
|
||||||
|
if (<functionname>indeterminate</functionname>(x)) {
|
||||||
|
// value of x is indeterminate
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// report success or failure of x
|
||||||
|
}</programlisting>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section>
|
||||||
|
<title>Renaming the indeterminate state</title>
|
||||||
|
<para> Users may introduce additional keywords for the indeterminate
|
||||||
|
value in addition to the implementation-supplied
|
||||||
|
<code><functionname>indeterminate</functionname></code> using the
|
||||||
|
<code><macroname>BOOST_TRIBOOL_THIRD_STATE</macroname></code>
|
||||||
|
macro. For instance, the following macro instantiation (at the
|
||||||
|
global scope) will introduce the keyword <code>maybe</code> as a
|
||||||
|
synonym for <code><functionname>indeterminate</functionname></code>
|
||||||
|
(also residing in the <code>boost</code> namespace):</para>
|
||||||
|
<programlisting><macroname>BOOST_TRIBOOL_THIRD_STATE</macroname>(maybe)
|
||||||
|
<classname>tribool</classname> x = maybe;
|
||||||
|
if (maybe(x)) { /* ... */ }</programlisting>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section>
|
||||||
|
<title><code>tribool</code> input/output</title>
|
||||||
|
<para><code><classname>tribool</classname></code> objects may be
|
||||||
|
read from and written to streams by including the
|
||||||
|
<headername>boost/logic/tribool_io.hpp</headername> header in a
|
||||||
|
manner very similar to <code>bool</code> values. When the
|
||||||
|
<code>boolalpha</code> flag is not set on the input/output stream,
|
||||||
|
the integral values 0, 1, and 2 correspond to <code>tribool</code>
|
||||||
|
values <code>false</code>, <code>true</code>, and
|
||||||
|
<code>indeterminate</code>, respectively. When
|
||||||
|
<code>boolalpha</code> is set on the stream, arbitrary strings can
|
||||||
|
be used to represent the three values, the default being "false",
|
||||||
|
"true", and "indeterminate". For instance:</para>
|
||||||
|
<programlisting><classname>tribool</classname> x;
|
||||||
|
cin >> x; // Type "0", "1", or "2" to get false, true, or indeterminate
|
||||||
|
cout << boolalpha << x; // Produces "false", "true", or "indeterminate"</programlisting>
|
||||||
|
|
||||||
|
<para><code><classname>tribool</classname></code> input and output
|
||||||
|
is sensitive to the stream's current locale. The strings associated
|
||||||
|
with false and true values are contained in the standard
|
||||||
|
<code><classname>std::numpunct</classname></code> facet, and the
|
||||||
|
string naming the indeterminate type is contained in the
|
||||||
|
<code><classname>indeterminate_name</classname></code> facet. To
|
||||||
|
replace the name of the indeterminate state, you need to imbue your
|
||||||
|
stream with a local containing a
|
||||||
|
<code><classname>indeterminate_name</classname></code> facet, e.g.:</para>
|
||||||
|
|
||||||
|
<programlisting><macroname>BOOST_TRIBOOL_THIRD_STATE</macroname>(maybe)
|
||||||
|
locale global;
|
||||||
|
locale test_locale(global, new <classname>indeterminate_name</classname><char>("maybe"));
|
||||||
|
cout.imbue(test_locale);
|
||||||
|
<classname>tribool</classname> x(maybe);
|
||||||
|
cout << boolalpha << x << endl; // Prints "maybe"</programlisting>
|
||||||
|
|
||||||
|
<para>If you C++ standard library implementation does not support
|
||||||
|
locales, <code>tribool</code> input/output will still work, but you
|
||||||
|
will be unable to customize the strings printed/parsed when
|
||||||
|
<code>boolalpha</code> is set.</para>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<xi:include href="reference.xml"/>
|
||||||
|
|
||||||
|
<testsuite id="tribool.tests">
|
||||||
|
<run-test filename="tribool_test.cpp">
|
||||||
|
<purpose><para>Test all features of the
|
||||||
|
<code><classname>boost::logic::tribool</classname></code>
|
||||||
|
class.</para></purpose>
|
||||||
|
</run-test>
|
||||||
|
|
||||||
|
<run-test filename="tribool_rename_test.cpp">
|
||||||
|
<purpose><para>Test the use of the
|
||||||
|
<code><macroname>BOOST_TRIBOOL_THIRD_STATE</macroname></code>
|
||||||
|
macro.</para></purpose>
|
||||||
|
</run-test>
|
||||||
|
|
||||||
|
<run-test filename="tribool_io_test.cpp">
|
||||||
|
<purpose><para>Test tribool input/output.</para></purpose>
|
||||||
|
</run-test>
|
||||||
|
</testsuite>
|
||||||
|
</library>
|
@ -93,7 +93,7 @@ public:
|
|||||||
*
|
*
|
||||||
* \throws nothrow
|
* \throws nothrow
|
||||||
*/
|
*/
|
||||||
tribool(bool value) : value(value? true_value : false_value) {}
|
tribool(bool initial_value) : value(initial_value? true_value : false_value) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new 3-state boolean value with an indeterminate value.
|
* Construct a new 3-state boolean value with an indeterminate value.
|
||||||
@ -336,7 +336,7 @@ inline tribool operator==(tribool x, tribool y)
|
|||||||
if (indeterminate(x) || indeterminate(y))
|
if (indeterminate(x) || indeterminate(y))
|
||||||
return indeterminate;
|
return indeterminate;
|
||||||
else
|
else
|
||||||
return x && y || !x && !y;
|
return (x && y) || (!x && !y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -399,7 +399,7 @@ inline tribool operator!=(tribool x, tribool y)
|
|||||||
if (indeterminate(x) || indeterminate(y))
|
if (indeterminate(x) || indeterminate(y))
|
||||||
return indeterminate;
|
return indeterminate;
|
||||||
else
|
else
|
||||||
return !(x && y || !x && !y);
|
return !((x && y) || (!x && !y));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -452,7 +452,7 @@ namespace boost {
|
|||||||
#define BOOST_TRIBOOL_THIRD_STATE(Name) \
|
#define BOOST_TRIBOOL_THIRD_STATE(Name) \
|
||||||
inline bool \
|
inline bool \
|
||||||
Name(boost::logic::tribool x, \
|
Name(boost::logic::tribool x, \
|
||||||
boost::logic::detail::indeterminate_t dummy = \
|
boost::logic::detail::indeterminate_t = \
|
||||||
boost::logic::detail::indeterminate_t()) \
|
boost::logic::detail::indeterminate_t()) \
|
||||||
{ return x.value == boost::logic::tribool::indeterminate_value; }
|
{ return x.value == boost::logic::tribool::indeterminate_value; }
|
||||||
|
|
||||||
|
@ -140,7 +140,8 @@ public:
|
|||||||
indeterminate_name() : name_(get_default_indeterminate_name<CharT>()) {}
|
indeterminate_name() : name_(get_default_indeterminate_name<CharT>()) {}
|
||||||
|
|
||||||
/// Construct the facet with the given name for the indeterminate value
|
/// Construct the facet with the given name for the indeterminate value
|
||||||
explicit indeterminate_name(const string_type& name) : name_(name) {}
|
explicit indeterminate_name(const string_type& initial_name)
|
||||||
|
: name_(initial_name) {}
|
||||||
|
|
||||||
/// Returns the name for the indeterminate value
|
/// Returns the name for the indeterminate value
|
||||||
string_type name() const { return name_; }
|
string_type name() const { return name_; }
|
||||||
|
16
index.html
Normal file
16
index.html
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
<!--
|
||||||
|
Copyright 2011 Daniel James.
|
||||||
|
Distributed under 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)
|
||||||
|
-->
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="refresh" content="0; URL=../../doc/html/tribool.html">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
Automatic redirection failed, please go to
|
||||||
|
<a href="../../doc/html/tribool.html">../../doc/html/tribool.html</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
17
test/Jamfile.v2
Normal file
17
test/Jamfile.v2
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# Tribool library
|
||||||
|
|
||||||
|
# Copyright (C) 2002-2003 Douglas Gregor
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
# For more information, see http://www.boost.org/
|
||||||
|
|
||||||
|
test-suite logic :
|
||||||
|
[ run tribool_test.cpp ]
|
||||||
|
[ run tribool_rename_test.cpp ]
|
||||||
|
[ run tribool_io_test.cpp ]
|
||||||
|
;
|
||||||
|
|
||||||
|
|
198
test/tribool_io_test.cpp
Normal file
198
test/tribool_io_test.cpp
Normal file
@ -0,0 +1,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;
|
||||||
|
}
|
123
test/tribool_rename_test.cpp
Normal file
123
test/tribool_rename_test.cpp
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
// Copyright Douglas Gregor 2002-2003. 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)
|
||||||
|
|
||||||
|
// For more information, see http://www.boost.org
|
||||||
|
|
||||||
|
#include <boost/logic/tribool.hpp>
|
||||||
|
#include <boost/test/minimal.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
BOOST_TRIBOOL_THIRD_STATE(maybe)
|
||||||
|
|
||||||
|
int test_main(int,char*[])
|
||||||
|
{
|
||||||
|
using namespace boost::logic;
|
||||||
|
|
||||||
|
tribool x; // false
|
||||||
|
tribool y(true); // true
|
||||||
|
tribool z(maybe); // maybe
|
||||||
|
|
||||||
|
BOOST_CHECK(!x);
|
||||||
|
BOOST_CHECK(x == false);
|
||||||
|
BOOST_CHECK(false == x);
|
||||||
|
BOOST_CHECK(x != true);
|
||||||
|
BOOST_CHECK(true != x);
|
||||||
|
BOOST_CHECK(maybe(x == maybe));
|
||||||
|
BOOST_CHECK(maybe(maybe == x));
|
||||||
|
BOOST_CHECK(maybe(x != maybe));
|
||||||
|
BOOST_CHECK(maybe(maybe != x));
|
||||||
|
BOOST_CHECK(x == x);
|
||||||
|
BOOST_CHECK(!(x != x));
|
||||||
|
BOOST_CHECK(!(x && true));
|
||||||
|
BOOST_CHECK(!(true && x));
|
||||||
|
BOOST_CHECK(x || true);
|
||||||
|
BOOST_CHECK(true || x);
|
||||||
|
|
||||||
|
BOOST_CHECK(y);
|
||||||
|
BOOST_CHECK(y == true);
|
||||||
|
BOOST_CHECK(true == y);
|
||||||
|
BOOST_CHECK(y != false);
|
||||||
|
BOOST_CHECK(false != y);
|
||||||
|
BOOST_CHECK(maybe(y == maybe));
|
||||||
|
BOOST_CHECK(maybe(maybe == y));
|
||||||
|
BOOST_CHECK(maybe(y != maybe));
|
||||||
|
BOOST_CHECK(maybe(maybe != y));
|
||||||
|
BOOST_CHECK(y == y);
|
||||||
|
BOOST_CHECK(!(y != y));
|
||||||
|
|
||||||
|
BOOST_CHECK(maybe(z || !z));
|
||||||
|
BOOST_CHECK(maybe(z == true));
|
||||||
|
BOOST_CHECK(maybe(true == z));
|
||||||
|
BOOST_CHECK(maybe(z == false));
|
||||||
|
BOOST_CHECK(maybe(false == z));
|
||||||
|
BOOST_CHECK(maybe(z == maybe));
|
||||||
|
BOOST_CHECK(maybe(maybe == z));
|
||||||
|
BOOST_CHECK(maybe(z != maybe));
|
||||||
|
BOOST_CHECK(maybe(maybe != z));
|
||||||
|
BOOST_CHECK(maybe(z == z));
|
||||||
|
BOOST_CHECK(maybe(z != z));
|
||||||
|
|
||||||
|
BOOST_CHECK(!(x == y));
|
||||||
|
BOOST_CHECK(x != y);
|
||||||
|
BOOST_CHECK(maybe(x == z));
|
||||||
|
BOOST_CHECK(maybe(x != z));
|
||||||
|
BOOST_CHECK(maybe(y == z));
|
||||||
|
BOOST_CHECK(maybe(y != z));
|
||||||
|
|
||||||
|
BOOST_CHECK(!(x && y));
|
||||||
|
BOOST_CHECK(x || y);
|
||||||
|
BOOST_CHECK(!(x && z));
|
||||||
|
BOOST_CHECK(maybe(y && z));
|
||||||
|
BOOST_CHECK(maybe(z && z));
|
||||||
|
BOOST_CHECK(maybe(z || z));
|
||||||
|
BOOST_CHECK(maybe(x || z));
|
||||||
|
BOOST_CHECK(y || z);
|
||||||
|
|
||||||
|
BOOST_CHECK(maybe(y && maybe));
|
||||||
|
BOOST_CHECK(maybe(maybe && y));
|
||||||
|
BOOST_CHECK(!(x && maybe));
|
||||||
|
BOOST_CHECK(!(maybe && x));
|
||||||
|
|
||||||
|
BOOST_CHECK(maybe || y);
|
||||||
|
BOOST_CHECK(y || maybe);
|
||||||
|
BOOST_CHECK(maybe(x || maybe));
|
||||||
|
BOOST_CHECK(maybe(maybe || x));
|
||||||
|
|
||||||
|
// Test the if (z) ... else (!z) ... else ... idiom
|
||||||
|
if (z) {
|
||||||
|
BOOST_CHECK(false);
|
||||||
|
}
|
||||||
|
else if (!z) {
|
||||||
|
BOOST_CHECK(false);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
BOOST_CHECK(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
z = true;
|
||||||
|
if (z) {
|
||||||
|
BOOST_CHECK(true);
|
||||||
|
}
|
||||||
|
else if (!z) {
|
||||||
|
BOOST_CHECK(false);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
BOOST_CHECK(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
z = false;
|
||||||
|
if (z) {
|
||||||
|
BOOST_CHECK(false);
|
||||||
|
}
|
||||||
|
else if (!z) {
|
||||||
|
BOOST_CHECK(true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
BOOST_CHECK(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "no errors detected\n";
|
||||||
|
return 0;
|
||||||
|
}
|
119
test/tribool_test.cpp
Normal file
119
test/tribool_test.cpp
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
// Copyright Douglas Gregor 2002-2003. 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/test/minimal.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int test_main(int, char*[])
|
||||||
|
{
|
||||||
|
using namespace boost::logic;
|
||||||
|
|
||||||
|
tribool x; // false
|
||||||
|
tribool y(true); // true
|
||||||
|
tribool z(indeterminate); // indeterminate
|
||||||
|
|
||||||
|
BOOST_CHECK(!x);
|
||||||
|
BOOST_CHECK(x == false);
|
||||||
|
BOOST_CHECK(false == x);
|
||||||
|
BOOST_CHECK(x != true);
|
||||||
|
BOOST_CHECK(true != x);
|
||||||
|
BOOST_CHECK(indeterminate(x == indeterminate));
|
||||||
|
BOOST_CHECK(indeterminate(indeterminate == x));
|
||||||
|
BOOST_CHECK(indeterminate(x != indeterminate));
|
||||||
|
BOOST_CHECK(indeterminate(indeterminate != x));
|
||||||
|
BOOST_CHECK(x == x);
|
||||||
|
BOOST_CHECK(!(x != x));
|
||||||
|
BOOST_CHECK(!(x && true));
|
||||||
|
BOOST_CHECK(!(true && x));
|
||||||
|
BOOST_CHECK(x || true);
|
||||||
|
BOOST_CHECK(true || x);
|
||||||
|
|
||||||
|
BOOST_CHECK(y);
|
||||||
|
BOOST_CHECK(y == true);
|
||||||
|
BOOST_CHECK(true == y);
|
||||||
|
BOOST_CHECK(y != false);
|
||||||
|
BOOST_CHECK(false != y);
|
||||||
|
BOOST_CHECK(indeterminate(y == indeterminate));
|
||||||
|
BOOST_CHECK(indeterminate(indeterminate == y));
|
||||||
|
BOOST_CHECK(indeterminate(y != indeterminate));
|
||||||
|
BOOST_CHECK(indeterminate(indeterminate != y));
|
||||||
|
BOOST_CHECK(y == y);
|
||||||
|
BOOST_CHECK(!(y != y));
|
||||||
|
|
||||||
|
BOOST_CHECK(indeterminate(z || !z));
|
||||||
|
BOOST_CHECK(indeterminate(z == true));
|
||||||
|
BOOST_CHECK(indeterminate(true == z));
|
||||||
|
BOOST_CHECK(indeterminate(z == false));
|
||||||
|
BOOST_CHECK(indeterminate(false == z));
|
||||||
|
BOOST_CHECK(indeterminate(z == indeterminate));
|
||||||
|
BOOST_CHECK(indeterminate(indeterminate == z));
|
||||||
|
BOOST_CHECK(indeterminate(z != indeterminate));
|
||||||
|
BOOST_CHECK(indeterminate(indeterminate != z));
|
||||||
|
BOOST_CHECK(indeterminate(z == z));
|
||||||
|
BOOST_CHECK(indeterminate(z != z));
|
||||||
|
|
||||||
|
BOOST_CHECK(!(x == y));
|
||||||
|
BOOST_CHECK(x != y);
|
||||||
|
BOOST_CHECK(indeterminate(x == z));
|
||||||
|
BOOST_CHECK(indeterminate(x != z));
|
||||||
|
BOOST_CHECK(indeterminate(y == z));
|
||||||
|
BOOST_CHECK(indeterminate(y != z));
|
||||||
|
|
||||||
|
BOOST_CHECK(!(x && y));
|
||||||
|
BOOST_CHECK(x || y);
|
||||||
|
BOOST_CHECK(!(x && z));
|
||||||
|
BOOST_CHECK(indeterminate(y && z));
|
||||||
|
BOOST_CHECK(indeterminate(z && z));
|
||||||
|
BOOST_CHECK(indeterminate(z || z));
|
||||||
|
BOOST_CHECK(indeterminate(x || z));
|
||||||
|
BOOST_CHECK(y || z);
|
||||||
|
|
||||||
|
BOOST_CHECK(indeterminate(y && indeterminate));
|
||||||
|
BOOST_CHECK(indeterminate(indeterminate && y));
|
||||||
|
BOOST_CHECK(!(x && indeterminate));
|
||||||
|
BOOST_CHECK(!(indeterminate && x));
|
||||||
|
|
||||||
|
BOOST_CHECK(indeterminate || y);
|
||||||
|
BOOST_CHECK(y || indeterminate);
|
||||||
|
BOOST_CHECK(indeterminate(x || indeterminate));
|
||||||
|
BOOST_CHECK(indeterminate(indeterminate || x));
|
||||||
|
|
||||||
|
// Test the if (z) ... else (!z) ... else ... idiom
|
||||||
|
if (z) {
|
||||||
|
BOOST_CHECK(false);
|
||||||
|
}
|
||||||
|
else if (!z) {
|
||||||
|
BOOST_CHECK(false);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
BOOST_CHECK(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
z = true;
|
||||||
|
if (z) {
|
||||||
|
BOOST_CHECK(true);
|
||||||
|
}
|
||||||
|
else if (!z) {
|
||||||
|
BOOST_CHECK(false);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
BOOST_CHECK(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
z = false;
|
||||||
|
if (z) {
|
||||||
|
BOOST_CHECK(false);
|
||||||
|
}
|
||||||
|
else if (!z) {
|
||||||
|
BOOST_CHECK(true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
BOOST_CHECK(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "no errors detected\n";
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user