2
0
mirror of https://github.com/boostorg/convert.git synced 2026-01-30 19:52:17 +00:00
Files
convert/test/sstream_manipulators.cpp

60 lines
2.0 KiB
C++

// Boost.Convert library test and usage example
// Copyright (c) 2009-2014 Vladimir Batov.
// Use, modification and distribution are subject to the Boost Software License,
// Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
#include "./test.hpp"
using std::string;
void
test::sstream_manipulators()
{
try
{
boost::lexical_cast<int>("FF");
BOOST_TEST(!"We should not be here");
}
catch (...)
{
}
boost::cstringstream_converter ccnv; // std::stringstream-based char converter
boost::wstringstream_converter wcnv; // std::stringstream-based wchar_t converter
int const hex_v01 = boost::convert<int>("FF", ccnv(std::hex)).value_or(0);
int const hex_v02 = boost::convert<int>(L"F", wcnv(std::hex)).value_or(0);
int const hex_v03 = boost::convert<int>("FF", ccnv(std::dec)).value_or(-5);
int const hex_v04 = boost::convert<int>(L"F", wcnv(std::dec)).value_or(-6);
BOOST_TEST(hex_v01 == 255); // "FF"
BOOST_TEST(hex_v02 == 15); // L"F"
BOOST_TEST(hex_v03 == -5); // Failed conversion
BOOST_TEST(hex_v04 == -6); // Failed conversion
ccnv(std::noshowbase)(std::nouppercase)(std::oct);
BOOST_TEST(boost::convert<string>(255, ccnv).value() == "377");
BOOST_TEST(boost::convert<string>( 15, ccnv).value() == "17");
ccnv(std::showbase);
BOOST_TEST(boost::convert<string>(255, ccnv).value() == "0377");
BOOST_TEST(boost::convert<string>( 15, ccnv).value() == "017");
ccnv(std::uppercase)(std::hex);
BOOST_TEST(boost::convert<string>(255, ccnv).value() == "0XFF");
BOOST_TEST(boost::convert<string>( 15, ccnv).value() == "0XF");
ccnv(std::noshowbase)(std::nouppercase)(std::oct);
BOOST_TEST(boost::convert<string>(255, ccnv).value() == "377");
BOOST_TEST(boost::convert<string>( 15, ccnv).value() == "17");
ccnv(std::showbase)(arg::uppercase = true)(arg::base = cnv::base::hex);
BOOST_TEST(boost::convert<string>(255, ccnv).value() == "0XFF");
BOOST_TEST(boost::convert<string>( 15, ccnv).value() == "0XF");
}