address.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // address.cpp
  3. // ~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Disable autolinking for unit tests.
  11. #if !defined(BOOST_ALL_NO_LIB)
  12. #define BOOST_ALL_NO_LIB 1
  13. #endif // !defined(BOOST_ALL_NO_LIB)
  14. // Test that header file is self-contained.
  15. #include <boost/asio/ip/address.hpp>
  16. #include "../unit_test.hpp"
  17. #include <sstream>
  18. //------------------------------------------------------------------------------
  19. // ip_address_compile test
  20. // ~~~~~~~~~~~~~~~~~~~~~~~
  21. // The following test checks that all public member functions on the class
  22. // ip::address compile and link correctly. Runtime failures are ignored.
  23. namespace ip_address_compile {
  24. void test()
  25. {
  26. using namespace boost::asio;
  27. namespace ip = boost::asio::ip;
  28. try
  29. {
  30. boost::system::error_code ec;
  31. // address constructors.
  32. ip::address addr1;
  33. const ip::address_v4 const_addr_v4;
  34. ip::address addr2(const_addr_v4);
  35. const ip::address_v6 const_addr_v6;
  36. ip::address addr3(const_addr_v6);
  37. // address functions.
  38. bool b = addr1.is_v4();
  39. (void)b;
  40. b = addr1.is_v6();
  41. (void)b;
  42. b = addr1.is_loopback();
  43. (void)b;
  44. b = addr1.is_unspecified();
  45. (void)b;
  46. b = addr1.is_multicast();
  47. (void)b;
  48. ip::address_v4 addr_v4_value = addr1.to_v4();
  49. (void)addr_v4_value;
  50. ip::address_v6 addr_v6_value = addr1.to_v6();
  51. (void)addr_v6_value;
  52. std::string string_value = addr1.to_string();
  53. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  54. string_value = addr1.to_string(ec);
  55. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  56. // address static functions.
  57. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  58. addr1 = ip::address::from_string("127.0.0.1");
  59. addr1 = ip::address::from_string("127.0.0.1", ec);
  60. addr1 = ip::address::from_string(string_value);
  61. addr1 = ip::address::from_string(string_value, ec);
  62. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  63. // address comparisons.
  64. b = (addr1 == addr2);
  65. (void)b;
  66. b = (addr1 != addr2);
  67. (void)b;
  68. b = (addr1 < addr2);
  69. (void)b;
  70. b = (addr1 > addr2);
  71. (void)b;
  72. b = (addr1 <= addr2);
  73. (void)b;
  74. b = (addr1 >= addr2);
  75. (void)b;
  76. // address creation functions.
  77. addr1 = ip::make_address("127.0.0.1");
  78. addr1 = ip::make_address("127.0.0.1", ec);
  79. addr1 = ip::make_address(string_value);
  80. addr1 = ip::make_address(string_value, ec);
  81. #if defined(BOOST_ASIO_HAS_STRING_VIEW)
  82. # if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
  83. std::string_view string_view_value("127.0.0.1");
  84. # elif defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
  85. std::experimental::string_view string_view_value("127.0.0.1");
  86. # endif // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
  87. addr1 = ip::make_address(string_view_value);
  88. addr1 = ip::make_address(string_view_value, ec);
  89. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  90. // address I/O.
  91. std::ostringstream os;
  92. os << addr1;
  93. #if !defined(BOOST_NO_STD_WSTREAMBUF)
  94. std::wostringstream wos;
  95. wos << addr1;
  96. #endif // !defined(BOOST_NO_STD_WSTREAMBUF)
  97. }
  98. catch (std::exception&)
  99. {
  100. }
  101. }
  102. } // namespace ip_address_compile
  103. //------------------------------------------------------------------------------
  104. BOOST_ASIO_TEST_SUITE
  105. (
  106. "ip/address",
  107. BOOST_ASIO_TEST_CASE(ip_address_compile::test)
  108. )