• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Convert test and usage example
2 // Copyright (c) 2009-2016 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_IS_NOT_SUPPORTED)
main(int,char const * [])9 int main(int, char const* []) { return 0; }
10 #else
11 
12 #include <boost/convert.hpp>
13 #include <boost/convert/spirit.hpp>
14 #include <boost/detail/lightweight_test.hpp>
15 #include <cstdio>
16 
17 using std::string;
18 using std::wstring;
19 using boost::convert;
20 
21 namespace cnv = boost::cnv;
22 namespace arg = boost::cnv::parameter;
23 
24 struct boost::cnv::by_default : boost::cnv::spirit {};
25 
26 int
main(int,char const * [])27 main(int, char const* [])
28 {
29     char const* const     c_stri ("12345");
30     char const* const     c_strd ("123.45");
31     wchar_t const* const c_wstri (L"12345");
32     wchar_t const* const c_wstrd (L"123.45");
33     std::string const   std_stri (c_stri);
34     std::string const   std_strd (c_strd);
35     std::wstring const std_wstri (c_wstri);
36     std::wstring const std_wstrd (c_wstrd);
37     my_string const      my_stri (c_stri, c_stri + strlen(c_stri));
38     my_string const      my_strd (c_strd, c_strd + strlen(c_strd));
39 
40     boost::cnv::spirit cnv;
41 
42     BOOST_TEST( 12345 == convert<     int>(   c_stri).value());
43     BOOST_TEST( 12345 == convert<     int>(  c_wstri).value());
44     BOOST_TEST( 12345 == convert<     int>( std_stri).value());
45     BOOST_TEST( 12345 == convert<     int>(std_wstri).value());
46     BOOST_TEST( 12345 == convert<     int>(  my_stri).value());
47     BOOST_TEST( 12345 == convert<unsigned int>(c_stri).value());
48     BOOST_TEST( 12345 == convert<long int>(   c_stri).value());
49     BOOST_TEST( 12345 == convert<long int>(  c_wstri).value());
50     BOOST_TEST( 12345 == convert<long int>( std_stri).value());
51     BOOST_TEST( 12345 == convert<long int>(std_wstri).value());
52     BOOST_TEST( 12345 == convert<long int>(  my_stri).value());
53     BOOST_TEST( 12345 == convert<unsigned long int>(c_stri).value());
54     BOOST_TEST( 12345 == convert<long long int>(c_stri).value());
55     BOOST_TEST( 12345 == convert<unsigned long long int>(c_stri).value());
56     BOOST_TEST(123.45 == convert<  double>(   c_strd).value());
57     BOOST_TEST(123.45 == convert<  double>(  c_wstrd).value());
58 //  BOOST_TEST(123.45 == convert<  double>( std_strd).value());
59 //  BOOST_TEST(123.45 == convert<  double>(std_wstrd).value());
60     BOOST_TEST(123.45 == convert<  double>(  my_strd).value());
61 
62     BOOST_TEST(!convert<   int>("uhm"));
63     BOOST_TEST(!convert<   int>(L"uhm"));
64     BOOST_TEST(!convert<double>("12.uhm"));
65     BOOST_TEST(!convert<double>("L12.uhm"));
66 
67     BOOST_TEST(  "1234" == convert<string>(1234).value());
68     BOOST_TEST(  "1234" == convert<string>(1234u).value());
69     BOOST_TEST(  "1234" == convert<string>(1234ll).value());
70     BOOST_TEST(  "1234" == convert<string>(1234ull).value());
71     BOOST_TEST( L"1234" == convert<wstring>(1234).value());
72     BOOST_TEST( "12xxx" == convert<string>(12, cnv(arg::width = 5)
73                                                   (arg::fill = 'x')
74                                                   (arg::adjust = cnv::adjust::left)).value());
75     BOOST_TEST(L"12xxx" == convert<wstring>(12, cnv(arg::width = 5)
76                                                    (arg::fill = 'x')
77                                                    (arg::adjust = cnv::adjust::left)).value());
78     BOOST_TEST( "x12xx" == convert<string>(12, cnv(arg::adjust = cnv::adjust::center)).value());
79     BOOST_TEST(L"x12xx" == convert<wstring>(12, cnv(arg::adjust = cnv::adjust::center)).value());
80 
81 //    BOOST_TEST("12.34" == convert<std::string>(12.34).value());
82 //    printf("%s\n", convert<std::string>(12.34).value().c_str());
83 
84     return boost::report_errors();
85 }
86 
87 #endif
88