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