Main Page
Related Pages
Modules
Namespaces
Namespace List
Namespace Members
All
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Functions
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Variables
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Typedefs
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
r
s
t
u
v
w
y
z
Enumerations
a
b
c
d
e
f
g
h
i
j
l
m
n
o
p
r
s
t
u
v
w
Enumerator
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
r
s
t
u
z
Classes
Class List
Class Hierarchy
Class Members
All
:
[
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Functions
[
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Variables
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Typedefs
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
r
s
t
u
v
w
y
Enumerations
a
b
c
d
e
f
h
i
k
l
m
n
o
p
r
s
t
u
v
w
Enumerator
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
z
Properties
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
r
s
t
u
v
w
Related Functions
:
a
b
c
d
e
f
g
h
i
j
l
m
n
o
p
q
r
s
t
u
v
w
z
Files
File List
File Members
All
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Functions
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
z
Variables
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Typedefs
_
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
x
z
Enumerations
_
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
r
s
t
u
v
w
x
Enumerator
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
r
s
t
u
v
w
x
Macros
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
grpc
third_party
boringssl-with-bazel
src
crypto
compiler_test.cc
Go to the documentation of this file.
1
/* Copyright (c) 2017, Google Inc.
2
*
3
* Permission to use, copy, modify, and/or distribute this software for any
4
* purpose with or without fee is hereby granted, provided that the above
5
* copyright notice and this permission notice appear in all copies.
6
*
7
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15
#include <limits.h>
16
#include <
stdint.h
>
17
18
#include <type_traits>
19
20
#include <gtest/gtest.h>
21
22
#include "
test/test_util.h
"
23
24
25
template
<
typename
T>
26
static
void
CheckRepresentation
(
T
value
) {
27
SCOPED_TRACE
(
value
);
28
29
// Convert to the corresponding two's-complement unsigned value. We use an
30
// unsigned value so the right-shift below has defined value. Right-shifts of
31
// negative numbers in C are implementation defined.
32
//
33
// If |T| is already unsigned, this is a no-op, as desired.
34
//
35
// If |T| is signed, conversion to unsigned is defined to repeatedly add or
36
// subtract (numerically, not within |T|) one more than the unsigned type's
37
// maximum value until it fits (this must be a power of two). This is the
38
// conversion we want.
39
using
UnsignedT =
typename
std::make_unsigned<T>::type
;
40
UnsignedT value_u =
static_cast<
UnsignedT
>
(
value
);
41
EXPECT_EQ
(
sizeof
(UnsignedT),
sizeof
(
T
));
42
43
// Integers must be little-endian.
44
uint8_t
expected[
sizeof
(UnsignedT)];
45
for
(
size_t
i
= 0;
i
<
sizeof
(UnsignedT);
i
++) {
46
expected[
i
] =
static_cast<
uint8_t
>
(value_u);
47
// Divide instead of right-shift to appease compilers that warn if |T| is a
48
// char. The explicit cast is also needed to appease MSVC if integer
49
// promotion happened.
50
value_u =
static_cast<
UnsignedT
>
(value_u / 256);
51
}
52
EXPECT_EQ
(0
u
, value_u);
53
54
// Check that |value| has the expected representation.
55
EXPECT_EQ
(
Bytes
(expected),
56
Bytes
(
reinterpret_cast<
const
uint8_t
*
>
(&
value
),
sizeof
(
value
)));
57
}
58
59
TEST
(CompilerTest, IntegerRepresentation) {
60
EXPECT_EQ
(8, CHAR_BIT);
61
EXPECT_EQ
(0xff,
static_cast<
int
>
(UCHAR_MAX));
62
63
// uint8_t is assumed to be unsigned char. I.e., casting to uint8_t should be
64
// as good as unsigned char for strict aliasing purposes.
65
uint8_t
u8 = 0;
66
unsigned
char
*
ptr
= &u8;
67
(void)
ptr
;
68
69
// Sized integers have the expected size.
70
EXPECT_EQ
(1
u
,
sizeof
(
uint8_t
));
71
EXPECT_EQ
(2
u
,
sizeof
(
uint16_t
));
72
EXPECT_EQ
(4
u
,
sizeof
(
uint32_t
));
73
EXPECT_EQ
(8
u
,
sizeof
(
uint64_t
));
74
75
// size_t does not exceed uint64_t.
76
EXPECT_LE
(
sizeof
(
size_t
), 8
u
);
77
78
// int must be 32-bit or larger.
79
EXPECT_LE
(0x7fffffff, INT_MAX);
80
EXPECT_LE
(0xffffffffu, UINT_MAX);
81
82
CheckRepresentation
(
static_cast<
signed
char
>
(127));
83
CheckRepresentation
(
static_cast<
signed
char
>
(1));
84
CheckRepresentation
(
static_cast<
signed
char
>
(0));
85
CheckRepresentation
(
static_cast<
signed
char
>
(-1));
86
CheckRepresentation
(
static_cast<
signed
char
>
(-42));
87
CheckRepresentation
(
static_cast<
signed
char
>
(-128));
88
89
CheckRepresentation
(
static_cast<
int
>
(INT_MAX));
90
CheckRepresentation
(
static_cast<
int
>
(0x12345678));
91
CheckRepresentation
(
static_cast<
int
>
(1));
92
CheckRepresentation
(
static_cast<
int
>
(0));
93
CheckRepresentation
(
static_cast<
int
>
(-1));
94
CheckRepresentation
(
static_cast<
int
>
(-0x12345678));
95
CheckRepresentation
(
static_cast<
int
>
(INT_MIN));
96
97
CheckRepresentation
(
static_cast<
unsigned
>
(UINT_MAX));
98
CheckRepresentation
(
static_cast<
unsigned
>
(0x12345678));
99
CheckRepresentation
(
static_cast<
unsigned
>
(1));
100
CheckRepresentation
(
static_cast<
unsigned
>
(0));
101
102
CheckRepresentation
(
static_cast<
long
>
(LONG_MAX));
103
CheckRepresentation
(
static_cast<
long
>
(0x12345678));
104
CheckRepresentation
(
static_cast<
long
>
(1));
105
CheckRepresentation
(
static_cast<
long
>
(0));
106
CheckRepresentation
(
static_cast<
long
>
(-1));
107
CheckRepresentation
(
static_cast<
long
>
(-0x12345678));
108
CheckRepresentation
(
static_cast<
long
>
(LONG_MIN));
109
110
CheckRepresentation
(
static_cast<
unsigned
long
>
(ULONG_MAX));
111
CheckRepresentation
(
static_cast<
unsigned
long
>
(0x12345678));
112
CheckRepresentation
(
static_cast<
unsigned
long
>
(1));
113
CheckRepresentation
(
static_cast<
unsigned
long
>
(0));
114
115
CheckRepresentation
(
static_cast<
int16_t
>
(0x7fff));
116
CheckRepresentation
(
static_cast<
int16_t
>
(0x1234));
117
CheckRepresentation
(
static_cast<
int16_t
>
(1));
118
CheckRepresentation
(
static_cast<
int16_t
>
(0));
119
CheckRepresentation
(
static_cast<
int16_t
>
(-1));
120
CheckRepresentation
(
static_cast<
int16_t
>
(-0x7fff - 1));
121
122
CheckRepresentation
(
static_cast<
uint16_t
>
(0xffff));
123
CheckRepresentation
(
static_cast<
uint16_t
>
(0x1234));
124
CheckRepresentation
(
static_cast<
uint16_t
>
(1));
125
CheckRepresentation
(
static_cast<
uint16_t
>
(0));
126
127
CheckRepresentation
(
static_cast<
int32_t
>
(0x7fffffff));
128
CheckRepresentation
(
static_cast<
int32_t
>
(0x12345678));
129
CheckRepresentation
(
static_cast<
int32_t
>
(1));
130
CheckRepresentation
(
static_cast<
int32_t
>
(0));
131
CheckRepresentation
(
static_cast<
int32_t
>
(-1));
132
CheckRepresentation
(
static_cast<
int32_t
>
(-0x7fffffff - 1));
133
134
CheckRepresentation
(
static_cast<
uint32_t
>
(0xffffffff));
135
CheckRepresentation
(
static_cast<
uint32_t
>
(0x12345678));
136
CheckRepresentation
(
static_cast<
uint32_t
>
(1));
137
CheckRepresentation
(
static_cast<
uint32_t
>
(0));
138
139
CheckRepresentation
(
static_cast<
int64_t
>
(0x7fffffffffffffff));
140
CheckRepresentation
(
static_cast<
int64_t
>
(0x123456789abcdef0));
141
CheckRepresentation
(
static_cast<
int64_t
>
(1));
142
CheckRepresentation
(
static_cast<
int64_t
>
(0));
143
CheckRepresentation
(
static_cast<
int64_t
>
(-1));
144
CheckRepresentation
(
static_cast<
int64_t
>
(-0x7fffffffffffffff - 1));
145
146
CheckRepresentation
(
static_cast<
uint64_t
>
(0xffffffffffffffff));
147
CheckRepresentation
(
static_cast<
uint64_t
>
(0x12345678abcdef0));
148
CheckRepresentation
(
static_cast<
uint64_t
>
(1));
149
CheckRepresentation
(
static_cast<
uint64_t
>
(0));
150
}
151
152
TEST
(CompilerTest, PointerRepresentation) {
153
// Converting pointers to integers and doing arithmetic on those values are
154
// both defined. Converting those values back into pointers is undefined,
155
// but, for aliasing checks, we require that the implementation-defined
156
// result of that computation commutes with pointer arithmetic.
157
char
chars[256];
158
for
(
size_t
i
= 0;
i
<
sizeof
(chars);
i
++) {
159
EXPECT_EQ
(
reinterpret_cast<
uintptr_t
>
(chars) +
i
,
160
reinterpret_cast<
uintptr_t
>
(chars +
i
));
161
}
162
163
int
ints[256];
164
for
(
size_t
i
= 0;
i
<
OPENSSL_ARRAY_SIZE
(ints);
i
++) {
165
EXPECT_EQ
(
reinterpret_cast<
uintptr_t
>
(ints) +
i
*
sizeof
(
int
),
166
reinterpret_cast<
uintptr_t
>
(ints +
i
));
167
}
168
169
// nullptr must be represented by all zeros in memory. This is necessary so
170
// structs may be initialized by memset(0).
171
int
*
null
=
nullptr
;
172
uint8_t
bytes
[
sizeof
(
null
)] = {0};
173
EXPECT_EQ
(
Bytes
(
bytes
),
174
Bytes
(
reinterpret_cast<
uint8_t
*
>
(&
null
),
sizeof
(
null
)));
175
}
ptr
char * ptr
Definition:
abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
Bytes
Definition:
boringssl-with-bazel/src/crypto/test/test_util.h:38
uint16_t
unsigned short uint16_t
Definition:
stdint-msvc2008.h:79
OPENSSL_ARRAY_SIZE
#define OPENSSL_ARRAY_SIZE(array)
Definition:
third_party/boringssl-with-bazel/src/crypto/internal.h:179
u
OPENSSL_EXPORT pem_password_cb void * u
Definition:
pem.h:351
CheckRepresentation
static void CheckRepresentation(T value)
Definition:
compiler_test.cc:26
TEST
TEST(CompilerTest, IntegerRepresentation)
Definition:
compiler_test.cc:59
EXPECT_LE
#define EXPECT_LE(val1, val2)
Definition:
bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2030
uint8_t
unsigned char uint8_t
Definition:
stdint-msvc2008.h:78
T
#define T(upbtypeconst, upbtype, ctype, default_value)
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition:
iomgr/time_averaged_stats_test.cc:27
uint32_t
unsigned int uint32_t
Definition:
stdint-msvc2008.h:80
SCOPED_TRACE
#define SCOPED_TRACE(message)
Definition:
bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2264
int16_t
signed short int16_t
Definition:
stdint-msvc2008.h:76
int64_t
signed __int64 int64_t
Definition:
stdint-msvc2008.h:89
uint64_t
unsigned __int64 uint64_t
Definition:
stdint-msvc2008.h:90
uintptr_t
_W64 unsigned int uintptr_t
Definition:
stdint-msvc2008.h:119
stdint.h
value
const char * value
Definition:
hpack_parser_table.cc:165
bytes
uint8 bytes[10]
Definition:
bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
test_util.h
asyncio_get_stats.type
type
Definition:
asyncio_get_stats.py:37
int32_t
signed int int32_t
Definition:
stdint-msvc2008.h:77
i
uint64_t i
Definition:
abseil-cpp/absl/container/btree_benchmark.cc:230
grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:52