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
8697709e
Commit
8697709e
authored
Nov 04, 2019
by
Xiaoyi Zhang
Browse files
Merge pull request #2453 from kuzkry:gtest-port-clean-up_kMaxBiggestInt
PiperOrigin-RevId: 278008286
parents
e8a82dc7
6e87238c
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
89 additions
and
70 deletions
+89
-70
googletest/include/gtest/gtest-printers.h
googletest/include/gtest/gtest-printers.h
+18
-17
googletest/include/gtest/gtest.h
googletest/include/gtest/gtest.h
+26
-22
googletest/include/gtest/internal/gtest-port.h
googletest/include/gtest/internal/gtest-port.h
+14
-0
googletest/src/gtest.cc
googletest/src/gtest.cc
+16
-15
googletest/test/googletest-printers-test.cc
googletest/test/googletest-printers-test.cc
+12
-13
googletest/test/gtest_unittest.cc
googletest/test/gtest_unittest.cc
+3
-3
No files found.
googletest/include/gtest/gtest-printers.h
View file @
8697709e
...
@@ -133,7 +133,7 @@ GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
...
@@ -133,7 +133,7 @@ GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
// nor PrintTo().
// nor PrintTo().
enum
TypeKind
{
enum
TypeKind
{
kProtobuf
,
// a protobuf type
kProtobuf
,
// a protobuf type
kConvertibleToInteger
,
// a type implicitly convertible to
std::intmax_
t
kConvertibleToInteger
,
// a type implicitly convertible to
BiggestIn
t
// (e.g. a named or unnamed enum type)
// (e.g. a named or unnamed enum type)
#if GTEST_HAS_ABSL
#if GTEST_HAS_ABSL
kConvertibleToStringView
,
// a type implicitly convertible to
kConvertibleToStringView
,
// a type implicitly convertible to
...
@@ -179,14 +179,14 @@ template <typename T>
...
@@ -179,14 +179,14 @@ template <typename T>
class
TypeWithoutFormatter
<
T
,
kConvertibleToInteger
>
{
class
TypeWithoutFormatter
<
T
,
kConvertibleToInteger
>
{
public:
public:
// Since T has no << operator or PrintTo() but can be implicitly
// Since T has no << operator or PrintTo() but can be implicitly
// converted to
the maximum width integer
, we print it as a
std::intmax_
t.
// converted to
BiggestInt
, we print it as a
BiggestIn
t.
//
//
// Most likely T is an enum type (either named or unnamed), in which
// Most likely T is an enum type (either named or unnamed), in which
// case printing it as an integer is the desired behavior. In case
// case printing it as an integer is the desired behavior.
In case
// T is not an enum, printing it as an integer is the best we can do
// T is not an enum, printing it as an integer is the best we can do
// given that it has no user-defined printer.
// given that it has no user-defined printer.
static
void
PrintValue
(
const
T
&
value
,
::
std
::
ostream
*
os
)
{
static
void
PrintValue
(
const
T
&
value
,
::
std
::
ostream
*
os
)
{
const
std
::
intmax_
t
kBigInt
=
value
;
const
internal
::
BiggestIn
t
kBigInt
=
value
;
*
os
<<
kBigInt
;
*
os
<<
kBigInt
;
}
}
};
};
...
@@ -204,10 +204,10 @@ class TypeWithoutFormatter<T, kConvertibleToStringView> {
...
@@ -204,10 +204,10 @@ class TypeWithoutFormatter<T, kConvertibleToStringView> {
};
};
#endif
#endif
// Prints the given value to the given ostream. If the value is a
// Prints the given value to the given ostream.
If the value is a
// protocol message, its debug string is printed; if it's an enum or
// protocol message, its debug string is printed; if it's an enum or
// of a type implicitly convertible to
std::intmax_
t, it's printed as an
// of a type implicitly convertible to
BiggestIn
t, it's printed as an
// integer; otherwise the bytes in the value are printed. This is
// integer; otherwise the bytes in the value are printed.
This is
// what UniversalPrinter<T>::Print() does when it knows nothing about
// what UniversalPrinter<T>::Print() does when it knows nothing about
// type T and T has neither << operator nor PrintTo().
// type T and T has neither << operator nor PrintTo().
//
//
...
@@ -231,18 +231,19 @@ class TypeWithoutFormatter<T, kConvertibleToStringView> {
...
@@ -231,18 +231,19 @@ class TypeWithoutFormatter<T, kConvertibleToStringView> {
template
<
typename
Char
,
typename
CharTraits
,
typename
T
>
template
<
typename
Char
,
typename
CharTraits
,
typename
T
>
::
std
::
basic_ostream
<
Char
,
CharTraits
>&
operator
<<
(
::
std
::
basic_ostream
<
Char
,
CharTraits
>&
operator
<<
(
::
std
::
basic_ostream
<
Char
,
CharTraits
>&
os
,
const
T
&
x
)
{
::
std
::
basic_ostream
<
Char
,
CharTraits
>&
os
,
const
T
&
x
)
{
TypeWithoutFormatter
<
TypeWithoutFormatter
<
T
,
(
internal
::
IsAProtocolMessage
<
T
>::
value
T
,
(
internal
::
IsAProtocolMessage
<
T
>::
value
?
kProtobuf
?
kProtobuf
:
std
::
is_convertible
<
:
std
::
is_convertible
<
const
T
&
,
std
::
intmax_
t
>::
value
const
T
&
,
internal
::
BiggestIn
t
>::
value
?
kConvertibleToInteger
?
kConvertibleToInteger
:
:
#if GTEST_HAS_ABSL
#if GTEST_HAS_ABSL
std
::
is_convertible
<
const
T
&
,
absl
::
string_view
>::
value
std
::
is_convertible
<
?
kConvertibleToStringView
const
T
&
,
absl
::
string_view
>::
value
:
?
kConvertibleToStringView
:
#endif
#endif
kOtherType
)
>::
PrintValue
(
x
,
&
os
);
kOtherType
)
>::
PrintValue
(
x
,
&
os
);
return
os
;
return
os
;
}
}
...
...
googletest/include/gtest/gtest.h
View file @
8697709e
...
@@ -1531,11 +1531,13 @@ AssertionResult CmpHelperEQ(const char* lhs_expression,
...
@@ -1531,11 +1531,13 @@ AssertionResult CmpHelperEQ(const char* lhs_expression,
return
CmpHelperEQFailure
(
lhs_expression
,
rhs_expression
,
lhs
,
rhs
);
return
CmpHelperEQFailure
(
lhs_expression
,
rhs_expression
,
lhs
,
rhs
);
}
}
// With this overloaded version, we allow anonymous enums to be used in
// With this overloaded version, we allow anonymous enums to be used
// {ASSERT|EXPECT}_EQ as anonymous enums can be implicitly cast to integers.
// in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums
// can be implicitly cast to BiggestInt.
GTEST_API_
AssertionResult
CmpHelperEQ
(
const
char
*
lhs_expression
,
GTEST_API_
AssertionResult
CmpHelperEQ
(
const
char
*
lhs_expression
,
const
char
*
rhs_expression
,
const
char
*
rhs_expression
,
std
::
intmax_t
lhs
,
std
::
intmax_t
rhs
);
BiggestInt
lhs
,
BiggestInt
rhs
);
class
EqHelper
{
class
EqHelper
{
public:
public:
...
@@ -1552,14 +1554,16 @@ class EqHelper {
...
@@ -1552,14 +1554,16 @@ class EqHelper {
return
CmpHelperEQ
(
lhs_expression
,
rhs_expression
,
lhs
,
rhs
);
return
CmpHelperEQ
(
lhs_expression
,
rhs_expression
,
lhs
,
rhs
);
}
}
// With this overloaded version, we allow anonymous enums to be used in
// With this overloaded version, we allow anonymous enums to be used
// {ASSERT|EXPECT}_EQ as anonymous enums can be implicitly cast to integers.
// in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous
// enums can be implicitly cast to BiggestInt.
//
//
// Even though its body looks the same as the above version, we
// Even though its body looks the same as the above version, we
// cannot merge the two, as it will make anonymous enums unhappy.
// cannot merge the two, as it will make anonymous enums unhappy.
static
AssertionResult
Compare
(
const
char
*
lhs_expression
,
static
AssertionResult
Compare
(
const
char
*
lhs_expression
,
const
char
*
rhs_expression
,
std
::
intmax_t
lhs
,
const
char
*
rhs_expression
,
std
::
intmax_t
rhs
)
{
BiggestInt
lhs
,
BiggestInt
rhs
)
{
return
CmpHelperEQ
(
lhs_expression
,
rhs_expression
,
lhs
,
rhs
);
return
CmpHelperEQ
(
lhs_expression
,
rhs_expression
,
lhs
,
rhs
);
}
}
...
@@ -1592,24 +1596,24 @@ AssertionResult CmpHelperOpFailure(const char* expr1, const char* expr2,
...
@@ -1592,24 +1596,24 @@ AssertionResult CmpHelperOpFailure(const char* expr1, const char* expr2,
// of similar code.
// of similar code.
//
//
// For each templatized helper function, we also define an overloaded
// For each templatized helper function, we also define an overloaded
// version for std::intmax_t in order to reduce code bloat and allow
// version for BiggestInt in order to reduce code bloat and allow
// anonymous enums to be used with {ASSERT|EXPECT}_??.
// anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled
// with gcc 4.
//
//
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
#define GTEST_IMPL_CMP_HELPER_(op_name, op) \
#define GTEST_IMPL_CMP_HELPER_(op_name, op)\
template <typename T1, typename T2> \
template <typename T1, typename T2>\
AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
const T1& val1, const T2& val2) { \
const T1& val1, const T2& val2) {\
if (val1 op val2) { \
if (val1 op val2) {\
return AssertionSuccess(); \
return AssertionSuccess();\
} else { \
} else {\
return CmpHelperOpFailure(expr1, expr2, val1, val2, #op); \
return CmpHelperOpFailure(expr1, expr2, val1, val2, #op);\
} \
}\
} \
}\
GTEST_API_ AssertionResult CmpHelper##op_name( \
GTEST_API_ AssertionResult CmpHelper##op_name(\
const char* expr1, const char* expr2, std::intmax_t val1, \
const char* expr1, const char* expr2, BiggestInt val1, BiggestInt val2)
std::intmax_t val2)
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
...
...
googletest/include/gtest/internal/gtest-port.h
View file @
8697709e
...
@@ -225,6 +225,7 @@
...
@@ -225,6 +225,7 @@
// TypeWithSize - maps an integer to a int type.
// TypeWithSize - maps an integer to a int type.
// Int32, UInt32, Int64, UInt64, TimeInMillis
// Int32, UInt32, Int64, UInt64, TimeInMillis
// - integers of known sizes.
// - integers of known sizes.
// BiggestInt - the biggest signed integer type.
//
//
// Command-line utilities:
// Command-line utilities:
// GTEST_DECLARE_*() - declares a flag.
// GTEST_DECLARE_*() - declares a flag.
...
@@ -1875,9 +1876,12 @@ GTEST_API_ size_t GetThreadCount();
...
@@ -1875,9 +1876,12 @@ GTEST_API_ size_t GetThreadCount();
#if GTEST_OS_WINDOWS
#if GTEST_OS_WINDOWS
# define GTEST_PATH_SEP_ "\\"
# define GTEST_PATH_SEP_ "\\"
# define GTEST_HAS_ALT_PATH_SEP_ 1
# define GTEST_HAS_ALT_PATH_SEP_ 1
// The biggest signed integer type the compiler supports.
typedef
__int64
BiggestInt
;
#else
#else
# define GTEST_PATH_SEP_ "/"
# define GTEST_PATH_SEP_ "/"
# define GTEST_HAS_ALT_PATH_SEP_ 0
# define GTEST_HAS_ALT_PATH_SEP_ 0
typedef
long
long
BiggestInt
;
// NOLINT
#endif // GTEST_OS_WINDOWS
#endif // GTEST_OS_WINDOWS
// Utilities for char.
// Utilities for char.
...
@@ -2080,6 +2084,16 @@ GTEST_DISABLE_MSC_DEPRECATED_POP_()
...
@@ -2080,6 +2084,16 @@ GTEST_DISABLE_MSC_DEPRECATED_POP_()
# define GTEST_SNPRINTF_ snprintf
# define GTEST_SNPRINTF_ snprintf
#endif
#endif
// The maximum number a BiggestInt can represent. This definition
// works no matter BiggestInt is represented in one's complement or
// two's complement.
//
// We cannot rely on numeric_limits in STL, as __int64 and long long
// are not part of standard C++ and numeric_limits doesn't need to be
// defined for them.
const
BiggestInt
kMaxBiggestInt
=
~
(
static_cast
<
BiggestInt
>
(
1
)
<<
(
8
*
sizeof
(
BiggestInt
)
-
1
));
// This template class serves as a compile-time function from size to
// This template class serves as a compile-time function from size to
// type. It maps a size in bytes to a primitive type with that
// type. It maps a size in bytes to a primitive type with that
// size. e.g.
// size. e.g.
...
...
googletest/src/gtest.cc
View file @
8697709e
...
@@ -1442,8 +1442,9 @@ namespace internal {
...
@@ -1442,8 +1442,9 @@ namespace internal {
// The helper function for {ASSERT|EXPECT}_EQ with int or enum
// The helper function for {ASSERT|EXPECT}_EQ with int or enum
// arguments.
// arguments.
AssertionResult
CmpHelperEQ
(
const
char
*
lhs_expression
,
AssertionResult
CmpHelperEQ
(
const
char
*
lhs_expression
,
const
char
*
rhs_expression
,
std
::
intmax_t
lhs
,
const
char
*
rhs_expression
,
std
::
intmax_t
rhs
)
{
BiggestInt
lhs
,
BiggestInt
rhs
)
{
if
(
lhs
==
rhs
)
{
if
(
lhs
==
rhs
)
{
return
AssertionSuccess
();
return
AssertionSuccess
();
}
}
...
@@ -1456,20 +1457,20 @@ AssertionResult CmpHelperEQ(const char* lhs_expression,
...
@@ -1456,20 +1457,20 @@ AssertionResult CmpHelperEQ(const char* lhs_expression,
}
}
// A macro for implementing the helper functions needed to implement
// A macro for implementing the helper functions needed to implement
// ASSERT_?? and EXPECT_?? with integer or enum arguments. It is here
// ASSERT_?? and EXPECT_?? with integer or enum arguments.
It is here
// just to avoid copy-and-paste of similar code.
// just to avoid copy-and-paste of similar code.
#define GTEST_IMPL_CMP_HELPER_(op_name, op)
\
#define GTEST_IMPL_CMP_HELPER_(op_name, op)\
AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2,
\
AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
std::intmax_t val1, std::intmax_
t val2) {
\
BiggestInt val1, BiggestIn
t val2) {\
if (val1 op val2) {
\
if (val1 op val2) {\
return AssertionSuccess();
\
return AssertionSuccess();\
} else {
\
} else {\
return AssertionFailure()
\
return AssertionFailure() \
<< "Expected: (" << expr1 << ") " #op " (" << expr2
\
<< "Expected: (" << expr1 << ") " #op " (" << expr2\
<< "), actual: " << FormatForComparisonFailureMessage(val1, val2)
\
<< "), actual: " << FormatForComparisonFailureMessage(val1, val2)\
<< " vs " << FormatForComparisonFailureMessage(val2, val1);
\
<< " vs " << FormatForComparisonFailureMessage(val2, val1);\
}
\
}\
}
}
// Implements the helper function for {ASSERT|EXPECT}_NE with int or
// Implements the helper function for {ASSERT|EXPECT}_NE with int or
// enum arguments.
// enum arguments.
...
...
googletest/test/googletest-printers-test.cc
View file @
8697709e
...
@@ -83,12 +83,10 @@ void PrintTo(EnumWithPrintTo e, std::ostream* os) {
...
@@ -83,12 +83,10 @@ void PrintTo(EnumWithPrintTo e, std::ostream* os) {
*
os
<<
(
e
==
kEWPT1
?
"kEWPT1"
:
"invalid"
);
*
os
<<
(
e
==
kEWPT1
?
"kEWPT1"
:
"invalid"
);
}
}
// A class implicitly convertible to
std::intmax_
t.
// A class implicitly convertible to
BiggestIn
t.
class
Int
Max
Convertible
{
class
Biggest
IntConvertible
{
public:
public:
constexpr
operator
std
::
intmax_t
()
const
{
// NOLINT(runtime/explicit)
operator
::
testing
::
internal
::
BiggestInt
()
const
{
return
42
;
}
return
42
;
}
};
};
// A user-defined unprintable class template in the global namespace.
// A user-defined unprintable class template in the global namespace.
...
@@ -269,10 +267,10 @@ TEST(PrintEnumTest, EnumWithPrintTo) {
...
@@ -269,10 +267,10 @@ TEST(PrintEnumTest, EnumWithPrintTo) {
EXPECT_EQ
(
"invalid"
,
Print
(
static_cast
<
EnumWithPrintTo
>
(
0
)));
EXPECT_EQ
(
"invalid"
,
Print
(
static_cast
<
EnumWithPrintTo
>
(
0
)));
}
}
// Tests printing a class implicitly convertible to
std::intmax_
t.
// Tests printing a class implicitly convertible to
BiggestIn
t.
TEST
(
PrintClassTest
,
Int
Max
Convertible
)
{
TEST
(
PrintClassTest
,
Biggest
IntConvertible
)
{
EXPECT_EQ
(
"42"
,
Print
(
Int
Max
Convertible
()));
EXPECT_EQ
(
"42"
,
Print
(
Biggest
IntConvertible
()));
}
}
// Tests printing various char types.
// Tests printing various char types.
...
@@ -527,9 +525,10 @@ TEST(PrintPointerTest, NonMemberFunctionPointer) {
...
@@ -527,9 +525,10 @@ TEST(PrintPointerTest, NonMemberFunctionPointer) {
// standard disallows casting between pointers to functions and
// standard disallows casting between pointers to functions and
// pointers to objects, and some compilers (e.g. GCC 3.4) enforce
// pointers to objects, and some compilers (e.g. GCC 3.4) enforce
// this limitation.
// this limitation.
EXPECT_EQ
(
PrintPointer
(
reinterpret_cast
<
const
void
*>
(
EXPECT_EQ
(
reinterpret_cast
<
std
::
intptr_t
>
(
&
MyFunction
))),
PrintPointer
(
reinterpret_cast
<
const
void
*>
(
Print
(
&
MyFunction
));
reinterpret_cast
<
internal
::
BiggestInt
>
(
&
MyFunction
))),
Print
(
&
MyFunction
));
int
(
*
p
)(
bool
)
=
NULL
;
// NOLINT
int
(
*
p
)(
bool
)
=
NULL
;
// NOLINT
EXPECT_EQ
(
"NULL"
,
Print
(
p
));
EXPECT_EQ
(
"NULL"
,
Print
(
p
));
}
}
...
@@ -1121,8 +1120,8 @@ TEST(PrintReferenceTest, HandlesFunctionPointer) {
...
@@ -1121,8 +1120,8 @@ TEST(PrintReferenceTest, HandlesFunctionPointer) {
// standard disallows casting between pointers to functions and
// standard disallows casting between pointers to functions and
// pointers to objects, and some compilers (e.g. GCC 3.4) enforce
// pointers to objects, and some compilers (e.g. GCC 3.4) enforce
// this limitation.
// this limitation.
const
std
::
string
fp_string
=
PrintPointer
(
const
std
::
string
fp_string
=
PrintPointer
(
reinterpret_cast
<
const
void
*>
(
reinterpret_cast
<
const
void
*>
(
reinterpret_cast
<
std
::
intptr_
t
>
(
fp
)));
reinterpret_cast
<
internal
::
BiggestIn
t
>
(
fp
)));
EXPECT_EQ
(
"@"
+
fp_pointer_string
+
" "
+
fp_string
,
EXPECT_EQ
(
"@"
+
fp_pointer_string
+
" "
+
fp_string
,
PrintByRef
(
fp
));
PrintByRef
(
fp
));
}
}
...
...
googletest/test/gtest_unittest.cc
View file @
8697709e
...
@@ -55,11 +55,11 @@ TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) {
...
@@ -55,11 +55,11 @@ TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) {
EXPECT_TRUE
(
dummy
||
!
dummy
);
// Suppresses warning that dummy is unused.
EXPECT_TRUE
(
dummy
||
!
dummy
);
// Suppresses warning that dummy is unused.
}
}
#include <limits.h> // For INT_MAX.
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <time.h>
#include <time.h>
#include <limits>
#include <map>
#include <map>
#include <ostream>
#include <ostream>
#include <type_traits>
#include <type_traits>
...
@@ -3952,11 +3952,11 @@ enum {
...
@@ -3952,11 +3952,11 @@ enum {
// On Linux, kCaseB and kCaseA have the same value when truncated to
// On Linux, kCaseB and kCaseA have the same value when truncated to
// int size. We want to test whether this will confuse the
// int size. We want to test whether this will confuse the
// assertions.
// assertions.
kCaseB
=
(
std
::
numeric_limits
<
std
::
intmax_t
>::
max
)()
,
kCaseB
=
testing
::
internal
::
kMaxBiggestInt
,
# else
# else
kCaseB
=
(
std
::
numeric_limits
<
int
>::
max
)()
,
kCaseB
=
INT_MAX
,
# endif // GTEST_OS_LINUX
# endif // GTEST_OS_LINUX
...
...
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