Ticket #1574: ptime-operator.cpp

File ptime-operator.cpp, 1.1 KB (added by christian.stimming@…, 12 years ago)

Test case for this bug: Due to the error, in the second line the date still appears even though it shouldn't have been printed because of the ios::failbit.

Line 
1// Test case to demonstrate that operator<< of
2// boost::posix_time::ptime doesn't take a set ios::failbit into
3// account correctly but instead prints itself everytime.
4
5// Compile and run as follows:
6// g++ ptime-operator.cpp -o ptime-operator
7// ./ptime-operator
8
9#include <ios>
10#include <boost/date_time/posix_time/posix_time.hpp>
11#include <boost/version.hpp>
12using namespace std;
13
14int main(int /*argc*/, char** /*argv*/)
15{
16 boost::posix_time::ptime p = boost::posix_time::microsec_clock::local_time();
17
18 // The following line should appear
19 cout << "Should appear: Text. And Date: " << p << " Boost v" << int(BOOST_VERSION) << endl;
20
21 cout.clear (std::ios::failbit); // set ios::failbit -> no more output
22
23 // The following line should *not* appear, BUT the time is printed
24 // anyway! (That's the bug.)
25 cout << "Should not appear: Text. And Date: " << p << endl;
26
27 cout.clear (); // remove ios::failbit -> resume output
28
29 // The following line should appear again
30 cout << "\nThis line should appear (but no single date above): Text. And Date: " << p << std::endl;
31}