]> git.ipfire.org Git - thirdparty/gcc.git/blob - 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
1 // Copyright (C) 2015-2020 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-add-options net_ts }
20
21 #include <experimental/internet>
22 #include <testsuite_hooks.h>
23
24 namespace net = std::experimental::net;
25 using net::ip::address_v4;
26
27 void
28 test01()
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
42 void
43 test02()
44 {
45 bool test __attribute__((unused)) = false;
46
47 auto a0 = net::ip::make_address_v4(0u);
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);
52 auto a1 = net::ip::make_address_v4( u1 );
53 VERIFY( a1.to_uint() == u1 );
54 VERIFY( a1.to_bytes() == address_v4::bytes_type( 5, 6, 7, 8 ) );
55 }
56
57 void
58 test03()
59 {
60 bool test __attribute__((unused)) = false;
61
62 auto a1 = net::ip::make_address_v4("127.0.0.1");
63 VERIFY( a1.is_loopback() );
64 auto a2 = net::ip::make_address_v4(std::string{"127.0.0.2"});
65 VERIFY( a2.is_loopback() );
66 auto a3 = net::ip::make_address_v4(std::experimental::string_view{"127.0.0.3"});
67 VERIFY( a3.is_loopback() );
68
69 std::error_code ec;
70 auto a4 = net::ip::make_address_v4("127...1", ec);
71 VERIFY( ec == std::errc::invalid_argument );
72
73 net::ip::make_address_v4("127.0.0.1", ec);
74 VERIFY( !ec );
75
76 a4 = net::ip::make_address_v4(std::string{"256.0.0.1"}, ec);
77 VERIFY( ec == std::errc::invalid_argument );
78
79 net::ip::make_address_v4(std::string{"127.0.0.1"}, ec);
80 VERIFY( !ec );
81
82 a4 = net::ip::make_address_v4(std::experimental::string_view{""}, ec);
83 VERIFY( ec == std::errc::invalid_argument );
84 }
85
86 int
87 main()
88 {
89 test01();
90 test02();
91 test03();
92 }