// 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("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("FF", ccnv(std::hex)).value_or(0); int const hex_v02 = boost::convert(L"F", wcnv(std::hex)).value_or(0); int const hex_v03 = boost::convert("FF", ccnv(std::dec)).value_or(-5); int const hex_v04 = boost::convert(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(255, ccnv).value() == "377"); BOOST_TEST(boost::convert( 15, ccnv).value() == "17"); ccnv(std::showbase); BOOST_TEST(boost::convert(255, ccnv).value() == "0377"); BOOST_TEST(boost::convert( 15, ccnv).value() == "017"); ccnv(std::uppercase)(std::hex); BOOST_TEST(boost::convert(255, ccnv).value() == "0XFF"); BOOST_TEST(boost::convert( 15, ccnv).value() == "0XF"); ccnv(std::noshowbase)(std::nouppercase)(std::oct); BOOST_TEST(boost::convert(255, ccnv).value() == "377"); BOOST_TEST(boost::convert( 15, ccnv).value() == "17"); ccnv(std::showbase)(arg::uppercase = true)(arg::base = cnv::base::hex); BOOST_TEST(boost::convert(255, ccnv).value() == "0XFF"); BOOST_TEST(boost::convert( 15, ccnv).value() == "0XF"); }