]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Boost.Convert test and usage example |
20effc67 | 2 | // Copyright (c) 2009-2020 Vladimir Batov. |
7c673cae FG |
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 | ||
b32b8144 | 8 | #if defined(BOOST_CONVERT_IS_NOT_SUPPORTED) |
7c673cae FG |
9 | int main(int, char const* []) { return 0; } |
10 | #else | |
11 | ||
12 | #include <boost/convert.hpp> | |
13 | #include <boost/convert/printf.hpp> | |
7c673cae FG |
14 | |
15 | using std::string; | |
16 | using boost::convert; | |
17 | ||
18 | namespace arg = boost::cnv::parameter; | |
19 | ||
20 | int | |
21 | main(int, char const* []) | |
22 | { | |
23 | boost::cnv::printf cnv; | |
24 | ||
25 | string const not_int_str = "not an int"; | |
26 | string const std_str = "-11"; | |
27 | char const* const c_str = "-12"; | |
28 | ||
29 | BOOST_TEST( -1 == convert<int>(not_int_str, cnv).value_or(-1)); | |
30 | BOOST_TEST(-11 == convert<int>(std_str, cnv).value_or(-1)); | |
31 | BOOST_TEST(-12 == convert<int>(c_str, cnv).value_or(-1)); | |
32 | ||
33 | BOOST_TEST("255" == convert<std::string>(255, cnv(arg::base = boost::cnv::base::dec)).value()); | |
34 | BOOST_TEST( "ff" == convert<std::string>(255, cnv(arg::base = boost::cnv::base::hex)).value()); | |
35 | BOOST_TEST("377" == convert<std::string>(255, cnv(arg::base = boost::cnv::base::oct)).value()); | |
36 | ||
37 | string s01 = convert<string>(12.3456, cnv(arg::precision = 6)).value(); | |
38 | string s02 = convert<string>(12.3456, cnv(arg::precision = 3)).value(); | |
39 | ||
40 | BOOST_TEST(s01 == "12.345600"); | |
41 | BOOST_TEST(s02 == "12.346"); | |
42 | ||
43 | return boost::report_errors(); | |
44 | } | |
45 | ||
46 | #endif |