openMSX
narrow_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
2#include "narrow.hh"
3#include <cstdint>
4#include <type_traits>
5
6template<typename From, typename To>
7bool test_narrow(From from, To expected)
8{
9 // When compiling unittests this will throw on error.
10 // But for normal builds it triggers an assertion.
11 auto result = narrow<To>(from);
12 static_assert(std::is_same_v<decltype(result), To>);
13 CHECK(result == expected);
14 return true;
15}
16
17TEST_CASE("narrow")
18{
19 int ERR = 0;
20
21 SECTION("int32_t -> uint8_t") {
22 CHECK_NOTHROW(test_narrow(int32_t( 0), uint8_t( 0)));
23 CHECK_NOTHROW(test_narrow(int32_t( 100), uint8_t(100)));
24 CHECK_NOTHROW(test_narrow(int32_t( 200), uint8_t(200)));
25 CHECK_NOTHROW(test_narrow(int32_t( 255), uint8_t(255)));
26
27 CHECK_THROWS (test_narrow(int32_t(-999), uint8_t(ERR)));
28 CHECK_THROWS (test_narrow(int32_t(-256), uint8_t(ERR)));
29 CHECK_THROWS (test_narrow(int32_t(-255), uint8_t(ERR)));
30 CHECK_THROWS (test_narrow(int32_t( -1), uint8_t(ERR)));
31 CHECK_THROWS (test_narrow(int32_t( 256), uint8_t(ERR)));
32 CHECK_THROWS (test_narrow(int32_t( 300), uint8_t(ERR)));
33 CHECK_THROWS (test_narrow(int32_t(9999), uint8_t(ERR)));
34 }
35 SECTION("int32_t -> int8_t") {
36 CHECK_NOTHROW(test_narrow(int32_t(-128), int8_t(-128)));
37 CHECK_NOTHROW(test_narrow(int32_t( -32), int8_t( -32)));
38 CHECK_NOTHROW(test_narrow(int32_t( 0), int8_t( 0)));
39 CHECK_NOTHROW(test_narrow(int32_t( 7), int8_t( 7)));
40 CHECK_NOTHROW(test_narrow(int32_t( 100), int8_t( 100)));
41 CHECK_NOTHROW(test_narrow(int32_t( 127), int8_t( 127)));
42
43 CHECK_THROWS (test_narrow(int32_t(-999), int8_t(ERR)));
44 CHECK_THROWS (test_narrow(int32_t(-456), int8_t(ERR)));
45 CHECK_THROWS (test_narrow(int32_t(-129), int8_t(ERR)));
46 CHECK_THROWS (test_narrow(int32_t( 128), int8_t(ERR)));
47 CHECK_THROWS (test_narrow(int32_t( 356), int8_t(ERR)));
48 CHECK_THROWS (test_narrow(int32_t(9999), int8_t(ERR)));
49 }
50 SECTION("uint32_t -> uint8_t") {
51 CHECK_NOTHROW(test_narrow(uint32_t( 0), uint8_t( 0)));
52 CHECK_NOTHROW(test_narrow(uint32_t( 100), uint8_t(100)));
53 CHECK_NOTHROW(test_narrow(uint32_t( 200), uint8_t(200)));
54 CHECK_NOTHROW(test_narrow(uint32_t( 255), uint8_t(255)));
55
56 CHECK_THROWS (test_narrow(uint32_t( 256), uint8_t(ERR)));
57 CHECK_THROWS (test_narrow(uint32_t( 300), uint8_t(ERR)));
58 CHECK_THROWS (test_narrow(uint32_t(9999), uint8_t(ERR)));
59 }
60 SECTION("uint32_t -> int8_t") {
61 CHECK_NOTHROW(test_narrow(uint32_t( 0), int8_t( 0)));
62 CHECK_NOTHROW(test_narrow(uint32_t( 7), int8_t( 7)));
63 CHECK_NOTHROW(test_narrow(uint32_t( 100), int8_t( 100)));
64 CHECK_NOTHROW(test_narrow(uint32_t( 127), int8_t( 127)));
65
66 CHECK_THROWS (test_narrow(uint32_t( 128), int8_t(ERR)));
67 CHECK_THROWS (test_narrow(uint32_t( 356), int8_t(ERR)));
68 CHECK_THROWS (test_narrow(uint32_t(9999), int8_t(ERR)));
69 }
70}
CHECK(m3==m3)
TEST_CASE("narrow")
bool test_narrow(From from, To expected)
Definition narrow_test.cc:7