Debian Codegraph @

libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc

raw
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 <sstream>
24 #include <testsuite_hooks.h>
25 #include <testsuite_allocator.h>
26
27 using std::experimental::net::ip::address_v4;
28
29 static_assert(std::is_standard_layout<address_v4::bytes_type>::value,
30 "net::ip::address_v4::bytes_type is a standard layout type");
31
32 constexpr bool
33 test01()
34 {
35 address_v4 a;
36 VERIFY( a.is_unspecified() );
37
38 a = address_v4::any();
39 VERIFY( a.is_unspecified() );
40
41 a = address_v4::loopback();
42 VERIFY( !a.is_unspecified() );
43
44 a = address_v4::broadcast();
45 VERIFY( !a.is_unspecified() );
46
47 return true;
48 }
49
50 static_assert(test01(), "");
51
52 constexpr bool
53 test02()
54 {
55 auto a = address_v4::loopback();
56 VERIFY( a.is_loopback() );
57
58 a = address_v4{0x7F000001};
59 VERIFY( a.is_loopback() );
60
61 a = address_v4{0x7F010203};
62 VERIFY( a.is_loopback() );
63
64 a = address_v4{0x7FFFFFFF};
65 VERIFY( a.is_loopback() );
66
67 a = address_v4::any();
68 VERIFY( !a.is_loopback() );
69
70 a = address_v4::broadcast();
71 VERIFY( !a.is_loopback() );
72
73 return true;
74 }
75
76 static_assert(test02(), "");
77
78 constexpr bool
79 test03()
80 {
81 auto a = address_v4{0xE0000001};
82 VERIFY( a.is_multicast() );
83
84 a = address_v4{0xE0010203};
85 VERIFY( a.is_multicast() );
86
87 a = address_v4{0xE0FFFFFF};
88 VERIFY( a.is_multicast() );
89
90 a = address_v4{0xF0000000};
91 VERIFY( !a.is_multicast() );
92
93 a = address_v4{0xDFFFFFFF};
94 VERIFY( !a.is_multicast() );
95
96 return true;
97 }
98
99 static_assert(test03(), "");
100
101 void
102 test04()
103 {
104 VERIFY( address_v4::any().to_string() == "0.0.0.0" );
105 VERIFY( address_v4::loopback().to_string() == "127.0.0.1" );
106 VERIFY( address_v4::broadcast().to_string() == "255.255.255.255" );
107 using b = address_v4::bytes_type;
108 VERIFY( address_v4(b(1, 23, 45, 67)).to_string() == "1.23.45.67" );
109 VERIFY( address_v4(b(12, 34, 56, 78)).to_string() == "12.34.56.78" );
110 VERIFY( address_v4(b(123, 4, 5, 6)).to_string() == "123.4.5.6" );
111 VERIFY( address_v4(b(123, 234, 124, 235)).to_string() == "123.234.124.235" );
112
113 __gnu_test::uneq_allocator<char> alloc(123);
114 auto str = address_v4(b(12, 34, 56, 78)).to_string(alloc);
115 VERIFY(str.get_allocator().get_personality() == alloc.get_personality());
116 VERIFY( str == "12.34.56.78" );
117 }
118
119 void
120 test05()
121 {
122 std::ostringstream ss;
123 ss << address_v4::any() << ' ' << address_v4::loopback() << ' '
124 << address_v4::broadcast();
125 VERIFY( ss.str() == "0.0.0.0 127.0.0.1 255.255.255.255" );
126 }
127
128 int
129 main()
130 {
131 test01();
132 test02();
133 test03();
134 test04();
135 test05();
136 }