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