ios_manip_fmtflags.cc [plain text]
#include <sstream>
#include <locale>
#include <iomanip>
#include <testsuite_hooks.h>
struct MyNP : std::numpunct<char>
{
std::string do_truename() const;
std::string do_falsename() const;
};
std::string MyNP::do_truename() const
{
std::string s("yea");
return s;
}
std::string MyNP::do_falsename() const
{
std::string s("nay");
return s;
}
void
test01()
{
bool test = true;
const char lit[] = "1 0\ntrue false\n: true:\n:true :\n: false:\n: 1:"
"\n:1 :\n: 0:\nyea nay\n: yea:\n:yea :\n: nay:\n";
std::ostringstream oss;
oss << true << " " << false << std::endl;
oss << std::boolalpha;
oss << true << " " << false << std::endl;
oss << ":" << std::setw(6) << std::internal << true << ":" << std::endl;
oss << ":" << std::setw(6) << std::left << true << ":" << std::endl;
oss << ":" << std::setw(6) << std::right << false << ":" << std::endl;
oss << std::noboolalpha;
oss << ":" << std::setw(3) << std::internal << true << ":" << std::endl;
oss << ":" << std::setw(3) << std::left << true << ":" << std::endl;
oss << ":" << std::setw(3) << std::right << false << ":" << std::endl;
std::locale loc = std::locale (std::locale::classic(), new MyNP);
oss.imbue(loc);
oss << std::boolalpha;
oss << true << " " << false << std::endl;
oss << ":" << std::setw(6) << std::internal << true << ":" << std::endl;
oss << ":" << std::setw(6) << std::left << true << ":" << std::endl;
oss << ":" << std::setw(6) << std::right << false << ":" << std::endl;
VERIFY( oss.good() );
VERIFY( oss.str() == lit );
}
void
test02()
{
bool test = true;
const std::string strue("true");
const std::string sfalse("false");
std::string str01;
std::string str02;
std::locale loc("");
std::ostringstream ostr01;
ostr01.imbue(loc);
std::ios_base::fmtflags ff = ostr01.flags(std::ios_base::boolalpha);
ostr01 << true;
str02 = ostr01.str();
VERIFY( str02 == strue );
ostr01.str(str01);
ostr01 << false;
str02 = ostr01.str();
VERIFY( str02 == sfalse );
VERIFY( test );
}
int
main()
{
test01();
test02();
return 0;
}