1 | // Boost.Convert test and usage example |
2 | // Copyright (c) 2009-2020 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_CXX14) |
9 | int main(int, char const* []) { return 0; } |
10 | #else |
11 | |
12 | #include <boost/convert.hpp> |
13 | #include <boost/convert/printf.hpp> |
14 | #include <boost/test/tools/floating_point_comparison.hpp> |
15 | |
16 | using std::string; |
17 | using boost::convert; |
18 | |
19 | namespace cnv = boost::cnv; |
20 | namespace arg = boost::cnv::parameter; |
21 | |
22 | static |
23 | void |
24 | test_notation() |
25 | { |
26 | //[printf_notation |
27 | boost::cnv::printf cnv; |
28 | |
29 | BOOST_TEST( "-3.14159" == convert<string>(-3.14159, cnv(arg::notation = cnv::notation::fixed)(arg::precision = 5)).value()); |
30 | BOOST_TEST( "-3.142e+00" == convert<string>(-3.14159, cnv(arg::notation = cnv::notation::scientific)(arg::precision = 3)).value()); |
31 | BOOST_TEST("-0x1.9220p+1" == convert<string>(-3.14159, cnv(arg::notation = cnv::notation::hex)(arg::precision = 4)).value()); |
32 | |
33 | const auto close = boost::math::fpc::close_at_tolerance<double>(1); |
34 | |
35 | BOOST_TEST_WITH(-3.14159, convert<double>("-3.14159" , cnv(arg::notation = cnv::notation::fixed)).value(), close); |
36 | BOOST_TEST_WITH(-3.14159, convert<double>("-3.142e+00" , cnv(arg::notation = cnv::notation::scientific)).value(), close); |
37 | BOOST_TEST_WITH(-3.14159, convert<double>("-0x1.9220p+1" , cnv(arg::notation = cnv::notation::hex)).value(), close); |
38 | //] |
39 | } |
40 | |
41 | int |
42 | main(int, char const* []) |
43 | { |
44 | boost::cnv::printf cnv; |
45 | |
46 | string const not_int_str = "not an int" ; |
47 | string const std_str = "-11" ; |
48 | char const* const c_str = "-12" ; |
49 | |
50 | BOOST_TEST( -1 == convert<int>(not_int_str, cnv).value_or(-1)); |
51 | BOOST_TEST(-11 == convert<int>(std_str, cnv).value_or(-1)); |
52 | BOOST_TEST(-12 == convert<int>(c_str, cnv).value_or(-1)); |
53 | |
54 | BOOST_TEST( float(3.14) == convert< float>("3.14" , cnv(arg::precision = 2)).value_or(-1)); |
55 | BOOST_TEST(double(3.14) == convert<double>("3.14" , cnv(arg::precision = 2)).value_or(-1)); |
56 | |
57 | BOOST_TEST("255" == convert<std::string>(255, cnv(arg::base = boost::cnv::base::dec)).value()); |
58 | BOOST_TEST( "ff" == convert<std::string>(255, cnv(arg::base = boost::cnv::base::hex)).value()); |
59 | BOOST_TEST("377" == convert<std::string>(255, cnv(arg::base = boost::cnv::base::oct)).value()); |
60 | |
61 | string s01 = convert<string>(12.3456, cnv(arg::precision = 6)).value(); |
62 | string s02 = convert<string>(12.3456, cnv(arg::precision = 3)).value(); |
63 | |
64 | BOOST_TEST(s01 == "12.345600" ); |
65 | BOOST_TEST(s02 == "12.346" ); |
66 | |
67 | test_notation(); |
68 | |
69 | return boost::report_errors(); |
70 | } |
71 | |
72 | #endif |
73 | |