summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/convert/test/spirit_converter.cpp
blob: bbbc5b976e7f658b07a7a34eb614b6ed2170b636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Boost.Convert test and usage example
// Copyright (c) 2009-2016 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"

#if defined(BOOST_CONVERT_IS_NOT_SUPPORTED)
int main(int, char const* []) { return 0; }
#else

#include <boost/convert.hpp>
#include <boost/convert/spirit.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <cstdio>

using std::string;
using std::wstring;
using boost::convert;

namespace cnv = boost::cnv;
namespace arg = boost::cnv::parameter;

struct boost::cnv::by_default : boost::cnv::spirit {};

int
main(int, char const* [])
{
    char const* const     c_stri ("12345");
    char const* const     c_strd ("123.45");
    wchar_t const* const c_wstri (L"12345");
    wchar_t const* const c_wstrd (L"123.45");
    std::string const   std_stri (c_stri);
    std::string const   std_strd (c_strd);
    std::wstring const std_wstri (c_wstri);
    std::wstring const std_wstrd (c_wstrd);
    my_string const      my_stri (c_stri, c_stri + strlen(c_stri));
    my_string const      my_strd (c_strd, c_strd + strlen(c_strd));

    boost::cnv::spirit cnv;

    BOOST_TEST( 12345 == convert<     int>(   c_stri).value());
    BOOST_TEST( 12345 == convert<     int>(  c_wstri).value());
    BOOST_TEST( 12345 == convert<     int>( std_stri).value());
    BOOST_TEST( 12345 == convert<     int>(std_wstri).value());
    BOOST_TEST( 12345 == convert<     int>(  my_stri).value());
    BOOST_TEST( 12345 == convert<unsigned int>(c_stri).value());
    BOOST_TEST( 12345 == convert<long int>(   c_stri).value());
    BOOST_TEST( 12345 == convert<long int>(  c_wstri).value());
    BOOST_TEST( 12345 == convert<long int>( std_stri).value());
    BOOST_TEST( 12345 == convert<long int>(std_wstri).value());
    BOOST_TEST( 12345 == convert<long int>(  my_stri).value());
    BOOST_TEST( 12345 == convert<unsigned long int>(c_stri).value());
    BOOST_TEST( 12345 == convert<long long int>(c_stri).value());
    BOOST_TEST( 12345 == convert<unsigned long long int>(c_stri).value());
    BOOST_TEST(123.45 == convert<  double>(   c_strd).value());
    BOOST_TEST(123.45 == convert<  double>(  c_wstrd).value());
//  BOOST_TEST(123.45 == convert<  double>( std_strd).value());
//  BOOST_TEST(123.45 == convert<  double>(std_wstrd).value());
    BOOST_TEST(123.45 == convert<  double>(  my_strd).value());

    BOOST_TEST(!convert<   int>("uhm"));
    BOOST_TEST(!convert<   int>(L"uhm"));
    BOOST_TEST(!convert<double>("12.uhm"));
    BOOST_TEST(!convert<double>("L12.uhm"));

    BOOST_TEST(  "1234" == convert<string>(1234).value());
    BOOST_TEST(  "1234" == convert<string>(1234u).value());
    BOOST_TEST(  "1234" == convert<string>(1234ll).value());
    BOOST_TEST(  "1234" == convert<string>(1234ull).value());
    BOOST_TEST( L"1234" == convert<wstring>(1234).value());
    BOOST_TEST( "12xxx" == convert<string>(12, cnv(arg::width = 5)
                                                  (arg::fill = 'x')
                                                  (arg::adjust = cnv::adjust::left)).value());
    BOOST_TEST(L"12xxx" == convert<wstring>(12, cnv(arg::width = 5)
                                                   (arg::fill = 'x')
                                                   (arg::adjust = cnv::adjust::left)).value());
    BOOST_TEST( "x12xx" == convert<string>(12, cnv(arg::adjust = cnv::adjust::center)).value());
    BOOST_TEST(L"x12xx" == convert<wstring>(12, cnv(arg::adjust = cnv::adjust::center)).value());

//    BOOST_TEST("12.34" == convert<std::string>(12.34).value());
//    printf("%s\n", convert<std::string>(12.34).value().c_str());

    return boost::report_errors();
}

#endif