Mercurial > malikania
comparison tests/libmlk/util/main.cpp @ 193:209bdaa13a49
Tests: rename directories to match targets
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 27 Oct 2018 07:16:28 +0200 |
parents | tests/libcommon/util/main.cpp@3107ce017c3a |
children |
comparison
equal
deleted
inserted
replaced
192:74afc5a41c83 | 193:209bdaa13a49 |
---|---|
1 /* | |
2 * main.cpp -- test util | |
3 * | |
4 * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #define BOOST_TEST_MODULE "Util" | |
20 #include <boost/test/unit_test.hpp> | |
21 | |
22 #include <malikania/util.hpp> | |
23 #include <malikania/size.hpp> | |
24 | |
25 using namespace mlk; | |
26 using namespace nlohmann; | |
27 | |
28 namespace nlohmann { | |
29 | |
30 namespace detail { | |
31 | |
32 auto operator<<(std::ostream& out, json::value_t type) -> std::ostream& | |
33 { | |
34 out << json(type).type_name(); | |
35 return out; | |
36 } | |
37 | |
38 } // !detail | |
39 | |
40 } // !nlohmann | |
41 | |
42 /* | |
43 * util::clamp | |
44 * ------------------------------------------------------------------ | |
45 */ | |
46 | |
47 BOOST_AUTO_TEST_SUITE(clamp) | |
48 | |
49 BOOST_AUTO_TEST_CASE(normal) | |
50 { | |
51 BOOST_REQUIRE_EQUAL(5, util::clamp(5, 0, 10)); | |
52 } | |
53 | |
54 BOOST_AUTO_TEST_CASE(minimum) | |
55 { | |
56 BOOST_REQUIRE_EQUAL(0, util::clamp(0, 0, 10)); | |
57 } | |
58 | |
59 BOOST_AUTO_TEST_CASE(maximum) | |
60 { | |
61 BOOST_REQUIRE_EQUAL(10, util::clamp(10, 0, 10)); | |
62 } | |
63 | |
64 BOOST_AUTO_TEST_CASE(less) | |
65 { | |
66 BOOST_REQUIRE_EQUAL(0, util::clamp(-10, 0, 10)); | |
67 } | |
68 | |
69 BOOST_AUTO_TEST_CASE(higher) | |
70 { | |
71 BOOST_REQUIRE_EQUAL(10, util::clamp(20, 0, 10)); | |
72 } | |
73 | |
74 BOOST_AUTO_TEST_SUITE_END() | |
75 | |
76 /* | |
77 * util::json::require | |
78 * ------------------------------------------------------------------ | |
79 */ | |
80 | |
81 BOOST_AUTO_TEST_SUITE(json_require) | |
82 | |
83 BOOST_AUTO_TEST_CASE(simple) | |
84 { | |
85 json object{ | |
86 { "b", true }, | |
87 { "i", 123 }, | |
88 { "s", "blabla" } | |
89 }; | |
90 | |
91 BOOST_REQUIRE_EQUAL(util::json::require( | |
92 object, "/b"_json_pointer, json::value_t::boolean).type(), json::value_t::boolean); | |
93 BOOST_REQUIRE_EQUAL(util::json::require( | |
94 object, "/i"_json_pointer, json::value_t::number_integer).type(), json::value_t::number_integer); | |
95 BOOST_REQUIRE_EQUAL(util::json::require( | |
96 object, "/s"_json_pointer, json::value_t::string).type(), json::value_t::string); | |
97 } | |
98 | |
99 BOOST_AUTO_TEST_CASE(nonexistent) | |
100 { | |
101 auto json = json::object(); | |
102 | |
103 try { | |
104 util::json::require(json, "/non-existent"_json_pointer, json::value_t::string); | |
105 BOOST_FAIL("exception expected"); | |
106 } catch (const util::json::property_error& ex) { | |
107 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::null); | |
108 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::string); | |
109 } | |
110 } | |
111 | |
112 BOOST_AUTO_TEST_CASE(invalid) | |
113 { | |
114 json object{ | |
115 { "not-string", 123 } | |
116 }; | |
117 | |
118 try { | |
119 util::json::require(object, "/not-string"_json_pointer, json::value_t::string); | |
120 BOOST_FAIL("exception expected"); | |
121 } catch (const util::json::property_error& ex) { | |
122 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer); | |
123 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::string); | |
124 } | |
125 } | |
126 | |
127 BOOST_AUTO_TEST_SUITE_END() | |
128 | |
129 /* | |
130 * util::json::require_array | |
131 * ------------------------------------------------------------------ | |
132 */ | |
133 | |
134 BOOST_AUTO_TEST_SUITE(json_require_array) | |
135 | |
136 BOOST_AUTO_TEST_CASE(simple) | |
137 { | |
138 json object{ | |
139 { "a", { 1, 2, 3 } }, | |
140 { "l1", { | |
141 { "l2", { 4, 5, 6 } } | |
142 } | |
143 } | |
144 }; | |
145 | |
146 auto a = util::json::require_array(object, "/a"_json_pointer); | |
147 auto l2 = util::json::require_array(object, "/l1/l2"_json_pointer); | |
148 | |
149 BOOST_REQUIRE(a.is_array()); | |
150 BOOST_REQUIRE(l2.is_array()); | |
151 } | |
152 | |
153 BOOST_AUTO_TEST_CASE(invalid) | |
154 { | |
155 json object{ | |
156 { "not-array", 123 } | |
157 }; | |
158 | |
159 try { | |
160 util::json::require_array(object, "/not-array"_json_pointer); | |
161 BOOST_FAIL("exception expected"); | |
162 } catch (const util::json::property_error& ex) { | |
163 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer); | |
164 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::array); | |
165 } | |
166 } | |
167 | |
168 BOOST_AUTO_TEST_SUITE_END() | |
169 | |
170 /* | |
171 * util::json::require_bool | |
172 * ------------------------------------------------------------------ | |
173 */ | |
174 | |
175 BOOST_AUTO_TEST_SUITE(json_require_bool) | |
176 | |
177 BOOST_AUTO_TEST_CASE(simple) | |
178 { | |
179 json object{ | |
180 { "b", true } | |
181 }; | |
182 | |
183 BOOST_REQUIRE_EQUAL(util::json::require_bool(object, "/b"_json_pointer), true); | |
184 } | |
185 | |
186 BOOST_AUTO_TEST_CASE(invalid) | |
187 { | |
188 json object{ | |
189 { "not-bool", 123 } | |
190 }; | |
191 | |
192 try { | |
193 util::json::require_bool(object, "/not-bool"_json_pointer); | |
194 BOOST_FAIL("exception expected"); | |
195 } catch (const util::json::property_error& ex) { | |
196 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer); | |
197 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::boolean); | |
198 } | |
199 } | |
200 | |
201 BOOST_AUTO_TEST_SUITE_END() | |
202 | |
203 /* | |
204 * util::json::require_int | |
205 * ------------------------------------------------------------------ | |
206 */ | |
207 | |
208 BOOST_AUTO_TEST_SUITE(json_require_int) | |
209 | |
210 BOOST_AUTO_TEST_CASE(simple) | |
211 { | |
212 json object{ | |
213 { "i", 123 } | |
214 }; | |
215 | |
216 BOOST_REQUIRE_EQUAL(util::json::require_int(object, "/i"_json_pointer), 123); | |
217 } | |
218 | |
219 BOOST_AUTO_TEST_CASE(invalid) | |
220 { | |
221 json object{ | |
222 { "not-int", true } | |
223 }; | |
224 | |
225 try { | |
226 util::json::require_int(object, "/not-int"_json_pointer); | |
227 BOOST_FAIL("exception expected"); | |
228 } catch (const util::json::property_error& ex) { | |
229 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::boolean); | |
230 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_integer); | |
231 } | |
232 } | |
233 | |
234 BOOST_AUTO_TEST_SUITE_END() | |
235 | |
236 /* | |
237 * util::json::require_object | |
238 * ------------------------------------------------------------------ | |
239 */ | |
240 | |
241 BOOST_AUTO_TEST_SUITE(json_require_object) | |
242 | |
243 BOOST_AUTO_TEST_CASE(simple) | |
244 { | |
245 json object{ | |
246 { | |
247 "network", json::object({ | |
248 { "host", "localhost" }, | |
249 { "port", 9090 } | |
250 }) | |
251 } | |
252 }; | |
253 | |
254 BOOST_REQUIRE(util::json::require_object(object, "/network"_json_pointer).is_object()); | |
255 } | |
256 | |
257 BOOST_AUTO_TEST_CASE(invalid) | |
258 { | |
259 json object{ | |
260 { "not-object", 123 } | |
261 }; | |
262 | |
263 try { | |
264 util::json::require_object(object, "/not-object"_json_pointer); | |
265 BOOST_FAIL("exception expected"); | |
266 } catch (const util::json::property_error& ex) { | |
267 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer); | |
268 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::object); | |
269 } | |
270 } | |
271 | |
272 BOOST_AUTO_TEST_SUITE_END() | |
273 | |
274 /* | |
275 * util::json::require_string | |
276 * ------------------------------------------------------------------ | |
277 */ | |
278 | |
279 BOOST_AUTO_TEST_SUITE(json_require_string) | |
280 | |
281 BOOST_AUTO_TEST_CASE(simple) | |
282 { | |
283 json object{ | |
284 { "s", "hello" } | |
285 }; | |
286 | |
287 BOOST_REQUIRE_EQUAL(util::json::require_string(object, "/s"_json_pointer), "hello"); | |
288 } | |
289 | |
290 BOOST_AUTO_TEST_CASE(invalid) | |
291 { | |
292 json object{ | |
293 { "not-string", 123 } | |
294 }; | |
295 | |
296 try { | |
297 util::json::require_string(object, "/not-string"_json_pointer); | |
298 BOOST_FAIL("exception expected"); | |
299 } catch (const util::json::property_error& ex) { | |
300 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer); | |
301 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::string); | |
302 } | |
303 } | |
304 | |
305 BOOST_AUTO_TEST_SUITE_END() | |
306 | |
307 /* | |
308 * util::json::require_uint | |
309 * ------------------------------------------------------------------ | |
310 */ | |
311 | |
312 BOOST_AUTO_TEST_SUITE(json_require_uint) | |
313 | |
314 BOOST_AUTO_TEST_CASE(simple) | |
315 { | |
316 json object{ | |
317 { "u1", 123U } | |
318 }; | |
319 | |
320 BOOST_REQUIRE_EQUAL(util::json::require_uint(object, "/u1"_json_pointer), 123U); | |
321 } | |
322 | |
323 BOOST_AUTO_TEST_CASE(invalid) | |
324 { | |
325 json object{ | |
326 { "not-uint", true } | |
327 }; | |
328 | |
329 try { | |
330 util::json::require_uint(object, "/not-uint"_json_pointer); | |
331 BOOST_FAIL("exception expected"); | |
332 } catch (const util::json::property_error& ex) { | |
333 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::boolean); | |
334 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned); | |
335 } | |
336 } | |
337 | |
338 BOOST_AUTO_TEST_SUITE_END() | |
339 | |
340 BOOST_AUTO_TEST_SUITE(json_require_size) | |
341 | |
342 /* | |
343 * util::json::require_size | |
344 * ------------------------------------------------------------------ | |
345 */ | |
346 | |
347 BOOST_AUTO_TEST_CASE(simple) | |
348 { | |
349 json object{ | |
350 { "size", { | |
351 { "width", 10 }, | |
352 { "height", 20 } | |
353 } | |
354 } | |
355 }; | |
356 | |
357 BOOST_TEST((util::json::require_size(object, "/size"_json_pointer) == mlk::size{10, 20})); | |
358 } | |
359 | |
360 BOOST_AUTO_TEST_CASE(not_object) | |
361 { | |
362 json object{ | |
363 { "size", false } | |
364 }; | |
365 | |
366 try { | |
367 util::json::require_size(object, "/size"_json_pointer); | |
368 BOOST_FAIL("exception expected"); | |
369 } catch (const util::json::property_error& ex) { | |
370 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::null); | |
371 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned); | |
372 } | |
373 } | |
374 | |
375 BOOST_AUTO_TEST_CASE(not_int_width) | |
376 { | |
377 json object{ | |
378 { "size", { | |
379 { "width", "10" }, | |
380 { "height", 20 } | |
381 } | |
382 } | |
383 }; | |
384 | |
385 try { | |
386 util::json::require_size(object, "/size"_json_pointer); | |
387 BOOST_FAIL("exception expected"); | |
388 } catch (const util::json::property_error& ex) { | |
389 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::string); | |
390 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned); | |
391 } | |
392 } | |
393 | |
394 BOOST_AUTO_TEST_CASE(not_int_height) | |
395 { | |
396 json object{ | |
397 { "size", { | |
398 { "width", 10 }, | |
399 { "height", "20" } | |
400 } | |
401 } | |
402 }; | |
403 | |
404 try { | |
405 util::json::require_size(object, "/size"_json_pointer); | |
406 BOOST_FAIL("exception expected"); | |
407 } catch (const util::json::property_error& ex) { | |
408 BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::string); | |
409 BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned); | |
410 } | |
411 } | |
412 | |
413 BOOST_AUTO_TEST_SUITE_END() | |
414 | |
415 BOOST_AUTO_TEST_SUITE(json_get_size) | |
416 | |
417 BOOST_AUTO_TEST_CASE(simple) | |
418 { | |
419 json object{ | |
420 { "size", { | |
421 { "width", 10 }, | |
422 { "height", 20 } | |
423 } | |
424 } | |
425 }; | |
426 | |
427 BOOST_TEST((util::json::get_size(object, "/size"_json_pointer) == mlk::size{10, 20})); | |
428 } | |
429 | |
430 BOOST_AUTO_TEST_CASE(not_object) | |
431 { | |
432 const json object{{ "size", false }}; | |
433 const mlk::size def{10, 20}; | |
434 | |
435 BOOST_TEST(util::json::get_size(object, "/size"_json_pointer, def) == def); | |
436 } | |
437 | |
438 BOOST_AUTO_TEST_CASE(not_int_width) | |
439 { | |
440 const json object{ | |
441 { "size", { | |
442 { "width", "10" }, | |
443 { "height", 20 } | |
444 } | |
445 } | |
446 }; | |
447 | |
448 const mlk::size def{10, 20}; | |
449 | |
450 BOOST_TEST(util::json::get_size(object, "/size"_json_pointer, def) == def); | |
451 } | |
452 | |
453 BOOST_AUTO_TEST_CASE(not_int_height) | |
454 { | |
455 const json object{ | |
456 { "size", { | |
457 { "width", 10 }, | |
458 { "height", "20" } | |
459 } | |
460 } | |
461 }; | |
462 | |
463 const mlk::size def{10, 20}; | |
464 | |
465 BOOST_TEST(util::json::get_size(object, "/size"_json_pointer, def) == def); | |
466 } | |
467 | |
468 BOOST_AUTO_TEST_SUITE_END() |