]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/convert/test/str_to_int.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / 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.
5
6 #include "./test.hpp"
7
8 #ifdef BOOST_CONVERT_INTEL_SFINAE_BROKEN
9 int main(int, char const* []) { return 0; }
10 #else
11
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>
19
20 using std::string;
21
22 template<typename Converter>
23 void
24 int_to_str(Converter const& cnv)
25 {
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";
30
31 ////////////////////////////////////////////////////////////////////////////
32 // Testing int-to-string conversion with various string
33 // containers as the fallback values.
34 ////////////////////////////////////////////////////////////////////////////
35
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);
42
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");
46
47 string s11 = boost::convert< string>(-1, cnv).value();
48 boost::optional< string> rs11 = boost::convert< string>(-1, cnv);
49
50 BOOST_TEST( s11 == "-1");
51 BOOST_TEST(rs11 && rs11.value() == "-1");
52 }
53
54 template<typename Converter>
55 void
56 str_to_int(Converter const& cnv)
57 {
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";
62
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 ////////////////////////////////////////////////////////////////////////////
69
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);
74
75 BOOST_TEST(a00 == -1); // Failed conversion
76 BOOST_TEST(a01 == -11);
77 BOOST_TEST(a02 == -123);
78 BOOST_TEST(a05 == 3456);
79
80 ////////////////////////////////////////////////////////////////////////////
81 // Testing "optional"
82 ////////////////////////////////////////////////////////////////////////////
83
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);
88
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);
93
94 ////////////////////////////////////////////////////////////////////////////
95 // Testing value() on invalid result.
96 ////////////////////////////////////////////////////////////////////////////
97
98 try
99 {
100 boost::convert<int>(not_int_str, cnv).value();
101 BOOST_TEST(!"failed to throw");
102 }
103 catch (std::exception&)
104 {
105 // Expected behavior: received 'boost::convert failed' exception. All well.
106 }
107 ////////////////////////////////////////////////////////////////////////////
108 // Testing value() on valid result.
109 ////////////////////////////////////////////////////////////////////////////
110
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();
114
115 BOOST_TEST(a21 == -11);
116 BOOST_TEST(a22 == -123);
117 BOOST_TEST(a25 == 3456);
118
119 ////////////////////////////////////////////////////////////////////////////
120 // Testing empty string.
121 ////////////////////////////////////////////////////////////////////////////
122
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);
126
127 BOOST_ASSERT(a31 == -1);
128 BOOST_ASSERT(a32 == -1);
129 BOOST_ASSERT(a33 == -1);
130 }
131
132 int
133 main(int, char const* [])
134 {
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;
140
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);
146
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);
152
153 return boost::report_errors();
154 }
155
156 #endif