• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // address.cpp
3 // ~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 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 
11 // Disable autolinking for unit tests.
12 #if !defined(BOOST_ALL_NO_LIB)
13 #define BOOST_ALL_NO_LIB 1
14 #endif // !defined(BOOST_ALL_NO_LIB)
15 
16 // Test that header file is self-contained.
17 #include <boost/asio/ip/address.hpp>
18 
19 #include "../unit_test.hpp"
20 #include <sstream>
21 
22 //------------------------------------------------------------------------------
23 
24 // ip_address_compile test
25 // ~~~~~~~~~~~~~~~~~~~~~~~
26 // The following test checks that all public member functions on the class
27 // ip::address compile and link correctly. Runtime failures are ignored.
28 
29 namespace ip_address_compile {
30 
test()31 void test()
32 {
33   using namespace boost::asio;
34   namespace ip = boost::asio::ip;
35 
36   try
37   {
38     boost::system::error_code ec;
39 
40     // address constructors.
41 
42     ip::address addr1;
43     const ip::address_v4 const_addr_v4;
44     ip::address addr2(const_addr_v4);
45     const ip::address_v6 const_addr_v6;
46     ip::address addr3(const_addr_v6);
47 
48     // address functions.
49 
50     bool b = addr1.is_v4();
51     (void)b;
52 
53     b = addr1.is_v6();
54     (void)b;
55 
56     b = addr1.is_loopback();
57     (void)b;
58 
59     b = addr1.is_unspecified();
60     (void)b;
61 
62     b = addr1.is_multicast();
63     (void)b;
64 
65     ip::address_v4 addr_v4_value = addr1.to_v4();
66     (void)addr_v4_value;
67 
68     ip::address_v6 addr_v6_value = addr1.to_v6();
69     (void)addr_v6_value;
70 
71     std::string string_value = addr1.to_string();
72 #if !defined(BOOST_ASIO_NO_DEPRECATED)
73     string_value = addr1.to_string(ec);
74 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
75 
76     // address static functions.
77 
78 #if !defined(BOOST_ASIO_NO_DEPRECATED)
79     addr1 = ip::address::from_string("127.0.0.1");
80     addr1 = ip::address::from_string("127.0.0.1", ec);
81     addr1 = ip::address::from_string(string_value);
82     addr1 = ip::address::from_string(string_value, ec);
83 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
84 
85     // address comparisons.
86 
87     b = (addr1 == addr2);
88     (void)b;
89 
90     b = (addr1 != addr2);
91     (void)b;
92 
93     b = (addr1 < addr2);
94     (void)b;
95 
96     b = (addr1 > addr2);
97     (void)b;
98 
99     b = (addr1 <= addr2);
100     (void)b;
101 
102     b = (addr1 >= addr2);
103     (void)b;
104 
105     // address creation functions.
106 
107     addr1 = ip::make_address("127.0.0.1");
108     addr1 = ip::make_address("127.0.0.1", ec);
109     addr1 = ip::make_address(string_value);
110     addr1 = ip::make_address(string_value, ec);
111 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
112 # if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
113     std::string_view string_view_value("127.0.0.1");
114 # elif defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
115     std::experimental::string_view string_view_value("127.0.0.1");
116 # endif // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
117     addr1 = ip::make_address(string_view_value);
118     addr1 = ip::make_address(string_view_value, ec);
119 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
120 
121     // address I/O.
122 
123     std::ostringstream os;
124     os << addr1;
125 
126 #if !defined(BOOST_NO_STD_WSTREAMBUF)
127     std::wostringstream wos;
128     wos << addr1;
129 #endif // !defined(BOOST_NO_STD_WSTREAMBUF)
130 
131 #if defined(BOOST_ASIO_HAS_STD_HASH)
132     std::size_t hash1 = std::hash<ip::address>()(addr1);
133     (void)hash1;
134 #endif // defined(BOOST_ASIO_HAS_STD_HASH)
135   }
136   catch (std::exception&)
137   {
138   }
139 }
140 
141 } // namespace ip_address_compile
142 
143 //------------------------------------------------------------------------------
144 
145 BOOST_ASIO_TEST_SUITE
146 (
147   "ip/address",
148   BOOST_ASIO_TEST_CASE(ip_address_compile::test)
149 )
150