mavtables  0.2.1
MAVLink router and firewall.
test_MAVAddress.cpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2017-2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program 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
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #include <stdexcept>
19 #include <utility>
20 
21 #include <catch.hpp>
22 
23 #include "MAVAddress.hpp"
24 #include "utility.hpp"
25 
26 
27 TEST_CASE("MAVAddress's store a system and component ID.", "[MAVAddress]")
28 {
29  MAVAddress a(0, 0);
30  REQUIRE(a.system() == 0);
31  REQUIRE(a.component() == 0);
32  REQUIRE(a.address() == 0);
33  MAVAddress b(255, 255);
34  REQUIRE(b.system() == 255);
35  REQUIRE(b.component() == 255);
36  REQUIRE(b.address() == 0xFFFF);
37  MAVAddress c(255, 0);
38  REQUIRE(c.system() == 255);
39  REQUIRE(c.component() == 0);
40  REQUIRE(c.address() == 0xFF00);
41  MAVAddress d(0, 255);
42  REQUIRE(d.system() == 0);
43  REQUIRE(d.component() == 255);
44  REQUIRE(d.address() == 0x00FF);
45 }
46 
47 
48 TEST_CASE("MAVAddress's are comparable.", "[MAVAddress]")
49 {
50  SECTION("with ==")
51  {
52  REQUIRE(MAVAddress(0, 0) == MAVAddress(0, 0));
53  REQUIRE(MAVAddress(128, 128) == MAVAddress(128, 128));
54  REQUIRE(MAVAddress(255, 255) == MAVAddress(255, 255));
55  REQUIRE_FALSE(MAVAddress(0, 0) == MAVAddress(0, 1));
56  REQUIRE_FALSE(MAVAddress(0, 0) == MAVAddress(1, 0));
57  REQUIRE_FALSE(MAVAddress(255, 255) == MAVAddress(255, 254));
58  REQUIRE_FALSE(MAVAddress(255, 255) == MAVAddress(254, 255));
59  }
60  SECTION("with !=")
61  {
62  REQUIRE(MAVAddress(0, 0) != MAVAddress(0, 1));
63  REQUIRE(MAVAddress(0, 0) != MAVAddress(1, 0));
64  REQUIRE(MAVAddress(255, 255) != MAVAddress(255, 254));
65  REQUIRE(MAVAddress(255, 255) != MAVAddress(254, 255));
66  REQUIRE_FALSE(MAVAddress(0, 0) != MAVAddress(0, 0));
67  REQUIRE_FALSE(MAVAddress(128, 128) != MAVAddress(128, 128));
68  REQUIRE_FALSE(MAVAddress(255, 255) != MAVAddress(255, 255));
69  }
70  SECTION("with <")
71  {
72  REQUIRE(MAVAddress(0, 0) < MAVAddress(0, 1));
73  REQUIRE(MAVAddress(0, 0) < MAVAddress(1, 0));
74  REQUIRE(MAVAddress(0, 1) < MAVAddress(1, 0));
75  REQUIRE(MAVAddress(255, 254) < MAVAddress(255, 255));
76  REQUIRE(MAVAddress(254, 255) < MAVAddress(255, 255));
77  REQUIRE(MAVAddress(0, 0) < MAVAddress(255, 255));
78  REQUIRE_FALSE(MAVAddress(0, 1) < MAVAddress(0, 0));
79  REQUIRE_FALSE(MAVAddress(1, 0) < MAVAddress(0, 0));
80  REQUIRE_FALSE(MAVAddress(1, 0) < MAVAddress(0, 1));
81  REQUIRE_FALSE(MAVAddress(255, 255) < MAVAddress(255, 254));
82  REQUIRE_FALSE(MAVAddress(255, 255) < MAVAddress(254, 255));
83  REQUIRE_FALSE(MAVAddress(0, 0) < MAVAddress(0, 0));
84  REQUIRE_FALSE(MAVAddress(128, 128) < MAVAddress(128, 128));
85  REQUIRE_FALSE(MAVAddress(255, 255) < MAVAddress(255, 255));
86  REQUIRE_FALSE(MAVAddress(255, 255) < MAVAddress(0, 0));
87  }
88  SECTION("with >")
89  {
90  REQUIRE(MAVAddress(0, 1) > MAVAddress(0, 0));
91  REQUIRE(MAVAddress(1, 0) > MAVAddress(0, 0));
92  REQUIRE(MAVAddress(1, 0) > MAVAddress(0, 1));
93  REQUIRE(MAVAddress(255, 255) > MAVAddress(255, 254));
94  REQUIRE(MAVAddress(255, 255) > MAVAddress(254, 254));
95  REQUIRE(MAVAddress(255, 255) > MAVAddress(0, 0));
96  REQUIRE_FALSE(MAVAddress(0, 0) > MAVAddress(0, 1));
97  REQUIRE_FALSE(MAVAddress(0, 0) > MAVAddress(1, 0));
98  REQUIRE_FALSE(MAVAddress(0, 1) > MAVAddress(1, 0));
99  REQUIRE_FALSE(MAVAddress(255, 254) > MAVAddress(255, 255));
100  REQUIRE_FALSE(MAVAddress(254, 255) > MAVAddress(255, 255));
101  REQUIRE_FALSE(MAVAddress(0, 0) > MAVAddress(0, 0));
102  REQUIRE_FALSE(MAVAddress(128, 128) > MAVAddress(128, 128));
103  REQUIRE_FALSE(MAVAddress(255, 255) > MAVAddress(255, 255));
104  REQUIRE_FALSE(MAVAddress(0, 0) > MAVAddress(255, 255));
105  }
106  SECTION("with <=")
107  {
108  REQUIRE(MAVAddress(0, 0) <= MAVAddress(0, 0));
109  REQUIRE(MAVAddress(128, 128) <= MAVAddress(128, 128));
110  REQUIRE(MAVAddress(255, 255) <= MAVAddress(255, 255));
111  REQUIRE(MAVAddress(0, 0) <= MAVAddress(0, 1));
112  REQUIRE(MAVAddress(0, 0) <= MAVAddress(1, 0));
113  REQUIRE(MAVAddress(0, 1) <= MAVAddress(1, 0));
114  REQUIRE(MAVAddress(255, 254) <= MAVAddress(255, 255));
115  REQUIRE(MAVAddress(254, 255) <= MAVAddress(255, 255));
116  REQUIRE(MAVAddress(0, 0) <= MAVAddress(255, 255));
117  REQUIRE_FALSE(MAVAddress(0, 1) <= MAVAddress(0, 0));
118  REQUIRE_FALSE(MAVAddress(1, 0) <= MAVAddress(0, 0));
119  REQUIRE_FALSE(MAVAddress(1, 0) <= MAVAddress(0, 1));
120  REQUIRE_FALSE(MAVAddress(255, 255) <= MAVAddress(255, 254));
121  REQUIRE_FALSE(MAVAddress(255, 255) <= MAVAddress(254, 255));
122  REQUIRE_FALSE(MAVAddress(255, 255) <= MAVAddress(0, 0));
123  }
124  SECTION("with >=")
125  {
126  REQUIRE(MAVAddress(0, 0) >= MAVAddress(0, 0));
127  REQUIRE(MAVAddress(128, 128) >= MAVAddress(128, 128));
128  REQUIRE(MAVAddress(255, 255) >= MAVAddress(255, 255));
129  REQUIRE(MAVAddress(0, 1) >= MAVAddress(0, 0));
130  REQUIRE(MAVAddress(1, 0) >= MAVAddress(0, 0));
131  REQUIRE(MAVAddress(1, 0) >= MAVAddress(0, 1));
132  REQUIRE(MAVAddress(255, 255) >= MAVAddress(255, 254));
133  REQUIRE(MAVAddress(255, 255) >= MAVAddress(254, 255));
134  REQUIRE(MAVAddress(255, 255) >= MAVAddress(0, 0));
135  REQUIRE_FALSE(MAVAddress(0, 0) >= MAVAddress(0, 1));
136  REQUIRE_FALSE(MAVAddress(0, 0) >= MAVAddress(1, 0));
137  REQUIRE_FALSE(MAVAddress(0, 1) >= MAVAddress(1, 0));
138  REQUIRE_FALSE(MAVAddress(255, 254) >= MAVAddress(255, 255));
139  REQUIRE_FALSE(MAVAddress(254, 255) >= MAVAddress(255, 255));
140  REQUIRE_FALSE(MAVAddress(0, 0) >= MAVAddress(255, 255));
141  }
142 }
143 
144 
145 TEST_CASE("MAVAddress's can be constructed from a numeric address.",
146  "[MAVAddress]")
147 {
148  REQUIRE(MAVAddress(0x0000) == MAVAddress(0x0000));
149  REQUIRE(MAVAddress(0x8000) == MAVAddress(0x8000));
150  REQUIRE(MAVAddress(0x0080) == MAVAddress(0x0080));
151  REQUIRE(MAVAddress(0xFF00) == MAVAddress(0xFF00));
152  REQUIRE(MAVAddress(0x00FF) == MAVAddress(0x00FF));
153  REQUIRE(MAVAddress(0xFFFF) == MAVAddress(0xFFFF));
154  SECTION("And ensures System and Component ID's are within range.")
155  {
156  REQUIRE_THROWS_AS(MAVAddress(static_cast<unsigned int>(0x0000 - 1)),
157  std::out_of_range);
158  REQUIRE_THROWS_AS(MAVAddress(0xFFFF + 1), std::out_of_range);
159  }
160 }
161 
162 
163 TEST_CASE("MAVAddress's can be constructed from System and Component ID's.",
164  "[MAVAddress]")
165 {
166  REQUIRE(MAVAddress(0, 0) == MAVAddress(0x0000));
167  REQUIRE(MAVAddress(128, 0) == MAVAddress(0x8000));
168  REQUIRE(MAVAddress(0, 128) == MAVAddress(0x0080));
169  REQUIRE(MAVAddress(255, 0) == MAVAddress(0xFF00));
170  REQUIRE(MAVAddress(0, 255) == MAVAddress(0x00FF));
171  REQUIRE(MAVAddress(255, 255) == MAVAddress(0xFFFF));
172  SECTION("And ensures System and Component ID's are within range.")
173  {
174  // Errors
175  REQUIRE_THROWS_AS(MAVAddress(static_cast<unsigned int>(-1), 0),
176  std::out_of_range);
177  REQUIRE_THROWS_AS(MAVAddress(0, static_cast<unsigned int>(-1)),
178  std::out_of_range);
179  REQUIRE_THROWS_AS(MAVAddress(256, 255), std::out_of_range);
180  REQUIRE_THROWS_AS(MAVAddress(255, 256), std::out_of_range);
181  REQUIRE_THROWS_AS(MAVAddress(256, 256), std::out_of_range);
182  // Error messages.
183  REQUIRE_THROWS_WITH(
184  MAVAddress(256, 255),
185  "System ID (256) is outside of the allowed range (0 - 255).");
186  REQUIRE_THROWS_WITH(
187  MAVAddress(255, 256),
188  "Component ID (256) is outside of the allowed range (0 - 255).");
189  // NOTE: MAVAddress(256, 256) is not checked because the order of
190  // checking the inputs is not defined.
191  }
192 }
193 
194 
195 TEST_CASE("MAVAddress's can be constructed from strings.", "[MAVAddress]")
196 {
197  REQUIRE(MAVAddress("0.0") == MAVAddress(0x0000));
198  REQUIRE(MAVAddress("128.0") == MAVAddress(0x8000));
199  REQUIRE(MAVAddress("0.128") == MAVAddress(0x0080));
200  REQUIRE(MAVAddress("255.0") == MAVAddress(0xFF00));
201  REQUIRE(MAVAddress("0.255") == MAVAddress(0x00FF));
202  REQUIRE(MAVAddress("255.255") == MAVAddress(0xFFFF));
203  REQUIRE(MAVAddress("000.000") == MAVAddress(0x0000));
204  REQUIRE(MAVAddress("001.001") == MAVAddress(0x0101));
205  REQUIRE(MAVAddress("010.010") == MAVAddress(0x0A0A));
206  REQUIRE(MAVAddress("192.168") == MAVAddress(192, 168));
207  SECTION("And ensures the address string is valid.")
208  {
209  // Errors
210  REQUIRE_THROWS_AS(MAVAddress("1"), std::invalid_argument);
211  REQUIRE_THROWS_AS(MAVAddress("1."), std::invalid_argument);
212  REQUIRE_THROWS_AS(MAVAddress("1.2."), std::invalid_argument);
213  REQUIRE_THROWS_AS(MAVAddress("1.2.3"), std::invalid_argument);
214  REQUIRE_THROWS_AS(MAVAddress("a.2.3"), std::invalid_argument);
215  REQUIRE_THROWS_AS(MAVAddress("1.b.3"), std::invalid_argument);
216  REQUIRE_THROWS_AS(MAVAddress("1.2.c"), std::invalid_argument);
217  REQUIRE_THROWS_AS(MAVAddress("+1.0"), std::invalid_argument);
218  REQUIRE_THROWS_AS(MAVAddress("0.+1"), std::invalid_argument);
219  REQUIRE_THROWS_AS(MAVAddress("-1.0"), std::invalid_argument);
220  REQUIRE_THROWS_AS(MAVAddress("0.-1"), std::invalid_argument);
221  // Error message.
222  REQUIRE_THROWS_WITH(
223  MAVAddress("1"), "Invalid MAVLink address string.");
224  REQUIRE_THROWS_WITH(
225  MAVAddress("1."), "Invalid MAVLink address string.");
226  REQUIRE_THROWS_WITH(
227  MAVAddress("1.2."), "Invalid MAVLink address string.");
228  REQUIRE_THROWS_WITH(
229  MAVAddress("1.2.3"), "Invalid MAVLink address string.");
230  REQUIRE_THROWS_WITH(
231  MAVAddress("a.2.3"), "Invalid MAVLink address string.");
232  REQUIRE_THROWS_WITH(
233  MAVAddress("1.b.3"), "Invalid MAVLink address string.");
234  REQUIRE_THROWS_WITH(
235  MAVAddress("1.2.c"), "Invalid MAVLink address string.");
236  REQUIRE_THROWS_WITH(
237  MAVAddress("+1.0"), "Invalid MAVLink address string.");
238  REQUIRE_THROWS_WITH(
239  MAVAddress("0.+1"), "Invalid MAVLink address string.");
240  REQUIRE_THROWS_WITH(
241  MAVAddress("-1.0"), "Invalid MAVLink address string.");
242  REQUIRE_THROWS_WITH(
243  MAVAddress("0.-1"), "Invalid MAVLink address string.");
244  }
245  SECTION("And ensures System and Component ID's are within range.")
246  {
247  // Errors
248  REQUIRE_THROWS_AS(MAVAddress("256.255"), std::out_of_range);
249  REQUIRE_THROWS_AS(MAVAddress("255.256"), std::out_of_range);
250  REQUIRE_THROWS_AS(MAVAddress("256.256"), std::out_of_range);
251  // Error messages.
252  REQUIRE_THROWS_WITH(
253  MAVAddress("256.255"),
254  "System ID (256) is outside of the allowed range (0 - 255).");
255  REQUIRE_THROWS_WITH(
256  MAVAddress("255.256"),
257  "Component ID (256) is outside of the allowed range (0 - 255).");
258  // NOTE: MAVAddress("256.256") is not checked because the order of
259  // checking the inputs is not defined.
260  }
261 }
262 
263 
264 TEST_CASE("MAVAddress's are copyable.", "[MAVAddress]")
265 {
266  MAVAddress address_a(0, 0);
267  MAVAddress address_b(255, 255);
268  MAVAddress address_a_copy = address_a;
269  MAVAddress address_b_copy(address_b);
270  REQUIRE(&address_a != &address_a_copy);
271  REQUIRE(address_a == address_a_copy);
272  REQUIRE(&address_b != &address_b_copy);
273  REQUIRE(address_b == address_b_copy);
274 }
275 
276 
277 TEST_CASE("MAVAddress's are movable.", "[MAVAddress]")
278 {
279  MAVAddress address_a(0, 0);
280  MAVAddress address_b(255, 255);
281  MAVAddress address_a_moved = std::move(address_a);
282  MAVAddress address_b_moved(std::move(address_b));
283  REQUIRE(address_a_moved == MAVAddress(0, 0));
284  REQUIRE(address_b_moved == MAVAddress(255, 255));
285 }
286 
287 
288 TEST_CASE("MAVAddress's are assignable.", "[MAVAddress]")
289 {
290  MAVAddress address_a(0, 0);
291  MAVAddress address_b(255, 255);
292  REQUIRE(address_a == MAVAddress(0, 0));
294  REQUIRE(address_a == MAVAddress(255, 255));
295 }
296 
297 
298 TEST_CASE("MAVAddress's are assignable (by move semantics).", "[MAVAddress]")
299 {
300  MAVAddress address_a(0, 0);
301  MAVAddress address_b(255, 255);
302  REQUIRE(address_a == MAVAddress(0, 0));
303  address_a = std::move(address_b);
304  REQUIRE(address_a == MAVAddress(255, 255));
305 }
306 
307 
308 TEST_CASE("MAVAddress's are printable", "[MAVAddress]")
309 {
310  REQUIRE(str(MAVAddress(192, 168)) == "192.168");
311  REQUIRE(str(MAVAddress(32, 128)) == "32.128");
312 }
unsigned int component() const
Definition: MAVAddress.cpp:171
std::string str(const T &object)
Definition: utility.hpp:128
unsigned int system() const
Definition: MAVAddress.cpp:161
TEST_CASE("MAVAddress's store a system and component ID.", "[MAVAddress]")
unsigned int address() const
Definition: MAVAddress.cpp:151
MAVAddress address_b(255, 255)