generated from saxbophone/CPP20-Cross-Platform-Template
/
casting.cpp
94 lines (71 loc) · 2.91 KB
/
casting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <cmath>
#include <limits>
#include <stdexcept>
#include <catch2/catch.hpp>
#include <arby/arby.hpp>
using namespace com::saxbophone;
TEST_CASE("Casting arby::Uint to uintmax_t", "[casting]") {
auto value = GENERATE(take(1000, random((uintmax_t)0, std::numeric_limits<uintmax_t>::max())));
CHECK((uintmax_t)arby::Uint(value) == value);
}
TEST_CASE("Casting arby::Uint with value higher than UINT_MAX to uintmax_t throws range_error", "[casting]") {
arby::Uint value = arby::Uint(std::numeric_limits<uintmax_t>::max()) + 1;
CHECK_THROWS_AS((uintmax_t)value, std::range_error);
}
TEST_CASE("Casting arby::Uint to long double", "[casting]") {
auto value = GENERATE(take(1000, random((uintmax_t)0, std::numeric_limits<uintmax_t>::max())));
CHECK((long double)arby::Uint(value) == (long double)value);
}
TEST_CASE("arby::Uint::from_float() with negative value throws std::domain_error") {
auto value = GENERATE(
take(1000,
random(
std::numeric_limits<long double>::lowest(),
-std::numeric_limits<long double>::denorm_min()
)
)
);
CHECK_THROWS_AS(arby::Uint::from_float(value), std::domain_error);
}
TEST_CASE("arby::Uint::from_float() with non-finite value throws std::domain_error") {
// need IEC 559 float to be sure that qNaN, sNan, ±Inf all exist
if (std::numeric_limits<long double>::is_iec559) {
auto value = GENERATE(
-std::numeric_limits<long double>::infinity(),
+std::numeric_limits<long double>::infinity(),
std::numeric_limits<long double>::quiet_NaN(),
std::numeric_limits<long double>::signaling_NaN()
);
CHECK_THROWS_AS(arby::Uint::from_float(value), std::domain_error);
} else {
WARN("Can't test for non-finite values because long double isn't IEC 559!");
}
}
TEST_CASE("arby::Uint::from_float() with positive value") {
auto power = GENERATE(0.125, 0.25, 0.5, 1, 2, 4, 8);
auto value = GENERATE_COPY(
take(100,
random(
0.0L,
std::pow((long double)std::numeric_limits<uintmax_t>::max(), power)
)
)
);
arby::Uint object = arby::Uint::from_float(value);
// value should be correct
CHECK((long double)object == Approx(std::trunc(value)));
}
TEST_CASE("arby::Uint can be constructed from unsigned types smaller than uintmax_t - uint8_t",) {
std::uint8_t input = GENERATE(take(1000, random((std::uint16_t)0x00, (std::uint16_t)0xFF)));
arby::Uint object = input;
CHECK((uintmax_t)object == input);
}
TEMPLATE_TEST_CASE(
"arby::Uint can be constructed from unsigned types smaller than uintmax_t",
"",
std::uint16_t, std::uint32_t, std::uint64_t
) {
TestType input = GENERATE(take(1000, random((TestType)0, std::numeric_limits<TestType>::max())));
arby::Uint object = input;
CHECK((uintmax_t)object == input);
}