25 TEST_CASE(
"'append' appends one vector to another.",
"[utility]")
27 SECTION(
"{} + {} = {}")
29 std::vector<int> vec_1;
30 std::vector<int> vec_2;
31 REQUIRE(vec_1.empty());
32 REQUIRE(vec_2.empty());
34 REQUIRE(vec_1.empty());
36 SECTION(
"{1, 2, 3, 4} + {} = {1, 2, 3, 4}")
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());
43 REQUIRE(vec_1 == std::vector<int>({1, 2, 3, 4}));
45 SECTION(
"{} + {1, 2, 3, 4} = {1, 2, 3, 4}")
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}));
52 REQUIRE(vec_1 == std::vector<int>({1, 2, 3, 4}));
54 SECTION(
"{1, 2, 3, 4} + {5, 6, 7, 8} = {1, 2, 3, 4, 5, 6, 7, 8}")
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}));
61 REQUIRE(vec_1 == std::vector<int>({1, 2, 3, 4, 5, 6, 7, 8}));
66 TEST_CASE(
"'append' (with move semantics) appends one vector to another.",
69 SECTION(
"{} + {} = {}")
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());
78 SECTION(
"{1, 2, 3, 4} + {} = {1, 2, 3, 4}")
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}));
87 SECTION(
"{} + {1, 2, 3, 4} = {1, 2, 3, 4}")
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}));
96 SECTION(
"{1, 2, 3, 4} + {5, 6, 7, 8} = {1, 2, 3, 4, 5, 6, 7, 8}")
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}));
108 TEST_CASE(
"'to_bytes' converts numeric types to bytes.",
"[utility]")
110 SECTION(
"char's can be converted to at least 1 bytes")
112 auto bytes =
to_bytes(static_cast<char>(0x89));
113 REQUIRE(bytes.size() >= 1);
114 REQUIRE(bytes[0] == 0x89);
116 SECTION(
"unsigned char's can be converted to at least 1 byte")
118 auto bytes =
to_bytes(static_cast<unsigned char>(0x89u));
119 REQUIRE(bytes.size() >= 1);
120 REQUIRE(bytes[0] == 0x89);
122 SECTION(
"short's can be converted to at least 2 bytes")
124 auto bytes =
to_bytes(static_cast<short>(0xFACE));
125 REQUIRE(bytes.size() >= 2);
126 REQUIRE(bytes[0] == 0xCE);
127 REQUIRE(bytes[1] == 0xFA);
129 SECTION(
"unsigned short's can be converted to at least 2 bytes")
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);
136 SECTION(
"int's can be converted to at least 2 bytes")
138 auto bytes =
to_bytes(static_cast<int>(0xFACE));
139 REQUIRE(bytes.size() >= 2);
140 REQUIRE(bytes[0] == 0xCE);
141 REQUIRE(bytes[1] == 0xFA);
143 SECTION(
"unsigned int's can be converted to at least 2 bytes")
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);
150 SECTION(
"long's can be converted to at least 4 bytes")
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);
159 SECTION(
"unsigned long's can be converted to at least 4 bytes")
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);
170 SECTION(
"long long's can be converted to at least 8 bytes")
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);
183 SECTION(
"unsigned long long's can be converted to at least 8 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);
200 TEST_CASE(
"'to_lower' converts string to lower case.",
"[utility]")
202 REQUIRE(
to_lower(
"HELLO WORLD") ==
"hello world");
203 REQUIRE(
to_lower(
"Hello World") ==
"hello world");
205 to_lower(
"1234567891!@#$%^&*()_+") ==
"1234567890!@#$%^&*()_+");
209 TEST_CASE(
"'str' converts printable types to strings.",
"[utility]")
211 REQUIRE(
str(256) ==
"256");
212 REQUIRE(
str(3.14159) ==
"3.14159");
213 REQUIRE(
str(
"Hello world") ==
"Hello world");
217 TEST_CASE(
"'operator<<' makes vectors printable",
"[utility]")
219 SECTION(
"When the vector is empty.")
221 std::vector<int> vec = {};
222 REQUIRE(
str(vec) ==
"[]");
224 SECTION(
"When the vector has a single element.")
226 std::vector<int> vec = {1};
227 REQUIRE(
str(vec) ==
"[1]");
229 SECTION(
"When the vector has two elements.")
231 std::vector<int> vec = {1, 2};
232 REQUIRE(
str(vec) ==
"[1, 2]");
234 SECTION(
"When the vector has multiple elements.")
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]");
std::string str(const T &object)
TEST_CASE("'append' appends one vector to another.", "[utility]")
std::string to_lower(std::string string)
std::array< ByteType, sizeof(T)> to_bytes(T number)
std::vector< T >::iterator append(std::vector< T > &dest, const std::vector< T > &source)