lexical_cast_pointers_test.cpp 2.7 KB

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