Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
yangql
googletest
Commits
16f637fb
Commit
16f637fb
authored
Oct 11, 2021
by
Abseil Team
Committed by
Andy Soffer
Oct 13, 2021
Browse files
Googletest export
Add printer for __{u,}int128_t. PiperOrigin-RevId: 402417369
parent
178cfacb
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
0 deletions
+69
-0
googletest/include/gtest/gtest-printers.h
googletest/include/gtest/gtest-printers.h
+6
-0
googletest/src/gtest-printers.cc
googletest/src/gtest-printers.cc
+45
-0
googletest/test/googletest-printers-test.cc
googletest/test/googletest-printers-test.cc
+18
-0
No files found.
googletest/include/gtest/gtest-printers.h
View file @
16f637fb
...
...
@@ -477,6 +477,12 @@ inline void PrintTo(char8_t c, ::std::ostream* os) {
}
#endif
// gcc/clang __{u,}int128_t
#if defined(__SIZEOF_INT128__)
GTEST_API_
void
PrintTo
(
__uint128_t
v
,
::
std
::
ostream
*
os
);
GTEST_API_
void
PrintTo
(
__int128_t
v
,
::
std
::
ostream
*
os
);
#endif // __SIZEOF_INT128__
// Overloads for C strings.
GTEST_API_
void
PrintTo
(
const
char
*
s
,
::
std
::
ostream
*
os
);
inline
void
PrintTo
(
char
*
s
,
::
std
::
ostream
*
os
)
{
...
...
googletest/src/gtest-printers.cc
View file @
16f637fb
...
...
@@ -304,6 +304,51 @@ void PrintTo(char32_t c, ::std::ostream* os) {
<<
static_cast
<
uint32_t
>
(
c
);
}
// gcc/clang __{u,}int128_t
#if defined(__SIZEOF_INT128__)
void
PrintTo
(
__uint128_t
v
,
::
std
::
ostream
*
os
)
{
if
(
v
==
0
)
{
*
os
<<
"0"
;
return
;
}
// Buffer large enough for ceil(log10(2^128))==39 and the null terminator
char
buf
[
40
];
char
*
p
=
buf
+
sizeof
(
buf
);
// Some configurations have a __uint128_t, but no support for built in
// division. Do manual long division instead.
uint64_t
high
=
static_cast
<
uint64_t
>
(
v
>>
64
);
uint64_t
low
=
static_cast
<
uint64_t
>
(
v
);
*--
p
=
0
;
while
(
high
!=
0
||
low
!=
0
)
{
uint64_t
high_mod
=
high
%
10
;
high
=
high
/
10
;
// This is the long division algorithm specialized for a divisor of 10 and
// only two elements.
// Notable values:
// 2^64 / 10 == 1844674407370955161
// 2^64 % 10 == 6
const
uint64_t
carry
=
6
*
high_mod
+
low
%
10
;
low
=
low
/
10
+
high_mod
*
1844674407370955161
+
carry
/
10
;
char
digit
=
static_cast
<
char
>
(
carry
%
10
);
*--
p
=
'0'
+
digit
;
}
*
os
<<
p
;
}
void
PrintTo
(
__int128_t
v
,
::
std
::
ostream
*
os
)
{
__uint128_t
uv
=
static_cast
<
__uint128_t
>
(
v
);
if
(
v
<
0
)
{
*
os
<<
"-"
;
uv
=
-
uv
;
}
PrintTo
(
uv
,
os
);
}
#endif // __SIZEOF_INT128__
// Prints the given array of characters to the ostream. CharType must be either
// char, char8_t, char16_t, char32_t, or wchar_t.
// The array starts at begin, the length is len, it may include '\0' characters
...
...
googletest/test/googletest-printers-test.cc
View file @
16f637fb
...
...
@@ -449,6 +449,24 @@ TEST(PrintBuiltInTypeTest, Size_t) {
#endif // !GTEST_OS_WINDOWS
}
// gcc/clang __{u,}int128_t values.
#if defined(__SIZEOF_INT128__)
TEST
(
PrintBuiltInTypeTest
,
Int128
)
{
// Small ones
EXPECT_EQ
(
"0"
,
Print
(
__int128_t
{
0
}));
EXPECT_EQ
(
"0"
,
Print
(
__uint128_t
{
0
}));
EXPECT_EQ
(
"12345"
,
Print
(
__int128_t
{
12345
}));
EXPECT_EQ
(
"12345"
,
Print
(
__uint128_t
{
12345
}));
EXPECT_EQ
(
"-12345"
,
Print
(
__int128_t
{
-
12345
}));
// Large ones
EXPECT_EQ
(
"340282366920938463463374607431768211455"
,
Print
(
~
__uint128_t
{}));
__int128_t
max_128
=
static_cast
<
__int128_t
>
(
~
__uint128_t
{}
/
2
);
EXPECT_EQ
(
"-170141183460469231731687303715884105728"
,
Print
(
~
max_128
));
EXPECT_EQ
(
"170141183460469231731687303715884105727"
,
Print
(
max_128
));
}
#endif // __SIZEOF_INT128__
// Floating-points.
TEST
(
PrintBuiltInTypeTest
,
FloatingPoints
)
{
EXPECT_EQ
(
"1.5"
,
Print
(
1.5
f
));
// float
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment