| 1 |
// Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++14 } } |
| 19 |
// { dg-require-effective-target net_ts_ip } |
| 20 |
// { dg-add-options net_ts } |
| 21 |
|
| 22 |
#include <experimental/internet> |
| 23 |
#include <testsuite_hooks.h> |
| 24 |
|
| 25 |
namespace net = std::experimental::net; |
| 26 |
using net::ip::address_v4; |
| 27 |
|
| 28 |
#if __cplusplus < 202002L |
| 29 |
// Naughty, but operator== for std::array is not constexpr until C++20. |
| 30 |
constexpr bool |
| 31 |
operator==(const address_v4::bytes_type& lhs, const address_v4::bytes_type& rhs) |
| 32 |
{ |
| 33 |
return lhs[0] == rhs[0] && lhs[1] == rhs[1] |
| 34 |
&& lhs[2] == rhs[2] && lhs[3] == rhs[3]; |
| 35 |
} |
| 36 |
#endif |
| 37 |
|
| 38 |
constexpr void |
| 39 |
test01() |
| 40 |
{ |
| 41 |
auto a0 = make_address_v4( address_v4::bytes_type{} ); |
| 42 |
VERIFY( a0.to_uint() == 0 ); |
| 43 |
VERIFY( a0.to_bytes() == address_v4::bytes_type{} ); |
| 44 |
|
| 45 |
address_v4::bytes_type b1{ 1, 2, 3, 4 }; |
| 46 |
auto a1 = make_address_v4( b1 ); |
| 47 |
VERIFY( a1.to_uint() == ((1 << 24) | (2 << 16) | (3 << 8) | 4) ); |
| 48 |
VERIFY( a1.to_bytes() == b1 ); |
| 49 |
} |
| 50 |
|
| 51 |
constexpr void |
| 52 |
test02() |
| 53 |
{ |
| 54 |
auto a0 = net::ip::make_address_v4(0u); |
| 55 |
VERIFY( a0.to_uint() == 0 ); |
| 56 |
VERIFY( a0.to_bytes() == address_v4::bytes_type{} ); |
| 57 |
|
| 58 |
address_v4::uint_type u1 = ((5 << 24) | (6 << 16) | (7 << 8) | 8); |
| 59 |
auto a1 = net::ip::make_address_v4( u1 ); |
| 60 |
VERIFY( a1.to_uint() == u1 ); |
| 61 |
VERIFY( a1.to_bytes() == address_v4::bytes_type( 5, 6, 7, 8 ) ); |
| 62 |
} |
| 63 |
|
| 64 |
void |
| 65 |
test03() |
| 66 |
{ |
| 67 |
auto a1 = net::ip::make_address_v4("127.0.0.1"); |
| 68 |
VERIFY( a1.is_loopback() ); |
| 69 |
auto a2 = net::ip::make_address_v4(std::string{"127.0.0.2"}); |
| 70 |
VERIFY( a2.is_loopback() ); |
| 71 |
auto a3 = net::ip::make_address_v4(std::experimental::string_view{"127.0.0.3"}); |
| 72 |
VERIFY( a3.is_loopback() ); |
| 73 |
|
| 74 |
std::error_code ec; |
| 75 |
auto a4 = net::ip::make_address_v4("127...1", ec); |
| 76 |
VERIFY( ec == std::errc::invalid_argument ); |
| 77 |
|
| 78 |
net::ip::make_address_v4("127.0.0.1", ec); |
| 79 |
VERIFY( !ec ); |
| 80 |
|
| 81 |
a4 = net::ip::make_address_v4(std::string{"256.0.0.1"}, ec); |
| 82 |
VERIFY( ec == std::errc::invalid_argument ); |
| 83 |
|
| 84 |
net::ip::make_address_v4(std::string{"127.0.0.1"}, ec); |
| 85 |
VERIFY( !ec ); |
| 86 |
|
| 87 |
a4 = net::ip::make_address_v4(std::experimental::string_view{""}, ec); |
| 88 |
VERIFY( ec == std::errc::invalid_argument ); |
| 89 |
} |
| 90 |
|
| 91 |
constexpr bool |
| 92 |
test_constexpr() |
| 93 |
{ |
| 94 |
test01(); |
| 95 |
test02(); |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
int |
| 100 |
main() |
| 101 |
{ |
| 102 |
test01(); |
| 103 |
test02(); |
| 104 |
test03(); |
| 105 |
|
| 106 |
static_assert( test_constexpr(), "valid in constant expressions" ); |
| 107 |
} |