]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/experimental/net/internet/address/v4/creation.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / net / internet / address / v4 / creation.cc
CommitLineData
8d9254fc 1// Copyright (C) 2015-2020 Free Software Foundation, Inc.
e5989e71
JW
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
7e8b87e9 18// { dg-do run { target c++14 } }
630f2da9 19// { dg-add-options net_ts }
e5989e71
JW
20
21#include <experimental/internet>
22#include <testsuite_hooks.h>
23
436ea0e5
JW
24namespace net = std::experimental::net;
25using net::ip::address_v4;
e5989e71
JW
26
27void
28test01()
29{
30 bool test __attribute__((unused)) = false;
31
32 auto a0 = make_address_v4( address_v4::bytes_type{} );
33 VERIFY( a0.to_uint() == 0 );
34 VERIFY( a0.to_bytes() == address_v4::bytes_type{} );
35
36 address_v4::bytes_type b1{ 1, 2, 3, 4 };
37 auto a1 = make_address_v4( b1 );
38 VERIFY( a1.to_uint() == ntohl((1 << 24) | (2 << 16) | (3 << 8) | 4) );
39 VERIFY( a1.to_bytes() == b1 );
40}
41
42void
43test02()
44{
45 bool test __attribute__((unused)) = false;
46
436ea0e5 47 auto a0 = net::ip::make_address_v4(0u);
e5989e71
JW
48 VERIFY( a0.to_uint() == 0 );
49 VERIFY( a0.to_bytes() == address_v4::bytes_type{} );
50
51 address_v4::uint_type u1 = ntohl((5 << 24) | (6 << 16) | (7 << 8) | 8);
436ea0e5 52 auto a1 = net::ip::make_address_v4( u1 );
e5989e71
JW
53 VERIFY( a1.to_uint() == u1 );
54 VERIFY( a1.to_bytes() == address_v4::bytes_type( 5, 6, 7, 8 ) );
55}
56
57void
58test03()
59{
60 bool test __attribute__((unused)) = false;
61
436ea0e5 62 auto a1 = net::ip::make_address_v4("127.0.0.1");
e5989e71 63 VERIFY( a1.is_loopback() );
436ea0e5 64 auto a2 = net::ip::make_address_v4(std::string{"127.0.0.2"});
e5989e71 65 VERIFY( a2.is_loopback() );
436ea0e5 66 auto a3 = net::ip::make_address_v4(std::experimental::string_view{"127.0.0.3"});
e5989e71
JW
67 VERIFY( a3.is_loopback() );
68
69 std::error_code ec;
436ea0e5 70 auto a4 = net::ip::make_address_v4("127...1", ec);
e5989e71
JW
71 VERIFY( ec == std::errc::invalid_argument );
72
436ea0e5 73 net::ip::make_address_v4("127.0.0.1", ec);
e5989e71
JW
74 VERIFY( !ec );
75
436ea0e5 76 a4 = net::ip::make_address_v4(std::string{"256.0.0.1"}, ec);
e5989e71
JW
77 VERIFY( ec == std::errc::invalid_argument );
78
436ea0e5 79 net::ip::make_address_v4(std::string{"127.0.0.1"}, ec);
e5989e71
JW
80 VERIFY( !ec );
81
436ea0e5 82 a4 = net::ip::make_address_v4(std::experimental::string_view{""}, ec);
e5989e71
JW
83 VERIFY( ec == std::errc::invalid_argument );
84}
85
86int
87main()
88{
89 test01();
90 test02();
91 test03();
92}