• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-2020.
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/config.hpp>
12 
13 #if defined(__INTEL_COMPILER)
14 #pragma warning(disable: 193 383 488 981 1418 1419)
15 #elif defined(BOOST_MSVC)
16 #pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800)
17 #endif
18 
19 #include <boost/lexical_cast.hpp>
20 #include <boost/test/unit_test.hpp>
21 
22 using namespace boost;
23 
24 #if defined(BOOST_NO_STRINGSTREAM)
25             typedef std::strstream ss_t;
26 #else
27             typedef std::stringstream ss_t;
28 #endif
29 
test_void_pointers_conversions()30 void test_void_pointers_conversions()
31 {
32     void *p_to_null = NULL;
33     const void *cp_to_data = "Some data";
34     char nonconst_data[5];
35     void *p_to_data = nonconst_data;
36     ss_t ss;
37 
38     ss << p_to_null;
39     BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(p_to_null), ss.str());
40     ss.str(std::string());
41 
42     ss << cp_to_data;
43     BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(cp_to_data), ss.str());
44     ss.str(std::string());
45 
46     ss << p_to_data;
47     BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(p_to_data), ss.str());
48     ss.str(std::string());
49 }
50 
51 struct incomplete_type;
52 
test_incomplete_type_pointers_conversions()53 void test_incomplete_type_pointers_conversions()
54 {
55     incomplete_type *p_to_null = NULL;
56     const incomplete_type *cp_to_data = NULL;
57     char nonconst_data[5];
58     incomplete_type *p_to_data = reinterpret_cast<incomplete_type*>(nonconst_data);
59     ss_t ss;
60 
61     ss << p_to_null;
62     BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(p_to_null), ss.str());
63     ss.str(std::string());
64 
65     ss << cp_to_data;
66     BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(cp_to_data), ss.str());
67     ss.str(std::string());
68 
69     ss << p_to_data;
70     BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(p_to_data), ss.str());
71     ss.str(std::string());
72 }
73 
74 struct ble;
75 typedef struct ble *meh;
operator <<(std::ostream & o,meh)76 std::ostream& operator <<(std::ostream &o, meh) {
77   o << "yay";
78   return o;
79 }
80 
test_inomplete_type_with_overloaded_ostream_op()81 void test_inomplete_type_with_overloaded_ostream_op() {
82     meh heh = NULL;
83     ss_t ss;
84     ss << heh;
85     BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(heh), ss.str());
86 }
87 
init_unit_test_suite(int,char * [])88 unit_test::test_suite *init_unit_test_suite(int, char *[])
89 {
90     unit_test::test_suite *suite =
91         BOOST_TEST_SUITE("lexical_cast pinters test");
92     suite->add(BOOST_TEST_CASE(&test_void_pointers_conversions));
93     suite->add(BOOST_TEST_CASE(&test_incomplete_type_pointers_conversions));
94     suite->add(BOOST_TEST_CASE(&test_inomplete_type_with_overloaded_ostream_op));
95     return suite;
96 }
97