// 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; using std::wstring; template void test::type_to_string(Converter const& cnv) { BOOST_TEST("255" == boost::convert(255, cnv(arg::base = cnv::base::dec)).value_or("bad")); BOOST_TEST( "ff" == boost::convert(255, cnv(arg::base = cnv::base::hex)).value_or("bad")); BOOST_TEST("377" == boost::convert(255, cnv(arg::base = cnv::base::oct)).value_or("bad")); } template void test::string_to_type(Converter const& cnv) { BOOST_TEST( 255 == boost::convert("255", cnv(arg::base = cnv::base::dec)).value_or(999)); BOOST_TEST( 999 == boost::convert( "FF", cnv(arg::base = cnv::base::dec)).value_or(999)); BOOST_TEST( 255 == boost::convert( "FF", cnv(arg::base = cnv::base::hex)).value_or(999)); BOOST_TEST( 173 == boost::convert("255", cnv(arg::base = cnv::base::oct)).value_or(999)); BOOST_TEST( 999 != boost::convert("1.23", cnv).value_or(999)); } void test::force_in_type() { boost::cstringstream_converter cnv; string s1 = boost::convert(-1, cnv).value(); string s2 = boost::convert(-1, cnv).value(); char const* expected = sizeof(unsigned int) == 4 ? "4294967295" : 0; if (expected) { BOOST_TEST(s1 == "-1"); BOOST_TEST(s2 == expected); } } int main(int argc, char const* argv[]) { test::sfinae(); test::int_to_string(); test::string_to_int(); test::string_to_bool(); test::string_to_type(boost::strtol_converter()); test::string_to_type(boost::printf_converter()); test::type_to_string(boost::printf_converter()); test::user_type(); test::force_in_type(); test::lcast_converter(); test::sstream_converter(); test::sstream_manipulators(); test::sstream_locale(); test::algorithms(); test::callables(); test::encryption(); test::performance(); return boost::report_errors(); }