-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathnarrow_cast_tests.cpp
More file actions
107 lines (91 loc) · 3.15 KB
/
narrow_cast_tests.cpp
File metadata and controls
107 lines (91 loc) · 3.15 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2022 Christian Mazakas
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/unordered/detail/narrow_cast.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/cstdint.hpp>
// want to prove that for the wider type, the higher bits of the value
// represenation don't affect the results of the narrowing, which in this case
// is masking out the high bits when comapred to the narrow type
static void signed_integral_narrowing()
{
// test positive range, fits
// [0, 127]
for (boost::int32_t i = 0x00; i < 0x80; ++i) {
boost::int8_t k = (boost::int8_t)i;
BOOST_TEST_GE(k, 0);
BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(i), k);
}
// test positive range, doesn't fit
// [0xff00, 0xff7f]
for (boost::int32_t i = 0x00; i < 0x80; ++i) {
boost::int32_t j = i + 0xff00;
boost::int8_t k = (boost::int8_t)i;
BOOST_TEST_GE(k, 0);
BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(j), k);
}
// test negative range, fits
// [-128, -1]
for (boost::int32_t i = 0x00; i < 0x80; ++i) {
boost::int32_t j = i + (boost::int32_t)0xffffff80;
boost::int8_t k = (boost::int8_t)j;
BOOST_TEST_LT(j, 0);
BOOST_TEST_LT(k, 0);
BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(j), k);
}
// test negative range, doesn't fit
for (boost::int32_t i = 0x00; i < 0x80; ++i) {
boost::int32_t j = i + (boost::int32_t)0x80000000;
boost::int8_t k = (boost::int8_t)(i);
BOOST_TEST_LT(j, 0);
BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(j), k);
}
for (boost::int32_t i = 0x00; i < 0x100; ++i) {
boost::int32_t j = (boost::int32_t)0x80ff0000 + i;
BOOST_TEST_LT(j, 0);
BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(j),
(boost::int8_t)i);
}
// test special values
{
boost::int32_t x = 0xff;
BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(x), -1);
}
{
boost::int32_t x = (boost::int32_t)0xffffff00;
BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(x),
(boost::int8_t)0x00);
}
{
boost::int32_t x = (boost::int32_t)0xffffff7f;
BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(x),
(boost::int8_t)0x7f);
}
{
boost::int32_t x = (boost::int32_t)0xffffffff;
BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::int8_t>(x),
(boost::int8_t)-1);
}
}
static void unsigned_integral_narrowing()
{
// test range: [0x00, 0xff]
for (boost::uint32_t i = 0x00; i < 0x100; ++i) {
BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::uint8_t>(i),
(boost::uint8_t)(i & 0xff));
}
// test range: [0xffffff00, 0xffffffff]
boost::uint32_t i = 0xffffff00;
for (; i < 0xffffffff; ++i) {
BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::uint8_t>(i),
(boost::uint8_t)(i & 0xff));
}
BOOST_TEST_EQ(boost::unordered::detail::narrow_cast<boost::uint8_t>(i),
(boost::uint8_t)(i & 0xff));
}
int main()
{
signed_integral_narrowing();
unsigned_integral_narrowing();
return boost::report_errors();
}