-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
84 lines (73 loc) · 3.28 KB
/
test.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
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <iostream>
#include "clamp-cast.hpp"
template <typename To, typename From> bool test_case(From from, To expected) {
const auto result = clamp_cast::clamp_cast<To, From>(from);
if (result != expected) {
// there is a `+` in front of expected to prevent cout from treating uint8
// as char and not printing it as a decimal number.
std::cout << "clamp_cast(" << from << ") == " << +result
<< " != " << +expected << "\n";
return false;
}
return true;
}
bool test() {
// works as constexpr
static_assert(clamp_cast::clamp_cast<uint8_t, float>(0.0) == 0);
bool success{true};
success &= test_case<uint8_t, float>(NAN, 0);
success &= test_case<uint8_t, float>(0.0, 0);
success &= test_case<uint8_t, float>(1.0, 1);
success &= test_case<uint8_t, float>(-1.0, 0);
success &= test_case<uint8_t, float>(254.0, 254);
success &= test_case<uint8_t, float>(255.0, 255);
success &= test_case<uint8_t, float>(256.0, 255);
success &= test_case<uint8_t, float>(FLT_MAX, 255);
success &= test_case<uint8_t, float>(-FLT_MAX, 0);
success &= test_case<int8_t, float>(NAN, 0);
success &= test_case<int8_t, float>(0.0, 0);
success &= test_case<int8_t, float>(1.0, 1);
success &= test_case<int8_t, float>(-1.0, -1);
success &= test_case<int8_t, float>(126.0, 126);
success &= test_case<int8_t, float>(127.0, 127);
success &= test_case<int8_t, float>(128.0, 127);
success &= test_case<int8_t, float>(-127.0, -127);
success &= test_case<int8_t, float>(-128.0, -128);
success &= test_case<int8_t, float>(-129.0, -128);
success &= test_case<int8_t, float>(FLT_MAX, 127);
success &= test_case<int8_t, float>(-FLT_MAX, -128);
const float bound{std::exp2f(63.0)};
const double bound_d{std::exp2(63.0)};
// `- 1` because minimum int64_t cannot be written as integer literal
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661 .
success &= test_case<int64_t, float>(std::nextafter(-bound, -INFINITY),
-9223372036854775807ll - 1);
success &= test_case<int64_t, float>(-bound, -9223372036854775807ll - 1);
success &= test_case<int64_t, float>(std::nextafter(-bound, INFINITY),
-9223371487098961920ll);
// double has more precision so the result is closer to -bound.
success &= test_case<int64_t, double>(std::nextafter(-bound_d, INFINITY),
-9223372036854774784ll);
success &= test_case<int64_t, float>(std::nextafter(bound, -INFINITY),
9223371487098961920ll);
success &= test_case<int64_t, double>(std::nextafter(bound_d, -INFINITY),
9223372036854774784ll);
success &= test_case<int64_t, float>(bound, 9223372036854775807ll);
success &= test_case<int64_t, float>(std::nextafter(bound, INFINITY),
9223372036854775807ll);
// We can't test the uncommon case mentioned in the comment in clamp_cast
// because we would need a uint128_t which does not exist. Some compilers have
// a __uint128_t but it does not implement std::numeric_limits.
return success;
}
int main() {
if (test()) {
std::cout << "no errors\n";
return 0;
} else {
return 1;
}
}