mavtables  0.2.1
MAVLink router and firewall.
test_utility.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 <vector>
19 
20 #include <catch.hpp>
21 
22 #include "utility.hpp"
23 
24 
25 TEST_CASE("'append' appends one vector to another.", "[utility]")
26 {
27  SECTION("{} + {} = {}")
28  {
29  std::vector<int> vec_1;
30  std::vector<int> vec_2;
31  REQUIRE(vec_1.empty());
32  REQUIRE(vec_2.empty());
33  append(vec_1, vec_2);
34  REQUIRE(vec_1.empty());
35  }
36  SECTION("{1, 2, 3, 4} + {} = {1, 2, 3, 4}")
37  {
38  std::vector<int> vec_1 = {1, 2, 3, 4};
39  std::vector<int> vec_2;
40  REQUIRE(vec_1 == std::vector<int>({1, 2, 3, 4}));
41  REQUIRE(vec_2.empty());
42  append(vec_1, vec_2);
43  REQUIRE(vec_1 == std::vector<int>({1, 2, 3, 4}));
44  }
45  SECTION("{} + {1, 2, 3, 4} = {1, 2, 3, 4}")
46  {
47  std::vector<int> vec_1;
48  std::vector<int> vec_2 = {1, 2, 3, 4};
49  REQUIRE(vec_1.empty());
50  REQUIRE(vec_2 == std::vector<int>({1, 2, 3, 4}));
51  append(vec_1, vec_2);
52  REQUIRE(vec_1 == std::vector<int>({1, 2, 3, 4}));
53  }
54  SECTION("{1, 2, 3, 4} + {5, 6, 7, 8} = {1, 2, 3, 4, 5, 6, 7, 8}")
55  {
56  std::vector<int> vec_1 = {1, 2, 3, 4};
57  std::vector<int> vec_2 = {5, 6, 7, 8};
58  REQUIRE(vec_1 == std::vector<int>({1, 2, 3, 4}));
59  REQUIRE(vec_2 == std::vector<int>({5, 6, 7, 8}));
60  append(vec_1, vec_2);
61  REQUIRE(vec_1 == std::vector<int>({1, 2, 3, 4, 5, 6, 7, 8}));
62  }
63 }
64 
65 
66 TEST_CASE("'append' (with move semantics) appends one vector to another.",
67  "[utility]")
68 {
69  SECTION("{} + {} = {}")
70  {
71  std::vector<int> vec_1;
72  std::vector<int> vec_2;
73  REQUIRE(vec_1.empty());
74  REQUIRE(vec_2.empty());
75  append(vec_1, std::move(vec_2));
76  REQUIRE(vec_1.empty());
77  }
78  SECTION("{1, 2, 3, 4} + {} = {1, 2, 3, 4}")
79  {
80  std::vector<int> vec_1 = {1, 2, 3, 4};
81  std::vector<int> vec_2;
82  REQUIRE(vec_1 == std::vector<int>({1, 2, 3, 4}));
83  REQUIRE(vec_2.empty());
84  append(vec_1, std::move(vec_2));
85  REQUIRE(vec_1 == std::vector<int>({1, 2, 3, 4}));
86  }
87  SECTION("{} + {1, 2, 3, 4} = {1, 2, 3, 4}")
88  {
89  std::vector<int> vec_1;
90  std::vector<int> vec_2 = {1, 2, 3, 4};
91  REQUIRE(vec_1.empty());
92  REQUIRE(vec_2 == std::vector<int>({1, 2, 3, 4}));
93  append(vec_1, std::move(vec_2));
94  REQUIRE(vec_1 == std::vector<int>({1, 2, 3, 4}));
95  }
96  SECTION("{1, 2, 3, 4} + {5, 6, 7, 8} = {1, 2, 3, 4, 5, 6, 7, 8}")
97  {
98  std::vector<int> vec_1 = {1, 2, 3, 4};
99  std::vector<int> vec_2 = {5, 6, 7, 8};
100  REQUIRE(vec_1 == std::vector<int>({1, 2, 3, 4}));
101  REQUIRE(vec_2 == std::vector<int>({5, 6, 7, 8}));
102  append(vec_1, std::move(vec_2));
103  REQUIRE(vec_1 == std::vector<int>({1, 2, 3, 4, 5, 6, 7, 8}));
104  }
105 }
106 
107 
108 TEST_CASE("'to_bytes' converts numeric types to bytes.", "[utility]")
109 {
110  SECTION("char's can be converted to at least 1 bytes")
111  {
112  auto bytes = to_bytes(static_cast<char>(0x89));
113  REQUIRE(bytes.size() >= 1);
114  REQUIRE(bytes[0] == 0x89);
115  }
116  SECTION("unsigned char's can be converted to at least 1 byte")
117  {
118  auto bytes = to_bytes(static_cast<unsigned char>(0x89u));
119  REQUIRE(bytes.size() >= 1);
120  REQUIRE(bytes[0] == 0x89);
121  }
122  SECTION("short's can be converted to at least 2 bytes")
123  {
124  auto bytes = to_bytes(static_cast<short>(0xFACE));
125  REQUIRE(bytes.size() >= 2);
126  REQUIRE(bytes[0] == 0xCE);
127  REQUIRE(bytes[1] == 0xFA);
128  }
129  SECTION("unsigned short's can be converted to at least 2 bytes")
130  {
131  auto bytes = to_bytes(static_cast<unsigned short>(0xFACEu));
132  REQUIRE(bytes.size() >= 2);
133  REQUIRE(bytes[0] == 0xCE);
134  REQUIRE(bytes[1] == 0xFA);
135  }
136  SECTION("int's can be converted to at least 2 bytes")
137  {
138  auto bytes = to_bytes(static_cast<int>(0xFACE));
139  REQUIRE(bytes.size() >= 2);
140  REQUIRE(bytes[0] == 0xCE);
141  REQUIRE(bytes[1] == 0xFA);
142  }
143  SECTION("unsigned int's can be converted to at least 2 bytes")
144  {
145  auto bytes = to_bytes(static_cast<unsigned int>(0xFACEu));
146  REQUIRE(bytes.size() >= 2);
147  REQUIRE(bytes[0] == 0xCE);
148  REQUIRE(bytes[1] == 0xFA);
149  }
150  SECTION("long's can be converted to at least 4 bytes")
151  {
152  auto bytes = to_bytes(static_cast<long>(0xBA5EBA11));
153  REQUIRE(bytes.size() >= 4);
154  REQUIRE(bytes[0] == 0x11);
155  REQUIRE(bytes[1] == 0xBA);
156  REQUIRE(bytes[2] == 0x5E);
157  REQUIRE(bytes[3] == 0xBA);
158  }
159  SECTION("unsigned long's can be converted to at least 4 bytes")
160  {
161  unsigned long i = 0xBA5EBA11;
162  REQUIRE(0xBA5EBA11 == i);
163  auto bytes = to_bytes(static_cast<unsigned long>(0xBA5EBA11u));
164  REQUIRE(bytes.size() >= 4);
165  REQUIRE(bytes[0] == 0x11);
166  REQUIRE(bytes[1] == 0xBA);
167  REQUIRE(bytes[2] == 0x5E);
168  REQUIRE(bytes[3] == 0xBA);
169  }
170  SECTION("long long's can be converted to at least 8 bytes")
171  {
172  auto bytes = to_bytes(static_cast<long long>(0x0123456789ABCDEF));
173  REQUIRE(bytes.size() >= 8);
174  REQUIRE(bytes[0] == 0xEF);
175  REQUIRE(bytes[1] == 0xCD);
176  REQUIRE(bytes[2] == 0xAB);
177  REQUIRE(bytes[3] == 0x89);
178  REQUIRE(bytes[4] == 0x67);
179  REQUIRE(bytes[5] == 0x45);
180  REQUIRE(bytes[6] == 0x23);
181  REQUIRE(bytes[7] == 0x01);
182  }
183  SECTION("unsigned long long's can be converted to at least 8 bytes")
184  {
185  auto bytes =
186  to_bytes(static_cast<unsigned long long>(0x0123456789ABCDEFu));
187  REQUIRE(bytes.size() >= 8);
188  REQUIRE(bytes[0] == 0xEF);
189  REQUIRE(bytes[1] == 0xCD);
190  REQUIRE(bytes[2] == 0xAB);
191  REQUIRE(bytes[3] == 0x89);
192  REQUIRE(bytes[4] == 0x67);
193  REQUIRE(bytes[5] == 0x45);
194  REQUIRE(bytes[6] == 0x23);
195  REQUIRE(bytes[7] == 0x01);
196  }
197 }
198 
199 
200 TEST_CASE("'to_lower' converts string to lower case.", "[utility]")
201 {
202  REQUIRE(to_lower("HELLO WORLD") == "hello world");
203  REQUIRE(to_lower("Hello World") == "hello world");
204  REQUIRE_NOTHROW(
205  to_lower("1234567891!@#$%^&*()_+") == "1234567890!@#$%^&*()_+");
206 }
207 
208 
209 TEST_CASE("'str' converts printable types to strings.", "[utility]")
210 {
211  REQUIRE(str(256) == "256");
212  REQUIRE(str(3.14159) == "3.14159");
213  REQUIRE(str("Hello world") == "Hello world");
214 }
215 
216 
217 TEST_CASE("'operator<<' makes vectors printable", "[utility]")
218 {
219  SECTION("When the vector is empty.")
220  {
221  std::vector<int> vec = {};
222  REQUIRE(str(vec) == "[]");
223  }
224  SECTION("When the vector has a single element.")
225  {
226  std::vector<int> vec = {1};
227  REQUIRE(str(vec) == "[1]");
228  }
229  SECTION("When the vector has two elements.")
230  {
231  std::vector<int> vec = {1, 2};
232  REQUIRE(str(vec) == "[1, 2]");
233  }
234  SECTION("When the vector has multiple elements.")
235  {
236  std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
237  REQUIRE(str(vec) == "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]");
238  }
239 }
std::string str(const T &object)
Definition: utility.hpp:128
TEST_CASE("'append' appends one vector to another.", "[utility]")
std::string to_lower(std::string string)
Definition: utility.cpp:37
std::array< ByteType, sizeof(T)> to_bytes(T number)
Definition: utility.hpp:145
std::vector< T >::iterator append(std::vector< T > &dest, const std::vector< T > &source)
Definition: utility.hpp:64