| 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/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 | |
| 19 | using std::string; |
| 20 | |
| 21 | template<typename Converter> |
| 22 | void |
| 23 | int_to_str(Converter const& 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 | char const array_str[] = "-15" ; |
| 29 | |
| 30 | //////////////////////////////////////////////////////////////////////////// |
| 31 | // Testing int-to-string conversion with various string |
| 32 | // containers as the fallback values. |
| 33 | //////////////////////////////////////////////////////////////////////////// |
| 34 | |
| 35 | string s01 = boost::convert<string>(-1, cnv).value_or(std_str); |
| 36 | string s02 = boost::convert<string>(-2, cnv).value_or(c_str); |
| 37 | string s05 = boost::convert<string>(-5, cnv).value_or(array_str); |
| 38 | boost::optional<string> rs01 = boost::convert<string>(-1, cnv); |
| 39 | boost::optional<string> rs02 = boost::convert<string>(-2, cnv); |
| 40 | boost::optional<string> rs05 = boost::convert<string>(-5, cnv); |
| 41 | |
| 42 | BOOST_TEST(s01 == "-1" ); BOOST_TEST(rs01 && rs01.value() == "-1" ); |
| 43 | BOOST_TEST(s02 == "-2" ); BOOST_TEST(rs02 && rs02.value() == "-2" ); |
| 44 | BOOST_TEST(s05 == "-5" ); BOOST_TEST(rs05 && rs05.value() == "-5" ); |
| 45 | |
| 46 | string s11 = boost::convert<string>(-1, cnv).value(); |
| 47 | boost::optional< string> rs11 = boost::convert<string>(-1, cnv); |
| 48 | |
| 49 | BOOST_TEST( s11 == "-1" ); |
| 50 | BOOST_TEST(rs11 && rs11.value() == "-1" ); |
| 51 | } |
| 52 | |
| 53 | template<typename Converter> |
| 54 | void |
| 55 | str_to_int(Converter const& cnv) |
| 56 | { |
| 57 | string const not_int_str = "not an int" ; |
| 58 | string const std_str = "-11" ; |
| 59 | char const* const c_str = "-123" ; |
| 60 | char const array_str[] = "3456" ; |
| 61 | |
| 62 | //////////////////////////////////////////////////////////////////////////// |
| 63 | // Testing various kinds of string containers. |
| 64 | // Testing implicit conversion directly to TypeOut (int). |
| 65 | // Testing with the fallback value value provided. |
| 66 | // On failure returns the provided fallback value and DOES NOT THROW. |
| 67 | //////////////////////////////////////////////////////////////////////////// |
| 68 | |
| 69 | int const a00 = boost::convert<int>(not_int_str, cnv).value_or(-1); |
| 70 | int const a01 = boost::convert<int>(std_str, cnv).value_or(-1); |
| 71 | int const a02 = boost::convert<int>(c_str, cnv).value_or(-1); |
| 72 | int const a05 = boost::convert<int>(array_str, cnv).value_or(-1); |
| 73 | |
| 74 | BOOST_TEST(a00 == -1); // Failed conversion |
| 75 | BOOST_TEST(a01 == -11); |
| 76 | BOOST_TEST(a02 == -123); |
| 77 | BOOST_TEST(a05 == 3456); |
| 78 | |
| 79 | //////////////////////////////////////////////////////////////////////////// |
| 80 | // Testing "optional" |
| 81 | //////////////////////////////////////////////////////////////////////////// |
| 82 | |
| 83 | boost::optional<int> const r00 = boost::convert<int>(not_int_str, cnv); |
| 84 | boost::optional<int> const r01 = boost::convert<int>(std_str, cnv); |
| 85 | boost::optional<int> const r02 = boost::convert<int>(c_str, cnv); |
| 86 | boost::optional<int> const r05 = boost::convert<int>(array_str, cnv); |
| 87 | |
| 88 | BOOST_TEST(!r00); // Failed conversion |
| 89 | BOOST_TEST( r01 && r01.value() == -11); |
| 90 | BOOST_TEST( r02 && r02.value() == -123); |
| 91 | BOOST_TEST( r05 && r05.value() == 3456); |
| 92 | |
| 93 | //////////////////////////////////////////////////////////////////////////// |
| 94 | // Testing value() on invalid result. |
| 95 | //////////////////////////////////////////////////////////////////////////// |
| 96 | |
| 97 | try |
| 98 | { |
| 99 | boost::convert<int>(not_int_str, cnv).value(); |
| 100 | BOOST_TEST(!"failed to throw" ); |
| 101 | } |
| 102 | catch (std::exception&) |
| 103 | { |
| 104 | // Expected behavior: received 'boost::convert failed' exception. All well. |
| 105 | } |
| 106 | //////////////////////////////////////////////////////////////////////////// |
| 107 | // Testing value() on valid result. |
| 108 | //////////////////////////////////////////////////////////////////////////// |
| 109 | |
| 110 | int a21 = boost::convert<int>(std_str, cnv).value(); |
| 111 | int a22 = boost::convert<int>(c_str, cnv).value(); |
| 112 | int a25 = boost::convert<int>(array_str, cnv).value(); |
| 113 | |
| 114 | BOOST_TEST(a21 == -11); |
| 115 | BOOST_TEST(a22 == -123); |
| 116 | BOOST_TEST(a25 == 3456); |
| 117 | |
| 118 | //////////////////////////////////////////////////////////////////////////// |
| 119 | // Testing empty string. |
| 120 | //////////////////////////////////////////////////////////////////////////// |
| 121 | |
| 122 | int a31 = boost::convert<int>(std::string(), cnv).value_or(-1); |
| 123 | int a32 = boost::convert<int>(std::string("" ), cnv).value_or(-1); |
| 124 | int a33 = boost::convert<int>("" , cnv).value_or(-1); |
| 125 | |
| 126 | BOOST_ASSERT(a31 == -1); |
| 127 | BOOST_ASSERT(a32 == -1); |
| 128 | BOOST_ASSERT(a33 == -1); |
| 129 | } |
| 130 | |
| 131 | int |
| 132 | main(int, char const* []) |
| 133 | { |
| 134 | auto lxcast_cnv = boost::cnv::lexical_cast(); |
| 135 | auto stream_cnv = boost::cnv::cstream(); |
| 136 | auto strtol_cnv = boost::cnv::strtol(); |
| 137 | auto printf_cnv = boost::cnv::printf(); |
| 138 | auto spirit_cnv = boost::cnv::spirit(); |
| 139 | |
| 140 | str_to_int(lxcast_cnv); |
| 141 | str_to_int(stream_cnv); |
| 142 | str_to_int(strtol_cnv); |
| 143 | str_to_int(printf_cnv); |
| 144 | str_to_int(spirit_cnv); |
| 145 | |
| 146 | int_to_str(lxcast_cnv); |
| 147 | int_to_str(stream_cnv); |
| 148 | int_to_str(strtol_cnv); |
| 149 | int_to_str(printf_cnv); |
| 150 | int_to_str(spirit_cnv); |
| 151 | |
| 152 | return boost::report_errors(); |
| 153 | } |
| 154 | |
| 155 | #endif |
| 156 | |