| 1 | // Unit test for boost::lexical_cast. |
| 2 | // |
| 3 | // See http://www.boost.org for most recent version, including documentation. |
| 4 | // |
| 5 | // Copyright Antony Polukhin, 2012-2024. |
| 6 | // |
| 7 | // Distributed under the Boost |
| 8 | // Software License, Version 1.0. (See accompanying file |
| 9 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). |
| 10 | |
| 11 | #include <boost/lexical_cast.hpp> |
| 12 | |
| 13 | #include <boost/core/lightweight_test.hpp> |
| 14 | |
| 15 | #if defined(BOOST_NO_STRINGSTREAM) |
| 16 | typedef std::strstream ss_t; |
| 17 | #else |
| 18 | typedef std::stringstream ss_t; |
| 19 | #endif |
| 20 | |
| 21 | void test_void_pointers_conversions() |
| 22 | { |
| 23 | void *p_to_null = NULL; |
| 24 | const void *cp_to_data = "Some data" ; |
| 25 | char nonconst_data[5]; |
| 26 | void *p_to_data = nonconst_data; |
| 27 | ss_t ss; |
| 28 | |
| 29 | ss << p_to_null; |
| 30 | BOOST_TEST_EQ(boost::lexical_cast<std::string>(p_to_null), ss.str()); |
| 31 | ss.str(s: std::string()); |
| 32 | |
| 33 | ss << cp_to_data; |
| 34 | BOOST_TEST_EQ(boost::lexical_cast<std::string>(cp_to_data), ss.str()); |
| 35 | ss.str(s: std::string()); |
| 36 | |
| 37 | ss << p_to_data; |
| 38 | BOOST_TEST_EQ(boost::lexical_cast<std::string>(p_to_data), ss.str()); |
| 39 | ss.str(s: std::string()); |
| 40 | } |
| 41 | |
| 42 | struct incomplete_type; |
| 43 | |
| 44 | void test_incomplete_type_pointers_conversions() |
| 45 | { |
| 46 | incomplete_type *p_to_null = NULL; |
| 47 | const incomplete_type *cp_to_data = NULL; |
| 48 | char nonconst_data[5]; |
| 49 | incomplete_type *p_to_data = reinterpret_cast<incomplete_type*>(nonconst_data); |
| 50 | ss_t ss; |
| 51 | |
| 52 | ss << p_to_null; |
| 53 | BOOST_TEST_EQ(boost::lexical_cast<std::string>(p_to_null), ss.str()); |
| 54 | ss.str(s: std::string()); |
| 55 | |
| 56 | ss << cp_to_data; |
| 57 | BOOST_TEST_EQ(boost::lexical_cast<std::string>(cp_to_data), ss.str()); |
| 58 | ss.str(s: std::string()); |
| 59 | |
| 60 | ss << p_to_data; |
| 61 | BOOST_TEST_EQ(boost::lexical_cast<std::string>(p_to_data), ss.str()); |
| 62 | ss.str(s: std::string()); |
| 63 | } |
| 64 | |
| 65 | struct ble; |
| 66 | typedef struct ble *meh; |
| 67 | std::ostream& operator <<(std::ostream &o, meh) { |
| 68 | o << "yay" ; |
| 69 | return o; |
| 70 | } |
| 71 | |
| 72 | void test_inomplete_type_with_overloaded_ostream_op() { |
| 73 | meh heh = NULL; |
| 74 | ss_t ss; |
| 75 | ss << heh; |
| 76 | BOOST_TEST_EQ(boost::lexical_cast<std::string>(heh), ss.str()); |
| 77 | } |
| 78 | |
| 79 | int main() |
| 80 | { |
| 81 | test_void_pointers_conversions(); |
| 82 | test_incomplete_type_pointers_conversions(); |
| 83 | test_inomplete_type_with_overloaded_ostream_op(); |
| 84 | |
| 85 | return boost::report_errors(); |
| 86 | } |
| 87 | |