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