Debian Codegraph @

libstdc++-v3/testsuite/experimental/net/internet/network/v4/cons.cc

raw
1 // { dg-do run { target c++14 } }
2 // { dg-require-effective-target net_ts_ip }
3 // { dg-add-options net_ts }
4
5 #include <experimental/internet>
6 #include <stdexcept>
7 #include <testsuite_hooks.h>
8
9 using std::experimental::net::ip::network_v4;
10 using std::experimental::net::ip::address_v4;
11
12 constexpr void
13 test01()
14 {
15 network_v4 n0;
16 VERIFY( n0.address().is_unspecified() );
17 VERIFY( n0.prefix_length() == 0 );
18 }
19
20 constexpr void
21 test02()
22 {
23 address_v4 a0;
24 network_v4 n0{ a0, 0 };
25 VERIFY( n0.address() == a0 );
26 VERIFY( n0.prefix_length() == 0 );
27
28 address_v4 a1{ address_v4::bytes_type{ 1, 2, 3, 4 } };
29 network_v4 n1{ a1, 12};
30 VERIFY( n1.address() == a1 );
31 VERIFY( n1.prefix_length() == 12 );
32 }
33
34 void
35 test02_errors()
36 {
37 address_v4 a0;
38 try
39 {
40 network_v4{a0, -1};
41 VERIFY(false);
42 }
43 catch(const std::out_of_range&)
44 {
45 }
46
47 try
48 {
49 network_v4{a0, 33};
50 VERIFY(false);
51 }
52 catch(const std::out_of_range&)
53 {
54 }
55 }
56
57 constexpr void
58 test03()
59 {
60 address_v4 a0;
61 network_v4 n0{ a0, a0 };
62 VERIFY( n0.address() == a0 );
63 VERIFY( n0.prefix_length() == 0 );
64
65 address_v4 a1{ address_v4::bytes_type{ 1, 2, 3, 4 } };
66 network_v4 n1{ a1, address_v4::broadcast() };
67 VERIFY( n1.address() == a1 );
68 VERIFY( n1.prefix_length() == 32 );
69
70 network_v4 n2{ a1, address_v4::bytes_type(128, 0, 0, 0) };
71 VERIFY( n2.address() == a1 );
72 VERIFY( n2.prefix_length() == 1 );
73
74 network_v4 n3{ a1, address_v4::bytes_type(255, 255, 255, 192) };
75 VERIFY( n3.address() == a1 );
76 VERIFY( n3.prefix_length() == 26 );
77 }
78
79 void
80 test03_errors()
81 {
82 address_v4 a0;
83 try
84 {
85 // Contains non-contiguous non-zero bits.
86 network_v4{a0, address_v4::bytes_type(255, 1, 0, 0)};
87 VERIFY(false);
88 }
89 catch(const std::invalid_argument&)
90 {
91 }
92
93 try
94 {
95 // Most significant bit is zero and any other bits are non-zero.
96 network_v4{a0, address_v4::bytes_type(1, 0, 0, 0)};
97 VERIFY(false);
98 }
99 catch(const std::invalid_argument&)
100 {
101 }
102
103 try
104 {
105 // Most significant bit is zero and any other bits are non-zero.
106 network_v4{a0, address_v4::bytes_type(0, 1, 0, 0)};
107 VERIFY(false);
108 }
109 catch(const std::invalid_argument&)
110 {
111 }
112 }
113
114 constexpr bool
115 test_constexpr()
116 {
117 test01();
118 test02();
119 test03();
120 return true;
121 }
122
123
124 int
125 main()
126 {
127 test01();
128 test02();
129 test02_errors();
130 test03();
131 test03_errors();
132
133 static_assert( test_constexpr(), "valid in constant expressions" );
134 }