From 8b085f0d2168e6e54712d295dcf26feac92bb9fc Mon Sep 17 00:00:00 2001 From: Mark Mentovai Date: Wed, 11 Nov 2015 12:28:15 -0500 Subject: [PATCH 1/5] Fix warnings encountered in MSVC build of gtest/gmock tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes these warnings: …\gtest\googletest\test\gtest-port_test.cc(78) : error C2220: warning treated as error - no 'object' file generated …\gtest\googletest\test\gtest-port_test.cc(78) : warning C4309: 'static_cast' : truncation of constant value …\gtest\googletest\test\gtest-port_test.cc(79) : warning C4309: 'static_cast' : truncation of constant value …\gtest\googlemock\test\gmock-matchers_test.cc(2712) : error C2220: warning treated as error - no 'object' file generated …\gtest\googlemock\test\gmock-matchers_test.cc(2706) : while compiling class template member function 'testing::gmock_matchers_test::FloatingPointTest::FloatingPointTest(void)' …\gtest\googlemock\test\gmock-matchers_test.cc(2896) : see reference to function template instantiation 'testing::gmock_matchers_test::FloatingPointTest::FloatingPointTest(void)' being compiled …\gtest\googlemock\test\gmock-matchers_test.cc(2896) : see reference to class template instantiation 'testing::gmock_matchers_test::FloatingPointTest' being compiled …\gtest\googlemock\test\gmock-matchers_test.cc(2712) : warning C4267: 'argument' : conversion from 'size_t' to 'const unsigned int', possible loss of data …\gtest\googlemock\test\gmock-matchers_test.cc(2714) : warning C4267: 'argument' : conversion from 'size_t' to 'const unsigned int', possible loss of data …\gtest\googlemock\test\gmock-matchers_test.cc(2716) : warning C4267: 'argument' : conversion from 'size_t' to 'const unsigned int', possible loss of data …\gtest\googlemock\test\gmock-matchers_test.cc(2717) : warning C4267: 'argument' : conversion from 'size_t' to 'const unsigned int', possible loss of data …\gtest\googlemock\test\gmock-matchers_test.cc(2718) : warning C4267: 'argument' : conversion from 'size_t' to 'const unsigned int', possible loss of data …\gtest\googlemock\test\gmock-matchers_test.cc(2721) : warning C4267: 'argument' : conversion from 'size_t' to 'const unsigned int', possible loss of data …\gtest\googlemock\test\gmock-matchers_test.cc(2723) : warning C4267: 'argument' : conversion from 'size_t' to 'const unsigned int', possible loss of data --- googlemock/test/gmock-matchers_test.cc | 2 +- googletest/test/gtest-port_test.cc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc index 72824699..8cc37e72 100644 --- a/googlemock/test/gmock-matchers_test.cc +++ b/googlemock/test/gmock-matchers_test.cc @@ -2778,7 +2778,7 @@ class FloatingPointTest : public testing::Test { // Pre-calculated numbers to be used by the tests. - const size_t max_ulps_; + const unsigned int max_ulps_; const Bits zero_bits_; // The bits that represent 0.0. const Bits one_bits_; // The bits that represent 1.0. diff --git a/googletest/test/gtest-port_test.cc b/googletest/test/gtest-port_test.cc index d17bad00..446660ff 100644 --- a/googletest/test/gtest-port_test.cc +++ b/googletest/test/gtest-port_test.cc @@ -75,8 +75,8 @@ TEST(IsXDigitTest, WorksForNarrowAscii) { } TEST(IsXDigitTest, ReturnsFalseForNarrowNonAscii) { - EXPECT_FALSE(IsXDigit(static_cast(0x80))); - EXPECT_FALSE(IsXDigit(static_cast('0' | 0x80))); + EXPECT_FALSE(IsXDigit(static_cast(0x80u))); + EXPECT_FALSE(IsXDigit(static_cast('0' | 0x80u))); } TEST(IsXDigitTest, WorksForWideAscii) { From 322a4914a309e950c0adb38c22dd44470619b994 Mon Sep 17 00:00:00 2001 From: Mark Mentovai Date: Wed, 11 Nov 2015 17:40:52 -0500 Subject: [PATCH 2/5] Better use of character constants --- googletest/test/gtest-port_test.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/googletest/test/gtest-port_test.cc b/googletest/test/gtest-port_test.cc index 446660ff..6ea607bc 100644 --- a/googletest/test/gtest-port_test.cc +++ b/googletest/test/gtest-port_test.cc @@ -75,8 +75,8 @@ TEST(IsXDigitTest, WorksForNarrowAscii) { } TEST(IsXDigitTest, ReturnsFalseForNarrowNonAscii) { - EXPECT_FALSE(IsXDigit(static_cast(0x80u))); - EXPECT_FALSE(IsXDigit(static_cast('0' | 0x80u))); + EXPECT_FALSE(IsXDigit('\x80')); + EXPECT_FALSE(IsXDigit(static_cast('0' | '\x80'))); } TEST(IsXDigitTest, WorksForWideAscii) { From cbce23fb86705aff7f9f1294a295027d7ed674ef Mon Sep 17 00:00:00 2001 From: Mark Mentovai Date: Wed, 11 Nov 2015 17:44:12 -0500 Subject: [PATCH 3/5] Leave decltype(max_ulps_) alone and cast, not sure this is better --- googlemock/test/gmock-matchers_test.cc | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc index 8cc37e72..11b6de2d 100644 --- a/googlemock/test/gmock-matchers_test.cc +++ b/googlemock/test/gmock-matchers_test.cc @@ -2708,19 +2708,21 @@ class FloatingPointTest : public testing::Test { zero_bits_(Floating(0).bits()), one_bits_(Floating(1).bits()), infinity_bits_(Floating(Floating::Infinity()).bits()), - close_to_positive_zero_( - Floating::ReinterpretBits(zero_bits_ + max_ulps_/2)), - close_to_negative_zero_( - -Floating::ReinterpretBits(zero_bits_ + max_ulps_ - max_ulps_/2)), + close_to_positive_zero_(Floating::ReinterpretBits( + static_cast(zero_bits_ + max_ulps_/2))), + close_to_negative_zero_(-Floating::ReinterpretBits( + static_cast(zero_bits_ + max_ulps_ - max_ulps_/2))), further_from_negative_zero_(-Floating::ReinterpretBits( - zero_bits_ + max_ulps_ + 1 - max_ulps_/2)), - close_to_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_)), - further_from_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_ + 1)), + static_cast(zero_bits_ + max_ulps_ + 1 - max_ulps_/2))), + close_to_one_(Floating::ReinterpretBits( + static_cast(one_bits_ + max_ulps_))), + further_from_one_(Floating::ReinterpretBits( + static_cast(one_bits_ + max_ulps_ + 1))), infinity_(Floating::Infinity()), - close_to_infinity_( - Floating::ReinterpretBits(infinity_bits_ - max_ulps_)), - further_from_infinity_( - Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)), + close_to_infinity_(Floating::ReinterpretBits( + static_cast(infinity_bits_ - max_ulps_))), + further_from_infinity_(Floating::ReinterpretBits( + static_cast(infinity_bits_ - max_ulps_ - 1))), max_(Floating::Max()), nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)), nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) { @@ -2778,7 +2780,7 @@ class FloatingPointTest : public testing::Test { // Pre-calculated numbers to be used by the tests. - const unsigned int max_ulps_; + const size_t max_ulps_; const Bits zero_bits_; // The bits that represent 0.0. const Bits one_bits_; // The bits that represent 1.0. From cfe466a0a75333a1c12baa99c172b9036fc5ccf1 Mon Sep 17 00:00:00 2001 From: Mark Mentovai Date: Wed, 11 Nov 2015 18:26:35 -0500 Subject: [PATCH 4/5] Use a templated helper to wrap the cast MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The helper needs to be templated because its argument type can’t be known. FloatingPointTest is instantiated with RawType = float and RawType = double, so Bits will be an unsigned 32-bit or 64-bit type. size_t will be either 32 or 64 bits depending on the system’s definition, typically based on pointer size. --- googlemock/test/gmock-matchers_test.cc | 29 +++++++++++++------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc index 11b6de2d..323ab25c 100644 --- a/googlemock/test/gmock-matchers_test.cc +++ b/googlemock/test/gmock-matchers_test.cc @@ -2708,21 +2708,16 @@ class FloatingPointTest : public testing::Test { zero_bits_(Floating(0).bits()), one_bits_(Floating(1).bits()), infinity_bits_(Floating(Floating::Infinity()).bits()), - close_to_positive_zero_(Floating::ReinterpretBits( - static_cast(zero_bits_ + max_ulps_/2))), - close_to_negative_zero_(-Floating::ReinterpretBits( - static_cast(zero_bits_ + max_ulps_ - max_ulps_/2))), - further_from_negative_zero_(-Floating::ReinterpretBits( - static_cast(zero_bits_ + max_ulps_ + 1 - max_ulps_/2))), - close_to_one_(Floating::ReinterpretBits( - static_cast(one_bits_ + max_ulps_))), - further_from_one_(Floating::ReinterpretBits( - static_cast(one_bits_ + max_ulps_ + 1))), + close_to_positive_zero_(ReinterpretBits(zero_bits_ + max_ulps_/2)), + close_to_negative_zero_(ReinterpretBits( + zero_bits_ + max_ulps_ - max_ulps_/2)), + further_from_negative_zero_(-ReinterpretBits( + zero_bits_ + max_ulps_ + 1 - max_ulps_/2)), + close_to_one_(ReinterpretBits(one_bits_ + max_ulps_)), + further_from_one_(ReinterpretBits(one_bits_ + max_ulps_ + 1)), infinity_(Floating::Infinity()), - close_to_infinity_(Floating::ReinterpretBits( - static_cast(infinity_bits_ - max_ulps_))), - further_from_infinity_(Floating::ReinterpretBits( - static_cast(infinity_bits_ - max_ulps_ - 1))), + close_to_infinity_(ReinterpretBits(infinity_bits_ - max_ulps_)), + further_from_infinity_(ReinterpretBits(infinity_bits_ - max_ulps_ - 1)), max_(Floating::Max()), nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)), nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) { @@ -2806,6 +2801,12 @@ class FloatingPointTest : public testing::Test { // Some NaNs. const RawType nan1_; const RawType nan2_; + + private: + template + static RawType ReinterpretBits(T value) { + return Floating::ReinterpretBits(static_cast(value)); + } }; // Tests floating-point matchers with fixed epsilons. From 4a8e54401e497794054defc49fde7b208a8c8fab Mon Sep 17 00:00:00 2001 From: Mark Mentovai Date: Thu, 12 Nov 2015 10:01:06 -0500 Subject: [PATCH 5/5] Name the helper AsBits() --- googlemock/test/gmock-matchers_test.cc | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc index 323ab25c..78c4c901 100644 --- a/googlemock/test/gmock-matchers_test.cc +++ b/googlemock/test/gmock-matchers_test.cc @@ -2708,19 +2708,18 @@ class FloatingPointTest : public testing::Test { zero_bits_(Floating(0).bits()), one_bits_(Floating(1).bits()), infinity_bits_(Floating(Floating::Infinity()).bits()), - close_to_positive_zero_(ReinterpretBits(zero_bits_ + max_ulps_/2)), - close_to_negative_zero_(ReinterpretBits( - zero_bits_ + max_ulps_ - max_ulps_/2)), - further_from_negative_zero_(-ReinterpretBits( + close_to_positive_zero_(AsBits(zero_bits_ + max_ulps_/2)), + close_to_negative_zero_(AsBits(zero_bits_ + max_ulps_ - max_ulps_/2)), + further_from_negative_zero_(-AsBits( zero_bits_ + max_ulps_ + 1 - max_ulps_/2)), - close_to_one_(ReinterpretBits(one_bits_ + max_ulps_)), - further_from_one_(ReinterpretBits(one_bits_ + max_ulps_ + 1)), + close_to_one_(AsBits(one_bits_ + max_ulps_)), + further_from_one_(AsBits(one_bits_ + max_ulps_ + 1)), infinity_(Floating::Infinity()), - close_to_infinity_(ReinterpretBits(infinity_bits_ - max_ulps_)), - further_from_infinity_(ReinterpretBits(infinity_bits_ - max_ulps_ - 1)), + close_to_infinity_(AsBits(infinity_bits_ - max_ulps_)), + further_from_infinity_(AsBits(infinity_bits_ - max_ulps_ - 1)), max_(Floating::Max()), - nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)), - nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) { + nan1_(AsBits(Floating::kExponentBitMask | 1)), + nan2_(AsBits(Floating::kExponentBitMask | 200)) { } void TestSize() { @@ -2804,7 +2803,7 @@ class FloatingPointTest : public testing::Test { private: template - static RawType ReinterpretBits(T value) { + static RawType AsBits(T value) { return Floating::ReinterpretBits(static_cast(value)); } };