]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/convert/test/str_to_int.cpp
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.
8 #ifdef BOOST_CONVERT_INTEL_SFINAE_BROKEN
9 int main(int, char const* []) { return 0; }
12 #include <boost/convert.hpp>
13 #include <boost/convert/stream.hpp>
14 #include <boost/convert/printf.hpp>
15 #include <boost/convert/strtol.hpp>
16 #include <boost/convert/lexical_cast.hpp>
17 #include <boost/convert/spirit.hpp>
18 #include <boost/detail/lightweight_test.hpp>
22 template<typename Converter
>
24 int_to_str(Converter
const& cnv
)
26 string
const not_int_str
= "not an int";
27 string
const std_str
= "-11";
28 char const* const c_str
= "-12";
29 char const array_str
[] = "-15";
31 ////////////////////////////////////////////////////////////////////////////
32 // Testing int-to-string conversion with various string
33 // containers as the fallback values.
34 ////////////////////////////////////////////////////////////////////////////
36 string s01
= boost::convert
< string
>(-1, cnv
).value_or(std_str
);
37 string s02
= boost::convert
< string
>(-2, cnv
).value_or(c_str
);
38 string s05
= boost::convert
< string
>(-5, cnv
).value_or(array_str
);
39 boost::optional
< string
> rs01
= boost::convert
< string
>(-1, cnv
);
40 boost::optional
< string
> rs02
= boost::convert
< string
>(-2, cnv
);
41 boost::optional
< string
> rs05
= boost::convert
< string
>(-5, cnv
);
43 BOOST_TEST(s01
== "-1"); BOOST_TEST(rs01
&& rs01
.value() == "-1");
44 BOOST_TEST(s02
== "-2"); BOOST_TEST(rs02
&& rs02
.value() == "-2");
45 BOOST_TEST(s05
== "-5"); BOOST_TEST(rs05
&& rs05
.value() == "-5");
47 string s11
= boost::convert
< string
>(-1, cnv
).value();
48 boost::optional
< string
> rs11
= boost::convert
< string
>(-1, cnv
);
50 BOOST_TEST( s11
== "-1");
51 BOOST_TEST(rs11
&& rs11
.value() == "-1");
54 template<typename Converter
>
56 str_to_int(Converter
const& cnv
)
58 string
const not_int_str
= "not an int";
59 string
const std_str
= "-11";
60 char const* const c_str
= "-123";
61 char const array_str
[] = "3456";
63 ////////////////////////////////////////////////////////////////////////////
64 // Testing various kinds of string containers.
65 // Testing implicit conversion directly to TypeOut (int).
66 // Testing with the fallback value value provided.
67 // On failure returns the provided fallback value and DOES NOT THROW.
68 ////////////////////////////////////////////////////////////////////////////
70 int const a00
= boost::convert
<int>(not_int_str
, cnv
).value_or(-1);
71 int const a01
= boost::convert
<int>(std_str
, cnv
).value_or(-1);
72 int const a02
= boost::convert
<int>(c_str
, cnv
).value_or(-1);
73 int const a05
= boost::convert
<int>(array_str
, cnv
).value_or(-1);
75 BOOST_TEST(a00
== -1); // Failed conversion
76 BOOST_TEST(a01
== -11);
77 BOOST_TEST(a02
== -123);
78 BOOST_TEST(a05
== 3456);
80 ////////////////////////////////////////////////////////////////////////////
82 ////////////////////////////////////////////////////////////////////////////
84 boost::optional
<int> const r00
= boost::convert
<int>(not_int_str
, cnv
);
85 boost::optional
<int> const r01
= boost::convert
<int>(std_str
, cnv
);
86 boost::optional
<int> const r02
= boost::convert
<int>(c_str
, cnv
);
87 boost::optional
<int> const r05
= boost::convert
<int>(array_str
, cnv
);
89 BOOST_TEST(!r00
); // Failed conversion
90 BOOST_TEST( r01
&& r01
.value() == -11);
91 BOOST_TEST( r02
&& r02
.value() == -123);
92 BOOST_TEST( r05
&& r05
.value() == 3456);
94 ////////////////////////////////////////////////////////////////////////////
95 // Testing value() on invalid result.
96 ////////////////////////////////////////////////////////////////////////////
100 boost::convert
<int>(not_int_str
, cnv
).value();
101 BOOST_TEST(!"failed to throw");
103 catch (std::exception
&)
105 // Expected behavior: received 'boost::convert failed' exception. All well.
107 ////////////////////////////////////////////////////////////////////////////
108 // Testing value() on valid result.
109 ////////////////////////////////////////////////////////////////////////////
111 int a21
= boost::convert
<int>(std_str
, cnv
).value();
112 int a22
= boost::convert
<int>(c_str
, cnv
).value();
113 int a25
= boost::convert
<int>(array_str
, cnv
).value();
115 BOOST_TEST(a21
== -11);
116 BOOST_TEST(a22
== -123);
117 BOOST_TEST(a25
== 3456);
119 ////////////////////////////////////////////////////////////////////////////
120 // Testing empty string.
121 ////////////////////////////////////////////////////////////////////////////
123 int const a31
= boost::convert
<int>(std::string(), cnv
).value_or(-1);
124 int const a32
= boost::convert
<int>(std::string(""), cnv
).value_or(-1);
125 int const a33
= boost::convert
<int>("", cnv
).value_or(-1);
127 BOOST_ASSERT(a31
== -1);
128 BOOST_ASSERT(a32
== -1);
129 BOOST_ASSERT(a33
== -1);
133 main(int, char const* [])
135 boost::cnv::lexical_cast lxcast_cnv
;
136 boost::cnv::cstream stream_cnv
;
137 boost::cnv::strtol strtol_cnv
;
138 boost::cnv::printf printf_cnv
;
139 boost::cnv::spirit spirit_cnv
;
141 str_to_int(lxcast_cnv
);
142 str_to_int(stream_cnv
);
143 str_to_int(strtol_cnv
);
144 str_to_int(printf_cnv
);
145 str_to_int(spirit_cnv
);
147 int_to_str(lxcast_cnv
);
148 int_to_str(stream_cnv
);
149 int_to_str(strtol_cnv
);
150 int_to_str(printf_cnv
);
151 int_to_str(spirit_cnv
);
153 return boost::report_errors();