1// Boost.Convert test and usage example
2// Copyright (c) 2009-2020 Vladimir Batov.
3// Use, modification and distribution are subject to the Boost Software License,
4// Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
5
6#include "./test.hpp"
7
8#if !defined(BOOST_CONVERT_CXX14)
9int main(int, char const* []) { return 0; }
10#else
11
12#include <boost/convert.hpp>
13#include <boost/convert/spirit.hpp>
14
15using std::string;
16using std::wstring;
17using boost::convert;
18
19namespace cnv = boost::cnv;
20namespace arg = boost::cnv::parameter;
21
22struct boost::cnv::by_default : boost::cnv::spirit {};
23
24int
25main(int, char const* [])
26{
27 char const* const c_stri ("12345");
28 char const* const c_strd ("123.45");
29 wchar_t const* const c_wstri (L"12345");
30 wchar_t const* const c_wstrd (L"123.45");
31 std::string const std_stri (c_stri);
32 std::string const std_strd (c_strd);
33 std::wstring const std_wstri (c_wstri);
34 std::wstring const std_wstrd (c_wstrd);
35 my_string const my_stri (c_stri, c_stri + strlen(c_stri));
36 my_string const my_strd (c_strd, c_strd + strlen(c_strd));
37
38 boost::cnv::spirit cnv;
39
40 BOOST_TEST( 12345 == convert< int>( c_stri).value());
41 BOOST_TEST( 12345 == convert< int>( c_wstri).value());
42 BOOST_TEST( 12345 == convert< int>( std_stri).value());
43 BOOST_TEST( 12345 == convert< int>(std_wstri).value());
44 BOOST_TEST( 12345 == convert< int>( my_stri).value());
45 BOOST_TEST( 12345 == convert<unsigned int>(c_stri).value());
46 BOOST_TEST( 12345 == convert<long int>( c_stri).value());
47 BOOST_TEST( 12345 == convert<long int>( c_wstri).value());
48 BOOST_TEST( 12345 == convert<long int>( std_stri).value());
49 BOOST_TEST( 12345 == convert<long int>(std_wstri).value());
50 BOOST_TEST( 12345 == convert<long int>( my_stri).value());
51 BOOST_TEST( 12345 == convert<unsigned long int>(c_stri).value());
52 BOOST_TEST( 12345 == convert<long long int>(c_stri).value());
53 BOOST_TEST( 12345 == convert<unsigned long long int>(c_stri).value());
54 BOOST_TEST(123.45 == convert< double>( c_strd).value());
55 BOOST_TEST(123.45 == convert< double>( c_wstrd).value());
56// BOOST_TEST(123.45 == convert< double>( std_strd).value());
57// BOOST_TEST(123.45 == convert< double>(std_wstrd).value());
58 BOOST_TEST(123.45 == convert< double>( my_strd).value());
59
60 BOOST_TEST(!convert< int>("uhm"));
61 BOOST_TEST(!convert< int>(L"uhm"));
62 BOOST_TEST(!convert<double>("12.uhm"));
63 BOOST_TEST(!convert<double>("L12.uhm"));
64
65 BOOST_TEST( "1234" == convert<string>(1234).value());
66 BOOST_TEST( "1234" == convert<string>(1234u).value());
67 BOOST_TEST( "1234" == convert<string>(1234ll).value());
68 BOOST_TEST( "1234" == convert<string>(1234ull).value());
69 BOOST_TEST( L"1234" == convert<wstring>(1234).value());
70 BOOST_TEST( "12xxx" == convert<string>(12, cnv(arg::width = 5)
71 (arg::fill = 'x')
72 (arg::adjust = cnv::adjust::left)).value());
73 BOOST_TEST(L"12xxx" == convert<wstring>(12, cnv(arg::width = 5)
74 (arg::fill = 'x')
75 (arg::adjust = cnv::adjust::left)).value());
76 BOOST_TEST( "x12xx" == convert<string>(12, cnv(arg::adjust = cnv::adjust::center)).value());
77 BOOST_TEST(L"x12xx" == convert<wstring>(12, cnv(arg::adjust = cnv::adjust::center)).value());
78
79// BOOST_TEST("12.34" == convert<std::string>(12.34).value());
80// printf("%s\n", convert<std::string>(12.34).value().c_str());
81
82 return boost::report_errors();
83}
84
85#endif
86

source code of boost/libs/convert/test/spirit_converter.cpp