1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/testing/gtest/test/gtest_unittest.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,7537 @@ 1.4 +// Copyright 2005, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 +// 1.33 +// Author: wan@google.com (Zhanyong Wan) 1.34 +// 1.35 +// Tests for Google Test itself. This verifies that the basic constructs of 1.36 +// Google Test work. 1.37 + 1.38 +#include "gtest/gtest.h" 1.39 + 1.40 +// Verifies that the command line flag variables can be accessed 1.41 +// in code once <gtest/gtest.h> has been #included. 1.42 +// Do not move it after other #includes. 1.43 +TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) { 1.44 + bool dummy = testing::GTEST_FLAG(also_run_disabled_tests) 1.45 + || testing::GTEST_FLAG(break_on_failure) 1.46 + || testing::GTEST_FLAG(catch_exceptions) 1.47 + || testing::GTEST_FLAG(color) != "unknown" 1.48 + || testing::GTEST_FLAG(filter) != "unknown" 1.49 + || testing::GTEST_FLAG(list_tests) 1.50 + || testing::GTEST_FLAG(output) != "unknown" 1.51 + || testing::GTEST_FLAG(print_time) 1.52 + || testing::GTEST_FLAG(random_seed) 1.53 + || testing::GTEST_FLAG(repeat) > 0 1.54 + || testing::GTEST_FLAG(show_internal_stack_frames) 1.55 + || testing::GTEST_FLAG(shuffle) 1.56 + || testing::GTEST_FLAG(stack_trace_depth) > 0 1.57 + || testing::GTEST_FLAG(stream_result_to) != "unknown" 1.58 + || testing::GTEST_FLAG(throw_on_failure); 1.59 + EXPECT_TRUE(dummy || !dummy); // Suppresses warning that dummy is unused. 1.60 +} 1.61 + 1.62 +#include <limits.h> // For INT_MAX. 1.63 +#include <stdlib.h> 1.64 +#include <string.h> 1.65 +#include <time.h> 1.66 + 1.67 +#include <map> 1.68 +#include <vector> 1.69 +#include <ostream> 1.70 + 1.71 +#include "gtest/gtest-spi.h" 1.72 + 1.73 +// Indicates that this translation unit is part of Google Test's 1.74 +// implementation. It must come before gtest-internal-inl.h is 1.75 +// included, or there will be a compiler error. This trick is to 1.76 +// prevent a user from accidentally including gtest-internal-inl.h in 1.77 +// his code. 1.78 +#define GTEST_IMPLEMENTATION_ 1 1.79 +#include "src/gtest-internal-inl.h" 1.80 +#undef GTEST_IMPLEMENTATION_ 1.81 + 1.82 +namespace testing { 1.83 +namespace internal { 1.84 + 1.85 +// Provides access to otherwise private parts of the TestEventListeners class 1.86 +// that are needed to test it. 1.87 +class TestEventListenersAccessor { 1.88 + public: 1.89 + static TestEventListener* GetRepeater(TestEventListeners* listeners) { 1.90 + return listeners->repeater(); 1.91 + } 1.92 + 1.93 + static void SetDefaultResultPrinter(TestEventListeners* listeners, 1.94 + TestEventListener* listener) { 1.95 + listeners->SetDefaultResultPrinter(listener); 1.96 + } 1.97 + static void SetDefaultXmlGenerator(TestEventListeners* listeners, 1.98 + TestEventListener* listener) { 1.99 + listeners->SetDefaultXmlGenerator(listener); 1.100 + } 1.101 + 1.102 + static bool EventForwardingEnabled(const TestEventListeners& listeners) { 1.103 + return listeners.EventForwardingEnabled(); 1.104 + } 1.105 + 1.106 + static void SuppressEventForwarding(TestEventListeners* listeners) { 1.107 + listeners->SuppressEventForwarding(); 1.108 + } 1.109 +}; 1.110 + 1.111 +} // namespace internal 1.112 +} // namespace testing 1.113 + 1.114 +using testing::AssertionFailure; 1.115 +using testing::AssertionResult; 1.116 +using testing::AssertionSuccess; 1.117 +using testing::DoubleLE; 1.118 +using testing::EmptyTestEventListener; 1.119 +using testing::FloatLE; 1.120 +using testing::GTEST_FLAG(also_run_disabled_tests); 1.121 +using testing::GTEST_FLAG(break_on_failure); 1.122 +using testing::GTEST_FLAG(catch_exceptions); 1.123 +using testing::GTEST_FLAG(color); 1.124 +using testing::GTEST_FLAG(death_test_use_fork); 1.125 +using testing::GTEST_FLAG(filter); 1.126 +using testing::GTEST_FLAG(list_tests); 1.127 +using testing::GTEST_FLAG(output); 1.128 +using testing::GTEST_FLAG(print_time); 1.129 +using testing::GTEST_FLAG(random_seed); 1.130 +using testing::GTEST_FLAG(repeat); 1.131 +using testing::GTEST_FLAG(show_internal_stack_frames); 1.132 +using testing::GTEST_FLAG(shuffle); 1.133 +using testing::GTEST_FLAG(stack_trace_depth); 1.134 +using testing::GTEST_FLAG(stream_result_to); 1.135 +using testing::GTEST_FLAG(throw_on_failure); 1.136 +using testing::IsNotSubstring; 1.137 +using testing::IsSubstring; 1.138 +using testing::Message; 1.139 +using testing::ScopedFakeTestPartResultReporter; 1.140 +using testing::StaticAssertTypeEq; 1.141 +using testing::Test; 1.142 +using testing::TestCase; 1.143 +using testing::TestEventListeners; 1.144 +using testing::TestPartResult; 1.145 +using testing::TestPartResultArray; 1.146 +using testing::TestProperty; 1.147 +using testing::TestResult; 1.148 +using testing::TimeInMillis; 1.149 +using testing::UnitTest; 1.150 +using testing::kMaxStackTraceDepth; 1.151 +using testing::internal::AddReference; 1.152 +using testing::internal::AlwaysFalse; 1.153 +using testing::internal::AlwaysTrue; 1.154 +using testing::internal::AppendUserMessage; 1.155 +using testing::internal::ArrayAwareFind; 1.156 +using testing::internal::ArrayEq; 1.157 +using testing::internal::CodePointToUtf8; 1.158 +using testing::internal::CompileAssertTypesEqual; 1.159 +using testing::internal::CopyArray; 1.160 +using testing::internal::CountIf; 1.161 +using testing::internal::EqFailure; 1.162 +using testing::internal::FloatingPoint; 1.163 +using testing::internal::ForEach; 1.164 +using testing::internal::FormatEpochTimeInMillisAsIso8601; 1.165 +using testing::internal::FormatTimeInMillisAsSeconds; 1.166 +using testing::internal::GTestFlagSaver; 1.167 +using testing::internal::GetCurrentOsStackTraceExceptTop; 1.168 +using testing::internal::GetElementOr; 1.169 +using testing::internal::GetNextRandomSeed; 1.170 +using testing::internal::GetRandomSeedFromFlag; 1.171 +using testing::internal::GetTestTypeId; 1.172 +using testing::internal::GetTimeInMillis; 1.173 +using testing::internal::GetTypeId; 1.174 +using testing::internal::GetUnitTestImpl; 1.175 +using testing::internal::ImplicitlyConvertible; 1.176 +using testing::internal::Int32; 1.177 +using testing::internal::Int32FromEnvOrDie; 1.178 +using testing::internal::IsAProtocolMessage; 1.179 +using testing::internal::IsContainer; 1.180 +using testing::internal::IsContainerTest; 1.181 +using testing::internal::IsNotContainer; 1.182 +using testing::internal::NativeArray; 1.183 +using testing::internal::ParseInt32Flag; 1.184 +using testing::internal::RemoveConst; 1.185 +using testing::internal::RemoveReference; 1.186 +using testing::internal::ShouldRunTestOnShard; 1.187 +using testing::internal::ShouldShard; 1.188 +using testing::internal::ShouldUseColor; 1.189 +using testing::internal::Shuffle; 1.190 +using testing::internal::ShuffleRange; 1.191 +using testing::internal::SkipPrefix; 1.192 +using testing::internal::StreamableToString; 1.193 +using testing::internal::String; 1.194 +using testing::internal::TestEventListenersAccessor; 1.195 +using testing::internal::TestResultAccessor; 1.196 +using testing::internal::UInt32; 1.197 +using testing::internal::WideStringToUtf8; 1.198 +using testing::internal::kCopy; 1.199 +using testing::internal::kMaxRandomSeed; 1.200 +using testing::internal::kReference; 1.201 +using testing::internal::kTestTypeIdInGoogleTest; 1.202 +using testing::internal::scoped_ptr; 1.203 + 1.204 +#if GTEST_HAS_STREAM_REDIRECTION 1.205 +using testing::internal::CaptureStdout; 1.206 +using testing::internal::GetCapturedStdout; 1.207 +#endif 1.208 + 1.209 +#if GTEST_IS_THREADSAFE 1.210 +using testing::internal::ThreadWithParam; 1.211 +#endif 1.212 + 1.213 +class TestingVector : public std::vector<int> { 1.214 +}; 1.215 + 1.216 +::std::ostream& operator<<(::std::ostream& os, 1.217 + const TestingVector& vector) { 1.218 + os << "{ "; 1.219 + for (size_t i = 0; i < vector.size(); i++) { 1.220 + os << vector[i] << " "; 1.221 + } 1.222 + os << "}"; 1.223 + return os; 1.224 +} 1.225 + 1.226 +// This line tests that we can define tests in an unnamed namespace. 1.227 +namespace { 1.228 + 1.229 +TEST(GetRandomSeedFromFlagTest, HandlesZero) { 1.230 + const int seed = GetRandomSeedFromFlag(0); 1.231 + EXPECT_LE(1, seed); 1.232 + EXPECT_LE(seed, static_cast<int>(kMaxRandomSeed)); 1.233 +} 1.234 + 1.235 +TEST(GetRandomSeedFromFlagTest, PreservesValidSeed) { 1.236 + EXPECT_EQ(1, GetRandomSeedFromFlag(1)); 1.237 + EXPECT_EQ(2, GetRandomSeedFromFlag(2)); 1.238 + EXPECT_EQ(kMaxRandomSeed - 1, GetRandomSeedFromFlag(kMaxRandomSeed - 1)); 1.239 + EXPECT_EQ(static_cast<int>(kMaxRandomSeed), 1.240 + GetRandomSeedFromFlag(kMaxRandomSeed)); 1.241 +} 1.242 + 1.243 +TEST(GetRandomSeedFromFlagTest, NormalizesInvalidSeed) { 1.244 + const int seed1 = GetRandomSeedFromFlag(-1); 1.245 + EXPECT_LE(1, seed1); 1.246 + EXPECT_LE(seed1, static_cast<int>(kMaxRandomSeed)); 1.247 + 1.248 + const int seed2 = GetRandomSeedFromFlag(kMaxRandomSeed + 1); 1.249 + EXPECT_LE(1, seed2); 1.250 + EXPECT_LE(seed2, static_cast<int>(kMaxRandomSeed)); 1.251 +} 1.252 + 1.253 +TEST(GetNextRandomSeedTest, WorksForValidInput) { 1.254 + EXPECT_EQ(2, GetNextRandomSeed(1)); 1.255 + EXPECT_EQ(3, GetNextRandomSeed(2)); 1.256 + EXPECT_EQ(static_cast<int>(kMaxRandomSeed), 1.257 + GetNextRandomSeed(kMaxRandomSeed - 1)); 1.258 + EXPECT_EQ(1, GetNextRandomSeed(kMaxRandomSeed)); 1.259 + 1.260 + // We deliberately don't test GetNextRandomSeed() with invalid 1.261 + // inputs, as that requires death tests, which are expensive. This 1.262 + // is fine as GetNextRandomSeed() is internal and has a 1.263 + // straightforward definition. 1.264 +} 1.265 + 1.266 +static void ClearCurrentTestPartResults() { 1.267 + TestResultAccessor::ClearTestPartResults( 1.268 + GetUnitTestImpl()->current_test_result()); 1.269 +} 1.270 + 1.271 +// Tests GetTypeId. 1.272 + 1.273 +TEST(GetTypeIdTest, ReturnsSameValueForSameType) { 1.274 + EXPECT_EQ(GetTypeId<int>(), GetTypeId<int>()); 1.275 + EXPECT_EQ(GetTypeId<Test>(), GetTypeId<Test>()); 1.276 +} 1.277 + 1.278 +class SubClassOfTest : public Test {}; 1.279 +class AnotherSubClassOfTest : public Test {}; 1.280 + 1.281 +TEST(GetTypeIdTest, ReturnsDifferentValuesForDifferentTypes) { 1.282 + EXPECT_NE(GetTypeId<int>(), GetTypeId<const int>()); 1.283 + EXPECT_NE(GetTypeId<int>(), GetTypeId<char>()); 1.284 + EXPECT_NE(GetTypeId<int>(), GetTestTypeId()); 1.285 + EXPECT_NE(GetTypeId<SubClassOfTest>(), GetTestTypeId()); 1.286 + EXPECT_NE(GetTypeId<AnotherSubClassOfTest>(), GetTestTypeId()); 1.287 + EXPECT_NE(GetTypeId<AnotherSubClassOfTest>(), GetTypeId<SubClassOfTest>()); 1.288 +} 1.289 + 1.290 +// Verifies that GetTestTypeId() returns the same value, no matter it 1.291 +// is called from inside Google Test or outside of it. 1.292 +TEST(GetTestTypeIdTest, ReturnsTheSameValueInsideOrOutsideOfGoogleTest) { 1.293 + EXPECT_EQ(kTestTypeIdInGoogleTest, GetTestTypeId()); 1.294 +} 1.295 + 1.296 +// Tests FormatTimeInMillisAsSeconds(). 1.297 + 1.298 +TEST(FormatTimeInMillisAsSecondsTest, FormatsZero) { 1.299 + EXPECT_EQ("0", FormatTimeInMillisAsSeconds(0)); 1.300 +} 1.301 + 1.302 +TEST(FormatTimeInMillisAsSecondsTest, FormatsPositiveNumber) { 1.303 + EXPECT_EQ("0.003", FormatTimeInMillisAsSeconds(3)); 1.304 + EXPECT_EQ("0.01", FormatTimeInMillisAsSeconds(10)); 1.305 + EXPECT_EQ("0.2", FormatTimeInMillisAsSeconds(200)); 1.306 + EXPECT_EQ("1.2", FormatTimeInMillisAsSeconds(1200)); 1.307 + EXPECT_EQ("3", FormatTimeInMillisAsSeconds(3000)); 1.308 +} 1.309 + 1.310 +TEST(FormatTimeInMillisAsSecondsTest, FormatsNegativeNumber) { 1.311 + EXPECT_EQ("-0.003", FormatTimeInMillisAsSeconds(-3)); 1.312 + EXPECT_EQ("-0.01", FormatTimeInMillisAsSeconds(-10)); 1.313 + EXPECT_EQ("-0.2", FormatTimeInMillisAsSeconds(-200)); 1.314 + EXPECT_EQ("-1.2", FormatTimeInMillisAsSeconds(-1200)); 1.315 + EXPECT_EQ("-3", FormatTimeInMillisAsSeconds(-3000)); 1.316 +} 1.317 + 1.318 +// Tests FormatEpochTimeInMillisAsIso8601(). The correctness of conversion 1.319 +// for particular dates below was verified in Python using 1.320 +// datetime.datetime.fromutctimestamp(<timetamp>/1000). 1.321 + 1.322 +// FormatEpochTimeInMillisAsIso8601 depends on the current timezone, so we 1.323 +// have to set up a particular timezone to obtain predictable results. 1.324 +class FormatEpochTimeInMillisAsIso8601Test : public Test { 1.325 + public: 1.326 + // On Cygwin, GCC doesn't allow unqualified integer literals to exceed 1.327 + // 32 bits, even when 64-bit integer types are available. We have to 1.328 + // force the constants to have a 64-bit type here. 1.329 + static const TimeInMillis kMillisPerSec = 1000; 1.330 + 1.331 + private: 1.332 + virtual void SetUp() { 1.333 + saved_tz_ = NULL; 1.334 +#if _MSC_VER 1.335 +# pragma warning(push) // Saves the current warning state. 1.336 +# pragma warning(disable:4996) // Temporarily disables warning 4996 1.337 + // (function or variable may be unsafe 1.338 + // for getenv, function is deprecated for 1.339 + // strdup). 1.340 + if (getenv("TZ")) 1.341 + saved_tz_ = strdup(getenv("TZ")); 1.342 +# pragma warning(pop) // Restores the warning state again. 1.343 +#else 1.344 + if (getenv("TZ")) 1.345 + saved_tz_ = strdup(getenv("TZ")); 1.346 +#endif 1.347 + 1.348 + // Set up the time zone for FormatEpochTimeInMillisAsIso8601 to use. We 1.349 + // cannot use the local time zone because the function's output depends 1.350 + // on the time zone. 1.351 + SetTimeZone("UTC+00"); 1.352 + } 1.353 + 1.354 + virtual void TearDown() { 1.355 + SetTimeZone(saved_tz_); 1.356 + free(const_cast<char*>(saved_tz_)); 1.357 + saved_tz_ = NULL; 1.358 + } 1.359 + 1.360 + static void SetTimeZone(const char* time_zone) { 1.361 + // tzset() distinguishes between the TZ variable being present and empty 1.362 + // and not being present, so we have to consider the case of time_zone 1.363 + // being NULL. 1.364 +#if _MSC_VER 1.365 + // ...Unless it's MSVC, whose standard library's _putenv doesn't 1.366 + // distinguish between an empty and a missing variable. 1.367 + const std::string env_var = 1.368 + std::string("TZ=") + (time_zone ? time_zone : ""); 1.369 + _putenv(env_var.c_str()); 1.370 +# pragma warning(push) // Saves the current warning state. 1.371 +# pragma warning(disable:4996) // Temporarily disables warning 4996 1.372 + // (function is deprecated). 1.373 + tzset(); 1.374 +# pragma warning(pop) // Restores the warning state again. 1.375 +#else 1.376 + if (time_zone) { 1.377 + setenv(("TZ"), time_zone, 1); 1.378 + } else { 1.379 + unsetenv("TZ"); 1.380 + } 1.381 + tzset(); 1.382 +#endif 1.383 + } 1.384 + 1.385 + const char* saved_tz_; 1.386 +}; 1.387 + 1.388 +const TimeInMillis FormatEpochTimeInMillisAsIso8601Test::kMillisPerSec; 1.389 + 1.390 +TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsTwoDigitSegments) { 1.391 + EXPECT_EQ("2011-10-31T18:52:42", 1.392 + FormatEpochTimeInMillisAsIso8601(1320087162 * kMillisPerSec)); 1.393 +} 1.394 + 1.395 +TEST_F(FormatEpochTimeInMillisAsIso8601Test, MillisecondsDoNotAffectResult) { 1.396 + EXPECT_EQ( 1.397 + "2011-10-31T18:52:42", 1.398 + FormatEpochTimeInMillisAsIso8601(1320087162 * kMillisPerSec + 234)); 1.399 +} 1.400 + 1.401 +TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsLeadingZeroes) { 1.402 + EXPECT_EQ("2011-09-03T05:07:02", 1.403 + FormatEpochTimeInMillisAsIso8601(1315026422 * kMillisPerSec)); 1.404 +} 1.405 + 1.406 +TEST_F(FormatEpochTimeInMillisAsIso8601Test, Prints24HourTime) { 1.407 + EXPECT_EQ("2011-09-28T17:08:22", 1.408 + FormatEpochTimeInMillisAsIso8601(1317229702 * kMillisPerSec)); 1.409 +} 1.410 + 1.411 +TEST_F(FormatEpochTimeInMillisAsIso8601Test, PrintsEpochStart) { 1.412 + EXPECT_EQ("1970-01-01T00:00:00", FormatEpochTimeInMillisAsIso8601(0)); 1.413 +} 1.414 + 1.415 +#if GTEST_CAN_COMPARE_NULL 1.416 + 1.417 +# ifdef __BORLANDC__ 1.418 +// Silences warnings: "Condition is always true", "Unreachable code" 1.419 +# pragma option push -w-ccc -w-rch 1.420 +# endif 1.421 + 1.422 +// Tests that GTEST_IS_NULL_LITERAL_(x) is true when x is a null 1.423 +// pointer literal. 1.424 +TEST(NullLiteralTest, IsTrueForNullLiterals) { 1.425 + EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(NULL)); 1.426 + EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0)); 1.427 + EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0U)); 1.428 + EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0L)); 1.429 + 1.430 +# ifndef __BORLANDC__ 1.431 + 1.432 + // Some compilers may fail to detect some null pointer literals; 1.433 + // as long as users of the framework don't use such literals, this 1.434 + // is harmless. 1.435 + EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(1 - 1)); 1.436 + 1.437 +# endif 1.438 +} 1.439 + 1.440 +// Tests that GTEST_IS_NULL_LITERAL_(x) is false when x is not a null 1.441 +// pointer literal. 1.442 +TEST(NullLiteralTest, IsFalseForNonNullLiterals) { 1.443 + EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(1)); 1.444 + EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(0.0)); 1.445 + EXPECT_FALSE(GTEST_IS_NULL_LITERAL_('a')); 1.446 + EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(static_cast<void*>(NULL))); 1.447 +} 1.448 + 1.449 +# ifdef __BORLANDC__ 1.450 +// Restores warnings after previous "#pragma option push" suppressed them. 1.451 +# pragma option pop 1.452 +# endif 1.453 + 1.454 +#endif // GTEST_CAN_COMPARE_NULL 1.455 +// 1.456 +// Tests CodePointToUtf8(). 1.457 + 1.458 +// Tests that the NUL character L'\0' is encoded correctly. 1.459 +TEST(CodePointToUtf8Test, CanEncodeNul) { 1.460 + char buffer[32]; 1.461 + EXPECT_STREQ("", CodePointToUtf8(L'\0', buffer)); 1.462 +} 1.463 + 1.464 +// Tests that ASCII characters are encoded correctly. 1.465 +TEST(CodePointToUtf8Test, CanEncodeAscii) { 1.466 + char buffer[32]; 1.467 + EXPECT_STREQ("a", CodePointToUtf8(L'a', buffer)); 1.468 + EXPECT_STREQ("Z", CodePointToUtf8(L'Z', buffer)); 1.469 + EXPECT_STREQ("&", CodePointToUtf8(L'&', buffer)); 1.470 + EXPECT_STREQ("\x7F", CodePointToUtf8(L'\x7F', buffer)); 1.471 +} 1.472 + 1.473 +// Tests that Unicode code-points that have 8 to 11 bits are encoded 1.474 +// as 110xxxxx 10xxxxxx. 1.475 +TEST(CodePointToUtf8Test, CanEncode8To11Bits) { 1.476 + char buffer[32]; 1.477 + // 000 1101 0011 => 110-00011 10-010011 1.478 + EXPECT_STREQ("\xC3\x93", CodePointToUtf8(L'\xD3', buffer)); 1.479 + 1.480 + // 101 0111 0110 => 110-10101 10-110110 1.481 + // Some compilers (e.g., GCC on MinGW) cannot handle non-ASCII codepoints 1.482 + // in wide strings and wide chars. In order to accomodate them, we have to 1.483 + // introduce such character constants as integers. 1.484 + EXPECT_STREQ("\xD5\xB6", 1.485 + CodePointToUtf8(static_cast<wchar_t>(0x576), buffer)); 1.486 +} 1.487 + 1.488 +// Tests that Unicode code-points that have 12 to 16 bits are encoded 1.489 +// as 1110xxxx 10xxxxxx 10xxxxxx. 1.490 +TEST(CodePointToUtf8Test, CanEncode12To16Bits) { 1.491 + char buffer[32]; 1.492 + // 0000 1000 1101 0011 => 1110-0000 10-100011 10-010011 1.493 + EXPECT_STREQ("\xE0\xA3\x93", 1.494 + CodePointToUtf8(static_cast<wchar_t>(0x8D3), buffer)); 1.495 + 1.496 + // 1100 0111 0100 1101 => 1110-1100 10-011101 10-001101 1.497 + EXPECT_STREQ("\xEC\x9D\x8D", 1.498 + CodePointToUtf8(static_cast<wchar_t>(0xC74D), buffer)); 1.499 +} 1.500 + 1.501 +#if !GTEST_WIDE_STRING_USES_UTF16_ 1.502 +// Tests in this group require a wchar_t to hold > 16 bits, and thus 1.503 +// are skipped on Windows, Cygwin, and Symbian, where a wchar_t is 1.504 +// 16-bit wide. This code may not compile on those systems. 1.505 + 1.506 +// Tests that Unicode code-points that have 17 to 21 bits are encoded 1.507 +// as 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx. 1.508 +TEST(CodePointToUtf8Test, CanEncode17To21Bits) { 1.509 + char buffer[32]; 1.510 + // 0 0001 0000 1000 1101 0011 => 11110-000 10-010000 10-100011 10-010011 1.511 + EXPECT_STREQ("\xF0\x90\xA3\x93", CodePointToUtf8(L'\x108D3', buffer)); 1.512 + 1.513 + // 0 0001 0000 0100 0000 0000 => 11110-000 10-010000 10-010000 10-000000 1.514 + EXPECT_STREQ("\xF0\x90\x90\x80", CodePointToUtf8(L'\x10400', buffer)); 1.515 + 1.516 + // 1 0000 1000 0110 0011 0100 => 11110-100 10-001000 10-011000 10-110100 1.517 + EXPECT_STREQ("\xF4\x88\x98\xB4", CodePointToUtf8(L'\x108634', buffer)); 1.518 +} 1.519 + 1.520 +// Tests that encoding an invalid code-point generates the expected result. 1.521 +TEST(CodePointToUtf8Test, CanEncodeInvalidCodePoint) { 1.522 + char buffer[32]; 1.523 + EXPECT_STREQ("(Invalid Unicode 0x1234ABCD)", 1.524 + CodePointToUtf8(L'\x1234ABCD', buffer)); 1.525 +} 1.526 + 1.527 +#endif // !GTEST_WIDE_STRING_USES_UTF16_ 1.528 + 1.529 +// Tests WideStringToUtf8(). 1.530 + 1.531 +// Tests that the NUL character L'\0' is encoded correctly. 1.532 +TEST(WideStringToUtf8Test, CanEncodeNul) { 1.533 + EXPECT_STREQ("", WideStringToUtf8(L"", 0).c_str()); 1.534 + EXPECT_STREQ("", WideStringToUtf8(L"", -1).c_str()); 1.535 +} 1.536 + 1.537 +// Tests that ASCII strings are encoded correctly. 1.538 +TEST(WideStringToUtf8Test, CanEncodeAscii) { 1.539 + EXPECT_STREQ("a", WideStringToUtf8(L"a", 1).c_str()); 1.540 + EXPECT_STREQ("ab", WideStringToUtf8(L"ab", 2).c_str()); 1.541 + EXPECT_STREQ("a", WideStringToUtf8(L"a", -1).c_str()); 1.542 + EXPECT_STREQ("ab", WideStringToUtf8(L"ab", -1).c_str()); 1.543 +} 1.544 + 1.545 +// Tests that Unicode code-points that have 8 to 11 bits are encoded 1.546 +// as 110xxxxx 10xxxxxx. 1.547 +TEST(WideStringToUtf8Test, CanEncode8To11Bits) { 1.548 + // 000 1101 0011 => 110-00011 10-010011 1.549 + EXPECT_STREQ("\xC3\x93", WideStringToUtf8(L"\xD3", 1).c_str()); 1.550 + EXPECT_STREQ("\xC3\x93", WideStringToUtf8(L"\xD3", -1).c_str()); 1.551 + 1.552 + // 101 0111 0110 => 110-10101 10-110110 1.553 + const wchar_t s[] = { 0x576, '\0' }; 1.554 + EXPECT_STREQ("\xD5\xB6", WideStringToUtf8(s, 1).c_str()); 1.555 + EXPECT_STREQ("\xD5\xB6", WideStringToUtf8(s, -1).c_str()); 1.556 +} 1.557 + 1.558 +// Tests that Unicode code-points that have 12 to 16 bits are encoded 1.559 +// as 1110xxxx 10xxxxxx 10xxxxxx. 1.560 +TEST(WideStringToUtf8Test, CanEncode12To16Bits) { 1.561 + // 0000 1000 1101 0011 => 1110-0000 10-100011 10-010011 1.562 + const wchar_t s1[] = { 0x8D3, '\0' }; 1.563 + EXPECT_STREQ("\xE0\xA3\x93", WideStringToUtf8(s1, 1).c_str()); 1.564 + EXPECT_STREQ("\xE0\xA3\x93", WideStringToUtf8(s1, -1).c_str()); 1.565 + 1.566 + // 1100 0111 0100 1101 => 1110-1100 10-011101 10-001101 1.567 + const wchar_t s2[] = { 0xC74D, '\0' }; 1.568 + EXPECT_STREQ("\xEC\x9D\x8D", WideStringToUtf8(s2, 1).c_str()); 1.569 + EXPECT_STREQ("\xEC\x9D\x8D", WideStringToUtf8(s2, -1).c_str()); 1.570 +} 1.571 + 1.572 +// Tests that the conversion stops when the function encounters \0 character. 1.573 +TEST(WideStringToUtf8Test, StopsOnNulCharacter) { 1.574 + EXPECT_STREQ("ABC", WideStringToUtf8(L"ABC\0XYZ", 100).c_str()); 1.575 +} 1.576 + 1.577 +// Tests that the conversion stops when the function reaches the limit 1.578 +// specified by the 'length' parameter. 1.579 +TEST(WideStringToUtf8Test, StopsWhenLengthLimitReached) { 1.580 + EXPECT_STREQ("ABC", WideStringToUtf8(L"ABCDEF", 3).c_str()); 1.581 +} 1.582 + 1.583 +#if !GTEST_WIDE_STRING_USES_UTF16_ 1.584 +// Tests that Unicode code-points that have 17 to 21 bits are encoded 1.585 +// as 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx. This code may not compile 1.586 +// on the systems using UTF-16 encoding. 1.587 +TEST(WideStringToUtf8Test, CanEncode17To21Bits) { 1.588 + // 0 0001 0000 1000 1101 0011 => 11110-000 10-010000 10-100011 10-010011 1.589 + EXPECT_STREQ("\xF0\x90\xA3\x93", WideStringToUtf8(L"\x108D3", 1).c_str()); 1.590 + EXPECT_STREQ("\xF0\x90\xA3\x93", WideStringToUtf8(L"\x108D3", -1).c_str()); 1.591 + 1.592 + // 1 0000 1000 0110 0011 0100 => 11110-100 10-001000 10-011000 10-110100 1.593 + EXPECT_STREQ("\xF4\x88\x98\xB4", WideStringToUtf8(L"\x108634", 1).c_str()); 1.594 + EXPECT_STREQ("\xF4\x88\x98\xB4", WideStringToUtf8(L"\x108634", -1).c_str()); 1.595 +} 1.596 + 1.597 +// Tests that encoding an invalid code-point generates the expected result. 1.598 +TEST(WideStringToUtf8Test, CanEncodeInvalidCodePoint) { 1.599 + EXPECT_STREQ("(Invalid Unicode 0xABCDFF)", 1.600 + WideStringToUtf8(L"\xABCDFF", -1).c_str()); 1.601 +} 1.602 +#else // !GTEST_WIDE_STRING_USES_UTF16_ 1.603 +// Tests that surrogate pairs are encoded correctly on the systems using 1.604 +// UTF-16 encoding in the wide strings. 1.605 +TEST(WideStringToUtf8Test, CanEncodeValidUtf16SUrrogatePairs) { 1.606 + const wchar_t s[] = { 0xD801, 0xDC00, '\0' }; 1.607 + EXPECT_STREQ("\xF0\x90\x90\x80", WideStringToUtf8(s, -1).c_str()); 1.608 +} 1.609 + 1.610 +// Tests that encoding an invalid UTF-16 surrogate pair 1.611 +// generates the expected result. 1.612 +TEST(WideStringToUtf8Test, CanEncodeInvalidUtf16SurrogatePair) { 1.613 + // Leading surrogate is at the end of the string. 1.614 + const wchar_t s1[] = { 0xD800, '\0' }; 1.615 + EXPECT_STREQ("\xED\xA0\x80", WideStringToUtf8(s1, -1).c_str()); 1.616 + // Leading surrogate is not followed by the trailing surrogate. 1.617 + const wchar_t s2[] = { 0xD800, 'M', '\0' }; 1.618 + EXPECT_STREQ("\xED\xA0\x80M", WideStringToUtf8(s2, -1).c_str()); 1.619 + // Trailing surrogate appearas without a leading surrogate. 1.620 + const wchar_t s3[] = { 0xDC00, 'P', 'Q', 'R', '\0' }; 1.621 + EXPECT_STREQ("\xED\xB0\x80PQR", WideStringToUtf8(s3, -1).c_str()); 1.622 +} 1.623 +#endif // !GTEST_WIDE_STRING_USES_UTF16_ 1.624 + 1.625 +// Tests that codepoint concatenation works correctly. 1.626 +#if !GTEST_WIDE_STRING_USES_UTF16_ 1.627 +TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) { 1.628 + const wchar_t s[] = { 0x108634, 0xC74D, '\n', 0x576, 0x8D3, 0x108634, '\0'}; 1.629 + EXPECT_STREQ( 1.630 + "\xF4\x88\x98\xB4" 1.631 + "\xEC\x9D\x8D" 1.632 + "\n" 1.633 + "\xD5\xB6" 1.634 + "\xE0\xA3\x93" 1.635 + "\xF4\x88\x98\xB4", 1.636 + WideStringToUtf8(s, -1).c_str()); 1.637 +} 1.638 +#else 1.639 +TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) { 1.640 + const wchar_t s[] = { 0xC74D, '\n', 0x576, 0x8D3, '\0'}; 1.641 + EXPECT_STREQ( 1.642 + "\xEC\x9D\x8D" "\n" "\xD5\xB6" "\xE0\xA3\x93", 1.643 + WideStringToUtf8(s, -1).c_str()); 1.644 +} 1.645 +#endif // !GTEST_WIDE_STRING_USES_UTF16_ 1.646 + 1.647 +// Tests the Random class. 1.648 + 1.649 +TEST(RandomDeathTest, GeneratesCrashesOnInvalidRange) { 1.650 + testing::internal::Random random(42); 1.651 + EXPECT_DEATH_IF_SUPPORTED( 1.652 + random.Generate(0), 1.653 + "Cannot generate a number in the range \\[0, 0\\)"); 1.654 + EXPECT_DEATH_IF_SUPPORTED( 1.655 + random.Generate(testing::internal::Random::kMaxRange + 1), 1.656 + "Generation of a number in \\[0, 2147483649\\) was requested, " 1.657 + "but this can only generate numbers in \\[0, 2147483648\\)"); 1.658 +} 1.659 + 1.660 +TEST(RandomTest, GeneratesNumbersWithinRange) { 1.661 + const UInt32 kRange = 10000; 1.662 + testing::internal::Random random(12345); 1.663 + for (int i = 0; i < 10; i++) { 1.664 + EXPECT_LT(random.Generate(kRange), kRange) << " for iteration " << i; 1.665 + } 1.666 + 1.667 + testing::internal::Random random2(testing::internal::Random::kMaxRange); 1.668 + for (int i = 0; i < 10; i++) { 1.669 + EXPECT_LT(random2.Generate(kRange), kRange) << " for iteration " << i; 1.670 + } 1.671 +} 1.672 + 1.673 +TEST(RandomTest, RepeatsWhenReseeded) { 1.674 + const int kSeed = 123; 1.675 + const int kArraySize = 10; 1.676 + const UInt32 kRange = 10000; 1.677 + UInt32 values[kArraySize]; 1.678 + 1.679 + testing::internal::Random random(kSeed); 1.680 + for (int i = 0; i < kArraySize; i++) { 1.681 + values[i] = random.Generate(kRange); 1.682 + } 1.683 + 1.684 + random.Reseed(kSeed); 1.685 + for (int i = 0; i < kArraySize; i++) { 1.686 + EXPECT_EQ(values[i], random.Generate(kRange)) << " for iteration " << i; 1.687 + } 1.688 +} 1.689 + 1.690 +// Tests STL container utilities. 1.691 + 1.692 +// Tests CountIf(). 1.693 + 1.694 +static bool IsPositive(int n) { return n > 0; } 1.695 + 1.696 +TEST(ContainerUtilityTest, CountIf) { 1.697 + std::vector<int> v; 1.698 + EXPECT_EQ(0, CountIf(v, IsPositive)); // Works for an empty container. 1.699 + 1.700 + v.push_back(-1); 1.701 + v.push_back(0); 1.702 + EXPECT_EQ(0, CountIf(v, IsPositive)); // Works when no value satisfies. 1.703 + 1.704 + v.push_back(2); 1.705 + v.push_back(-10); 1.706 + v.push_back(10); 1.707 + EXPECT_EQ(2, CountIf(v, IsPositive)); 1.708 +} 1.709 + 1.710 +// Tests ForEach(). 1.711 + 1.712 +static int g_sum = 0; 1.713 +static void Accumulate(int n) { g_sum += n; } 1.714 + 1.715 +TEST(ContainerUtilityTest, ForEach) { 1.716 + std::vector<int> v; 1.717 + g_sum = 0; 1.718 + ForEach(v, Accumulate); 1.719 + EXPECT_EQ(0, g_sum); // Works for an empty container; 1.720 + 1.721 + g_sum = 0; 1.722 + v.push_back(1); 1.723 + ForEach(v, Accumulate); 1.724 + EXPECT_EQ(1, g_sum); // Works for a container with one element. 1.725 + 1.726 + g_sum = 0; 1.727 + v.push_back(20); 1.728 + v.push_back(300); 1.729 + ForEach(v, Accumulate); 1.730 + EXPECT_EQ(321, g_sum); 1.731 +} 1.732 + 1.733 +// Tests GetElementOr(). 1.734 +TEST(ContainerUtilityTest, GetElementOr) { 1.735 + std::vector<char> a; 1.736 + EXPECT_EQ('x', GetElementOr(a, 0, 'x')); 1.737 + 1.738 + a.push_back('a'); 1.739 + a.push_back('b'); 1.740 + EXPECT_EQ('a', GetElementOr(a, 0, 'x')); 1.741 + EXPECT_EQ('b', GetElementOr(a, 1, 'x')); 1.742 + EXPECT_EQ('x', GetElementOr(a, -2, 'x')); 1.743 + EXPECT_EQ('x', GetElementOr(a, 2, 'x')); 1.744 +} 1.745 + 1.746 +TEST(ContainerUtilityDeathTest, ShuffleRange) { 1.747 + std::vector<int> a; 1.748 + a.push_back(0); 1.749 + a.push_back(1); 1.750 + a.push_back(2); 1.751 + testing::internal::Random random(1); 1.752 + 1.753 + EXPECT_DEATH_IF_SUPPORTED( 1.754 + ShuffleRange(&random, -1, 1, &a), 1.755 + "Invalid shuffle range start -1: must be in range \\[0, 3\\]"); 1.756 + EXPECT_DEATH_IF_SUPPORTED( 1.757 + ShuffleRange(&random, 4, 4, &a), 1.758 + "Invalid shuffle range start 4: must be in range \\[0, 3\\]"); 1.759 + EXPECT_DEATH_IF_SUPPORTED( 1.760 + ShuffleRange(&random, 3, 2, &a), 1.761 + "Invalid shuffle range finish 2: must be in range \\[3, 3\\]"); 1.762 + EXPECT_DEATH_IF_SUPPORTED( 1.763 + ShuffleRange(&random, 3, 4, &a), 1.764 + "Invalid shuffle range finish 4: must be in range \\[3, 3\\]"); 1.765 +} 1.766 + 1.767 +class VectorShuffleTest : public Test { 1.768 + protected: 1.769 + static const int kVectorSize = 20; 1.770 + 1.771 + VectorShuffleTest() : random_(1) { 1.772 + for (int i = 0; i < kVectorSize; i++) { 1.773 + vector_.push_back(i); 1.774 + } 1.775 + } 1.776 + 1.777 + static bool VectorIsCorrupt(const TestingVector& vector) { 1.778 + if (kVectorSize != static_cast<int>(vector.size())) { 1.779 + return true; 1.780 + } 1.781 + 1.782 + bool found_in_vector[kVectorSize] = { false }; 1.783 + for (size_t i = 0; i < vector.size(); i++) { 1.784 + const int e = vector[i]; 1.785 + if (e < 0 || e >= kVectorSize || found_in_vector[e]) { 1.786 + return true; 1.787 + } 1.788 + found_in_vector[e] = true; 1.789 + } 1.790 + 1.791 + // Vector size is correct, elements' range is correct, no 1.792 + // duplicate elements. Therefore no corruption has occurred. 1.793 + return false; 1.794 + } 1.795 + 1.796 + static bool VectorIsNotCorrupt(const TestingVector& vector) { 1.797 + return !VectorIsCorrupt(vector); 1.798 + } 1.799 + 1.800 + static bool RangeIsShuffled(const TestingVector& vector, int begin, int end) { 1.801 + for (int i = begin; i < end; i++) { 1.802 + if (i != vector[i]) { 1.803 + return true; 1.804 + } 1.805 + } 1.806 + return false; 1.807 + } 1.808 + 1.809 + static bool RangeIsUnshuffled( 1.810 + const TestingVector& vector, int begin, int end) { 1.811 + return !RangeIsShuffled(vector, begin, end); 1.812 + } 1.813 + 1.814 + static bool VectorIsShuffled(const TestingVector& vector) { 1.815 + return RangeIsShuffled(vector, 0, static_cast<int>(vector.size())); 1.816 + } 1.817 + 1.818 + static bool VectorIsUnshuffled(const TestingVector& vector) { 1.819 + return !VectorIsShuffled(vector); 1.820 + } 1.821 + 1.822 + testing::internal::Random random_; 1.823 + TestingVector vector_; 1.824 +}; // class VectorShuffleTest 1.825 + 1.826 +const int VectorShuffleTest::kVectorSize; 1.827 + 1.828 +TEST_F(VectorShuffleTest, HandlesEmptyRange) { 1.829 + // Tests an empty range at the beginning... 1.830 + ShuffleRange(&random_, 0, 0, &vector_); 1.831 + ASSERT_PRED1(VectorIsNotCorrupt, vector_); 1.832 + ASSERT_PRED1(VectorIsUnshuffled, vector_); 1.833 + 1.834 + // ...in the middle... 1.835 + ShuffleRange(&random_, kVectorSize/2, kVectorSize/2, &vector_); 1.836 + ASSERT_PRED1(VectorIsNotCorrupt, vector_); 1.837 + ASSERT_PRED1(VectorIsUnshuffled, vector_); 1.838 + 1.839 + // ...at the end... 1.840 + ShuffleRange(&random_, kVectorSize - 1, kVectorSize - 1, &vector_); 1.841 + ASSERT_PRED1(VectorIsNotCorrupt, vector_); 1.842 + ASSERT_PRED1(VectorIsUnshuffled, vector_); 1.843 + 1.844 + // ...and past the end. 1.845 + ShuffleRange(&random_, kVectorSize, kVectorSize, &vector_); 1.846 + ASSERT_PRED1(VectorIsNotCorrupt, vector_); 1.847 + ASSERT_PRED1(VectorIsUnshuffled, vector_); 1.848 +} 1.849 + 1.850 +TEST_F(VectorShuffleTest, HandlesRangeOfSizeOne) { 1.851 + // Tests a size one range at the beginning... 1.852 + ShuffleRange(&random_, 0, 1, &vector_); 1.853 + ASSERT_PRED1(VectorIsNotCorrupt, vector_); 1.854 + ASSERT_PRED1(VectorIsUnshuffled, vector_); 1.855 + 1.856 + // ...in the middle... 1.857 + ShuffleRange(&random_, kVectorSize/2, kVectorSize/2 + 1, &vector_); 1.858 + ASSERT_PRED1(VectorIsNotCorrupt, vector_); 1.859 + ASSERT_PRED1(VectorIsUnshuffled, vector_); 1.860 + 1.861 + // ...and at the end. 1.862 + ShuffleRange(&random_, kVectorSize - 1, kVectorSize, &vector_); 1.863 + ASSERT_PRED1(VectorIsNotCorrupt, vector_); 1.864 + ASSERT_PRED1(VectorIsUnshuffled, vector_); 1.865 +} 1.866 + 1.867 +// Because we use our own random number generator and a fixed seed, 1.868 +// we can guarantee that the following "random" tests will succeed. 1.869 + 1.870 +TEST_F(VectorShuffleTest, ShufflesEntireVector) { 1.871 + Shuffle(&random_, &vector_); 1.872 + ASSERT_PRED1(VectorIsNotCorrupt, vector_); 1.873 + EXPECT_FALSE(VectorIsUnshuffled(vector_)) << vector_; 1.874 + 1.875 + // Tests the first and last elements in particular to ensure that 1.876 + // there are no off-by-one problems in our shuffle algorithm. 1.877 + EXPECT_NE(0, vector_[0]); 1.878 + EXPECT_NE(kVectorSize - 1, vector_[kVectorSize - 1]); 1.879 +} 1.880 + 1.881 +TEST_F(VectorShuffleTest, ShufflesStartOfVector) { 1.882 + const int kRangeSize = kVectorSize/2; 1.883 + 1.884 + ShuffleRange(&random_, 0, kRangeSize, &vector_); 1.885 + 1.886 + ASSERT_PRED1(VectorIsNotCorrupt, vector_); 1.887 + EXPECT_PRED3(RangeIsShuffled, vector_, 0, kRangeSize); 1.888 + EXPECT_PRED3(RangeIsUnshuffled, vector_, kRangeSize, kVectorSize); 1.889 +} 1.890 + 1.891 +TEST_F(VectorShuffleTest, ShufflesEndOfVector) { 1.892 + const int kRangeSize = kVectorSize / 2; 1.893 + ShuffleRange(&random_, kRangeSize, kVectorSize, &vector_); 1.894 + 1.895 + ASSERT_PRED1(VectorIsNotCorrupt, vector_); 1.896 + EXPECT_PRED3(RangeIsUnshuffled, vector_, 0, kRangeSize); 1.897 + EXPECT_PRED3(RangeIsShuffled, vector_, kRangeSize, kVectorSize); 1.898 +} 1.899 + 1.900 +TEST_F(VectorShuffleTest, ShufflesMiddleOfVector) { 1.901 + int kRangeSize = kVectorSize/3; 1.902 + ShuffleRange(&random_, kRangeSize, 2*kRangeSize, &vector_); 1.903 + 1.904 + ASSERT_PRED1(VectorIsNotCorrupt, vector_); 1.905 + EXPECT_PRED3(RangeIsUnshuffled, vector_, 0, kRangeSize); 1.906 + EXPECT_PRED3(RangeIsShuffled, vector_, kRangeSize, 2*kRangeSize); 1.907 + EXPECT_PRED3(RangeIsUnshuffled, vector_, 2*kRangeSize, kVectorSize); 1.908 +} 1.909 + 1.910 +TEST_F(VectorShuffleTest, ShufflesRepeatably) { 1.911 + TestingVector vector2; 1.912 + for (int i = 0; i < kVectorSize; i++) { 1.913 + vector2.push_back(i); 1.914 + } 1.915 + 1.916 + random_.Reseed(1234); 1.917 + Shuffle(&random_, &vector_); 1.918 + random_.Reseed(1234); 1.919 + Shuffle(&random_, &vector2); 1.920 + 1.921 + ASSERT_PRED1(VectorIsNotCorrupt, vector_); 1.922 + ASSERT_PRED1(VectorIsNotCorrupt, vector2); 1.923 + 1.924 + for (int i = 0; i < kVectorSize; i++) { 1.925 + EXPECT_EQ(vector_[i], vector2[i]) << " where i is " << i; 1.926 + } 1.927 +} 1.928 + 1.929 +// Tests the size of the AssertHelper class. 1.930 + 1.931 +TEST(AssertHelperTest, AssertHelperIsSmall) { 1.932 + // To avoid breaking clients that use lots of assertions in one 1.933 + // function, we cannot grow the size of AssertHelper. 1.934 + EXPECT_LE(sizeof(testing::internal::AssertHelper), sizeof(void*)); 1.935 +} 1.936 + 1.937 +// Tests the String class. 1.938 + 1.939 +// Tests String's constructors. 1.940 +TEST(StringTest, Constructors) { 1.941 + // Default ctor. 1.942 + String s1; 1.943 + // We aren't using EXPECT_EQ(NULL, s1.c_str()) because comparing 1.944 + // pointers with NULL isn't supported on all platforms. 1.945 + EXPECT_EQ(0U, s1.length()); 1.946 + EXPECT_TRUE(NULL == s1.c_str()); 1.947 + 1.948 + // Implicitly constructs from a C-string. 1.949 + String s2 = "Hi"; 1.950 + EXPECT_EQ(2U, s2.length()); 1.951 + EXPECT_STREQ("Hi", s2.c_str()); 1.952 + 1.953 + // Constructs from a C-string and a length. 1.954 + String s3("hello", 3); 1.955 + EXPECT_EQ(3U, s3.length()); 1.956 + EXPECT_STREQ("hel", s3.c_str()); 1.957 + 1.958 + // The empty String should be created when String is constructed with 1.959 + // a NULL pointer and length 0. 1.960 + EXPECT_EQ(0U, String(NULL, 0).length()); 1.961 + EXPECT_FALSE(String(NULL, 0).c_str() == NULL); 1.962 + 1.963 + // Constructs a String that contains '\0'. 1.964 + String s4("a\0bcd", 4); 1.965 + EXPECT_EQ(4U, s4.length()); 1.966 + EXPECT_EQ('a', s4.c_str()[0]); 1.967 + EXPECT_EQ('\0', s4.c_str()[1]); 1.968 + EXPECT_EQ('b', s4.c_str()[2]); 1.969 + EXPECT_EQ('c', s4.c_str()[3]); 1.970 + 1.971 + // Copy ctor where the source is NULL. 1.972 + const String null_str; 1.973 + String s5 = null_str; 1.974 + EXPECT_TRUE(s5.c_str() == NULL); 1.975 + 1.976 + // Copy ctor where the source isn't NULL. 1.977 + String s6 = s3; 1.978 + EXPECT_EQ(3U, s6.length()); 1.979 + EXPECT_STREQ("hel", s6.c_str()); 1.980 + 1.981 + // Copy ctor where the source contains '\0'. 1.982 + String s7 = s4; 1.983 + EXPECT_EQ(4U, s7.length()); 1.984 + EXPECT_EQ('a', s7.c_str()[0]); 1.985 + EXPECT_EQ('\0', s7.c_str()[1]); 1.986 + EXPECT_EQ('b', s7.c_str()[2]); 1.987 + EXPECT_EQ('c', s7.c_str()[3]); 1.988 +} 1.989 + 1.990 +TEST(StringTest, ConvertsFromStdString) { 1.991 + // An empty std::string. 1.992 + const std::string src1(""); 1.993 + const String dest1 = src1; 1.994 + EXPECT_EQ(0U, dest1.length()); 1.995 + EXPECT_STREQ("", dest1.c_str()); 1.996 + 1.997 + // A normal std::string. 1.998 + const std::string src2("Hi"); 1.999 + const String dest2 = src2; 1.1000 + EXPECT_EQ(2U, dest2.length()); 1.1001 + EXPECT_STREQ("Hi", dest2.c_str()); 1.1002 + 1.1003 + // An std::string with an embedded NUL character. 1.1004 + const char src3[] = "a\0b"; 1.1005 + const String dest3 = std::string(src3, sizeof(src3)); 1.1006 + EXPECT_EQ(sizeof(src3), dest3.length()); 1.1007 + EXPECT_EQ('a', dest3.c_str()[0]); 1.1008 + EXPECT_EQ('\0', dest3.c_str()[1]); 1.1009 + EXPECT_EQ('b', dest3.c_str()[2]); 1.1010 +} 1.1011 + 1.1012 +TEST(StringTest, ConvertsToStdString) { 1.1013 + // An empty String. 1.1014 + const String src1(""); 1.1015 + const std::string dest1 = src1; 1.1016 + EXPECT_EQ("", dest1); 1.1017 + 1.1018 + // A normal String. 1.1019 + const String src2("Hi"); 1.1020 + const std::string dest2 = src2; 1.1021 + EXPECT_EQ("Hi", dest2); 1.1022 + 1.1023 + // A String containing a '\0'. 1.1024 + const String src3("x\0y", 3); 1.1025 + const std::string dest3 = src3; 1.1026 + EXPECT_EQ(std::string("x\0y", 3), dest3); 1.1027 +} 1.1028 + 1.1029 +#if GTEST_HAS_GLOBAL_STRING 1.1030 + 1.1031 +TEST(StringTest, ConvertsFromGlobalString) { 1.1032 + // An empty ::string. 1.1033 + const ::string src1(""); 1.1034 + const String dest1 = src1; 1.1035 + EXPECT_EQ(0U, dest1.length()); 1.1036 + EXPECT_STREQ("", dest1.c_str()); 1.1037 + 1.1038 + // A normal ::string. 1.1039 + const ::string src2("Hi"); 1.1040 + const String dest2 = src2; 1.1041 + EXPECT_EQ(2U, dest2.length()); 1.1042 + EXPECT_STREQ("Hi", dest2.c_str()); 1.1043 + 1.1044 + // An ::string with an embedded NUL character. 1.1045 + const char src3[] = "x\0y"; 1.1046 + const String dest3 = ::string(src3, sizeof(src3)); 1.1047 + EXPECT_EQ(sizeof(src3), dest3.length()); 1.1048 + EXPECT_EQ('x', dest3.c_str()[0]); 1.1049 + EXPECT_EQ('\0', dest3.c_str()[1]); 1.1050 + EXPECT_EQ('y', dest3.c_str()[2]); 1.1051 +} 1.1052 + 1.1053 +TEST(StringTest, ConvertsToGlobalString) { 1.1054 + // An empty String. 1.1055 + const String src1(""); 1.1056 + const ::string dest1 = src1; 1.1057 + EXPECT_EQ("", dest1); 1.1058 + 1.1059 + // A normal String. 1.1060 + const String src2("Hi"); 1.1061 + const ::string dest2 = src2; 1.1062 + EXPECT_EQ("Hi", dest2); 1.1063 + 1.1064 + const String src3("x\0y", 3); 1.1065 + const ::string dest3 = src3; 1.1066 + EXPECT_EQ(::string("x\0y", 3), dest3); 1.1067 +} 1.1068 + 1.1069 +#endif // GTEST_HAS_GLOBAL_STRING 1.1070 + 1.1071 +// Tests String::empty(). 1.1072 +TEST(StringTest, Empty) { 1.1073 + EXPECT_TRUE(String("").empty()); 1.1074 + EXPECT_FALSE(String().empty()); 1.1075 + EXPECT_FALSE(String(NULL).empty()); 1.1076 + EXPECT_FALSE(String("a").empty()); 1.1077 + EXPECT_FALSE(String("\0", 1).empty()); 1.1078 +} 1.1079 + 1.1080 +// Tests String::Compare(). 1.1081 +TEST(StringTest, Compare) { 1.1082 + // NULL vs NULL. 1.1083 + EXPECT_EQ(0, String().Compare(String())); 1.1084 + 1.1085 + // NULL vs non-NULL. 1.1086 + EXPECT_EQ(-1, String().Compare(String(""))); 1.1087 + 1.1088 + // Non-NULL vs NULL. 1.1089 + EXPECT_EQ(1, String("").Compare(String())); 1.1090 + 1.1091 + // The following covers non-NULL vs non-NULL. 1.1092 + 1.1093 + // "" vs "". 1.1094 + EXPECT_EQ(0, String("").Compare(String(""))); 1.1095 + 1.1096 + // "" vs non-"". 1.1097 + EXPECT_EQ(-1, String("").Compare(String("\0", 1))); 1.1098 + EXPECT_EQ(-1, String("").Compare(" ")); 1.1099 + 1.1100 + // Non-"" vs "". 1.1101 + EXPECT_EQ(1, String("a").Compare(String(""))); 1.1102 + 1.1103 + // The following covers non-"" vs non-"". 1.1104 + 1.1105 + // Same length and equal. 1.1106 + EXPECT_EQ(0, String("a").Compare(String("a"))); 1.1107 + 1.1108 + // Same length and different. 1.1109 + EXPECT_EQ(-1, String("a\0b", 3).Compare(String("a\0c", 3))); 1.1110 + EXPECT_EQ(1, String("b").Compare(String("a"))); 1.1111 + 1.1112 + // Different lengths. 1.1113 + EXPECT_EQ(-1, String("a").Compare(String("ab"))); 1.1114 + EXPECT_EQ(-1, String("a").Compare(String("a\0", 2))); 1.1115 + EXPECT_EQ(1, String("abc").Compare(String("aacd"))); 1.1116 +} 1.1117 + 1.1118 +// Tests String::operator==(). 1.1119 +TEST(StringTest, Equals) { 1.1120 + const String null(NULL); 1.1121 + EXPECT_TRUE(null == NULL); // NOLINT 1.1122 + EXPECT_FALSE(null == ""); // NOLINT 1.1123 + EXPECT_FALSE(null == "bar"); // NOLINT 1.1124 + 1.1125 + const String empty(""); 1.1126 + EXPECT_FALSE(empty == NULL); // NOLINT 1.1127 + EXPECT_TRUE(empty == ""); // NOLINT 1.1128 + EXPECT_FALSE(empty == "bar"); // NOLINT 1.1129 + 1.1130 + const String foo("foo"); 1.1131 + EXPECT_FALSE(foo == NULL); // NOLINT 1.1132 + EXPECT_FALSE(foo == ""); // NOLINT 1.1133 + EXPECT_FALSE(foo == "bar"); // NOLINT 1.1134 + EXPECT_TRUE(foo == "foo"); // NOLINT 1.1135 + 1.1136 + const String bar("x\0y", 3); 1.1137 + EXPECT_NE(bar, "x"); 1.1138 +} 1.1139 + 1.1140 +// Tests String::operator!=(). 1.1141 +TEST(StringTest, NotEquals) { 1.1142 + const String null(NULL); 1.1143 + EXPECT_FALSE(null != NULL); // NOLINT 1.1144 + EXPECT_TRUE(null != ""); // NOLINT 1.1145 + EXPECT_TRUE(null != "bar"); // NOLINT 1.1146 + 1.1147 + const String empty(""); 1.1148 + EXPECT_TRUE(empty != NULL); // NOLINT 1.1149 + EXPECT_FALSE(empty != ""); // NOLINT 1.1150 + EXPECT_TRUE(empty != "bar"); // NOLINT 1.1151 + 1.1152 + const String foo("foo"); 1.1153 + EXPECT_TRUE(foo != NULL); // NOLINT 1.1154 + EXPECT_TRUE(foo != ""); // NOLINT 1.1155 + EXPECT_TRUE(foo != "bar"); // NOLINT 1.1156 + EXPECT_FALSE(foo != "foo"); // NOLINT 1.1157 + 1.1158 + const String bar("x\0y", 3); 1.1159 + EXPECT_NE(bar, "x"); 1.1160 +} 1.1161 + 1.1162 +// Tests String::length(). 1.1163 +TEST(StringTest, Length) { 1.1164 + EXPECT_EQ(0U, String().length()); 1.1165 + EXPECT_EQ(0U, String("").length()); 1.1166 + EXPECT_EQ(2U, String("ab").length()); 1.1167 + EXPECT_EQ(3U, String("a\0b", 3).length()); 1.1168 +} 1.1169 + 1.1170 +// Tests String::EndsWith(). 1.1171 +TEST(StringTest, EndsWith) { 1.1172 + EXPECT_TRUE(String("foobar").EndsWith("bar")); 1.1173 + EXPECT_TRUE(String("foobar").EndsWith("")); 1.1174 + EXPECT_TRUE(String("").EndsWith("")); 1.1175 + 1.1176 + EXPECT_FALSE(String("foobar").EndsWith("foo")); 1.1177 + EXPECT_FALSE(String("").EndsWith("foo")); 1.1178 +} 1.1179 + 1.1180 +// Tests String::EndsWithCaseInsensitive(). 1.1181 +TEST(StringTest, EndsWithCaseInsensitive) { 1.1182 + EXPECT_TRUE(String("foobar").EndsWithCaseInsensitive("BAR")); 1.1183 + EXPECT_TRUE(String("foobaR").EndsWithCaseInsensitive("bar")); 1.1184 + EXPECT_TRUE(String("foobar").EndsWithCaseInsensitive("")); 1.1185 + EXPECT_TRUE(String("").EndsWithCaseInsensitive("")); 1.1186 + 1.1187 + EXPECT_FALSE(String("Foobar").EndsWithCaseInsensitive("foo")); 1.1188 + EXPECT_FALSE(String("foobar").EndsWithCaseInsensitive("Foo")); 1.1189 + EXPECT_FALSE(String("").EndsWithCaseInsensitive("foo")); 1.1190 +} 1.1191 + 1.1192 +// C++Builder's preprocessor is buggy; it fails to expand macros that 1.1193 +// appear in macro parameters after wide char literals. Provide an alias 1.1194 +// for NULL as a workaround. 1.1195 +static const wchar_t* const kNull = NULL; 1.1196 + 1.1197 +// Tests String::CaseInsensitiveWideCStringEquals 1.1198 +TEST(StringTest, CaseInsensitiveWideCStringEquals) { 1.1199 + EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(NULL, NULL)); 1.1200 + EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(kNull, L"")); 1.1201 + EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(L"", kNull)); 1.1202 + EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(kNull, L"foobar")); 1.1203 + EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(L"foobar", kNull)); 1.1204 + EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"foobar", L"foobar")); 1.1205 + EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"foobar", L"FOOBAR")); 1.1206 + EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"FOOBAR", L"foobar")); 1.1207 +} 1.1208 + 1.1209 +// Tests that NULL can be assigned to a String. 1.1210 +TEST(StringTest, CanBeAssignedNULL) { 1.1211 + const String src(NULL); 1.1212 + String dest; 1.1213 + 1.1214 + dest = src; 1.1215 + EXPECT_STREQ(NULL, dest.c_str()); 1.1216 +} 1.1217 + 1.1218 +// Tests that the empty string "" can be assigned to a String. 1.1219 +TEST(StringTest, CanBeAssignedEmpty) { 1.1220 + const String src(""); 1.1221 + String dest; 1.1222 + 1.1223 + dest = src; 1.1224 + EXPECT_STREQ("", dest.c_str()); 1.1225 +} 1.1226 + 1.1227 +// Tests that a non-empty string can be assigned to a String. 1.1228 +TEST(StringTest, CanBeAssignedNonEmpty) { 1.1229 + const String src("hello"); 1.1230 + String dest; 1.1231 + dest = src; 1.1232 + EXPECT_EQ(5U, dest.length()); 1.1233 + EXPECT_STREQ("hello", dest.c_str()); 1.1234 + 1.1235 + const String src2("x\0y", 3); 1.1236 + String dest2; 1.1237 + dest2 = src2; 1.1238 + EXPECT_EQ(3U, dest2.length()); 1.1239 + EXPECT_EQ('x', dest2.c_str()[0]); 1.1240 + EXPECT_EQ('\0', dest2.c_str()[1]); 1.1241 + EXPECT_EQ('y', dest2.c_str()[2]); 1.1242 +} 1.1243 + 1.1244 +// Tests that a String can be assigned to itself. 1.1245 +TEST(StringTest, CanBeAssignedSelf) { 1.1246 + String dest("hello"); 1.1247 + 1.1248 + // Use explicit function call notation here to suppress self-assign warning. 1.1249 + dest.operator=(dest); 1.1250 + EXPECT_STREQ("hello", dest.c_str()); 1.1251 +} 1.1252 + 1.1253 +// Sun Studio < 12 incorrectly rejects this code due to an overloading 1.1254 +// ambiguity. 1.1255 +#if !(defined(__SUNPRO_CC) && __SUNPRO_CC < 0x590) 1.1256 +// Tests streaming a String. 1.1257 +TEST(StringTest, Streams) { 1.1258 + EXPECT_EQ(StreamableToString(String()), "(null)"); 1.1259 + EXPECT_EQ(StreamableToString(String("")), ""); 1.1260 + EXPECT_EQ(StreamableToString(String("a\0b", 3)), "a\\0b"); 1.1261 +} 1.1262 +#endif 1.1263 + 1.1264 +// Tests that String::Format() works. 1.1265 +TEST(StringTest, FormatWorks) { 1.1266 + // Normal case: the format spec is valid, the arguments match the 1.1267 + // spec, and the result is < 4095 characters. 1.1268 + EXPECT_STREQ("Hello, 42", String::Format("%s, %d", "Hello", 42).c_str()); 1.1269 + 1.1270 + // Edge case: the result is 4095 characters. 1.1271 + char buffer[4096]; 1.1272 + const size_t kSize = sizeof(buffer); 1.1273 + memset(buffer, 'a', kSize - 1); 1.1274 + buffer[kSize - 1] = '\0'; 1.1275 + EXPECT_STREQ(buffer, String::Format("%s", buffer).c_str()); 1.1276 + 1.1277 + // The result needs to be 4096 characters, exceeding Format()'s limit. 1.1278 + EXPECT_STREQ("<formatting error or buffer exceeded>", 1.1279 + String::Format("x%s", buffer).c_str()); 1.1280 + 1.1281 +#if GTEST_OS_LINUX 1.1282 + // On Linux, invalid format spec should lead to an error message. 1.1283 + // In other environment (e.g. MSVC on Windows), String::Format() may 1.1284 + // simply ignore a bad format spec, so this assertion is run on 1.1285 + // Linux only. 1.1286 + EXPECT_STREQ("<formatting error or buffer exceeded>", 1.1287 + String::Format("%").c_str()); 1.1288 +#endif 1.1289 +} 1.1290 + 1.1291 +#if GTEST_OS_WINDOWS 1.1292 + 1.1293 +// Tests String::ShowWideCString(). 1.1294 +TEST(StringTest, ShowWideCString) { 1.1295 + EXPECT_STREQ("(null)", 1.1296 + String::ShowWideCString(NULL).c_str()); 1.1297 + EXPECT_STREQ("", String::ShowWideCString(L"").c_str()); 1.1298 + EXPECT_STREQ("foo", String::ShowWideCString(L"foo").c_str()); 1.1299 +} 1.1300 + 1.1301 +# if GTEST_OS_WINDOWS_MOBILE 1.1302 +TEST(StringTest, AnsiAndUtf16Null) { 1.1303 + EXPECT_EQ(NULL, String::AnsiToUtf16(NULL)); 1.1304 + EXPECT_EQ(NULL, String::Utf16ToAnsi(NULL)); 1.1305 +} 1.1306 + 1.1307 +TEST(StringTest, AnsiAndUtf16ConvertBasic) { 1.1308 + const char* ansi = String::Utf16ToAnsi(L"str"); 1.1309 + EXPECT_STREQ("str", ansi); 1.1310 + delete [] ansi; 1.1311 + const WCHAR* utf16 = String::AnsiToUtf16("str"); 1.1312 + EXPECT_EQ(0, wcsncmp(L"str", utf16, 3)); 1.1313 + delete [] utf16; 1.1314 +} 1.1315 + 1.1316 +TEST(StringTest, AnsiAndUtf16ConvertPathChars) { 1.1317 + const char* ansi = String::Utf16ToAnsi(L".:\\ \"*?"); 1.1318 + EXPECT_STREQ(".:\\ \"*?", ansi); 1.1319 + delete [] ansi; 1.1320 + const WCHAR* utf16 = String::AnsiToUtf16(".:\\ \"*?"); 1.1321 + EXPECT_EQ(0, wcsncmp(L".:\\ \"*?", utf16, 3)); 1.1322 + delete [] utf16; 1.1323 +} 1.1324 +# endif // GTEST_OS_WINDOWS_MOBILE 1.1325 + 1.1326 +#endif // GTEST_OS_WINDOWS 1.1327 + 1.1328 +// Tests TestProperty construction. 1.1329 +TEST(TestPropertyTest, StringValue) { 1.1330 + TestProperty property("key", "1"); 1.1331 + EXPECT_STREQ("key", property.key()); 1.1332 + EXPECT_STREQ("1", property.value()); 1.1333 +} 1.1334 + 1.1335 +// Tests TestProperty replacing a value. 1.1336 +TEST(TestPropertyTest, ReplaceStringValue) { 1.1337 + TestProperty property("key", "1"); 1.1338 + EXPECT_STREQ("1", property.value()); 1.1339 + property.SetValue("2"); 1.1340 + EXPECT_STREQ("2", property.value()); 1.1341 +} 1.1342 + 1.1343 +// AddFatalFailure() and AddNonfatalFailure() must be stand-alone 1.1344 +// functions (i.e. their definitions cannot be inlined at the call 1.1345 +// sites), or C++Builder won't compile the code. 1.1346 +static void AddFatalFailure() { 1.1347 + FAIL() << "Expected fatal failure."; 1.1348 +} 1.1349 + 1.1350 +static void AddNonfatalFailure() { 1.1351 + ADD_FAILURE() << "Expected non-fatal failure."; 1.1352 +} 1.1353 + 1.1354 +class ScopedFakeTestPartResultReporterTest : public Test { 1.1355 + public: // Must be public and not protected due to a bug in g++ 3.4.2. 1.1356 + enum FailureMode { 1.1357 + FATAL_FAILURE, 1.1358 + NONFATAL_FAILURE 1.1359 + }; 1.1360 + static void AddFailure(FailureMode failure) { 1.1361 + if (failure == FATAL_FAILURE) { 1.1362 + AddFatalFailure(); 1.1363 + } else { 1.1364 + AddNonfatalFailure(); 1.1365 + } 1.1366 + } 1.1367 +}; 1.1368 + 1.1369 +// Tests that ScopedFakeTestPartResultReporter intercepts test 1.1370 +// failures. 1.1371 +TEST_F(ScopedFakeTestPartResultReporterTest, InterceptsTestFailures) { 1.1372 + TestPartResultArray results; 1.1373 + { 1.1374 + ScopedFakeTestPartResultReporter reporter( 1.1375 + ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD, 1.1376 + &results); 1.1377 + AddFailure(NONFATAL_FAILURE); 1.1378 + AddFailure(FATAL_FAILURE); 1.1379 + } 1.1380 + 1.1381 + EXPECT_EQ(2, results.size()); 1.1382 + EXPECT_TRUE(results.GetTestPartResult(0).nonfatally_failed()); 1.1383 + EXPECT_TRUE(results.GetTestPartResult(1).fatally_failed()); 1.1384 +} 1.1385 + 1.1386 +TEST_F(ScopedFakeTestPartResultReporterTest, DeprecatedConstructor) { 1.1387 + TestPartResultArray results; 1.1388 + { 1.1389 + // Tests, that the deprecated constructor still works. 1.1390 + ScopedFakeTestPartResultReporter reporter(&results); 1.1391 + AddFailure(NONFATAL_FAILURE); 1.1392 + } 1.1393 + EXPECT_EQ(1, results.size()); 1.1394 +} 1.1395 + 1.1396 +#if GTEST_IS_THREADSAFE 1.1397 + 1.1398 +class ScopedFakeTestPartResultReporterWithThreadsTest 1.1399 + : public ScopedFakeTestPartResultReporterTest { 1.1400 + protected: 1.1401 + static void AddFailureInOtherThread(FailureMode failure) { 1.1402 + ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL); 1.1403 + thread.Join(); 1.1404 + } 1.1405 +}; 1.1406 + 1.1407 +TEST_F(ScopedFakeTestPartResultReporterWithThreadsTest, 1.1408 + InterceptsTestFailuresInAllThreads) { 1.1409 + TestPartResultArray results; 1.1410 + { 1.1411 + ScopedFakeTestPartResultReporter reporter( 1.1412 + ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, &results); 1.1413 + AddFailure(NONFATAL_FAILURE); 1.1414 + AddFailure(FATAL_FAILURE); 1.1415 + AddFailureInOtherThread(NONFATAL_FAILURE); 1.1416 + AddFailureInOtherThread(FATAL_FAILURE); 1.1417 + } 1.1418 + 1.1419 + EXPECT_EQ(4, results.size()); 1.1420 + EXPECT_TRUE(results.GetTestPartResult(0).nonfatally_failed()); 1.1421 + EXPECT_TRUE(results.GetTestPartResult(1).fatally_failed()); 1.1422 + EXPECT_TRUE(results.GetTestPartResult(2).nonfatally_failed()); 1.1423 + EXPECT_TRUE(results.GetTestPartResult(3).fatally_failed()); 1.1424 +} 1.1425 + 1.1426 +#endif // GTEST_IS_THREADSAFE 1.1427 + 1.1428 +// Tests EXPECT_FATAL_FAILURE{,ON_ALL_THREADS}. Makes sure that they 1.1429 +// work even if the failure is generated in a called function rather than 1.1430 +// the current context. 1.1431 + 1.1432 +typedef ScopedFakeTestPartResultReporterTest ExpectFatalFailureTest; 1.1433 + 1.1434 +TEST_F(ExpectFatalFailureTest, CatchesFatalFaliure) { 1.1435 + EXPECT_FATAL_FAILURE(AddFatalFailure(), "Expected fatal failure."); 1.1436 +} 1.1437 + 1.1438 +#if GTEST_HAS_GLOBAL_STRING 1.1439 +TEST_F(ExpectFatalFailureTest, AcceptsStringObject) { 1.1440 + EXPECT_FATAL_FAILURE(AddFatalFailure(), ::string("Expected fatal failure.")); 1.1441 +} 1.1442 +#endif 1.1443 + 1.1444 +TEST_F(ExpectFatalFailureTest, AcceptsStdStringObject) { 1.1445 + EXPECT_FATAL_FAILURE(AddFatalFailure(), 1.1446 + ::std::string("Expected fatal failure.")); 1.1447 +} 1.1448 + 1.1449 +TEST_F(ExpectFatalFailureTest, CatchesFatalFailureOnAllThreads) { 1.1450 + // We have another test below to verify that the macro catches fatal 1.1451 + // failures generated on another thread. 1.1452 + EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFatalFailure(), 1.1453 + "Expected fatal failure."); 1.1454 +} 1.1455 + 1.1456 +#ifdef __BORLANDC__ 1.1457 +// Silences warnings: "Condition is always true" 1.1458 +# pragma option push -w-ccc 1.1459 +#endif 1.1460 + 1.1461 +// Tests that EXPECT_FATAL_FAILURE() can be used in a non-void 1.1462 +// function even when the statement in it contains ASSERT_*. 1.1463 + 1.1464 +int NonVoidFunction() { 1.1465 + EXPECT_FATAL_FAILURE(ASSERT_TRUE(false), ""); 1.1466 + EXPECT_FATAL_FAILURE_ON_ALL_THREADS(FAIL(), ""); 1.1467 + return 0; 1.1468 +} 1.1469 + 1.1470 +TEST_F(ExpectFatalFailureTest, CanBeUsedInNonVoidFunction) { 1.1471 + NonVoidFunction(); 1.1472 +} 1.1473 + 1.1474 +// Tests that EXPECT_FATAL_FAILURE(statement, ...) doesn't abort the 1.1475 +// current function even though 'statement' generates a fatal failure. 1.1476 + 1.1477 +void DoesNotAbortHelper(bool* aborted) { 1.1478 + EXPECT_FATAL_FAILURE(ASSERT_TRUE(false), ""); 1.1479 + EXPECT_FATAL_FAILURE_ON_ALL_THREADS(FAIL(), ""); 1.1480 + 1.1481 + *aborted = false; 1.1482 +} 1.1483 + 1.1484 +#ifdef __BORLANDC__ 1.1485 +// Restores warnings after previous "#pragma option push" suppressed them. 1.1486 +# pragma option pop 1.1487 +#endif 1.1488 + 1.1489 +TEST_F(ExpectFatalFailureTest, DoesNotAbort) { 1.1490 + bool aborted = true; 1.1491 + DoesNotAbortHelper(&aborted); 1.1492 + EXPECT_FALSE(aborted); 1.1493 +} 1.1494 + 1.1495 +// Tests that the EXPECT_FATAL_FAILURE{,_ON_ALL_THREADS} accepts a 1.1496 +// statement that contains a macro which expands to code containing an 1.1497 +// unprotected comma. 1.1498 + 1.1499 +static int global_var = 0; 1.1500 +#define GTEST_USE_UNPROTECTED_COMMA_ global_var++, global_var++ 1.1501 + 1.1502 +TEST_F(ExpectFatalFailureTest, AcceptsMacroThatExpandsToUnprotectedComma) { 1.1503 +#ifndef __BORLANDC__ 1.1504 + // ICE's in C++Builder. 1.1505 + EXPECT_FATAL_FAILURE({ 1.1506 + GTEST_USE_UNPROTECTED_COMMA_; 1.1507 + AddFatalFailure(); 1.1508 + }, ""); 1.1509 +#endif 1.1510 + 1.1511 + EXPECT_FATAL_FAILURE_ON_ALL_THREADS({ 1.1512 + GTEST_USE_UNPROTECTED_COMMA_; 1.1513 + AddFatalFailure(); 1.1514 + }, ""); 1.1515 +} 1.1516 + 1.1517 +// Tests EXPECT_NONFATAL_FAILURE{,ON_ALL_THREADS}. 1.1518 + 1.1519 +typedef ScopedFakeTestPartResultReporterTest ExpectNonfatalFailureTest; 1.1520 + 1.1521 +TEST_F(ExpectNonfatalFailureTest, CatchesNonfatalFailure) { 1.1522 + EXPECT_NONFATAL_FAILURE(AddNonfatalFailure(), 1.1523 + "Expected non-fatal failure."); 1.1524 +} 1.1525 + 1.1526 +#if GTEST_HAS_GLOBAL_STRING 1.1527 +TEST_F(ExpectNonfatalFailureTest, AcceptsStringObject) { 1.1528 + EXPECT_NONFATAL_FAILURE(AddNonfatalFailure(), 1.1529 + ::string("Expected non-fatal failure.")); 1.1530 +} 1.1531 +#endif 1.1532 + 1.1533 +TEST_F(ExpectNonfatalFailureTest, AcceptsStdStringObject) { 1.1534 + EXPECT_NONFATAL_FAILURE(AddNonfatalFailure(), 1.1535 + ::std::string("Expected non-fatal failure.")); 1.1536 +} 1.1537 + 1.1538 +TEST_F(ExpectNonfatalFailureTest, CatchesNonfatalFailureOnAllThreads) { 1.1539 + // We have another test below to verify that the macro catches 1.1540 + // non-fatal failures generated on another thread. 1.1541 + EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddNonfatalFailure(), 1.1542 + "Expected non-fatal failure."); 1.1543 +} 1.1544 + 1.1545 +// Tests that the EXPECT_NONFATAL_FAILURE{,_ON_ALL_THREADS} accepts a 1.1546 +// statement that contains a macro which expands to code containing an 1.1547 +// unprotected comma. 1.1548 +TEST_F(ExpectNonfatalFailureTest, AcceptsMacroThatExpandsToUnprotectedComma) { 1.1549 + EXPECT_NONFATAL_FAILURE({ 1.1550 + GTEST_USE_UNPROTECTED_COMMA_; 1.1551 + AddNonfatalFailure(); 1.1552 + }, ""); 1.1553 + 1.1554 + EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS({ 1.1555 + GTEST_USE_UNPROTECTED_COMMA_; 1.1556 + AddNonfatalFailure(); 1.1557 + }, ""); 1.1558 +} 1.1559 + 1.1560 +#if GTEST_IS_THREADSAFE 1.1561 + 1.1562 +typedef ScopedFakeTestPartResultReporterWithThreadsTest 1.1563 + ExpectFailureWithThreadsTest; 1.1564 + 1.1565 +TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailureOnAllThreads) { 1.1566 + EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailureInOtherThread(FATAL_FAILURE), 1.1567 + "Expected fatal failure."); 1.1568 +} 1.1569 + 1.1570 +TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailureOnAllThreads) { 1.1571 + EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS( 1.1572 + AddFailureInOtherThread(NONFATAL_FAILURE), "Expected non-fatal failure."); 1.1573 +} 1.1574 + 1.1575 +#endif // GTEST_IS_THREADSAFE 1.1576 + 1.1577 +// Tests the TestProperty class. 1.1578 + 1.1579 +TEST(TestPropertyTest, ConstructorWorks) { 1.1580 + const TestProperty property("key", "value"); 1.1581 + EXPECT_STREQ("key", property.key()); 1.1582 + EXPECT_STREQ("value", property.value()); 1.1583 +} 1.1584 + 1.1585 +TEST(TestPropertyTest, SetValue) { 1.1586 + TestProperty property("key", "value_1"); 1.1587 + EXPECT_STREQ("key", property.key()); 1.1588 + property.SetValue("value_2"); 1.1589 + EXPECT_STREQ("key", property.key()); 1.1590 + EXPECT_STREQ("value_2", property.value()); 1.1591 +} 1.1592 + 1.1593 +// Tests the TestResult class 1.1594 + 1.1595 +// The test fixture for testing TestResult. 1.1596 +class TestResultTest : public Test { 1.1597 + protected: 1.1598 + typedef std::vector<TestPartResult> TPRVector; 1.1599 + 1.1600 + // We make use of 2 TestPartResult objects, 1.1601 + TestPartResult * pr1, * pr2; 1.1602 + 1.1603 + // ... and 3 TestResult objects. 1.1604 + TestResult * r0, * r1, * r2; 1.1605 + 1.1606 + virtual void SetUp() { 1.1607 + // pr1 is for success. 1.1608 + pr1 = new TestPartResult(TestPartResult::kSuccess, 1.1609 + "foo/bar.cc", 1.1610 + 10, 1.1611 + "Success!"); 1.1612 + 1.1613 + // pr2 is for fatal failure. 1.1614 + pr2 = new TestPartResult(TestPartResult::kFatalFailure, 1.1615 + "foo/bar.cc", 1.1616 + -1, // This line number means "unknown" 1.1617 + "Failure!"); 1.1618 + 1.1619 + // Creates the TestResult objects. 1.1620 + r0 = new TestResult(); 1.1621 + r1 = new TestResult(); 1.1622 + r2 = new TestResult(); 1.1623 + 1.1624 + // In order to test TestResult, we need to modify its internal 1.1625 + // state, in particular the TestPartResult vector it holds. 1.1626 + // test_part_results() returns a const reference to this vector. 1.1627 + // We cast it to a non-const object s.t. it can be modified (yes, 1.1628 + // this is a hack). 1.1629 + TPRVector* results1 = const_cast<TPRVector*>( 1.1630 + &TestResultAccessor::test_part_results(*r1)); 1.1631 + TPRVector* results2 = const_cast<TPRVector*>( 1.1632 + &TestResultAccessor::test_part_results(*r2)); 1.1633 + 1.1634 + // r0 is an empty TestResult. 1.1635 + 1.1636 + // r1 contains a single SUCCESS TestPartResult. 1.1637 + results1->push_back(*pr1); 1.1638 + 1.1639 + // r2 contains a SUCCESS, and a FAILURE. 1.1640 + results2->push_back(*pr1); 1.1641 + results2->push_back(*pr2); 1.1642 + } 1.1643 + 1.1644 + virtual void TearDown() { 1.1645 + delete pr1; 1.1646 + delete pr2; 1.1647 + 1.1648 + delete r0; 1.1649 + delete r1; 1.1650 + delete r2; 1.1651 + } 1.1652 + 1.1653 + // Helper that compares two two TestPartResults. 1.1654 + static void CompareTestPartResult(const TestPartResult& expected, 1.1655 + const TestPartResult& actual) { 1.1656 + EXPECT_EQ(expected.type(), actual.type()); 1.1657 + EXPECT_STREQ(expected.file_name(), actual.file_name()); 1.1658 + EXPECT_EQ(expected.line_number(), actual.line_number()); 1.1659 + EXPECT_STREQ(expected.summary(), actual.summary()); 1.1660 + EXPECT_STREQ(expected.message(), actual.message()); 1.1661 + EXPECT_EQ(expected.passed(), actual.passed()); 1.1662 + EXPECT_EQ(expected.failed(), actual.failed()); 1.1663 + EXPECT_EQ(expected.nonfatally_failed(), actual.nonfatally_failed()); 1.1664 + EXPECT_EQ(expected.fatally_failed(), actual.fatally_failed()); 1.1665 + } 1.1666 +}; 1.1667 + 1.1668 +// Tests TestResult::total_part_count(). 1.1669 +TEST_F(TestResultTest, total_part_count) { 1.1670 + ASSERT_EQ(0, r0->total_part_count()); 1.1671 + ASSERT_EQ(1, r1->total_part_count()); 1.1672 + ASSERT_EQ(2, r2->total_part_count()); 1.1673 +} 1.1674 + 1.1675 +// Tests TestResult::Passed(). 1.1676 +TEST_F(TestResultTest, Passed) { 1.1677 + ASSERT_TRUE(r0->Passed()); 1.1678 + ASSERT_TRUE(r1->Passed()); 1.1679 + ASSERT_FALSE(r2->Passed()); 1.1680 +} 1.1681 + 1.1682 +// Tests TestResult::Failed(). 1.1683 +TEST_F(TestResultTest, Failed) { 1.1684 + ASSERT_FALSE(r0->Failed()); 1.1685 + ASSERT_FALSE(r1->Failed()); 1.1686 + ASSERT_TRUE(r2->Failed()); 1.1687 +} 1.1688 + 1.1689 +// Tests TestResult::GetTestPartResult(). 1.1690 + 1.1691 +typedef TestResultTest TestResultDeathTest; 1.1692 + 1.1693 +TEST_F(TestResultDeathTest, GetTestPartResult) { 1.1694 + CompareTestPartResult(*pr1, r2->GetTestPartResult(0)); 1.1695 + CompareTestPartResult(*pr2, r2->GetTestPartResult(1)); 1.1696 + EXPECT_DEATH_IF_SUPPORTED(r2->GetTestPartResult(2), ""); 1.1697 + EXPECT_DEATH_IF_SUPPORTED(r2->GetTestPartResult(-1), ""); 1.1698 +} 1.1699 + 1.1700 +// Tests TestResult has no properties when none are added. 1.1701 +TEST(TestResultPropertyTest, NoPropertiesFoundWhenNoneAreAdded) { 1.1702 + TestResult test_result; 1.1703 + ASSERT_EQ(0, test_result.test_property_count()); 1.1704 +} 1.1705 + 1.1706 +// Tests TestResult has the expected property when added. 1.1707 +TEST(TestResultPropertyTest, OnePropertyFoundWhenAdded) { 1.1708 + TestResult test_result; 1.1709 + TestProperty property("key_1", "1"); 1.1710 + TestResultAccessor::RecordProperty(&test_result, property); 1.1711 + ASSERT_EQ(1, test_result.test_property_count()); 1.1712 + const TestProperty& actual_property = test_result.GetTestProperty(0); 1.1713 + EXPECT_STREQ("key_1", actual_property.key()); 1.1714 + EXPECT_STREQ("1", actual_property.value()); 1.1715 +} 1.1716 + 1.1717 +// Tests TestResult has multiple properties when added. 1.1718 +TEST(TestResultPropertyTest, MultiplePropertiesFoundWhenAdded) { 1.1719 + TestResult test_result; 1.1720 + TestProperty property_1("key_1", "1"); 1.1721 + TestProperty property_2("key_2", "2"); 1.1722 + TestResultAccessor::RecordProperty(&test_result, property_1); 1.1723 + TestResultAccessor::RecordProperty(&test_result, property_2); 1.1724 + ASSERT_EQ(2, test_result.test_property_count()); 1.1725 + const TestProperty& actual_property_1 = test_result.GetTestProperty(0); 1.1726 + EXPECT_STREQ("key_1", actual_property_1.key()); 1.1727 + EXPECT_STREQ("1", actual_property_1.value()); 1.1728 + 1.1729 + const TestProperty& actual_property_2 = test_result.GetTestProperty(1); 1.1730 + EXPECT_STREQ("key_2", actual_property_2.key()); 1.1731 + EXPECT_STREQ("2", actual_property_2.value()); 1.1732 +} 1.1733 + 1.1734 +// Tests TestResult::RecordProperty() overrides values for duplicate keys. 1.1735 +TEST(TestResultPropertyTest, OverridesValuesForDuplicateKeys) { 1.1736 + TestResult test_result; 1.1737 + TestProperty property_1_1("key_1", "1"); 1.1738 + TestProperty property_2_1("key_2", "2"); 1.1739 + TestProperty property_1_2("key_1", "12"); 1.1740 + TestProperty property_2_2("key_2", "22"); 1.1741 + TestResultAccessor::RecordProperty(&test_result, property_1_1); 1.1742 + TestResultAccessor::RecordProperty(&test_result, property_2_1); 1.1743 + TestResultAccessor::RecordProperty(&test_result, property_1_2); 1.1744 + TestResultAccessor::RecordProperty(&test_result, property_2_2); 1.1745 + 1.1746 + ASSERT_EQ(2, test_result.test_property_count()); 1.1747 + const TestProperty& actual_property_1 = test_result.GetTestProperty(0); 1.1748 + EXPECT_STREQ("key_1", actual_property_1.key()); 1.1749 + EXPECT_STREQ("12", actual_property_1.value()); 1.1750 + 1.1751 + const TestProperty& actual_property_2 = test_result.GetTestProperty(1); 1.1752 + EXPECT_STREQ("key_2", actual_property_2.key()); 1.1753 + EXPECT_STREQ("22", actual_property_2.value()); 1.1754 +} 1.1755 + 1.1756 +// Tests TestResult::GetTestProperty(). 1.1757 +TEST(TestResultPropertyDeathTest, GetTestProperty) { 1.1758 + TestResult test_result; 1.1759 + TestProperty property_1("key_1", "1"); 1.1760 + TestProperty property_2("key_2", "2"); 1.1761 + TestProperty property_3("key_3", "3"); 1.1762 + TestResultAccessor::RecordProperty(&test_result, property_1); 1.1763 + TestResultAccessor::RecordProperty(&test_result, property_2); 1.1764 + TestResultAccessor::RecordProperty(&test_result, property_3); 1.1765 + 1.1766 + const TestProperty& fetched_property_1 = test_result.GetTestProperty(0); 1.1767 + const TestProperty& fetched_property_2 = test_result.GetTestProperty(1); 1.1768 + const TestProperty& fetched_property_3 = test_result.GetTestProperty(2); 1.1769 + 1.1770 + EXPECT_STREQ("key_1", fetched_property_1.key()); 1.1771 + EXPECT_STREQ("1", fetched_property_1.value()); 1.1772 + 1.1773 + EXPECT_STREQ("key_2", fetched_property_2.key()); 1.1774 + EXPECT_STREQ("2", fetched_property_2.value()); 1.1775 + 1.1776 + EXPECT_STREQ("key_3", fetched_property_3.key()); 1.1777 + EXPECT_STREQ("3", fetched_property_3.value()); 1.1778 + 1.1779 + EXPECT_DEATH_IF_SUPPORTED(test_result.GetTestProperty(3), ""); 1.1780 + EXPECT_DEATH_IF_SUPPORTED(test_result.GetTestProperty(-1), ""); 1.1781 +} 1.1782 + 1.1783 +// When a property using a reserved key is supplied to this function, it tests 1.1784 +// that a non-fatal failure is added, a fatal failure is not added, and that the 1.1785 +// property is not recorded. 1.1786 +void ExpectNonFatalFailureRecordingPropertyWithReservedKey(const char* key) { 1.1787 + TestResult test_result; 1.1788 + TestProperty property(key, "1"); 1.1789 + EXPECT_NONFATAL_FAILURE( 1.1790 + TestResultAccessor::RecordProperty(&test_result, property), 1.1791 + "Reserved key"); 1.1792 + ASSERT_EQ(0, test_result.test_property_count()) << "Not recorded"; 1.1793 +} 1.1794 + 1.1795 +// Attempting to recording a property with the Reserved literal "name" 1.1796 +// should add a non-fatal failure and the property should not be recorded. 1.1797 +TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledName) { 1.1798 + ExpectNonFatalFailureRecordingPropertyWithReservedKey("name"); 1.1799 +} 1.1800 + 1.1801 +// Attempting to recording a property with the Reserved literal "status" 1.1802 +// should add a non-fatal failure and the property should not be recorded. 1.1803 +TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledStatus) { 1.1804 + ExpectNonFatalFailureRecordingPropertyWithReservedKey("status"); 1.1805 +} 1.1806 + 1.1807 +// Attempting to recording a property with the Reserved literal "time" 1.1808 +// should add a non-fatal failure and the property should not be recorded. 1.1809 +TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledTime) { 1.1810 + ExpectNonFatalFailureRecordingPropertyWithReservedKey("time"); 1.1811 +} 1.1812 + 1.1813 +// Attempting to recording a property with the Reserved literal "classname" 1.1814 +// should add a non-fatal failure and the property should not be recorded. 1.1815 +TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledClassname) { 1.1816 + ExpectNonFatalFailureRecordingPropertyWithReservedKey("classname"); 1.1817 +} 1.1818 + 1.1819 +// Tests that GTestFlagSaver works on Windows and Mac. 1.1820 + 1.1821 +class GTestFlagSaverTest : public Test { 1.1822 + protected: 1.1823 + // Saves the Google Test flags such that we can restore them later, and 1.1824 + // then sets them to their default values. This will be called 1.1825 + // before the first test in this test case is run. 1.1826 + static void SetUpTestCase() { 1.1827 + saver_ = new GTestFlagSaver; 1.1828 + 1.1829 + GTEST_FLAG(also_run_disabled_tests) = false; 1.1830 + GTEST_FLAG(break_on_failure) = false; 1.1831 + GTEST_FLAG(catch_exceptions) = false; 1.1832 + GTEST_FLAG(death_test_use_fork) = false; 1.1833 + GTEST_FLAG(color) = "auto"; 1.1834 + GTEST_FLAG(filter) = ""; 1.1835 + GTEST_FLAG(list_tests) = false; 1.1836 + GTEST_FLAG(output) = ""; 1.1837 + GTEST_FLAG(print_time) = true; 1.1838 + GTEST_FLAG(random_seed) = 0; 1.1839 + GTEST_FLAG(repeat) = 1; 1.1840 + GTEST_FLAG(shuffle) = false; 1.1841 + GTEST_FLAG(stack_trace_depth) = kMaxStackTraceDepth; 1.1842 + GTEST_FLAG(stream_result_to) = ""; 1.1843 + GTEST_FLAG(throw_on_failure) = false; 1.1844 + } 1.1845 + 1.1846 + // Restores the Google Test flags that the tests have modified. This will 1.1847 + // be called after the last test in this test case is run. 1.1848 + static void TearDownTestCase() { 1.1849 + delete saver_; 1.1850 + saver_ = NULL; 1.1851 + } 1.1852 + 1.1853 + // Verifies that the Google Test flags have their default values, and then 1.1854 + // modifies each of them. 1.1855 + void VerifyAndModifyFlags() { 1.1856 + EXPECT_FALSE(GTEST_FLAG(also_run_disabled_tests)); 1.1857 + EXPECT_FALSE(GTEST_FLAG(break_on_failure)); 1.1858 + EXPECT_FALSE(GTEST_FLAG(catch_exceptions)); 1.1859 + EXPECT_STREQ("auto", GTEST_FLAG(color).c_str()); 1.1860 + EXPECT_FALSE(GTEST_FLAG(death_test_use_fork)); 1.1861 + EXPECT_STREQ("", GTEST_FLAG(filter).c_str()); 1.1862 + EXPECT_FALSE(GTEST_FLAG(list_tests)); 1.1863 + EXPECT_STREQ("", GTEST_FLAG(output).c_str()); 1.1864 + EXPECT_TRUE(GTEST_FLAG(print_time)); 1.1865 + EXPECT_EQ(0, GTEST_FLAG(random_seed)); 1.1866 + EXPECT_EQ(1, GTEST_FLAG(repeat)); 1.1867 + EXPECT_FALSE(GTEST_FLAG(shuffle)); 1.1868 + EXPECT_EQ(kMaxStackTraceDepth, GTEST_FLAG(stack_trace_depth)); 1.1869 + EXPECT_STREQ("", GTEST_FLAG(stream_result_to).c_str()); 1.1870 + EXPECT_FALSE(GTEST_FLAG(throw_on_failure)); 1.1871 + 1.1872 + GTEST_FLAG(also_run_disabled_tests) = true; 1.1873 + GTEST_FLAG(break_on_failure) = true; 1.1874 + GTEST_FLAG(catch_exceptions) = true; 1.1875 + GTEST_FLAG(color) = "no"; 1.1876 + GTEST_FLAG(death_test_use_fork) = true; 1.1877 + GTEST_FLAG(filter) = "abc"; 1.1878 + GTEST_FLAG(list_tests) = true; 1.1879 + GTEST_FLAG(output) = "xml:foo.xml"; 1.1880 + GTEST_FLAG(print_time) = false; 1.1881 + GTEST_FLAG(random_seed) = 1; 1.1882 + GTEST_FLAG(repeat) = 100; 1.1883 + GTEST_FLAG(shuffle) = true; 1.1884 + GTEST_FLAG(stack_trace_depth) = 1; 1.1885 + GTEST_FLAG(stream_result_to) = "localhost:1234"; 1.1886 + GTEST_FLAG(throw_on_failure) = true; 1.1887 + } 1.1888 + 1.1889 + private: 1.1890 + // For saving Google Test flags during this test case. 1.1891 + static GTestFlagSaver* saver_; 1.1892 +}; 1.1893 + 1.1894 +GTestFlagSaver* GTestFlagSaverTest::saver_ = NULL; 1.1895 + 1.1896 +// Google Test doesn't guarantee the order of tests. The following two 1.1897 +// tests are designed to work regardless of their order. 1.1898 + 1.1899 +// Modifies the Google Test flags in the test body. 1.1900 +TEST_F(GTestFlagSaverTest, ModifyGTestFlags) { 1.1901 + VerifyAndModifyFlags(); 1.1902 +} 1.1903 + 1.1904 +// Verifies that the Google Test flags in the body of the previous test were 1.1905 +// restored to their original values. 1.1906 +TEST_F(GTestFlagSaverTest, VerifyGTestFlags) { 1.1907 + VerifyAndModifyFlags(); 1.1908 +} 1.1909 + 1.1910 +// Sets an environment variable with the given name to the given 1.1911 +// value. If the value argument is "", unsets the environment 1.1912 +// variable. The caller must ensure that both arguments are not NULL. 1.1913 +static void SetEnv(const char* name, const char* value) { 1.1914 +#if GTEST_OS_WINDOWS_MOBILE 1.1915 + // Environment variables are not supported on Windows CE. 1.1916 + return; 1.1917 +#elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9) 1.1918 + // C++Builder's putenv only stores a pointer to its parameter; we have to 1.1919 + // ensure that the string remains valid as long as it might be needed. 1.1920 + // We use an std::map to do so. 1.1921 + static std::map<String, String*> added_env; 1.1922 + 1.1923 + // Because putenv stores a pointer to the string buffer, we can't delete the 1.1924 + // previous string (if present) until after it's replaced. 1.1925 + String *prev_env = NULL; 1.1926 + if (added_env.find(name) != added_env.end()) { 1.1927 + prev_env = added_env[name]; 1.1928 + } 1.1929 + added_env[name] = new String((Message() << name << "=" << value).GetString()); 1.1930 + 1.1931 + // The standard signature of putenv accepts a 'char*' argument. Other 1.1932 + // implementations, like C++Builder's, accept a 'const char*'. 1.1933 + // We cast away the 'const' since that would work for both variants. 1.1934 + putenv(const_cast<char*>(added_env[name]->c_str())); 1.1935 + delete prev_env; 1.1936 +#elif GTEST_OS_WINDOWS // If we are on Windows proper. 1.1937 + _putenv((Message() << name << "=" << value).GetString().c_str()); 1.1938 +#else 1.1939 + if (*value == '\0') { 1.1940 + unsetenv(name); 1.1941 + } else { 1.1942 + setenv(name, value, 1); 1.1943 + } 1.1944 +#endif // GTEST_OS_WINDOWS_MOBILE 1.1945 +} 1.1946 + 1.1947 +#if !GTEST_OS_WINDOWS_MOBILE 1.1948 +// Environment variables are not supported on Windows CE. 1.1949 + 1.1950 +using testing::internal::Int32FromGTestEnv; 1.1951 + 1.1952 +// Tests Int32FromGTestEnv(). 1.1953 + 1.1954 +// Tests that Int32FromGTestEnv() returns the default value when the 1.1955 +// environment variable is not set. 1.1956 +TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenVariableIsNotSet) { 1.1957 + SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", ""); 1.1958 + EXPECT_EQ(10, Int32FromGTestEnv("temp", 10)); 1.1959 +} 1.1960 + 1.1961 +// Tests that Int32FromGTestEnv() returns the default value when the 1.1962 +// environment variable overflows as an Int32. 1.1963 +TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenValueOverflows) { 1.1964 + printf("(expecting 2 warnings)\n"); 1.1965 + 1.1966 + SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "12345678987654321"); 1.1967 + EXPECT_EQ(20, Int32FromGTestEnv("temp", 20)); 1.1968 + 1.1969 + SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "-12345678987654321"); 1.1970 + EXPECT_EQ(30, Int32FromGTestEnv("temp", 30)); 1.1971 +} 1.1972 + 1.1973 +// Tests that Int32FromGTestEnv() returns the default value when the 1.1974 +// environment variable does not represent a valid decimal integer. 1.1975 +TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenValueIsInvalid) { 1.1976 + printf("(expecting 2 warnings)\n"); 1.1977 + 1.1978 + SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "A1"); 1.1979 + EXPECT_EQ(40, Int32FromGTestEnv("temp", 40)); 1.1980 + 1.1981 + SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "12X"); 1.1982 + EXPECT_EQ(50, Int32FromGTestEnv("temp", 50)); 1.1983 +} 1.1984 + 1.1985 +// Tests that Int32FromGTestEnv() parses and returns the value of the 1.1986 +// environment variable when it represents a valid decimal integer in 1.1987 +// the range of an Int32. 1.1988 +TEST(Int32FromGTestEnvTest, ParsesAndReturnsValidValue) { 1.1989 + SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "123"); 1.1990 + EXPECT_EQ(123, Int32FromGTestEnv("temp", 0)); 1.1991 + 1.1992 + SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "-321"); 1.1993 + EXPECT_EQ(-321, Int32FromGTestEnv("temp", 0)); 1.1994 +} 1.1995 +#endif // !GTEST_OS_WINDOWS_MOBILE 1.1996 + 1.1997 +// Tests ParseInt32Flag(). 1.1998 + 1.1999 +// Tests that ParseInt32Flag() returns false and doesn't change the 1.2000 +// output value when the flag has wrong format 1.2001 +TEST(ParseInt32FlagTest, ReturnsFalseForInvalidFlag) { 1.2002 + Int32 value = 123; 1.2003 + EXPECT_FALSE(ParseInt32Flag("--a=100", "b", &value)); 1.2004 + EXPECT_EQ(123, value); 1.2005 + 1.2006 + EXPECT_FALSE(ParseInt32Flag("a=100", "a", &value)); 1.2007 + EXPECT_EQ(123, value); 1.2008 +} 1.2009 + 1.2010 +// Tests that ParseInt32Flag() returns false and doesn't change the 1.2011 +// output value when the flag overflows as an Int32. 1.2012 +TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueOverflows) { 1.2013 + printf("(expecting 2 warnings)\n"); 1.2014 + 1.2015 + Int32 value = 123; 1.2016 + EXPECT_FALSE(ParseInt32Flag("--abc=12345678987654321", "abc", &value)); 1.2017 + EXPECT_EQ(123, value); 1.2018 + 1.2019 + EXPECT_FALSE(ParseInt32Flag("--abc=-12345678987654321", "abc", &value)); 1.2020 + EXPECT_EQ(123, value); 1.2021 +} 1.2022 + 1.2023 +// Tests that ParseInt32Flag() returns false and doesn't change the 1.2024 +// output value when the flag does not represent a valid decimal 1.2025 +// integer. 1.2026 +TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueIsInvalid) { 1.2027 + printf("(expecting 2 warnings)\n"); 1.2028 + 1.2029 + Int32 value = 123; 1.2030 + EXPECT_FALSE(ParseInt32Flag("--abc=A1", "abc", &value)); 1.2031 + EXPECT_EQ(123, value); 1.2032 + 1.2033 + EXPECT_FALSE(ParseInt32Flag("--abc=12X", "abc", &value)); 1.2034 + EXPECT_EQ(123, value); 1.2035 +} 1.2036 + 1.2037 +// Tests that ParseInt32Flag() parses the value of the flag and 1.2038 +// returns true when the flag represents a valid decimal integer in 1.2039 +// the range of an Int32. 1.2040 +TEST(ParseInt32FlagTest, ParsesAndReturnsValidValue) { 1.2041 + Int32 value = 123; 1.2042 + EXPECT_TRUE(ParseInt32Flag("--" GTEST_FLAG_PREFIX_ "abc=456", "abc", &value)); 1.2043 + EXPECT_EQ(456, value); 1.2044 + 1.2045 + EXPECT_TRUE(ParseInt32Flag("--" GTEST_FLAG_PREFIX_ "abc=-789", 1.2046 + "abc", &value)); 1.2047 + EXPECT_EQ(-789, value); 1.2048 +} 1.2049 + 1.2050 +// Tests that Int32FromEnvOrDie() parses the value of the var or 1.2051 +// returns the correct default. 1.2052 +// Environment variables are not supported on Windows CE. 1.2053 +#if !GTEST_OS_WINDOWS_MOBILE 1.2054 +TEST(Int32FromEnvOrDieTest, ParsesAndReturnsValidValue) { 1.2055 + EXPECT_EQ(333, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333)); 1.2056 + SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "123"); 1.2057 + EXPECT_EQ(123, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333)); 1.2058 + SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "-123"); 1.2059 + EXPECT_EQ(-123, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333)); 1.2060 +} 1.2061 +#endif // !GTEST_OS_WINDOWS_MOBILE 1.2062 + 1.2063 +// Tests that Int32FromEnvOrDie() aborts with an error message 1.2064 +// if the variable is not an Int32. 1.2065 +TEST(Int32FromEnvOrDieDeathTest, AbortsOnFailure) { 1.2066 + SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "xxx"); 1.2067 + EXPECT_DEATH_IF_SUPPORTED( 1.2068 + Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123), 1.2069 + ".*"); 1.2070 +} 1.2071 + 1.2072 +// Tests that Int32FromEnvOrDie() aborts with an error message 1.2073 +// if the variable cannot be represnted by an Int32. 1.2074 +TEST(Int32FromEnvOrDieDeathTest, AbortsOnInt32Overflow) { 1.2075 + SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "1234567891234567891234"); 1.2076 + EXPECT_DEATH_IF_SUPPORTED( 1.2077 + Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123), 1.2078 + ".*"); 1.2079 +} 1.2080 + 1.2081 +// Tests that ShouldRunTestOnShard() selects all tests 1.2082 +// where there is 1 shard. 1.2083 +TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereIsOneShard) { 1.2084 + EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 0)); 1.2085 + EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 1)); 1.2086 + EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 2)); 1.2087 + EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 3)); 1.2088 + EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 4)); 1.2089 +} 1.2090 + 1.2091 +class ShouldShardTest : public testing::Test { 1.2092 + protected: 1.2093 + virtual void SetUp() { 1.2094 + index_var_ = GTEST_FLAG_PREFIX_UPPER_ "INDEX"; 1.2095 + total_var_ = GTEST_FLAG_PREFIX_UPPER_ "TOTAL"; 1.2096 + } 1.2097 + 1.2098 + virtual void TearDown() { 1.2099 + SetEnv(index_var_, ""); 1.2100 + SetEnv(total_var_, ""); 1.2101 + } 1.2102 + 1.2103 + const char* index_var_; 1.2104 + const char* total_var_; 1.2105 +}; 1.2106 + 1.2107 +// Tests that sharding is disabled if neither of the environment variables 1.2108 +// are set. 1.2109 +TEST_F(ShouldShardTest, ReturnsFalseWhenNeitherEnvVarIsSet) { 1.2110 + SetEnv(index_var_, ""); 1.2111 + SetEnv(total_var_, ""); 1.2112 + 1.2113 + EXPECT_FALSE(ShouldShard(total_var_, index_var_, false)); 1.2114 + EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); 1.2115 +} 1.2116 + 1.2117 +// Tests that sharding is not enabled if total_shards == 1. 1.2118 +TEST_F(ShouldShardTest, ReturnsFalseWhenTotalShardIsOne) { 1.2119 + SetEnv(index_var_, "0"); 1.2120 + SetEnv(total_var_, "1"); 1.2121 + EXPECT_FALSE(ShouldShard(total_var_, index_var_, false)); 1.2122 + EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); 1.2123 +} 1.2124 + 1.2125 +// Tests that sharding is enabled if total_shards > 1 and 1.2126 +// we are not in a death test subprocess. 1.2127 +// Environment variables are not supported on Windows CE. 1.2128 +#if !GTEST_OS_WINDOWS_MOBILE 1.2129 +TEST_F(ShouldShardTest, WorksWhenShardEnvVarsAreValid) { 1.2130 + SetEnv(index_var_, "4"); 1.2131 + SetEnv(total_var_, "22"); 1.2132 + EXPECT_TRUE(ShouldShard(total_var_, index_var_, false)); 1.2133 + EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); 1.2134 + 1.2135 + SetEnv(index_var_, "8"); 1.2136 + SetEnv(total_var_, "9"); 1.2137 + EXPECT_TRUE(ShouldShard(total_var_, index_var_, false)); 1.2138 + EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); 1.2139 + 1.2140 + SetEnv(index_var_, "0"); 1.2141 + SetEnv(total_var_, "9"); 1.2142 + EXPECT_TRUE(ShouldShard(total_var_, index_var_, false)); 1.2143 + EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); 1.2144 +} 1.2145 +#endif // !GTEST_OS_WINDOWS_MOBILE 1.2146 + 1.2147 +// Tests that we exit in error if the sharding values are not valid. 1.2148 + 1.2149 +typedef ShouldShardTest ShouldShardDeathTest; 1.2150 + 1.2151 +TEST_F(ShouldShardDeathTest, AbortsWhenShardingEnvVarsAreInvalid) { 1.2152 + SetEnv(index_var_, "4"); 1.2153 + SetEnv(total_var_, "4"); 1.2154 + EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); 1.2155 + 1.2156 + SetEnv(index_var_, "4"); 1.2157 + SetEnv(total_var_, "-2"); 1.2158 + EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); 1.2159 + 1.2160 + SetEnv(index_var_, "5"); 1.2161 + SetEnv(total_var_, ""); 1.2162 + EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); 1.2163 + 1.2164 + SetEnv(index_var_, ""); 1.2165 + SetEnv(total_var_, "5"); 1.2166 + EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); 1.2167 +} 1.2168 + 1.2169 +// Tests that ShouldRunTestOnShard is a partition when 5 1.2170 +// shards are used. 1.2171 +TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereAreFiveShards) { 1.2172 + // Choose an arbitrary number of tests and shards. 1.2173 + const int num_tests = 17; 1.2174 + const int num_shards = 5; 1.2175 + 1.2176 + // Check partitioning: each test should be on exactly 1 shard. 1.2177 + for (int test_id = 0; test_id < num_tests; test_id++) { 1.2178 + int prev_selected_shard_index = -1; 1.2179 + for (int shard_index = 0; shard_index < num_shards; shard_index++) { 1.2180 + if (ShouldRunTestOnShard(num_shards, shard_index, test_id)) { 1.2181 + if (prev_selected_shard_index < 0) { 1.2182 + prev_selected_shard_index = shard_index; 1.2183 + } else { 1.2184 + ADD_FAILURE() << "Shard " << prev_selected_shard_index << " and " 1.2185 + << shard_index << " are both selected to run test " << test_id; 1.2186 + } 1.2187 + } 1.2188 + } 1.2189 + } 1.2190 + 1.2191 + // Check balance: This is not required by the sharding protocol, but is a 1.2192 + // desirable property for performance. 1.2193 + for (int shard_index = 0; shard_index < num_shards; shard_index++) { 1.2194 + int num_tests_on_shard = 0; 1.2195 + for (int test_id = 0; test_id < num_tests; test_id++) { 1.2196 + num_tests_on_shard += 1.2197 + ShouldRunTestOnShard(num_shards, shard_index, test_id); 1.2198 + } 1.2199 + EXPECT_GE(num_tests_on_shard, num_tests / num_shards); 1.2200 + } 1.2201 +} 1.2202 + 1.2203 +// For the same reason we are not explicitly testing everything in the 1.2204 +// Test class, there are no separate tests for the following classes 1.2205 +// (except for some trivial cases): 1.2206 +// 1.2207 +// TestCase, UnitTest, UnitTestResultPrinter. 1.2208 +// 1.2209 +// Similarly, there are no separate tests for the following macros: 1.2210 +// 1.2211 +// TEST, TEST_F, RUN_ALL_TESTS 1.2212 + 1.2213 +TEST(UnitTestTest, CanGetOriginalWorkingDir) { 1.2214 + ASSERT_TRUE(UnitTest::GetInstance()->original_working_dir() != NULL); 1.2215 + EXPECT_STRNE(UnitTest::GetInstance()->original_working_dir(), ""); 1.2216 +} 1.2217 + 1.2218 +TEST(UnitTestTest, ReturnsPlausibleTimestamp) { 1.2219 + EXPECT_LT(0, UnitTest::GetInstance()->start_timestamp()); 1.2220 + EXPECT_LE(UnitTest::GetInstance()->start_timestamp(), GetTimeInMillis()); 1.2221 +} 1.2222 + 1.2223 +// This group of tests is for predicate assertions (ASSERT_PRED*, etc) 1.2224 +// of various arities. They do not attempt to be exhaustive. Rather, 1.2225 +// view them as smoke tests that can be easily reviewed and verified. 1.2226 +// A more complete set of tests for predicate assertions can be found 1.2227 +// in gtest_pred_impl_unittest.cc. 1.2228 + 1.2229 +// First, some predicates and predicate-formatters needed by the tests. 1.2230 + 1.2231 +// Returns true iff the argument is an even number. 1.2232 +bool IsEven(int n) { 1.2233 + return (n % 2) == 0; 1.2234 +} 1.2235 + 1.2236 +// A functor that returns true iff the argument is an even number. 1.2237 +struct IsEvenFunctor { 1.2238 + bool operator()(int n) { return IsEven(n); } 1.2239 +}; 1.2240 + 1.2241 +// A predicate-formatter function that asserts the argument is an even 1.2242 +// number. 1.2243 +AssertionResult AssertIsEven(const char* expr, int n) { 1.2244 + if (IsEven(n)) { 1.2245 + return AssertionSuccess(); 1.2246 + } 1.2247 + 1.2248 + Message msg; 1.2249 + msg << expr << " evaluates to " << n << ", which is not even."; 1.2250 + return AssertionFailure(msg); 1.2251 +} 1.2252 + 1.2253 +// A predicate function that returns AssertionResult for use in 1.2254 +// EXPECT/ASSERT_TRUE/FALSE. 1.2255 +AssertionResult ResultIsEven(int n) { 1.2256 + if (IsEven(n)) 1.2257 + return AssertionSuccess() << n << " is even"; 1.2258 + else 1.2259 + return AssertionFailure() << n << " is odd"; 1.2260 +} 1.2261 + 1.2262 +// A predicate function that returns AssertionResult but gives no 1.2263 +// explanation why it succeeds. Needed for testing that 1.2264 +// EXPECT/ASSERT_FALSE handles such functions correctly. 1.2265 +AssertionResult ResultIsEvenNoExplanation(int n) { 1.2266 + if (IsEven(n)) 1.2267 + return AssertionSuccess(); 1.2268 + else 1.2269 + return AssertionFailure() << n << " is odd"; 1.2270 +} 1.2271 + 1.2272 +// A predicate-formatter functor that asserts the argument is an even 1.2273 +// number. 1.2274 +struct AssertIsEvenFunctor { 1.2275 + AssertionResult operator()(const char* expr, int n) { 1.2276 + return AssertIsEven(expr, n); 1.2277 + } 1.2278 +}; 1.2279 + 1.2280 +// Returns true iff the sum of the arguments is an even number. 1.2281 +bool SumIsEven2(int n1, int n2) { 1.2282 + return IsEven(n1 + n2); 1.2283 +} 1.2284 + 1.2285 +// A functor that returns true iff the sum of the arguments is an even 1.2286 +// number. 1.2287 +struct SumIsEven3Functor { 1.2288 + bool operator()(int n1, int n2, int n3) { 1.2289 + return IsEven(n1 + n2 + n3); 1.2290 + } 1.2291 +}; 1.2292 + 1.2293 +// A predicate-formatter function that asserts the sum of the 1.2294 +// arguments is an even number. 1.2295 +AssertionResult AssertSumIsEven4( 1.2296 + const char* e1, const char* e2, const char* e3, const char* e4, 1.2297 + int n1, int n2, int n3, int n4) { 1.2298 + const int sum = n1 + n2 + n3 + n4; 1.2299 + if (IsEven(sum)) { 1.2300 + return AssertionSuccess(); 1.2301 + } 1.2302 + 1.2303 + Message msg; 1.2304 + msg << e1 << " + " << e2 << " + " << e3 << " + " << e4 1.2305 + << " (" << n1 << " + " << n2 << " + " << n3 << " + " << n4 1.2306 + << ") evaluates to " << sum << ", which is not even."; 1.2307 + return AssertionFailure(msg); 1.2308 +} 1.2309 + 1.2310 +// A predicate-formatter functor that asserts the sum of the arguments 1.2311 +// is an even number. 1.2312 +struct AssertSumIsEven5Functor { 1.2313 + AssertionResult operator()( 1.2314 + const char* e1, const char* e2, const char* e3, const char* e4, 1.2315 + const char* e5, int n1, int n2, int n3, int n4, int n5) { 1.2316 + const int sum = n1 + n2 + n3 + n4 + n5; 1.2317 + if (IsEven(sum)) { 1.2318 + return AssertionSuccess(); 1.2319 + } 1.2320 + 1.2321 + Message msg; 1.2322 + msg << e1 << " + " << e2 << " + " << e3 << " + " << e4 << " + " << e5 1.2323 + << " (" 1.2324 + << n1 << " + " << n2 << " + " << n3 << " + " << n4 << " + " << n5 1.2325 + << ") evaluates to " << sum << ", which is not even."; 1.2326 + return AssertionFailure(msg); 1.2327 + } 1.2328 +}; 1.2329 + 1.2330 + 1.2331 +// Tests unary predicate assertions. 1.2332 + 1.2333 +// Tests unary predicate assertions that don't use a custom formatter. 1.2334 +TEST(Pred1Test, WithoutFormat) { 1.2335 + // Success cases. 1.2336 + EXPECT_PRED1(IsEvenFunctor(), 2) << "This failure is UNEXPECTED!"; 1.2337 + ASSERT_PRED1(IsEven, 4); 1.2338 + 1.2339 + // Failure cases. 1.2340 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.2341 + EXPECT_PRED1(IsEven, 5) << "This failure is expected."; 1.2342 + }, "This failure is expected."); 1.2343 + EXPECT_FATAL_FAILURE(ASSERT_PRED1(IsEvenFunctor(), 5), 1.2344 + "evaluates to false"); 1.2345 +} 1.2346 + 1.2347 +// Tests unary predicate assertions that use a custom formatter. 1.2348 +TEST(Pred1Test, WithFormat) { 1.2349 + // Success cases. 1.2350 + EXPECT_PRED_FORMAT1(AssertIsEven, 2); 1.2351 + ASSERT_PRED_FORMAT1(AssertIsEvenFunctor(), 4) 1.2352 + << "This failure is UNEXPECTED!"; 1.2353 + 1.2354 + // Failure cases. 1.2355 + const int n = 5; 1.2356 + EXPECT_NONFATAL_FAILURE(EXPECT_PRED_FORMAT1(AssertIsEvenFunctor(), n), 1.2357 + "n evaluates to 5, which is not even."); 1.2358 + EXPECT_FATAL_FAILURE({ // NOLINT 1.2359 + ASSERT_PRED_FORMAT1(AssertIsEven, 5) << "This failure is expected."; 1.2360 + }, "This failure is expected."); 1.2361 +} 1.2362 + 1.2363 +// Tests that unary predicate assertions evaluates their arguments 1.2364 +// exactly once. 1.2365 +TEST(Pred1Test, SingleEvaluationOnFailure) { 1.2366 + // A success case. 1.2367 + static int n = 0; 1.2368 + EXPECT_PRED1(IsEven, n++); 1.2369 + EXPECT_EQ(1, n) << "The argument is not evaluated exactly once."; 1.2370 + 1.2371 + // A failure case. 1.2372 + EXPECT_FATAL_FAILURE({ // NOLINT 1.2373 + ASSERT_PRED_FORMAT1(AssertIsEvenFunctor(), n++) 1.2374 + << "This failure is expected."; 1.2375 + }, "This failure is expected."); 1.2376 + EXPECT_EQ(2, n) << "The argument is not evaluated exactly once."; 1.2377 +} 1.2378 + 1.2379 + 1.2380 +// Tests predicate assertions whose arity is >= 2. 1.2381 + 1.2382 +// Tests predicate assertions that don't use a custom formatter. 1.2383 +TEST(PredTest, WithoutFormat) { 1.2384 + // Success cases. 1.2385 + ASSERT_PRED2(SumIsEven2, 2, 4) << "This failure is UNEXPECTED!"; 1.2386 + EXPECT_PRED3(SumIsEven3Functor(), 4, 6, 8); 1.2387 + 1.2388 + // Failure cases. 1.2389 + const int n1 = 1; 1.2390 + const int n2 = 2; 1.2391 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.2392 + EXPECT_PRED2(SumIsEven2, n1, n2) << "This failure is expected."; 1.2393 + }, "This failure is expected."); 1.2394 + EXPECT_FATAL_FAILURE({ // NOLINT 1.2395 + ASSERT_PRED3(SumIsEven3Functor(), 1, 2, 4); 1.2396 + }, "evaluates to false"); 1.2397 +} 1.2398 + 1.2399 +// Tests predicate assertions that use a custom formatter. 1.2400 +TEST(PredTest, WithFormat) { 1.2401 + // Success cases. 1.2402 + ASSERT_PRED_FORMAT4(AssertSumIsEven4, 4, 6, 8, 10) << 1.2403 + "This failure is UNEXPECTED!"; 1.2404 + EXPECT_PRED_FORMAT5(AssertSumIsEven5Functor(), 2, 4, 6, 8, 10); 1.2405 + 1.2406 + // Failure cases. 1.2407 + const int n1 = 1; 1.2408 + const int n2 = 2; 1.2409 + const int n3 = 4; 1.2410 + const int n4 = 6; 1.2411 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.2412 + EXPECT_PRED_FORMAT4(AssertSumIsEven4, n1, n2, n3, n4); 1.2413 + }, "evaluates to 13, which is not even."); 1.2414 + EXPECT_FATAL_FAILURE({ // NOLINT 1.2415 + ASSERT_PRED_FORMAT5(AssertSumIsEven5Functor(), 1, 2, 4, 6, 8) 1.2416 + << "This failure is expected."; 1.2417 + }, "This failure is expected."); 1.2418 +} 1.2419 + 1.2420 +// Tests that predicate assertions evaluates their arguments 1.2421 +// exactly once. 1.2422 +TEST(PredTest, SingleEvaluationOnFailure) { 1.2423 + // A success case. 1.2424 + int n1 = 0; 1.2425 + int n2 = 0; 1.2426 + EXPECT_PRED2(SumIsEven2, n1++, n2++); 1.2427 + EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; 1.2428 + EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; 1.2429 + 1.2430 + // Another success case. 1.2431 + n1 = n2 = 0; 1.2432 + int n3 = 0; 1.2433 + int n4 = 0; 1.2434 + int n5 = 0; 1.2435 + ASSERT_PRED_FORMAT5(AssertSumIsEven5Functor(), 1.2436 + n1++, n2++, n3++, n4++, n5++) 1.2437 + << "This failure is UNEXPECTED!"; 1.2438 + EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; 1.2439 + EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; 1.2440 + EXPECT_EQ(1, n3) << "Argument 3 is not evaluated exactly once."; 1.2441 + EXPECT_EQ(1, n4) << "Argument 4 is not evaluated exactly once."; 1.2442 + EXPECT_EQ(1, n5) << "Argument 5 is not evaluated exactly once."; 1.2443 + 1.2444 + // A failure case. 1.2445 + n1 = n2 = n3 = 0; 1.2446 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.2447 + EXPECT_PRED3(SumIsEven3Functor(), ++n1, n2++, n3++) 1.2448 + << "This failure is expected."; 1.2449 + }, "This failure is expected."); 1.2450 + EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; 1.2451 + EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; 1.2452 + EXPECT_EQ(1, n3) << "Argument 3 is not evaluated exactly once."; 1.2453 + 1.2454 + // Another failure case. 1.2455 + n1 = n2 = n3 = n4 = 0; 1.2456 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.2457 + EXPECT_PRED_FORMAT4(AssertSumIsEven4, ++n1, n2++, n3++, n4++); 1.2458 + }, "evaluates to 1, which is not even."); 1.2459 + EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; 1.2460 + EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; 1.2461 + EXPECT_EQ(1, n3) << "Argument 3 is not evaluated exactly once."; 1.2462 + EXPECT_EQ(1, n4) << "Argument 4 is not evaluated exactly once."; 1.2463 +} 1.2464 + 1.2465 + 1.2466 +// Some helper functions for testing using overloaded/template 1.2467 +// functions with ASSERT_PREDn and EXPECT_PREDn. 1.2468 + 1.2469 +bool IsPositive(double x) { 1.2470 + return x > 0; 1.2471 +} 1.2472 + 1.2473 +template <typename T> 1.2474 +bool IsNegative(T x) { 1.2475 + return x < 0; 1.2476 +} 1.2477 + 1.2478 +template <typename T1, typename T2> 1.2479 +bool GreaterThan(T1 x1, T2 x2) { 1.2480 + return x1 > x2; 1.2481 +} 1.2482 + 1.2483 +// Tests that overloaded functions can be used in *_PRED* as long as 1.2484 +// their types are explicitly specified. 1.2485 +TEST(PredicateAssertionTest, AcceptsOverloadedFunction) { 1.2486 + // C++Builder requires C-style casts rather than static_cast. 1.2487 + EXPECT_PRED1((bool (*)(int))(IsPositive), 5); // NOLINT 1.2488 + ASSERT_PRED1((bool (*)(double))(IsPositive), 6.0); // NOLINT 1.2489 +} 1.2490 + 1.2491 +// Tests that template functions can be used in *_PRED* as long as 1.2492 +// their types are explicitly specified. 1.2493 +TEST(PredicateAssertionTest, AcceptsTemplateFunction) { 1.2494 + EXPECT_PRED1(IsNegative<int>, -5); 1.2495 + // Makes sure that we can handle templates with more than one 1.2496 + // parameter. 1.2497 + ASSERT_PRED2((GreaterThan<int, int>), 5, 0); 1.2498 +} 1.2499 + 1.2500 + 1.2501 +// Some helper functions for testing using overloaded/template 1.2502 +// functions with ASSERT_PRED_FORMATn and EXPECT_PRED_FORMATn. 1.2503 + 1.2504 +AssertionResult IsPositiveFormat(const char* /* expr */, int n) { 1.2505 + return n > 0 ? AssertionSuccess() : 1.2506 + AssertionFailure(Message() << "Failure"); 1.2507 +} 1.2508 + 1.2509 +AssertionResult IsPositiveFormat(const char* /* expr */, double x) { 1.2510 + return x > 0 ? AssertionSuccess() : 1.2511 + AssertionFailure(Message() << "Failure"); 1.2512 +} 1.2513 + 1.2514 +template <typename T> 1.2515 +AssertionResult IsNegativeFormat(const char* /* expr */, T x) { 1.2516 + return x < 0 ? AssertionSuccess() : 1.2517 + AssertionFailure(Message() << "Failure"); 1.2518 +} 1.2519 + 1.2520 +template <typename T1, typename T2> 1.2521 +AssertionResult EqualsFormat(const char* /* expr1 */, const char* /* expr2 */, 1.2522 + const T1& x1, const T2& x2) { 1.2523 + return x1 == x2 ? AssertionSuccess() : 1.2524 + AssertionFailure(Message() << "Failure"); 1.2525 +} 1.2526 + 1.2527 +// Tests that overloaded functions can be used in *_PRED_FORMAT* 1.2528 +// without explicitly specifying their types. 1.2529 +TEST(PredicateFormatAssertionTest, AcceptsOverloadedFunction) { 1.2530 + EXPECT_PRED_FORMAT1(IsPositiveFormat, 5); 1.2531 + ASSERT_PRED_FORMAT1(IsPositiveFormat, 6.0); 1.2532 +} 1.2533 + 1.2534 +// Tests that template functions can be used in *_PRED_FORMAT* without 1.2535 +// explicitly specifying their types. 1.2536 +TEST(PredicateFormatAssertionTest, AcceptsTemplateFunction) { 1.2537 + EXPECT_PRED_FORMAT1(IsNegativeFormat, -5); 1.2538 + ASSERT_PRED_FORMAT2(EqualsFormat, 3, 3); 1.2539 +} 1.2540 + 1.2541 + 1.2542 +// Tests string assertions. 1.2543 + 1.2544 +// Tests ASSERT_STREQ with non-NULL arguments. 1.2545 +TEST(StringAssertionTest, ASSERT_STREQ) { 1.2546 + const char * const p1 = "good"; 1.2547 + ASSERT_STREQ(p1, p1); 1.2548 + 1.2549 + // Let p2 have the same content as p1, but be at a different address. 1.2550 + const char p2[] = "good"; 1.2551 + ASSERT_STREQ(p1, p2); 1.2552 + 1.2553 + EXPECT_FATAL_FAILURE(ASSERT_STREQ("bad", "good"), 1.2554 + "Expected: \"bad\""); 1.2555 +} 1.2556 + 1.2557 +// Tests ASSERT_STREQ with NULL arguments. 1.2558 +TEST(StringAssertionTest, ASSERT_STREQ_Null) { 1.2559 + ASSERT_STREQ(static_cast<const char *>(NULL), NULL); 1.2560 + EXPECT_FATAL_FAILURE(ASSERT_STREQ(NULL, "non-null"), 1.2561 + "non-null"); 1.2562 +} 1.2563 + 1.2564 +// Tests ASSERT_STREQ with NULL arguments. 1.2565 +TEST(StringAssertionTest, ASSERT_STREQ_Null2) { 1.2566 + EXPECT_FATAL_FAILURE(ASSERT_STREQ("non-null", NULL), 1.2567 + "non-null"); 1.2568 +} 1.2569 + 1.2570 +// Tests ASSERT_STRNE. 1.2571 +TEST(StringAssertionTest, ASSERT_STRNE) { 1.2572 + ASSERT_STRNE("hi", "Hi"); 1.2573 + ASSERT_STRNE("Hi", NULL); 1.2574 + ASSERT_STRNE(NULL, "Hi"); 1.2575 + ASSERT_STRNE("", NULL); 1.2576 + ASSERT_STRNE(NULL, ""); 1.2577 + ASSERT_STRNE("", "Hi"); 1.2578 + ASSERT_STRNE("Hi", ""); 1.2579 + EXPECT_FATAL_FAILURE(ASSERT_STRNE("Hi", "Hi"), 1.2580 + "\"Hi\" vs \"Hi\""); 1.2581 +} 1.2582 + 1.2583 +// Tests ASSERT_STRCASEEQ. 1.2584 +TEST(StringAssertionTest, ASSERT_STRCASEEQ) { 1.2585 + ASSERT_STRCASEEQ("hi", "Hi"); 1.2586 + ASSERT_STRCASEEQ(static_cast<const char *>(NULL), NULL); 1.2587 + 1.2588 + ASSERT_STRCASEEQ("", ""); 1.2589 + EXPECT_FATAL_FAILURE(ASSERT_STRCASEEQ("Hi", "hi2"), 1.2590 + "(ignoring case)"); 1.2591 +} 1.2592 + 1.2593 +// Tests ASSERT_STRCASENE. 1.2594 +TEST(StringAssertionTest, ASSERT_STRCASENE) { 1.2595 + ASSERT_STRCASENE("hi1", "Hi2"); 1.2596 + ASSERT_STRCASENE("Hi", NULL); 1.2597 + ASSERT_STRCASENE(NULL, "Hi"); 1.2598 + ASSERT_STRCASENE("", NULL); 1.2599 + ASSERT_STRCASENE(NULL, ""); 1.2600 + ASSERT_STRCASENE("", "Hi"); 1.2601 + ASSERT_STRCASENE("Hi", ""); 1.2602 + EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("Hi", "hi"), 1.2603 + "(ignoring case)"); 1.2604 +} 1.2605 + 1.2606 +// Tests *_STREQ on wide strings. 1.2607 +TEST(StringAssertionTest, STREQ_Wide) { 1.2608 + // NULL strings. 1.2609 + ASSERT_STREQ(static_cast<const wchar_t *>(NULL), NULL); 1.2610 + 1.2611 + // Empty strings. 1.2612 + ASSERT_STREQ(L"", L""); 1.2613 + 1.2614 + // Non-null vs NULL. 1.2615 + EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"non-null", NULL), 1.2616 + "non-null"); 1.2617 + 1.2618 + // Equal strings. 1.2619 + EXPECT_STREQ(L"Hi", L"Hi"); 1.2620 + 1.2621 + // Unequal strings. 1.2622 + EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"abc", L"Abc"), 1.2623 + "Abc"); 1.2624 + 1.2625 + // Strings containing wide characters. 1.2626 + EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"abc\x8119", L"abc\x8120"), 1.2627 + "abc"); 1.2628 + 1.2629 + // The streaming variation. 1.2630 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.2631 + EXPECT_STREQ(L"abc\x8119", L"abc\x8121") << "Expected failure"; 1.2632 + }, "Expected failure"); 1.2633 +} 1.2634 + 1.2635 +// Tests *_STRNE on wide strings. 1.2636 +TEST(StringAssertionTest, STRNE_Wide) { 1.2637 + // NULL strings. 1.2638 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.2639 + EXPECT_STRNE(static_cast<const wchar_t *>(NULL), NULL); 1.2640 + }, ""); 1.2641 + 1.2642 + // Empty strings. 1.2643 + EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"", L""), 1.2644 + "L\"\""); 1.2645 + 1.2646 + // Non-null vs NULL. 1.2647 + ASSERT_STRNE(L"non-null", NULL); 1.2648 + 1.2649 + // Equal strings. 1.2650 + EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"Hi", L"Hi"), 1.2651 + "L\"Hi\""); 1.2652 + 1.2653 + // Unequal strings. 1.2654 + EXPECT_STRNE(L"abc", L"Abc"); 1.2655 + 1.2656 + // Strings containing wide characters. 1.2657 + EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"abc\x8119", L"abc\x8119"), 1.2658 + "abc"); 1.2659 + 1.2660 + // The streaming variation. 1.2661 + ASSERT_STRNE(L"abc\x8119", L"abc\x8120") << "This shouldn't happen"; 1.2662 +} 1.2663 + 1.2664 +// Tests for ::testing::IsSubstring(). 1.2665 + 1.2666 +// Tests that IsSubstring() returns the correct result when the input 1.2667 +// argument type is const char*. 1.2668 +TEST(IsSubstringTest, ReturnsCorrectResultForCString) { 1.2669 + EXPECT_FALSE(IsSubstring("", "", NULL, "a")); 1.2670 + EXPECT_FALSE(IsSubstring("", "", "b", NULL)); 1.2671 + EXPECT_FALSE(IsSubstring("", "", "needle", "haystack")); 1.2672 + 1.2673 + EXPECT_TRUE(IsSubstring("", "", static_cast<const char*>(NULL), NULL)); 1.2674 + EXPECT_TRUE(IsSubstring("", "", "needle", "two needles")); 1.2675 +} 1.2676 + 1.2677 +// Tests that IsSubstring() returns the correct result when the input 1.2678 +// argument type is const wchar_t*. 1.2679 +TEST(IsSubstringTest, ReturnsCorrectResultForWideCString) { 1.2680 + EXPECT_FALSE(IsSubstring("", "", kNull, L"a")); 1.2681 + EXPECT_FALSE(IsSubstring("", "", L"b", kNull)); 1.2682 + EXPECT_FALSE(IsSubstring("", "", L"needle", L"haystack")); 1.2683 + 1.2684 + EXPECT_TRUE(IsSubstring("", "", static_cast<const wchar_t*>(NULL), NULL)); 1.2685 + EXPECT_TRUE(IsSubstring("", "", L"needle", L"two needles")); 1.2686 +} 1.2687 + 1.2688 +// Tests that IsSubstring() generates the correct message when the input 1.2689 +// argument type is const char*. 1.2690 +TEST(IsSubstringTest, GeneratesCorrectMessageForCString) { 1.2691 + EXPECT_STREQ("Value of: needle_expr\n" 1.2692 + " Actual: \"needle\"\n" 1.2693 + "Expected: a substring of haystack_expr\n" 1.2694 + "Which is: \"haystack\"", 1.2695 + IsSubstring("needle_expr", "haystack_expr", 1.2696 + "needle", "haystack").failure_message()); 1.2697 +} 1.2698 + 1.2699 +// Tests that IsSubstring returns the correct result when the input 1.2700 +// argument type is ::std::string. 1.2701 +TEST(IsSubstringTest, ReturnsCorrectResultsForStdString) { 1.2702 + EXPECT_TRUE(IsSubstring("", "", std::string("hello"), "ahellob")); 1.2703 + EXPECT_FALSE(IsSubstring("", "", "hello", std::string("world"))); 1.2704 +} 1.2705 + 1.2706 +#if GTEST_HAS_STD_WSTRING 1.2707 +// Tests that IsSubstring returns the correct result when the input 1.2708 +// argument type is ::std::wstring. 1.2709 +TEST(IsSubstringTest, ReturnsCorrectResultForStdWstring) { 1.2710 + EXPECT_TRUE(IsSubstring("", "", ::std::wstring(L"needle"), L"two needles")); 1.2711 + EXPECT_FALSE(IsSubstring("", "", L"needle", ::std::wstring(L"haystack"))); 1.2712 +} 1.2713 + 1.2714 +// Tests that IsSubstring() generates the correct message when the input 1.2715 +// argument type is ::std::wstring. 1.2716 +TEST(IsSubstringTest, GeneratesCorrectMessageForWstring) { 1.2717 + EXPECT_STREQ("Value of: needle_expr\n" 1.2718 + " Actual: L\"needle\"\n" 1.2719 + "Expected: a substring of haystack_expr\n" 1.2720 + "Which is: L\"haystack\"", 1.2721 + IsSubstring( 1.2722 + "needle_expr", "haystack_expr", 1.2723 + ::std::wstring(L"needle"), L"haystack").failure_message()); 1.2724 +} 1.2725 + 1.2726 +#endif // GTEST_HAS_STD_WSTRING 1.2727 + 1.2728 +// Tests for ::testing::IsNotSubstring(). 1.2729 + 1.2730 +// Tests that IsNotSubstring() returns the correct result when the input 1.2731 +// argument type is const char*. 1.2732 +TEST(IsNotSubstringTest, ReturnsCorrectResultForCString) { 1.2733 + EXPECT_TRUE(IsNotSubstring("", "", "needle", "haystack")); 1.2734 + EXPECT_FALSE(IsNotSubstring("", "", "needle", "two needles")); 1.2735 +} 1.2736 + 1.2737 +// Tests that IsNotSubstring() returns the correct result when the input 1.2738 +// argument type is const wchar_t*. 1.2739 +TEST(IsNotSubstringTest, ReturnsCorrectResultForWideCString) { 1.2740 + EXPECT_TRUE(IsNotSubstring("", "", L"needle", L"haystack")); 1.2741 + EXPECT_FALSE(IsNotSubstring("", "", L"needle", L"two needles")); 1.2742 +} 1.2743 + 1.2744 +// Tests that IsNotSubstring() generates the correct message when the input 1.2745 +// argument type is const wchar_t*. 1.2746 +TEST(IsNotSubstringTest, GeneratesCorrectMessageForWideCString) { 1.2747 + EXPECT_STREQ("Value of: needle_expr\n" 1.2748 + " Actual: L\"needle\"\n" 1.2749 + "Expected: not a substring of haystack_expr\n" 1.2750 + "Which is: L\"two needles\"", 1.2751 + IsNotSubstring( 1.2752 + "needle_expr", "haystack_expr", 1.2753 + L"needle", L"two needles").failure_message()); 1.2754 +} 1.2755 + 1.2756 +// Tests that IsNotSubstring returns the correct result when the input 1.2757 +// argument type is ::std::string. 1.2758 +TEST(IsNotSubstringTest, ReturnsCorrectResultsForStdString) { 1.2759 + EXPECT_FALSE(IsNotSubstring("", "", std::string("hello"), "ahellob")); 1.2760 + EXPECT_TRUE(IsNotSubstring("", "", "hello", std::string("world"))); 1.2761 +} 1.2762 + 1.2763 +// Tests that IsNotSubstring() generates the correct message when the input 1.2764 +// argument type is ::std::string. 1.2765 +TEST(IsNotSubstringTest, GeneratesCorrectMessageForStdString) { 1.2766 + EXPECT_STREQ("Value of: needle_expr\n" 1.2767 + " Actual: \"needle\"\n" 1.2768 + "Expected: not a substring of haystack_expr\n" 1.2769 + "Which is: \"two needles\"", 1.2770 + IsNotSubstring( 1.2771 + "needle_expr", "haystack_expr", 1.2772 + ::std::string("needle"), "two needles").failure_message()); 1.2773 +} 1.2774 + 1.2775 +#if GTEST_HAS_STD_WSTRING 1.2776 + 1.2777 +// Tests that IsNotSubstring returns the correct result when the input 1.2778 +// argument type is ::std::wstring. 1.2779 +TEST(IsNotSubstringTest, ReturnsCorrectResultForStdWstring) { 1.2780 + EXPECT_FALSE( 1.2781 + IsNotSubstring("", "", ::std::wstring(L"needle"), L"two needles")); 1.2782 + EXPECT_TRUE(IsNotSubstring("", "", L"needle", ::std::wstring(L"haystack"))); 1.2783 +} 1.2784 + 1.2785 +#endif // GTEST_HAS_STD_WSTRING 1.2786 + 1.2787 +// Tests floating-point assertions. 1.2788 + 1.2789 +template <typename RawType> 1.2790 +class FloatingPointTest : public Test { 1.2791 + protected: 1.2792 + // Pre-calculated numbers to be used by the tests. 1.2793 + struct TestValues { 1.2794 + RawType close_to_positive_zero; 1.2795 + RawType close_to_negative_zero; 1.2796 + RawType further_from_negative_zero; 1.2797 + 1.2798 + RawType close_to_one; 1.2799 + RawType further_from_one; 1.2800 + 1.2801 + RawType infinity; 1.2802 + RawType close_to_infinity; 1.2803 + RawType further_from_infinity; 1.2804 + 1.2805 + RawType nan1; 1.2806 + RawType nan2; 1.2807 + }; 1.2808 + 1.2809 + typedef typename testing::internal::FloatingPoint<RawType> Floating; 1.2810 + typedef typename Floating::Bits Bits; 1.2811 + 1.2812 + virtual void SetUp() { 1.2813 + const size_t max_ulps = Floating::kMaxUlps; 1.2814 + 1.2815 + // The bits that represent 0.0. 1.2816 + const Bits zero_bits = Floating(0).bits(); 1.2817 + 1.2818 + // Makes some numbers close to 0.0. 1.2819 + values_.close_to_positive_zero = Floating::ReinterpretBits( 1.2820 + zero_bits + max_ulps/2); 1.2821 + values_.close_to_negative_zero = -Floating::ReinterpretBits( 1.2822 + zero_bits + max_ulps - max_ulps/2); 1.2823 + values_.further_from_negative_zero = -Floating::ReinterpretBits( 1.2824 + zero_bits + max_ulps + 1 - max_ulps/2); 1.2825 + 1.2826 + // The bits that represent 1.0. 1.2827 + const Bits one_bits = Floating(1).bits(); 1.2828 + 1.2829 + // Makes some numbers close to 1.0. 1.2830 + values_.close_to_one = Floating::ReinterpretBits(one_bits + max_ulps); 1.2831 + values_.further_from_one = Floating::ReinterpretBits( 1.2832 + one_bits + max_ulps + 1); 1.2833 + 1.2834 + // +infinity. 1.2835 + values_.infinity = Floating::Infinity(); 1.2836 + 1.2837 + // The bits that represent +infinity. 1.2838 + const Bits infinity_bits = Floating(values_.infinity).bits(); 1.2839 + 1.2840 + // Makes some numbers close to infinity. 1.2841 + values_.close_to_infinity = Floating::ReinterpretBits( 1.2842 + infinity_bits - max_ulps); 1.2843 + values_.further_from_infinity = Floating::ReinterpretBits( 1.2844 + infinity_bits - max_ulps - 1); 1.2845 + 1.2846 + // Makes some NAN's. Sets the most significant bit of the fraction so that 1.2847 + // our NaN's are quiet; trying to process a signaling NaN would raise an 1.2848 + // exception if our environment enables floating point exceptions. 1.2849 + values_.nan1 = Floating::ReinterpretBits(Floating::kExponentBitMask 1.2850 + | (static_cast<Bits>(1) << (Floating::kFractionBitCount - 1)) | 1); 1.2851 + values_.nan2 = Floating::ReinterpretBits(Floating::kExponentBitMask 1.2852 + | (static_cast<Bits>(1) << (Floating::kFractionBitCount - 1)) | 200); 1.2853 + } 1.2854 + 1.2855 + void TestSize() { 1.2856 + EXPECT_EQ(sizeof(RawType), sizeof(Bits)); 1.2857 + } 1.2858 + 1.2859 + static TestValues values_; 1.2860 +}; 1.2861 + 1.2862 +template <typename RawType> 1.2863 +typename FloatingPointTest<RawType>::TestValues 1.2864 + FloatingPointTest<RawType>::values_; 1.2865 + 1.2866 +// Instantiates FloatingPointTest for testing *_FLOAT_EQ. 1.2867 +typedef FloatingPointTest<float> FloatTest; 1.2868 + 1.2869 +// Tests that the size of Float::Bits matches the size of float. 1.2870 +TEST_F(FloatTest, Size) { 1.2871 + TestSize(); 1.2872 +} 1.2873 + 1.2874 +// Tests comparing with +0 and -0. 1.2875 +TEST_F(FloatTest, Zeros) { 1.2876 + EXPECT_FLOAT_EQ(0.0, -0.0); 1.2877 + EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(-0.0, 1.0), 1.2878 + "1.0"); 1.2879 + EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(0.0, 1.5), 1.2880 + "1.5"); 1.2881 +} 1.2882 + 1.2883 +// Tests comparing numbers close to 0. 1.2884 +// 1.2885 +// This ensures that *_FLOAT_EQ handles the sign correctly and no 1.2886 +// overflow occurs when comparing numbers whose absolute value is very 1.2887 +// small. 1.2888 +TEST_F(FloatTest, AlmostZeros) { 1.2889 + // In C++Builder, names within local classes (such as used by 1.2890 + // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the 1.2891 + // scoping class. Use a static local alias as a workaround. 1.2892 + // We use the assignment syntax since some compilers, like Sun Studio, 1.2893 + // don't allow initializing references using construction syntax 1.2894 + // (parentheses). 1.2895 + static const FloatTest::TestValues& v = this->values_; 1.2896 + 1.2897 + EXPECT_FLOAT_EQ(0.0, v.close_to_positive_zero); 1.2898 + EXPECT_FLOAT_EQ(-0.0, v.close_to_negative_zero); 1.2899 + EXPECT_FLOAT_EQ(v.close_to_positive_zero, v.close_to_negative_zero); 1.2900 + 1.2901 + EXPECT_FATAL_FAILURE({ // NOLINT 1.2902 + ASSERT_FLOAT_EQ(v.close_to_positive_zero, 1.2903 + v.further_from_negative_zero); 1.2904 + }, "v.further_from_negative_zero"); 1.2905 +} 1.2906 + 1.2907 +// Tests comparing numbers close to each other. 1.2908 +TEST_F(FloatTest, SmallDiff) { 1.2909 + EXPECT_FLOAT_EQ(1.0, values_.close_to_one); 1.2910 + EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(1.0, values_.further_from_one), 1.2911 + "values_.further_from_one"); 1.2912 +} 1.2913 + 1.2914 +// Tests comparing numbers far apart. 1.2915 +TEST_F(FloatTest, LargeDiff) { 1.2916 + EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(2.5, 3.0), 1.2917 + "3.0"); 1.2918 +} 1.2919 + 1.2920 +// Tests comparing with infinity. 1.2921 +// 1.2922 +// This ensures that no overflow occurs when comparing numbers whose 1.2923 +// absolute value is very large. 1.2924 +TEST_F(FloatTest, Infinity) { 1.2925 + EXPECT_FLOAT_EQ(values_.infinity, values_.close_to_infinity); 1.2926 + EXPECT_FLOAT_EQ(-values_.infinity, -values_.close_to_infinity); 1.2927 +#if !GTEST_OS_SYMBIAN 1.2928 + // Nokia's STLport crashes if we try to output infinity or NaN. 1.2929 + EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.infinity, -values_.infinity), 1.2930 + "-values_.infinity"); 1.2931 + 1.2932 + // This is interesting as the representations of infinity and nan1 1.2933 + // are only 1 DLP apart. 1.2934 + EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.infinity, values_.nan1), 1.2935 + "values_.nan1"); 1.2936 +#endif // !GTEST_OS_SYMBIAN 1.2937 +} 1.2938 + 1.2939 +// Tests that comparing with NAN always returns false. 1.2940 +TEST_F(FloatTest, NaN) { 1.2941 +#if !GTEST_OS_SYMBIAN 1.2942 +// Nokia's STLport crashes if we try to output infinity or NaN. 1.2943 + 1.2944 + // In C++Builder, names within local classes (such as used by 1.2945 + // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the 1.2946 + // scoping class. Use a static local alias as a workaround. 1.2947 + // We use the assignment syntax since some compilers, like Sun Studio, 1.2948 + // don't allow initializing references using construction syntax 1.2949 + // (parentheses). 1.2950 + static const FloatTest::TestValues& v = this->values_; 1.2951 + 1.2952 + EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(v.nan1, v.nan1), 1.2953 + "v.nan1"); 1.2954 + EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(v.nan1, v.nan2), 1.2955 + "v.nan2"); 1.2956 + EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(1.0, v.nan1), 1.2957 + "v.nan1"); 1.2958 + 1.2959 + EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(v.nan1, v.infinity), 1.2960 + "v.infinity"); 1.2961 +#endif // !GTEST_OS_SYMBIAN 1.2962 +} 1.2963 + 1.2964 +// Tests that *_FLOAT_EQ are reflexive. 1.2965 +TEST_F(FloatTest, Reflexive) { 1.2966 + EXPECT_FLOAT_EQ(0.0, 0.0); 1.2967 + EXPECT_FLOAT_EQ(1.0, 1.0); 1.2968 + ASSERT_FLOAT_EQ(values_.infinity, values_.infinity); 1.2969 +} 1.2970 + 1.2971 +// Tests that *_FLOAT_EQ are commutative. 1.2972 +TEST_F(FloatTest, Commutative) { 1.2973 + // We already tested EXPECT_FLOAT_EQ(1.0, values_.close_to_one). 1.2974 + EXPECT_FLOAT_EQ(values_.close_to_one, 1.0); 1.2975 + 1.2976 + // We already tested EXPECT_FLOAT_EQ(1.0, values_.further_from_one). 1.2977 + EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.further_from_one, 1.0), 1.2978 + "1.0"); 1.2979 +} 1.2980 + 1.2981 +// Tests EXPECT_NEAR. 1.2982 +TEST_F(FloatTest, EXPECT_NEAR) { 1.2983 + EXPECT_NEAR(-1.0f, -1.1f, 0.2f); 1.2984 + EXPECT_NEAR(2.0f, 3.0f, 1.0f); 1.2985 + EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0f,1.5f, 0.25f), // NOLINT 1.2986 + "The difference between 1.0f and 1.5f is 0.5, " 1.2987 + "which exceeds 0.25f"); 1.2988 + // To work around a bug in gcc 2.95.0, there is intentionally no 1.2989 + // space after the first comma in the previous line. 1.2990 +} 1.2991 + 1.2992 +// Tests ASSERT_NEAR. 1.2993 +TEST_F(FloatTest, ASSERT_NEAR) { 1.2994 + ASSERT_NEAR(-1.0f, -1.1f, 0.2f); 1.2995 + ASSERT_NEAR(2.0f, 3.0f, 1.0f); 1.2996 + EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0f,1.5f, 0.25f), // NOLINT 1.2997 + "The difference between 1.0f and 1.5f is 0.5, " 1.2998 + "which exceeds 0.25f"); 1.2999 + // To work around a bug in gcc 2.95.0, there is intentionally no 1.3000 + // space after the first comma in the previous line. 1.3001 +} 1.3002 + 1.3003 +// Tests the cases where FloatLE() should succeed. 1.3004 +TEST_F(FloatTest, FloatLESucceeds) { 1.3005 + EXPECT_PRED_FORMAT2(FloatLE, 1.0f, 2.0f); // When val1 < val2, 1.3006 + ASSERT_PRED_FORMAT2(FloatLE, 1.0f, 1.0f); // val1 == val2, 1.3007 + 1.3008 + // or when val1 is greater than, but almost equals to, val2. 1.3009 + EXPECT_PRED_FORMAT2(FloatLE, values_.close_to_positive_zero, 0.0f); 1.3010 +} 1.3011 + 1.3012 +// Tests the cases where FloatLE() should fail. 1.3013 +TEST_F(FloatTest, FloatLEFails) { 1.3014 + // When val1 is greater than val2 by a large margin, 1.3015 + EXPECT_NONFATAL_FAILURE(EXPECT_PRED_FORMAT2(FloatLE, 2.0f, 1.0f), 1.3016 + "(2.0f) <= (1.0f)"); 1.3017 + 1.3018 + // or by a small yet non-negligible margin, 1.3019 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.3020 + EXPECT_PRED_FORMAT2(FloatLE, values_.further_from_one, 1.0f); 1.3021 + }, "(values_.further_from_one) <= (1.0f)"); 1.3022 + 1.3023 +#if !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) 1.3024 + // Nokia's STLport crashes if we try to output infinity or NaN. 1.3025 + // C++Builder gives bad results for ordered comparisons involving NaNs 1.3026 + // due to compiler bugs. 1.3027 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.3028 + EXPECT_PRED_FORMAT2(FloatLE, values_.nan1, values_.infinity); 1.3029 + }, "(values_.nan1) <= (values_.infinity)"); 1.3030 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.3031 + EXPECT_PRED_FORMAT2(FloatLE, -values_.infinity, values_.nan1); 1.3032 + }, "(-values_.infinity) <= (values_.nan1)"); 1.3033 + EXPECT_FATAL_FAILURE({ // NOLINT 1.3034 + ASSERT_PRED_FORMAT2(FloatLE, values_.nan1, values_.nan1); 1.3035 + }, "(values_.nan1) <= (values_.nan1)"); 1.3036 +#endif // !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) 1.3037 +} 1.3038 + 1.3039 +// Instantiates FloatingPointTest for testing *_DOUBLE_EQ. 1.3040 +typedef FloatingPointTest<double> DoubleTest; 1.3041 + 1.3042 +// Tests that the size of Double::Bits matches the size of double. 1.3043 +TEST_F(DoubleTest, Size) { 1.3044 + TestSize(); 1.3045 +} 1.3046 + 1.3047 +// Tests comparing with +0 and -0. 1.3048 +TEST_F(DoubleTest, Zeros) { 1.3049 + EXPECT_DOUBLE_EQ(0.0, -0.0); 1.3050 + EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(-0.0, 1.0), 1.3051 + "1.0"); 1.3052 + EXPECT_FATAL_FAILURE(ASSERT_DOUBLE_EQ(0.0, 1.0), 1.3053 + "1.0"); 1.3054 +} 1.3055 + 1.3056 +// Tests comparing numbers close to 0. 1.3057 +// 1.3058 +// This ensures that *_DOUBLE_EQ handles the sign correctly and no 1.3059 +// overflow occurs when comparing numbers whose absolute value is very 1.3060 +// small. 1.3061 +TEST_F(DoubleTest, AlmostZeros) { 1.3062 + // In C++Builder, names within local classes (such as used by 1.3063 + // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the 1.3064 + // scoping class. Use a static local alias as a workaround. 1.3065 + // We use the assignment syntax since some compilers, like Sun Studio, 1.3066 + // don't allow initializing references using construction syntax 1.3067 + // (parentheses). 1.3068 + static const DoubleTest::TestValues& v = this->values_; 1.3069 + 1.3070 + EXPECT_DOUBLE_EQ(0.0, v.close_to_positive_zero); 1.3071 + EXPECT_DOUBLE_EQ(-0.0, v.close_to_negative_zero); 1.3072 + EXPECT_DOUBLE_EQ(v.close_to_positive_zero, v.close_to_negative_zero); 1.3073 + 1.3074 + EXPECT_FATAL_FAILURE({ // NOLINT 1.3075 + ASSERT_DOUBLE_EQ(v.close_to_positive_zero, 1.3076 + v.further_from_negative_zero); 1.3077 + }, "v.further_from_negative_zero"); 1.3078 +} 1.3079 + 1.3080 +// Tests comparing numbers close to each other. 1.3081 +TEST_F(DoubleTest, SmallDiff) { 1.3082 + EXPECT_DOUBLE_EQ(1.0, values_.close_to_one); 1.3083 + EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(1.0, values_.further_from_one), 1.3084 + "values_.further_from_one"); 1.3085 +} 1.3086 + 1.3087 +// Tests comparing numbers far apart. 1.3088 +TEST_F(DoubleTest, LargeDiff) { 1.3089 + EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(2.0, 3.0), 1.3090 + "3.0"); 1.3091 +} 1.3092 + 1.3093 +// Tests comparing with infinity. 1.3094 +// 1.3095 +// This ensures that no overflow occurs when comparing numbers whose 1.3096 +// absolute value is very large. 1.3097 +TEST_F(DoubleTest, Infinity) { 1.3098 + EXPECT_DOUBLE_EQ(values_.infinity, values_.close_to_infinity); 1.3099 + EXPECT_DOUBLE_EQ(-values_.infinity, -values_.close_to_infinity); 1.3100 +#if !GTEST_OS_SYMBIAN 1.3101 + // Nokia's STLport crashes if we try to output infinity or NaN. 1.3102 + EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(values_.infinity, -values_.infinity), 1.3103 + "-values_.infinity"); 1.3104 + 1.3105 + // This is interesting as the representations of infinity_ and nan1_ 1.3106 + // are only 1 DLP apart. 1.3107 + EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(values_.infinity, values_.nan1), 1.3108 + "values_.nan1"); 1.3109 +#endif // !GTEST_OS_SYMBIAN 1.3110 +} 1.3111 + 1.3112 +// Tests that comparing with NAN always returns false. 1.3113 +TEST_F(DoubleTest, NaN) { 1.3114 +#if !GTEST_OS_SYMBIAN 1.3115 + // In C++Builder, names within local classes (such as used by 1.3116 + // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the 1.3117 + // scoping class. Use a static local alias as a workaround. 1.3118 + // We use the assignment syntax since some compilers, like Sun Studio, 1.3119 + // don't allow initializing references using construction syntax 1.3120 + // (parentheses). 1.3121 + static const DoubleTest::TestValues& v = this->values_; 1.3122 + 1.3123 + // Nokia's STLport crashes if we try to output infinity or NaN. 1.3124 + EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(v.nan1, v.nan1), 1.3125 + "v.nan1"); 1.3126 + EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(v.nan1, v.nan2), "v.nan2"); 1.3127 + EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(1.0, v.nan1), "v.nan1"); 1.3128 + EXPECT_FATAL_FAILURE(ASSERT_DOUBLE_EQ(v.nan1, v.infinity), 1.3129 + "v.infinity"); 1.3130 +#endif // !GTEST_OS_SYMBIAN 1.3131 +} 1.3132 + 1.3133 +// Tests that *_DOUBLE_EQ are reflexive. 1.3134 +TEST_F(DoubleTest, Reflexive) { 1.3135 + EXPECT_DOUBLE_EQ(0.0, 0.0); 1.3136 + EXPECT_DOUBLE_EQ(1.0, 1.0); 1.3137 +#if !GTEST_OS_SYMBIAN 1.3138 + // Nokia's STLport crashes if we try to output infinity or NaN. 1.3139 + ASSERT_DOUBLE_EQ(values_.infinity, values_.infinity); 1.3140 +#endif // !GTEST_OS_SYMBIAN 1.3141 +} 1.3142 + 1.3143 +// Tests that *_DOUBLE_EQ are commutative. 1.3144 +TEST_F(DoubleTest, Commutative) { 1.3145 + // We already tested EXPECT_DOUBLE_EQ(1.0, values_.close_to_one). 1.3146 + EXPECT_DOUBLE_EQ(values_.close_to_one, 1.0); 1.3147 + 1.3148 + // We already tested EXPECT_DOUBLE_EQ(1.0, values_.further_from_one). 1.3149 + EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(values_.further_from_one, 1.0), 1.3150 + "1.0"); 1.3151 +} 1.3152 + 1.3153 +// Tests EXPECT_NEAR. 1.3154 +TEST_F(DoubleTest, EXPECT_NEAR) { 1.3155 + EXPECT_NEAR(-1.0, -1.1, 0.2); 1.3156 + EXPECT_NEAR(2.0, 3.0, 1.0); 1.3157 + EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0, 1.5, 0.25), // NOLINT 1.3158 + "The difference between 1.0 and 1.5 is 0.5, " 1.3159 + "which exceeds 0.25"); 1.3160 + // To work around a bug in gcc 2.95.0, there is intentionally no 1.3161 + // space after the first comma in the previous statement. 1.3162 +} 1.3163 + 1.3164 +// Tests ASSERT_NEAR. 1.3165 +TEST_F(DoubleTest, ASSERT_NEAR) { 1.3166 + ASSERT_NEAR(-1.0, -1.1, 0.2); 1.3167 + ASSERT_NEAR(2.0, 3.0, 1.0); 1.3168 + EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0, 1.5, 0.25), // NOLINT 1.3169 + "The difference between 1.0 and 1.5 is 0.5, " 1.3170 + "which exceeds 0.25"); 1.3171 + // To work around a bug in gcc 2.95.0, there is intentionally no 1.3172 + // space after the first comma in the previous statement. 1.3173 +} 1.3174 + 1.3175 +// Tests the cases where DoubleLE() should succeed. 1.3176 +TEST_F(DoubleTest, DoubleLESucceeds) { 1.3177 + EXPECT_PRED_FORMAT2(DoubleLE, 1.0, 2.0); // When val1 < val2, 1.3178 + ASSERT_PRED_FORMAT2(DoubleLE, 1.0, 1.0); // val1 == val2, 1.3179 + 1.3180 + // or when val1 is greater than, but almost equals to, val2. 1.3181 + EXPECT_PRED_FORMAT2(DoubleLE, values_.close_to_positive_zero, 0.0); 1.3182 +} 1.3183 + 1.3184 +// Tests the cases where DoubleLE() should fail. 1.3185 +TEST_F(DoubleTest, DoubleLEFails) { 1.3186 + // When val1 is greater than val2 by a large margin, 1.3187 + EXPECT_NONFATAL_FAILURE(EXPECT_PRED_FORMAT2(DoubleLE, 2.0, 1.0), 1.3188 + "(2.0) <= (1.0)"); 1.3189 + 1.3190 + // or by a small yet non-negligible margin, 1.3191 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.3192 + EXPECT_PRED_FORMAT2(DoubleLE, values_.further_from_one, 1.0); 1.3193 + }, "(values_.further_from_one) <= (1.0)"); 1.3194 + 1.3195 +#if !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) 1.3196 + // Nokia's STLport crashes if we try to output infinity or NaN. 1.3197 + // C++Builder gives bad results for ordered comparisons involving NaNs 1.3198 + // due to compiler bugs. 1.3199 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.3200 + EXPECT_PRED_FORMAT2(DoubleLE, values_.nan1, values_.infinity); 1.3201 + }, "(values_.nan1) <= (values_.infinity)"); 1.3202 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.3203 + EXPECT_PRED_FORMAT2(DoubleLE, -values_.infinity, values_.nan1); 1.3204 + }, " (-values_.infinity) <= (values_.nan1)"); 1.3205 + EXPECT_FATAL_FAILURE({ // NOLINT 1.3206 + ASSERT_PRED_FORMAT2(DoubleLE, values_.nan1, values_.nan1); 1.3207 + }, "(values_.nan1) <= (values_.nan1)"); 1.3208 +#endif // !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) 1.3209 +} 1.3210 + 1.3211 + 1.3212 +// Verifies that a test or test case whose name starts with DISABLED_ is 1.3213 +// not run. 1.3214 + 1.3215 +// A test whose name starts with DISABLED_. 1.3216 +// Should not run. 1.3217 +TEST(DisabledTest, DISABLED_TestShouldNotRun) { 1.3218 + FAIL() << "Unexpected failure: Disabled test should not be run."; 1.3219 +} 1.3220 + 1.3221 +// A test whose name does not start with DISABLED_. 1.3222 +// Should run. 1.3223 +TEST(DisabledTest, NotDISABLED_TestShouldRun) { 1.3224 + EXPECT_EQ(1, 1); 1.3225 +} 1.3226 + 1.3227 +// A test case whose name starts with DISABLED_. 1.3228 +// Should not run. 1.3229 +TEST(DISABLED_TestCase, TestShouldNotRun) { 1.3230 + FAIL() << "Unexpected failure: Test in disabled test case should not be run."; 1.3231 +} 1.3232 + 1.3233 +// A test case and test whose names start with DISABLED_. 1.3234 +// Should not run. 1.3235 +TEST(DISABLED_TestCase, DISABLED_TestShouldNotRun) { 1.3236 + FAIL() << "Unexpected failure: Test in disabled test case should not be run."; 1.3237 +} 1.3238 + 1.3239 +// Check that when all tests in a test case are disabled, SetupTestCase() and 1.3240 +// TearDownTestCase() are not called. 1.3241 +class DisabledTestsTest : public Test { 1.3242 + protected: 1.3243 + static void SetUpTestCase() { 1.3244 + FAIL() << "Unexpected failure: All tests disabled in test case. " 1.3245 + "SetupTestCase() should not be called."; 1.3246 + } 1.3247 + 1.3248 + static void TearDownTestCase() { 1.3249 + FAIL() << "Unexpected failure: All tests disabled in test case. " 1.3250 + "TearDownTestCase() should not be called."; 1.3251 + } 1.3252 +}; 1.3253 + 1.3254 +TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_1) { 1.3255 + FAIL() << "Unexpected failure: Disabled test should not be run."; 1.3256 +} 1.3257 + 1.3258 +TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_2) { 1.3259 + FAIL() << "Unexpected failure: Disabled test should not be run."; 1.3260 +} 1.3261 + 1.3262 +// Tests that disabled typed tests aren't run. 1.3263 + 1.3264 +#if GTEST_HAS_TYPED_TEST 1.3265 + 1.3266 +template <typename T> 1.3267 +class TypedTest : public Test { 1.3268 +}; 1.3269 + 1.3270 +typedef testing::Types<int, double> NumericTypes; 1.3271 +TYPED_TEST_CASE(TypedTest, NumericTypes); 1.3272 + 1.3273 +TYPED_TEST(TypedTest, DISABLED_ShouldNotRun) { 1.3274 + FAIL() << "Unexpected failure: Disabled typed test should not run."; 1.3275 +} 1.3276 + 1.3277 +template <typename T> 1.3278 +class DISABLED_TypedTest : public Test { 1.3279 +}; 1.3280 + 1.3281 +TYPED_TEST_CASE(DISABLED_TypedTest, NumericTypes); 1.3282 + 1.3283 +TYPED_TEST(DISABLED_TypedTest, ShouldNotRun) { 1.3284 + FAIL() << "Unexpected failure: Disabled typed test should not run."; 1.3285 +} 1.3286 + 1.3287 +#endif // GTEST_HAS_TYPED_TEST 1.3288 + 1.3289 +// Tests that disabled type-parameterized tests aren't run. 1.3290 + 1.3291 +#if GTEST_HAS_TYPED_TEST_P 1.3292 + 1.3293 +template <typename T> 1.3294 +class TypedTestP : public Test { 1.3295 +}; 1.3296 + 1.3297 +TYPED_TEST_CASE_P(TypedTestP); 1.3298 + 1.3299 +TYPED_TEST_P(TypedTestP, DISABLED_ShouldNotRun) { 1.3300 + FAIL() << "Unexpected failure: " 1.3301 + << "Disabled type-parameterized test should not run."; 1.3302 +} 1.3303 + 1.3304 +REGISTER_TYPED_TEST_CASE_P(TypedTestP, DISABLED_ShouldNotRun); 1.3305 + 1.3306 +INSTANTIATE_TYPED_TEST_CASE_P(My, TypedTestP, NumericTypes); 1.3307 + 1.3308 +template <typename T> 1.3309 +class DISABLED_TypedTestP : public Test { 1.3310 +}; 1.3311 + 1.3312 +TYPED_TEST_CASE_P(DISABLED_TypedTestP); 1.3313 + 1.3314 +TYPED_TEST_P(DISABLED_TypedTestP, ShouldNotRun) { 1.3315 + FAIL() << "Unexpected failure: " 1.3316 + << "Disabled type-parameterized test should not run."; 1.3317 +} 1.3318 + 1.3319 +REGISTER_TYPED_TEST_CASE_P(DISABLED_TypedTestP, ShouldNotRun); 1.3320 + 1.3321 +INSTANTIATE_TYPED_TEST_CASE_P(My, DISABLED_TypedTestP, NumericTypes); 1.3322 + 1.3323 +#endif // GTEST_HAS_TYPED_TEST_P 1.3324 + 1.3325 +// Tests that assertion macros evaluate their arguments exactly once. 1.3326 + 1.3327 +class SingleEvaluationTest : public Test { 1.3328 + public: // Must be public and not protected due to a bug in g++ 3.4.2. 1.3329 + // This helper function is needed by the FailedASSERT_STREQ test 1.3330 + // below. It's public to work around C++Builder's bug with scoping local 1.3331 + // classes. 1.3332 + static void CompareAndIncrementCharPtrs() { 1.3333 + ASSERT_STREQ(p1_++, p2_++); 1.3334 + } 1.3335 + 1.3336 + // This helper function is needed by the FailedASSERT_NE test below. It's 1.3337 + // public to work around C++Builder's bug with scoping local classes. 1.3338 + static void CompareAndIncrementInts() { 1.3339 + ASSERT_NE(a_++, b_++); 1.3340 + } 1.3341 + 1.3342 + protected: 1.3343 + SingleEvaluationTest() { 1.3344 + p1_ = s1_; 1.3345 + p2_ = s2_; 1.3346 + a_ = 0; 1.3347 + b_ = 0; 1.3348 + } 1.3349 + 1.3350 + static const char* const s1_; 1.3351 + static const char* const s2_; 1.3352 + static const char* p1_; 1.3353 + static const char* p2_; 1.3354 + 1.3355 + static int a_; 1.3356 + static int b_; 1.3357 +}; 1.3358 + 1.3359 +const char* const SingleEvaluationTest::s1_ = "01234"; 1.3360 +const char* const SingleEvaluationTest::s2_ = "abcde"; 1.3361 +const char* SingleEvaluationTest::p1_; 1.3362 +const char* SingleEvaluationTest::p2_; 1.3363 +int SingleEvaluationTest::a_; 1.3364 +int SingleEvaluationTest::b_; 1.3365 + 1.3366 +// Tests that when ASSERT_STREQ fails, it evaluates its arguments 1.3367 +// exactly once. 1.3368 +TEST_F(SingleEvaluationTest, FailedASSERT_STREQ) { 1.3369 + EXPECT_FATAL_FAILURE(SingleEvaluationTest::CompareAndIncrementCharPtrs(), 1.3370 + "p2_++"); 1.3371 + EXPECT_EQ(s1_ + 1, p1_); 1.3372 + EXPECT_EQ(s2_ + 1, p2_); 1.3373 +} 1.3374 + 1.3375 +// Tests that string assertion arguments are evaluated exactly once. 1.3376 +TEST_F(SingleEvaluationTest, ASSERT_STR) { 1.3377 + // successful EXPECT_STRNE 1.3378 + EXPECT_STRNE(p1_++, p2_++); 1.3379 + EXPECT_EQ(s1_ + 1, p1_); 1.3380 + EXPECT_EQ(s2_ + 1, p2_); 1.3381 + 1.3382 + // failed EXPECT_STRCASEEQ 1.3383 + EXPECT_NONFATAL_FAILURE(EXPECT_STRCASEEQ(p1_++, p2_++), 1.3384 + "ignoring case"); 1.3385 + EXPECT_EQ(s1_ + 2, p1_); 1.3386 + EXPECT_EQ(s2_ + 2, p2_); 1.3387 +} 1.3388 + 1.3389 +// Tests that when ASSERT_NE fails, it evaluates its arguments exactly 1.3390 +// once. 1.3391 +TEST_F(SingleEvaluationTest, FailedASSERT_NE) { 1.3392 + EXPECT_FATAL_FAILURE(SingleEvaluationTest::CompareAndIncrementInts(), 1.3393 + "(a_++) != (b_++)"); 1.3394 + EXPECT_EQ(1, a_); 1.3395 + EXPECT_EQ(1, b_); 1.3396 +} 1.3397 + 1.3398 +// Tests that assertion arguments are evaluated exactly once. 1.3399 +TEST_F(SingleEvaluationTest, OtherCases) { 1.3400 + // successful EXPECT_TRUE 1.3401 + EXPECT_TRUE(0 == a_++); // NOLINT 1.3402 + EXPECT_EQ(1, a_); 1.3403 + 1.3404 + // failed EXPECT_TRUE 1.3405 + EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(-1 == a_++), "-1 == a_++"); 1.3406 + EXPECT_EQ(2, a_); 1.3407 + 1.3408 + // successful EXPECT_GT 1.3409 + EXPECT_GT(a_++, b_++); 1.3410 + EXPECT_EQ(3, a_); 1.3411 + EXPECT_EQ(1, b_); 1.3412 + 1.3413 + // failed EXPECT_LT 1.3414 + EXPECT_NONFATAL_FAILURE(EXPECT_LT(a_++, b_++), "(a_++) < (b_++)"); 1.3415 + EXPECT_EQ(4, a_); 1.3416 + EXPECT_EQ(2, b_); 1.3417 + 1.3418 + // successful ASSERT_TRUE 1.3419 + ASSERT_TRUE(0 < a_++); // NOLINT 1.3420 + EXPECT_EQ(5, a_); 1.3421 + 1.3422 + // successful ASSERT_GT 1.3423 + ASSERT_GT(a_++, b_++); 1.3424 + EXPECT_EQ(6, a_); 1.3425 + EXPECT_EQ(3, b_); 1.3426 +} 1.3427 + 1.3428 +#if GTEST_HAS_EXCEPTIONS 1.3429 + 1.3430 +void ThrowAnInteger() { 1.3431 + throw 1; 1.3432 +} 1.3433 + 1.3434 +// Tests that assertion arguments are evaluated exactly once. 1.3435 +TEST_F(SingleEvaluationTest, ExceptionTests) { 1.3436 + // successful EXPECT_THROW 1.3437 + EXPECT_THROW({ // NOLINT 1.3438 + a_++; 1.3439 + ThrowAnInteger(); 1.3440 + }, int); 1.3441 + EXPECT_EQ(1, a_); 1.3442 + 1.3443 + // failed EXPECT_THROW, throws different 1.3444 + EXPECT_NONFATAL_FAILURE(EXPECT_THROW({ // NOLINT 1.3445 + a_++; 1.3446 + ThrowAnInteger(); 1.3447 + }, bool), "throws a different type"); 1.3448 + EXPECT_EQ(2, a_); 1.3449 + 1.3450 + // failed EXPECT_THROW, throws nothing 1.3451 + EXPECT_NONFATAL_FAILURE(EXPECT_THROW(a_++, bool), "throws nothing"); 1.3452 + EXPECT_EQ(3, a_); 1.3453 + 1.3454 + // successful EXPECT_NO_THROW 1.3455 + EXPECT_NO_THROW(a_++); 1.3456 + EXPECT_EQ(4, a_); 1.3457 + 1.3458 + // failed EXPECT_NO_THROW 1.3459 + EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW({ // NOLINT 1.3460 + a_++; 1.3461 + ThrowAnInteger(); 1.3462 + }), "it throws"); 1.3463 + EXPECT_EQ(5, a_); 1.3464 + 1.3465 + // successful EXPECT_ANY_THROW 1.3466 + EXPECT_ANY_THROW({ // NOLINT 1.3467 + a_++; 1.3468 + ThrowAnInteger(); 1.3469 + }); 1.3470 + EXPECT_EQ(6, a_); 1.3471 + 1.3472 + // failed EXPECT_ANY_THROW 1.3473 + EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(a_++), "it doesn't"); 1.3474 + EXPECT_EQ(7, a_); 1.3475 +} 1.3476 + 1.3477 +#endif // GTEST_HAS_EXCEPTIONS 1.3478 + 1.3479 +// Tests {ASSERT|EXPECT}_NO_FATAL_FAILURE. 1.3480 +class NoFatalFailureTest : public Test { 1.3481 + protected: 1.3482 + void Succeeds() {} 1.3483 + void FailsNonFatal() { 1.3484 + ADD_FAILURE() << "some non-fatal failure"; 1.3485 + } 1.3486 + void Fails() { 1.3487 + FAIL() << "some fatal failure"; 1.3488 + } 1.3489 + 1.3490 + void DoAssertNoFatalFailureOnFails() { 1.3491 + ASSERT_NO_FATAL_FAILURE(Fails()); 1.3492 + ADD_FAILURE() << "shold not reach here."; 1.3493 + } 1.3494 + 1.3495 + void DoExpectNoFatalFailureOnFails() { 1.3496 + EXPECT_NO_FATAL_FAILURE(Fails()); 1.3497 + ADD_FAILURE() << "other failure"; 1.3498 + } 1.3499 +}; 1.3500 + 1.3501 +TEST_F(NoFatalFailureTest, NoFailure) { 1.3502 + EXPECT_NO_FATAL_FAILURE(Succeeds()); 1.3503 + ASSERT_NO_FATAL_FAILURE(Succeeds()); 1.3504 +} 1.3505 + 1.3506 +TEST_F(NoFatalFailureTest, NonFatalIsNoFailure) { 1.3507 + EXPECT_NONFATAL_FAILURE( 1.3508 + EXPECT_NO_FATAL_FAILURE(FailsNonFatal()), 1.3509 + "some non-fatal failure"); 1.3510 + EXPECT_NONFATAL_FAILURE( 1.3511 + ASSERT_NO_FATAL_FAILURE(FailsNonFatal()), 1.3512 + "some non-fatal failure"); 1.3513 +} 1.3514 + 1.3515 +TEST_F(NoFatalFailureTest, AssertNoFatalFailureOnFatalFailure) { 1.3516 + TestPartResultArray gtest_failures; 1.3517 + { 1.3518 + ScopedFakeTestPartResultReporter gtest_reporter(>est_failures); 1.3519 + DoAssertNoFatalFailureOnFails(); 1.3520 + } 1.3521 + ASSERT_EQ(2, gtest_failures.size()); 1.3522 + EXPECT_EQ(TestPartResult::kFatalFailure, 1.3523 + gtest_failures.GetTestPartResult(0).type()); 1.3524 + EXPECT_EQ(TestPartResult::kFatalFailure, 1.3525 + gtest_failures.GetTestPartResult(1).type()); 1.3526 + EXPECT_PRED_FORMAT2(testing::IsSubstring, "some fatal failure", 1.3527 + gtest_failures.GetTestPartResult(0).message()); 1.3528 + EXPECT_PRED_FORMAT2(testing::IsSubstring, "it does", 1.3529 + gtest_failures.GetTestPartResult(1).message()); 1.3530 +} 1.3531 + 1.3532 +TEST_F(NoFatalFailureTest, ExpectNoFatalFailureOnFatalFailure) { 1.3533 + TestPartResultArray gtest_failures; 1.3534 + { 1.3535 + ScopedFakeTestPartResultReporter gtest_reporter(>est_failures); 1.3536 + DoExpectNoFatalFailureOnFails(); 1.3537 + } 1.3538 + ASSERT_EQ(3, gtest_failures.size()); 1.3539 + EXPECT_EQ(TestPartResult::kFatalFailure, 1.3540 + gtest_failures.GetTestPartResult(0).type()); 1.3541 + EXPECT_EQ(TestPartResult::kNonFatalFailure, 1.3542 + gtest_failures.GetTestPartResult(1).type()); 1.3543 + EXPECT_EQ(TestPartResult::kNonFatalFailure, 1.3544 + gtest_failures.GetTestPartResult(2).type()); 1.3545 + EXPECT_PRED_FORMAT2(testing::IsSubstring, "some fatal failure", 1.3546 + gtest_failures.GetTestPartResult(0).message()); 1.3547 + EXPECT_PRED_FORMAT2(testing::IsSubstring, "it does", 1.3548 + gtest_failures.GetTestPartResult(1).message()); 1.3549 + EXPECT_PRED_FORMAT2(testing::IsSubstring, "other failure", 1.3550 + gtest_failures.GetTestPartResult(2).message()); 1.3551 +} 1.3552 + 1.3553 +TEST_F(NoFatalFailureTest, MessageIsStreamable) { 1.3554 + TestPartResultArray gtest_failures; 1.3555 + { 1.3556 + ScopedFakeTestPartResultReporter gtest_reporter(>est_failures); 1.3557 + EXPECT_NO_FATAL_FAILURE(FAIL() << "foo") << "my message"; 1.3558 + } 1.3559 + ASSERT_EQ(2, gtest_failures.size()); 1.3560 + EXPECT_EQ(TestPartResult::kNonFatalFailure, 1.3561 + gtest_failures.GetTestPartResult(0).type()); 1.3562 + EXPECT_EQ(TestPartResult::kNonFatalFailure, 1.3563 + gtest_failures.GetTestPartResult(1).type()); 1.3564 + EXPECT_PRED_FORMAT2(testing::IsSubstring, "foo", 1.3565 + gtest_failures.GetTestPartResult(0).message()); 1.3566 + EXPECT_PRED_FORMAT2(testing::IsSubstring, "my message", 1.3567 + gtest_failures.GetTestPartResult(1).message()); 1.3568 +} 1.3569 + 1.3570 +// Tests non-string assertions. 1.3571 + 1.3572 +// Tests EqFailure(), used for implementing *EQ* assertions. 1.3573 +TEST(AssertionTest, EqFailure) { 1.3574 + const String foo_val("5"), bar_val("6"); 1.3575 + const String msg1( 1.3576 + EqFailure("foo", "bar", foo_val, bar_val, false) 1.3577 + .failure_message()); 1.3578 + EXPECT_STREQ( 1.3579 + "Value of: bar\n" 1.3580 + " Actual: 6\n" 1.3581 + "Expected: foo\n" 1.3582 + "Which is: 5", 1.3583 + msg1.c_str()); 1.3584 + 1.3585 + const String msg2( 1.3586 + EqFailure("foo", "6", foo_val, bar_val, false) 1.3587 + .failure_message()); 1.3588 + EXPECT_STREQ( 1.3589 + "Value of: 6\n" 1.3590 + "Expected: foo\n" 1.3591 + "Which is: 5", 1.3592 + msg2.c_str()); 1.3593 + 1.3594 + const String msg3( 1.3595 + EqFailure("5", "bar", foo_val, bar_val, false) 1.3596 + .failure_message()); 1.3597 + EXPECT_STREQ( 1.3598 + "Value of: bar\n" 1.3599 + " Actual: 6\n" 1.3600 + "Expected: 5", 1.3601 + msg3.c_str()); 1.3602 + 1.3603 + const String msg4( 1.3604 + EqFailure("5", "6", foo_val, bar_val, false).failure_message()); 1.3605 + EXPECT_STREQ( 1.3606 + "Value of: 6\n" 1.3607 + "Expected: 5", 1.3608 + msg4.c_str()); 1.3609 + 1.3610 + const String msg5( 1.3611 + EqFailure("foo", "bar", 1.3612 + String("\"x\""), String("\"y\""), 1.3613 + true).failure_message()); 1.3614 + EXPECT_STREQ( 1.3615 + "Value of: bar\n" 1.3616 + " Actual: \"y\"\n" 1.3617 + "Expected: foo (ignoring case)\n" 1.3618 + "Which is: \"x\"", 1.3619 + msg5.c_str()); 1.3620 +} 1.3621 + 1.3622 +// Tests AppendUserMessage(), used for implementing the *EQ* macros. 1.3623 +TEST(AssertionTest, AppendUserMessage) { 1.3624 + const String foo("foo"); 1.3625 + 1.3626 + Message msg; 1.3627 + EXPECT_STREQ("foo", 1.3628 + AppendUserMessage(foo, msg).c_str()); 1.3629 + 1.3630 + msg << "bar"; 1.3631 + EXPECT_STREQ("foo\nbar", 1.3632 + AppendUserMessage(foo, msg).c_str()); 1.3633 +} 1.3634 + 1.3635 +#ifdef __BORLANDC__ 1.3636 +// Silences warnings: "Condition is always true", "Unreachable code" 1.3637 +# pragma option push -w-ccc -w-rch 1.3638 +#endif 1.3639 + 1.3640 +// Tests ASSERT_TRUE. 1.3641 +TEST(AssertionTest, ASSERT_TRUE) { 1.3642 + ASSERT_TRUE(2 > 1); // NOLINT 1.3643 + EXPECT_FATAL_FAILURE(ASSERT_TRUE(2 < 1), 1.3644 + "2 < 1"); 1.3645 +} 1.3646 + 1.3647 +// Tests ASSERT_TRUE(predicate) for predicates returning AssertionResult. 1.3648 +TEST(AssertionTest, AssertTrueWithAssertionResult) { 1.3649 + ASSERT_TRUE(ResultIsEven(2)); 1.3650 +#ifndef __BORLANDC__ 1.3651 + // ICE's in C++Builder. 1.3652 + EXPECT_FATAL_FAILURE(ASSERT_TRUE(ResultIsEven(3)), 1.3653 + "Value of: ResultIsEven(3)\n" 1.3654 + " Actual: false (3 is odd)\n" 1.3655 + "Expected: true"); 1.3656 +#endif 1.3657 + ASSERT_TRUE(ResultIsEvenNoExplanation(2)); 1.3658 + EXPECT_FATAL_FAILURE(ASSERT_TRUE(ResultIsEvenNoExplanation(3)), 1.3659 + "Value of: ResultIsEvenNoExplanation(3)\n" 1.3660 + " Actual: false (3 is odd)\n" 1.3661 + "Expected: true"); 1.3662 +} 1.3663 + 1.3664 +// Tests ASSERT_FALSE. 1.3665 +TEST(AssertionTest, ASSERT_FALSE) { 1.3666 + ASSERT_FALSE(2 < 1); // NOLINT 1.3667 + EXPECT_FATAL_FAILURE(ASSERT_FALSE(2 > 1), 1.3668 + "Value of: 2 > 1\n" 1.3669 + " Actual: true\n" 1.3670 + "Expected: false"); 1.3671 +} 1.3672 + 1.3673 +// Tests ASSERT_FALSE(predicate) for predicates returning AssertionResult. 1.3674 +TEST(AssertionTest, AssertFalseWithAssertionResult) { 1.3675 + ASSERT_FALSE(ResultIsEven(3)); 1.3676 +#ifndef __BORLANDC__ 1.3677 + // ICE's in C++Builder. 1.3678 + EXPECT_FATAL_FAILURE(ASSERT_FALSE(ResultIsEven(2)), 1.3679 + "Value of: ResultIsEven(2)\n" 1.3680 + " Actual: true (2 is even)\n" 1.3681 + "Expected: false"); 1.3682 +#endif 1.3683 + ASSERT_FALSE(ResultIsEvenNoExplanation(3)); 1.3684 + EXPECT_FATAL_FAILURE(ASSERT_FALSE(ResultIsEvenNoExplanation(2)), 1.3685 + "Value of: ResultIsEvenNoExplanation(2)\n" 1.3686 + " Actual: true\n" 1.3687 + "Expected: false"); 1.3688 +} 1.3689 + 1.3690 +#ifdef __BORLANDC__ 1.3691 +// Restores warnings after previous "#pragma option push" supressed them 1.3692 +# pragma option pop 1.3693 +#endif 1.3694 + 1.3695 +// Tests using ASSERT_EQ on double values. The purpose is to make 1.3696 +// sure that the specialization we did for integer and anonymous enums 1.3697 +// isn't used for double arguments. 1.3698 +TEST(ExpectTest, ASSERT_EQ_Double) { 1.3699 + // A success. 1.3700 + ASSERT_EQ(5.6, 5.6); 1.3701 + 1.3702 + // A failure. 1.3703 + EXPECT_FATAL_FAILURE(ASSERT_EQ(5.1, 5.2), 1.3704 + "5.1"); 1.3705 +} 1.3706 + 1.3707 +// Tests ASSERT_EQ. 1.3708 +TEST(AssertionTest, ASSERT_EQ) { 1.3709 + ASSERT_EQ(5, 2 + 3); 1.3710 + EXPECT_FATAL_FAILURE(ASSERT_EQ(5, 2*3), 1.3711 + "Value of: 2*3\n" 1.3712 + " Actual: 6\n" 1.3713 + "Expected: 5"); 1.3714 +} 1.3715 + 1.3716 +// Tests ASSERT_EQ(NULL, pointer). 1.3717 +#if GTEST_CAN_COMPARE_NULL 1.3718 +TEST(AssertionTest, ASSERT_EQ_NULL) { 1.3719 + // A success. 1.3720 + const char* p = NULL; 1.3721 + // Some older GCC versions may issue a spurious waring in this or the next 1.3722 + // assertion statement. This warning should not be suppressed with 1.3723 + // static_cast since the test verifies the ability to use bare NULL as the 1.3724 + // expected parameter to the macro. 1.3725 + ASSERT_EQ(NULL, p); 1.3726 + 1.3727 + // A failure. 1.3728 + static int n = 0; 1.3729 + EXPECT_FATAL_FAILURE(ASSERT_EQ(NULL, &n), 1.3730 + "Value of: &n\n"); 1.3731 +} 1.3732 +#endif // GTEST_CAN_COMPARE_NULL 1.3733 + 1.3734 +// Tests ASSERT_EQ(0, non_pointer). Since the literal 0 can be 1.3735 +// treated as a null pointer by the compiler, we need to make sure 1.3736 +// that ASSERT_EQ(0, non_pointer) isn't interpreted by Google Test as 1.3737 +// ASSERT_EQ(static_cast<void*>(NULL), non_pointer). 1.3738 +TEST(ExpectTest, ASSERT_EQ_0) { 1.3739 + int n = 0; 1.3740 + 1.3741 + // A success. 1.3742 + ASSERT_EQ(0, n); 1.3743 + 1.3744 + // A failure. 1.3745 + EXPECT_FATAL_FAILURE(ASSERT_EQ(0, 5.6), 1.3746 + "Expected: 0"); 1.3747 +} 1.3748 + 1.3749 +// Tests ASSERT_NE. 1.3750 +TEST(AssertionTest, ASSERT_NE) { 1.3751 + ASSERT_NE(6, 7); 1.3752 + EXPECT_FATAL_FAILURE(ASSERT_NE('a', 'a'), 1.3753 + "Expected: ('a') != ('a'), " 1.3754 + "actual: 'a' (97, 0x61) vs 'a' (97, 0x61)"); 1.3755 +} 1.3756 + 1.3757 +// Tests ASSERT_LE. 1.3758 +TEST(AssertionTest, ASSERT_LE) { 1.3759 + ASSERT_LE(2, 3); 1.3760 + ASSERT_LE(2, 2); 1.3761 + EXPECT_FATAL_FAILURE(ASSERT_LE(2, 0), 1.3762 + "Expected: (2) <= (0), actual: 2 vs 0"); 1.3763 +} 1.3764 + 1.3765 +// Tests ASSERT_LT. 1.3766 +TEST(AssertionTest, ASSERT_LT) { 1.3767 + ASSERT_LT(2, 3); 1.3768 + EXPECT_FATAL_FAILURE(ASSERT_LT(2, 2), 1.3769 + "Expected: (2) < (2), actual: 2 vs 2"); 1.3770 +} 1.3771 + 1.3772 +// Tests ASSERT_GE. 1.3773 +TEST(AssertionTest, ASSERT_GE) { 1.3774 + ASSERT_GE(2, 1); 1.3775 + ASSERT_GE(2, 2); 1.3776 + EXPECT_FATAL_FAILURE(ASSERT_GE(2, 3), 1.3777 + "Expected: (2) >= (3), actual: 2 vs 3"); 1.3778 +} 1.3779 + 1.3780 +// Tests ASSERT_GT. 1.3781 +TEST(AssertionTest, ASSERT_GT) { 1.3782 + ASSERT_GT(2, 1); 1.3783 + EXPECT_FATAL_FAILURE(ASSERT_GT(2, 2), 1.3784 + "Expected: (2) > (2), actual: 2 vs 2"); 1.3785 +} 1.3786 + 1.3787 +#if GTEST_HAS_EXCEPTIONS 1.3788 + 1.3789 +void ThrowNothing() {} 1.3790 + 1.3791 +// Tests ASSERT_THROW. 1.3792 +TEST(AssertionTest, ASSERT_THROW) { 1.3793 + ASSERT_THROW(ThrowAnInteger(), int); 1.3794 + 1.3795 +# ifndef __BORLANDC__ 1.3796 + 1.3797 + // ICE's in C++Builder 2007 and 2009. 1.3798 + EXPECT_FATAL_FAILURE( 1.3799 + ASSERT_THROW(ThrowAnInteger(), bool), 1.3800 + "Expected: ThrowAnInteger() throws an exception of type bool.\n" 1.3801 + " Actual: it throws a different type."); 1.3802 +# endif 1.3803 + 1.3804 + EXPECT_FATAL_FAILURE( 1.3805 + ASSERT_THROW(ThrowNothing(), bool), 1.3806 + "Expected: ThrowNothing() throws an exception of type bool.\n" 1.3807 + " Actual: it throws nothing."); 1.3808 +} 1.3809 + 1.3810 +// Tests ASSERT_NO_THROW. 1.3811 +TEST(AssertionTest, ASSERT_NO_THROW) { 1.3812 + ASSERT_NO_THROW(ThrowNothing()); 1.3813 + EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()), 1.3814 + "Expected: ThrowAnInteger() doesn't throw an exception." 1.3815 + "\n Actual: it throws."); 1.3816 +} 1.3817 + 1.3818 +// Tests ASSERT_ANY_THROW. 1.3819 +TEST(AssertionTest, ASSERT_ANY_THROW) { 1.3820 + ASSERT_ANY_THROW(ThrowAnInteger()); 1.3821 + EXPECT_FATAL_FAILURE( 1.3822 + ASSERT_ANY_THROW(ThrowNothing()), 1.3823 + "Expected: ThrowNothing() throws an exception.\n" 1.3824 + " Actual: it doesn't."); 1.3825 +} 1.3826 + 1.3827 +#endif // GTEST_HAS_EXCEPTIONS 1.3828 + 1.3829 +// Makes sure we deal with the precedence of <<. This test should 1.3830 +// compile. 1.3831 +TEST(AssertionTest, AssertPrecedence) { 1.3832 + ASSERT_EQ(1 < 2, true); 1.3833 + bool false_value = false; 1.3834 + ASSERT_EQ(true && false_value, false); 1.3835 +} 1.3836 + 1.3837 +// A subroutine used by the following test. 1.3838 +void TestEq1(int x) { 1.3839 + ASSERT_EQ(1, x); 1.3840 +} 1.3841 + 1.3842 +// Tests calling a test subroutine that's not part of a fixture. 1.3843 +TEST(AssertionTest, NonFixtureSubroutine) { 1.3844 + EXPECT_FATAL_FAILURE(TestEq1(2), 1.3845 + "Value of: x"); 1.3846 +} 1.3847 + 1.3848 +// An uncopyable class. 1.3849 +class Uncopyable { 1.3850 + public: 1.3851 + explicit Uncopyable(int a_value) : value_(a_value) {} 1.3852 + 1.3853 + int value() const { return value_; } 1.3854 + bool operator==(const Uncopyable& rhs) const { 1.3855 + return value() == rhs.value(); 1.3856 + } 1.3857 + private: 1.3858 + // This constructor deliberately has no implementation, as we don't 1.3859 + // want this class to be copyable. 1.3860 + Uncopyable(const Uncopyable&); // NOLINT 1.3861 + 1.3862 + int value_; 1.3863 +}; 1.3864 + 1.3865 +::std::ostream& operator<<(::std::ostream& os, const Uncopyable& value) { 1.3866 + return os << value.value(); 1.3867 +} 1.3868 + 1.3869 + 1.3870 +bool IsPositiveUncopyable(const Uncopyable& x) { 1.3871 + return x.value() > 0; 1.3872 +} 1.3873 + 1.3874 +// A subroutine used by the following test. 1.3875 +void TestAssertNonPositive() { 1.3876 + Uncopyable y(-1); 1.3877 + ASSERT_PRED1(IsPositiveUncopyable, y); 1.3878 +} 1.3879 +// A subroutine used by the following test. 1.3880 +void TestAssertEqualsUncopyable() { 1.3881 + Uncopyable x(5); 1.3882 + Uncopyable y(-1); 1.3883 + ASSERT_EQ(x, y); 1.3884 +} 1.3885 + 1.3886 +// Tests that uncopyable objects can be used in assertions. 1.3887 +TEST(AssertionTest, AssertWorksWithUncopyableObject) { 1.3888 + Uncopyable x(5); 1.3889 + ASSERT_PRED1(IsPositiveUncopyable, x); 1.3890 + ASSERT_EQ(x, x); 1.3891 + EXPECT_FATAL_FAILURE(TestAssertNonPositive(), 1.3892 + "IsPositiveUncopyable(y) evaluates to false, where\ny evaluates to -1"); 1.3893 + EXPECT_FATAL_FAILURE(TestAssertEqualsUncopyable(), 1.3894 + "Value of: y\n Actual: -1\nExpected: x\nWhich is: 5"); 1.3895 +} 1.3896 + 1.3897 +// Tests that uncopyable objects can be used in expects. 1.3898 +TEST(AssertionTest, ExpectWorksWithUncopyableObject) { 1.3899 + Uncopyable x(5); 1.3900 + EXPECT_PRED1(IsPositiveUncopyable, x); 1.3901 + Uncopyable y(-1); 1.3902 + EXPECT_NONFATAL_FAILURE(EXPECT_PRED1(IsPositiveUncopyable, y), 1.3903 + "IsPositiveUncopyable(y) evaluates to false, where\ny evaluates to -1"); 1.3904 + EXPECT_EQ(x, x); 1.3905 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(x, y), 1.3906 + "Value of: y\n Actual: -1\nExpected: x\nWhich is: 5"); 1.3907 +} 1.3908 + 1.3909 +enum NamedEnum { 1.3910 + kE1 = 0, 1.3911 + kE2 = 1 1.3912 +}; 1.3913 + 1.3914 +TEST(AssertionTest, NamedEnum) { 1.3915 + EXPECT_EQ(kE1, kE1); 1.3916 + EXPECT_LT(kE1, kE2); 1.3917 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(kE1, kE2), "Which is: 0"); 1.3918 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(kE1, kE2), "Actual: 1"); 1.3919 +} 1.3920 + 1.3921 +// The version of gcc used in XCode 2.2 has a bug and doesn't allow 1.3922 +// anonymous enums in assertions. Therefore the following test is not 1.3923 +// done on Mac. 1.3924 +// Sun Studio and HP aCC also reject this code. 1.3925 +#if !GTEST_OS_MAC && !defined(__SUNPRO_CC) && !defined(__HP_aCC) 1.3926 + 1.3927 +// Tests using assertions with anonymous enums. 1.3928 +enum { 1.3929 + kCaseA = -1, 1.3930 + 1.3931 +# if GTEST_OS_LINUX 1.3932 + 1.3933 + // We want to test the case where the size of the anonymous enum is 1.3934 + // larger than sizeof(int), to make sure our implementation of the 1.3935 + // assertions doesn't truncate the enums. However, MSVC 1.3936 + // (incorrectly) doesn't allow an enum value to exceed the range of 1.3937 + // an int, so this has to be conditionally compiled. 1.3938 + // 1.3939 + // On Linux, kCaseB and kCaseA have the same value when truncated to 1.3940 + // int size. We want to test whether this will confuse the 1.3941 + // assertions. 1.3942 + kCaseB = testing::internal::kMaxBiggestInt, 1.3943 + 1.3944 +# else 1.3945 + 1.3946 + kCaseB = INT_MAX, 1.3947 + 1.3948 +# endif // GTEST_OS_LINUX 1.3949 + 1.3950 + kCaseC = 42 1.3951 +}; 1.3952 + 1.3953 +TEST(AssertionTest, AnonymousEnum) { 1.3954 +# if GTEST_OS_LINUX 1.3955 + 1.3956 + EXPECT_EQ(static_cast<int>(kCaseA), static_cast<int>(kCaseB)); 1.3957 + 1.3958 +# endif // GTEST_OS_LINUX 1.3959 + 1.3960 + EXPECT_EQ(kCaseA, kCaseA); 1.3961 + EXPECT_NE(kCaseA, kCaseB); 1.3962 + EXPECT_LT(kCaseA, kCaseB); 1.3963 + EXPECT_LE(kCaseA, kCaseB); 1.3964 + EXPECT_GT(kCaseB, kCaseA); 1.3965 + EXPECT_GE(kCaseA, kCaseA); 1.3966 + EXPECT_NONFATAL_FAILURE(EXPECT_GE(kCaseA, kCaseB), 1.3967 + "(kCaseA) >= (kCaseB)"); 1.3968 + EXPECT_NONFATAL_FAILURE(EXPECT_GE(kCaseA, kCaseC), 1.3969 + "-1 vs 42"); 1.3970 + 1.3971 + ASSERT_EQ(kCaseA, kCaseA); 1.3972 + ASSERT_NE(kCaseA, kCaseB); 1.3973 + ASSERT_LT(kCaseA, kCaseB); 1.3974 + ASSERT_LE(kCaseA, kCaseB); 1.3975 + ASSERT_GT(kCaseB, kCaseA); 1.3976 + ASSERT_GE(kCaseA, kCaseA); 1.3977 + 1.3978 +# ifndef __BORLANDC__ 1.3979 + 1.3980 + // ICE's in C++Builder. 1.3981 + EXPECT_FATAL_FAILURE(ASSERT_EQ(kCaseA, kCaseB), 1.3982 + "Value of: kCaseB"); 1.3983 + EXPECT_FATAL_FAILURE(ASSERT_EQ(kCaseA, kCaseC), 1.3984 + "Actual: 42"); 1.3985 +# endif 1.3986 + 1.3987 + EXPECT_FATAL_FAILURE(ASSERT_EQ(kCaseA, kCaseC), 1.3988 + "Which is: -1"); 1.3989 +} 1.3990 + 1.3991 +#endif // !GTEST_OS_MAC && !defined(__SUNPRO_CC) 1.3992 + 1.3993 +#if GTEST_OS_WINDOWS 1.3994 + 1.3995 +static HRESULT UnexpectedHRESULTFailure() { 1.3996 + return E_UNEXPECTED; 1.3997 +} 1.3998 + 1.3999 +static HRESULT OkHRESULTSuccess() { 1.4000 + return S_OK; 1.4001 +} 1.4002 + 1.4003 +static HRESULT FalseHRESULTSuccess() { 1.4004 + return S_FALSE; 1.4005 +} 1.4006 + 1.4007 +// HRESULT assertion tests test both zero and non-zero 1.4008 +// success codes as well as failure message for each. 1.4009 +// 1.4010 +// Windows CE doesn't support message texts. 1.4011 +TEST(HRESULTAssertionTest, EXPECT_HRESULT_SUCCEEDED) { 1.4012 + EXPECT_HRESULT_SUCCEEDED(S_OK); 1.4013 + EXPECT_HRESULT_SUCCEEDED(S_FALSE); 1.4014 + 1.4015 + EXPECT_NONFATAL_FAILURE(EXPECT_HRESULT_SUCCEEDED(UnexpectedHRESULTFailure()), 1.4016 + "Expected: (UnexpectedHRESULTFailure()) succeeds.\n" 1.4017 + " Actual: 0x8000FFFF"); 1.4018 +} 1.4019 + 1.4020 +TEST(HRESULTAssertionTest, ASSERT_HRESULT_SUCCEEDED) { 1.4021 + ASSERT_HRESULT_SUCCEEDED(S_OK); 1.4022 + ASSERT_HRESULT_SUCCEEDED(S_FALSE); 1.4023 + 1.4024 + EXPECT_FATAL_FAILURE(ASSERT_HRESULT_SUCCEEDED(UnexpectedHRESULTFailure()), 1.4025 + "Expected: (UnexpectedHRESULTFailure()) succeeds.\n" 1.4026 + " Actual: 0x8000FFFF"); 1.4027 +} 1.4028 + 1.4029 +TEST(HRESULTAssertionTest, EXPECT_HRESULT_FAILED) { 1.4030 + EXPECT_HRESULT_FAILED(E_UNEXPECTED); 1.4031 + 1.4032 + EXPECT_NONFATAL_FAILURE(EXPECT_HRESULT_FAILED(OkHRESULTSuccess()), 1.4033 + "Expected: (OkHRESULTSuccess()) fails.\n" 1.4034 + " Actual: 0x00000000"); 1.4035 + EXPECT_NONFATAL_FAILURE(EXPECT_HRESULT_FAILED(FalseHRESULTSuccess()), 1.4036 + "Expected: (FalseHRESULTSuccess()) fails.\n" 1.4037 + " Actual: 0x00000001"); 1.4038 +} 1.4039 + 1.4040 +TEST(HRESULTAssertionTest, ASSERT_HRESULT_FAILED) { 1.4041 + ASSERT_HRESULT_FAILED(E_UNEXPECTED); 1.4042 + 1.4043 +# ifndef __BORLANDC__ 1.4044 + 1.4045 + // ICE's in C++Builder 2007 and 2009. 1.4046 + EXPECT_FATAL_FAILURE(ASSERT_HRESULT_FAILED(OkHRESULTSuccess()), 1.4047 + "Expected: (OkHRESULTSuccess()) fails.\n" 1.4048 + " Actual: 0x00000000"); 1.4049 +# endif 1.4050 + 1.4051 + EXPECT_FATAL_FAILURE(ASSERT_HRESULT_FAILED(FalseHRESULTSuccess()), 1.4052 + "Expected: (FalseHRESULTSuccess()) fails.\n" 1.4053 + " Actual: 0x00000001"); 1.4054 +} 1.4055 + 1.4056 +// Tests that streaming to the HRESULT macros works. 1.4057 +TEST(HRESULTAssertionTest, Streaming) { 1.4058 + EXPECT_HRESULT_SUCCEEDED(S_OK) << "unexpected failure"; 1.4059 + ASSERT_HRESULT_SUCCEEDED(S_OK) << "unexpected failure"; 1.4060 + EXPECT_HRESULT_FAILED(E_UNEXPECTED) << "unexpected failure"; 1.4061 + ASSERT_HRESULT_FAILED(E_UNEXPECTED) << "unexpected failure"; 1.4062 + 1.4063 + EXPECT_NONFATAL_FAILURE( 1.4064 + EXPECT_HRESULT_SUCCEEDED(E_UNEXPECTED) << "expected failure", 1.4065 + "expected failure"); 1.4066 + 1.4067 +# ifndef __BORLANDC__ 1.4068 + 1.4069 + // ICE's in C++Builder 2007 and 2009. 1.4070 + EXPECT_FATAL_FAILURE( 1.4071 + ASSERT_HRESULT_SUCCEEDED(E_UNEXPECTED) << "expected failure", 1.4072 + "expected failure"); 1.4073 +# endif 1.4074 + 1.4075 + EXPECT_NONFATAL_FAILURE( 1.4076 + EXPECT_HRESULT_FAILED(S_OK) << "expected failure", 1.4077 + "expected failure"); 1.4078 + 1.4079 + EXPECT_FATAL_FAILURE( 1.4080 + ASSERT_HRESULT_FAILED(S_OK) << "expected failure", 1.4081 + "expected failure"); 1.4082 +} 1.4083 + 1.4084 +#endif // GTEST_OS_WINDOWS 1.4085 + 1.4086 +#ifdef __BORLANDC__ 1.4087 +// Silences warnings: "Condition is always true", "Unreachable code" 1.4088 +# pragma option push -w-ccc -w-rch 1.4089 +#endif 1.4090 + 1.4091 +// Tests that the assertion macros behave like single statements. 1.4092 +TEST(AssertionSyntaxTest, BasicAssertionsBehavesLikeSingleStatement) { 1.4093 + if (AlwaysFalse()) 1.4094 + ASSERT_TRUE(false) << "This should never be executed; " 1.4095 + "It's a compilation test only."; 1.4096 + 1.4097 + if (AlwaysTrue()) 1.4098 + EXPECT_FALSE(false); 1.4099 + else 1.4100 + ; // NOLINT 1.4101 + 1.4102 + if (AlwaysFalse()) 1.4103 + ASSERT_LT(1, 3); 1.4104 + 1.4105 + if (AlwaysFalse()) 1.4106 + ; // NOLINT 1.4107 + else 1.4108 + EXPECT_GT(3, 2) << ""; 1.4109 +} 1.4110 + 1.4111 +#if GTEST_HAS_EXCEPTIONS 1.4112 +// Tests that the compiler will not complain about unreachable code in the 1.4113 +// EXPECT_THROW/EXPECT_ANY_THROW/EXPECT_NO_THROW macros. 1.4114 +TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) { 1.4115 + int n = 0; 1.4116 + 1.4117 + EXPECT_THROW(throw 1, int); 1.4118 + EXPECT_NONFATAL_FAILURE(EXPECT_THROW(n++, int), ""); 1.4119 + EXPECT_NONFATAL_FAILURE(EXPECT_THROW(throw 1, const char*), ""); 1.4120 + EXPECT_NO_THROW(n++); 1.4121 + EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(throw 1), ""); 1.4122 + EXPECT_ANY_THROW(throw 1); 1.4123 + EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(n++), ""); 1.4124 +} 1.4125 + 1.4126 +TEST(AssertionSyntaxTest, ExceptionAssertionsBehavesLikeSingleStatement) { 1.4127 + if (AlwaysFalse()) 1.4128 + EXPECT_THROW(ThrowNothing(), bool); 1.4129 + 1.4130 + if (AlwaysTrue()) 1.4131 + EXPECT_THROW(ThrowAnInteger(), int); 1.4132 + else 1.4133 + ; // NOLINT 1.4134 + 1.4135 + if (AlwaysFalse()) 1.4136 + EXPECT_NO_THROW(ThrowAnInteger()); 1.4137 + 1.4138 + if (AlwaysTrue()) 1.4139 + EXPECT_NO_THROW(ThrowNothing()); 1.4140 + else 1.4141 + ; // NOLINT 1.4142 + 1.4143 + if (AlwaysFalse()) 1.4144 + EXPECT_ANY_THROW(ThrowNothing()); 1.4145 + 1.4146 + if (AlwaysTrue()) 1.4147 + EXPECT_ANY_THROW(ThrowAnInteger()); 1.4148 + else 1.4149 + ; // NOLINT 1.4150 +} 1.4151 +#endif // GTEST_HAS_EXCEPTIONS 1.4152 + 1.4153 +TEST(AssertionSyntaxTest, NoFatalFailureAssertionsBehavesLikeSingleStatement) { 1.4154 + if (AlwaysFalse()) 1.4155 + EXPECT_NO_FATAL_FAILURE(FAIL()) << "This should never be executed. " 1.4156 + << "It's a compilation test only."; 1.4157 + else 1.4158 + ; // NOLINT 1.4159 + 1.4160 + if (AlwaysFalse()) 1.4161 + ASSERT_NO_FATAL_FAILURE(FAIL()) << ""; 1.4162 + else 1.4163 + ; // NOLINT 1.4164 + 1.4165 + if (AlwaysTrue()) 1.4166 + EXPECT_NO_FATAL_FAILURE(SUCCEED()); 1.4167 + else 1.4168 + ; // NOLINT 1.4169 + 1.4170 + if (AlwaysFalse()) 1.4171 + ; // NOLINT 1.4172 + else 1.4173 + ASSERT_NO_FATAL_FAILURE(SUCCEED()); 1.4174 +} 1.4175 + 1.4176 +// Tests that the assertion macros work well with switch statements. 1.4177 +TEST(AssertionSyntaxTest, WorksWithSwitch) { 1.4178 + switch (0) { 1.4179 + case 1: 1.4180 + break; 1.4181 + default: 1.4182 + ASSERT_TRUE(true); 1.4183 + } 1.4184 + 1.4185 + switch (0) 1.4186 + case 0: 1.4187 + EXPECT_FALSE(false) << "EXPECT_FALSE failed in switch case"; 1.4188 + 1.4189 + // Binary assertions are implemented using a different code path 1.4190 + // than the Boolean assertions. Hence we test them separately. 1.4191 + switch (0) { 1.4192 + case 1: 1.4193 + default: 1.4194 + ASSERT_EQ(1, 1) << "ASSERT_EQ failed in default switch handler"; 1.4195 + } 1.4196 + 1.4197 + switch (0) 1.4198 + case 0: 1.4199 + EXPECT_NE(1, 2); 1.4200 +} 1.4201 + 1.4202 +#if GTEST_HAS_EXCEPTIONS 1.4203 + 1.4204 +void ThrowAString() { 1.4205 + throw "String"; 1.4206 +} 1.4207 + 1.4208 +// Test that the exception assertion macros compile and work with const 1.4209 +// type qualifier. 1.4210 +TEST(AssertionSyntaxTest, WorksWithConst) { 1.4211 + ASSERT_THROW(ThrowAString(), const char*); 1.4212 + 1.4213 + EXPECT_THROW(ThrowAString(), const char*); 1.4214 +} 1.4215 + 1.4216 +#endif // GTEST_HAS_EXCEPTIONS 1.4217 + 1.4218 +} // namespace 1.4219 + 1.4220 +namespace testing { 1.4221 + 1.4222 +// Tests that Google Test tracks SUCCEED*. 1.4223 +TEST(SuccessfulAssertionTest, SUCCEED) { 1.4224 + SUCCEED(); 1.4225 + SUCCEED() << "OK"; 1.4226 + EXPECT_EQ(2, GetUnitTestImpl()->current_test_result()->total_part_count()); 1.4227 +} 1.4228 + 1.4229 +// Tests that Google Test doesn't track successful EXPECT_*. 1.4230 +TEST(SuccessfulAssertionTest, EXPECT) { 1.4231 + EXPECT_TRUE(true); 1.4232 + EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); 1.4233 +} 1.4234 + 1.4235 +// Tests that Google Test doesn't track successful EXPECT_STR*. 1.4236 +TEST(SuccessfulAssertionTest, EXPECT_STR) { 1.4237 + EXPECT_STREQ("", ""); 1.4238 + EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); 1.4239 +} 1.4240 + 1.4241 +// Tests that Google Test doesn't track successful ASSERT_*. 1.4242 +TEST(SuccessfulAssertionTest, ASSERT) { 1.4243 + ASSERT_TRUE(true); 1.4244 + EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); 1.4245 +} 1.4246 + 1.4247 +// Tests that Google Test doesn't track successful ASSERT_STR*. 1.4248 +TEST(SuccessfulAssertionTest, ASSERT_STR) { 1.4249 + ASSERT_STREQ("", ""); 1.4250 + EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); 1.4251 +} 1.4252 + 1.4253 +} // namespace testing 1.4254 + 1.4255 +namespace { 1.4256 + 1.4257 +// Tests the message streaming variation of assertions. 1.4258 + 1.4259 +TEST(AssertionWithMessageTest, EXPECT) { 1.4260 + EXPECT_EQ(1, 1) << "This should succeed."; 1.4261 + EXPECT_NONFATAL_FAILURE(EXPECT_NE(1, 1) << "Expected failure #1.", 1.4262 + "Expected failure #1"); 1.4263 + EXPECT_LE(1, 2) << "This should succeed."; 1.4264 + EXPECT_NONFATAL_FAILURE(EXPECT_LT(1, 0) << "Expected failure #2.", 1.4265 + "Expected failure #2."); 1.4266 + EXPECT_GE(1, 0) << "This should succeed."; 1.4267 + EXPECT_NONFATAL_FAILURE(EXPECT_GT(1, 2) << "Expected failure #3.", 1.4268 + "Expected failure #3."); 1.4269 + 1.4270 + EXPECT_STREQ("1", "1") << "This should succeed."; 1.4271 + EXPECT_NONFATAL_FAILURE(EXPECT_STRNE("1", "1") << "Expected failure #4.", 1.4272 + "Expected failure #4."); 1.4273 + EXPECT_STRCASEEQ("a", "A") << "This should succeed."; 1.4274 + EXPECT_NONFATAL_FAILURE(EXPECT_STRCASENE("a", "A") << "Expected failure #5.", 1.4275 + "Expected failure #5."); 1.4276 + 1.4277 + EXPECT_FLOAT_EQ(1, 1) << "This should succeed."; 1.4278 + EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(1, 1.2) << "Expected failure #6.", 1.4279 + "Expected failure #6."); 1.4280 + EXPECT_NEAR(1, 1.1, 0.2) << "This should succeed."; 1.4281 +} 1.4282 + 1.4283 +TEST(AssertionWithMessageTest, ASSERT) { 1.4284 + ASSERT_EQ(1, 1) << "This should succeed."; 1.4285 + ASSERT_NE(1, 2) << "This should succeed."; 1.4286 + ASSERT_LE(1, 2) << "This should succeed."; 1.4287 + ASSERT_LT(1, 2) << "This should succeed."; 1.4288 + ASSERT_GE(1, 0) << "This should succeed."; 1.4289 + EXPECT_FATAL_FAILURE(ASSERT_GT(1, 2) << "Expected failure.", 1.4290 + "Expected failure."); 1.4291 +} 1.4292 + 1.4293 +TEST(AssertionWithMessageTest, ASSERT_STR) { 1.4294 + ASSERT_STREQ("1", "1") << "This should succeed."; 1.4295 + ASSERT_STRNE("1", "2") << "This should succeed."; 1.4296 + ASSERT_STRCASEEQ("a", "A") << "This should succeed."; 1.4297 + EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("a", "A") << "Expected failure.", 1.4298 + "Expected failure."); 1.4299 +} 1.4300 + 1.4301 +TEST(AssertionWithMessageTest, ASSERT_FLOATING) { 1.4302 + ASSERT_FLOAT_EQ(1, 1) << "This should succeed."; 1.4303 + ASSERT_DOUBLE_EQ(1, 1) << "This should succeed."; 1.4304 + EXPECT_FATAL_FAILURE(ASSERT_NEAR(1,1.2, 0.1) << "Expect failure.", // NOLINT 1.4305 + "Expect failure."); 1.4306 + // To work around a bug in gcc 2.95.0, there is intentionally no 1.4307 + // space after the first comma in the previous statement. 1.4308 +} 1.4309 + 1.4310 +// Tests using ASSERT_FALSE with a streamed message. 1.4311 +TEST(AssertionWithMessageTest, ASSERT_FALSE) { 1.4312 + ASSERT_FALSE(false) << "This shouldn't fail."; 1.4313 + EXPECT_FATAL_FAILURE({ // NOLINT 1.4314 + ASSERT_FALSE(true) << "Expected failure: " << 2 << " > " << 1 1.4315 + << " evaluates to " << true; 1.4316 + }, "Expected failure"); 1.4317 +} 1.4318 + 1.4319 +// Tests using FAIL with a streamed message. 1.4320 +TEST(AssertionWithMessageTest, FAIL) { 1.4321 + EXPECT_FATAL_FAILURE(FAIL() << 0, 1.4322 + "0"); 1.4323 +} 1.4324 + 1.4325 +// Tests using SUCCEED with a streamed message. 1.4326 +TEST(AssertionWithMessageTest, SUCCEED) { 1.4327 + SUCCEED() << "Success == " << 1; 1.4328 +} 1.4329 + 1.4330 +// Tests using ASSERT_TRUE with a streamed message. 1.4331 +TEST(AssertionWithMessageTest, ASSERT_TRUE) { 1.4332 + ASSERT_TRUE(true) << "This should succeed."; 1.4333 + ASSERT_TRUE(true) << true; 1.4334 + EXPECT_FATAL_FAILURE({ // NOLINT 1.4335 + ASSERT_TRUE(false) << static_cast<const char *>(NULL) 1.4336 + << static_cast<char *>(NULL); 1.4337 + }, "(null)(null)"); 1.4338 +} 1.4339 + 1.4340 +#if GTEST_OS_WINDOWS 1.4341 +// Tests using wide strings in assertion messages. 1.4342 +TEST(AssertionWithMessageTest, WideStringMessage) { 1.4343 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.4344 + EXPECT_TRUE(false) << L"This failure is expected.\x8119"; 1.4345 + }, "This failure is expected."); 1.4346 + EXPECT_FATAL_FAILURE({ // NOLINT 1.4347 + ASSERT_EQ(1, 2) << "This failure is " 1.4348 + << L"expected too.\x8120"; 1.4349 + }, "This failure is expected too."); 1.4350 +} 1.4351 +#endif // GTEST_OS_WINDOWS 1.4352 + 1.4353 +// Tests EXPECT_TRUE. 1.4354 +TEST(ExpectTest, EXPECT_TRUE) { 1.4355 + EXPECT_TRUE(true) << "Intentional success"; 1.4356 + EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(false) << "Intentional failure #1.", 1.4357 + "Intentional failure #1."); 1.4358 + EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(false) << "Intentional failure #2.", 1.4359 + "Intentional failure #2."); 1.4360 + EXPECT_TRUE(2 > 1); // NOLINT 1.4361 + EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(2 < 1), 1.4362 + "Value of: 2 < 1\n" 1.4363 + " Actual: false\n" 1.4364 + "Expected: true"); 1.4365 + EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(2 > 3), 1.4366 + "2 > 3"); 1.4367 +} 1.4368 + 1.4369 +// Tests EXPECT_TRUE(predicate) for predicates returning AssertionResult. 1.4370 +TEST(ExpectTest, ExpectTrueWithAssertionResult) { 1.4371 + EXPECT_TRUE(ResultIsEven(2)); 1.4372 + EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(ResultIsEven(3)), 1.4373 + "Value of: ResultIsEven(3)\n" 1.4374 + " Actual: false (3 is odd)\n" 1.4375 + "Expected: true"); 1.4376 + EXPECT_TRUE(ResultIsEvenNoExplanation(2)); 1.4377 + EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(ResultIsEvenNoExplanation(3)), 1.4378 + "Value of: ResultIsEvenNoExplanation(3)\n" 1.4379 + " Actual: false (3 is odd)\n" 1.4380 + "Expected: true"); 1.4381 +} 1.4382 + 1.4383 +// Tests EXPECT_FALSE with a streamed message. 1.4384 +TEST(ExpectTest, EXPECT_FALSE) { 1.4385 + EXPECT_FALSE(2 < 1); // NOLINT 1.4386 + EXPECT_FALSE(false) << "Intentional success"; 1.4387 + EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(true) << "Intentional failure #1.", 1.4388 + "Intentional failure #1."); 1.4389 + EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(true) << "Intentional failure #2.", 1.4390 + "Intentional failure #2."); 1.4391 + EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(2 > 1), 1.4392 + "Value of: 2 > 1\n" 1.4393 + " Actual: true\n" 1.4394 + "Expected: false"); 1.4395 + EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(2 < 3), 1.4396 + "2 < 3"); 1.4397 +} 1.4398 + 1.4399 +// Tests EXPECT_FALSE(predicate) for predicates returning AssertionResult. 1.4400 +TEST(ExpectTest, ExpectFalseWithAssertionResult) { 1.4401 + EXPECT_FALSE(ResultIsEven(3)); 1.4402 + EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(ResultIsEven(2)), 1.4403 + "Value of: ResultIsEven(2)\n" 1.4404 + " Actual: true (2 is even)\n" 1.4405 + "Expected: false"); 1.4406 + EXPECT_FALSE(ResultIsEvenNoExplanation(3)); 1.4407 + EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(ResultIsEvenNoExplanation(2)), 1.4408 + "Value of: ResultIsEvenNoExplanation(2)\n" 1.4409 + " Actual: true\n" 1.4410 + "Expected: false"); 1.4411 +} 1.4412 + 1.4413 +#ifdef __BORLANDC__ 1.4414 +// Restores warnings after previous "#pragma option push" supressed them 1.4415 +# pragma option pop 1.4416 +#endif 1.4417 + 1.4418 +// Tests EXPECT_EQ. 1.4419 +TEST(ExpectTest, EXPECT_EQ) { 1.4420 + EXPECT_EQ(5, 2 + 3); 1.4421 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5, 2*3), 1.4422 + "Value of: 2*3\n" 1.4423 + " Actual: 6\n" 1.4424 + "Expected: 5"); 1.4425 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5, 2 - 3), 1.4426 + "2 - 3"); 1.4427 +} 1.4428 + 1.4429 +// Tests using EXPECT_EQ on double values. The purpose is to make 1.4430 +// sure that the specialization we did for integer and anonymous enums 1.4431 +// isn't used for double arguments. 1.4432 +TEST(ExpectTest, EXPECT_EQ_Double) { 1.4433 + // A success. 1.4434 + EXPECT_EQ(5.6, 5.6); 1.4435 + 1.4436 + // A failure. 1.4437 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5.1, 5.2), 1.4438 + "5.1"); 1.4439 +} 1.4440 + 1.4441 +#if GTEST_CAN_COMPARE_NULL 1.4442 +// Tests EXPECT_EQ(NULL, pointer). 1.4443 +TEST(ExpectTest, EXPECT_EQ_NULL) { 1.4444 + // A success. 1.4445 + const char* p = NULL; 1.4446 + // Some older GCC versions may issue a spurious warning in this or the next 1.4447 + // assertion statement. This warning should not be suppressed with 1.4448 + // static_cast since the test verifies the ability to use bare NULL as the 1.4449 + // expected parameter to the macro. 1.4450 + EXPECT_EQ(NULL, p); 1.4451 + 1.4452 + // A failure. 1.4453 + int n = 0; 1.4454 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(NULL, &n), 1.4455 + "Value of: &n\n"); 1.4456 +} 1.4457 +#endif // GTEST_CAN_COMPARE_NULL 1.4458 + 1.4459 +// Tests EXPECT_EQ(0, non_pointer). Since the literal 0 can be 1.4460 +// treated as a null pointer by the compiler, we need to make sure 1.4461 +// that EXPECT_EQ(0, non_pointer) isn't interpreted by Google Test as 1.4462 +// EXPECT_EQ(static_cast<void*>(NULL), non_pointer). 1.4463 +TEST(ExpectTest, EXPECT_EQ_0) { 1.4464 + int n = 0; 1.4465 + 1.4466 + // A success. 1.4467 + EXPECT_EQ(0, n); 1.4468 + 1.4469 + // A failure. 1.4470 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(0, 5.6), 1.4471 + "Expected: 0"); 1.4472 +} 1.4473 + 1.4474 +// Tests EXPECT_NE. 1.4475 +TEST(ExpectTest, EXPECT_NE) { 1.4476 + EXPECT_NE(6, 7); 1.4477 + 1.4478 + EXPECT_NONFATAL_FAILURE(EXPECT_NE('a', 'a'), 1.4479 + "Expected: ('a') != ('a'), " 1.4480 + "actual: 'a' (97, 0x61) vs 'a' (97, 0x61)"); 1.4481 + EXPECT_NONFATAL_FAILURE(EXPECT_NE(2, 2), 1.4482 + "2"); 1.4483 + char* const p0 = NULL; 1.4484 + EXPECT_NONFATAL_FAILURE(EXPECT_NE(p0, p0), 1.4485 + "p0"); 1.4486 + // Only way to get the Nokia compiler to compile the cast 1.4487 + // is to have a separate void* variable first. Putting 1.4488 + // the two casts on the same line doesn't work, neither does 1.4489 + // a direct C-style to char*. 1.4490 + void* pv1 = (void*)0x1234; // NOLINT 1.4491 + char* const p1 = reinterpret_cast<char*>(pv1); 1.4492 + EXPECT_NONFATAL_FAILURE(EXPECT_NE(p1, p1), 1.4493 + "p1"); 1.4494 +} 1.4495 + 1.4496 +// Tests EXPECT_LE. 1.4497 +TEST(ExpectTest, EXPECT_LE) { 1.4498 + EXPECT_LE(2, 3); 1.4499 + EXPECT_LE(2, 2); 1.4500 + EXPECT_NONFATAL_FAILURE(EXPECT_LE(2, 0), 1.4501 + "Expected: (2) <= (0), actual: 2 vs 0"); 1.4502 + EXPECT_NONFATAL_FAILURE(EXPECT_LE(1.1, 0.9), 1.4503 + "(1.1) <= (0.9)"); 1.4504 +} 1.4505 + 1.4506 +// Tests EXPECT_LT. 1.4507 +TEST(ExpectTest, EXPECT_LT) { 1.4508 + EXPECT_LT(2, 3); 1.4509 + EXPECT_NONFATAL_FAILURE(EXPECT_LT(2, 2), 1.4510 + "Expected: (2) < (2), actual: 2 vs 2"); 1.4511 + EXPECT_NONFATAL_FAILURE(EXPECT_LT(2, 1), 1.4512 + "(2) < (1)"); 1.4513 +} 1.4514 + 1.4515 +// Tests EXPECT_GE. 1.4516 +TEST(ExpectTest, EXPECT_GE) { 1.4517 + EXPECT_GE(2, 1); 1.4518 + EXPECT_GE(2, 2); 1.4519 + EXPECT_NONFATAL_FAILURE(EXPECT_GE(2, 3), 1.4520 + "Expected: (2) >= (3), actual: 2 vs 3"); 1.4521 + EXPECT_NONFATAL_FAILURE(EXPECT_GE(0.9, 1.1), 1.4522 + "(0.9) >= (1.1)"); 1.4523 +} 1.4524 + 1.4525 +// Tests EXPECT_GT. 1.4526 +TEST(ExpectTest, EXPECT_GT) { 1.4527 + EXPECT_GT(2, 1); 1.4528 + EXPECT_NONFATAL_FAILURE(EXPECT_GT(2, 2), 1.4529 + "Expected: (2) > (2), actual: 2 vs 2"); 1.4530 + EXPECT_NONFATAL_FAILURE(EXPECT_GT(2, 3), 1.4531 + "(2) > (3)"); 1.4532 +} 1.4533 + 1.4534 +#if GTEST_HAS_EXCEPTIONS 1.4535 + 1.4536 +// Tests EXPECT_THROW. 1.4537 +TEST(ExpectTest, EXPECT_THROW) { 1.4538 + EXPECT_THROW(ThrowAnInteger(), int); 1.4539 + EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnInteger(), bool), 1.4540 + "Expected: ThrowAnInteger() throws an exception of " 1.4541 + "type bool.\n Actual: it throws a different type."); 1.4542 + EXPECT_NONFATAL_FAILURE( 1.4543 + EXPECT_THROW(ThrowNothing(), bool), 1.4544 + "Expected: ThrowNothing() throws an exception of type bool.\n" 1.4545 + " Actual: it throws nothing."); 1.4546 +} 1.4547 + 1.4548 +// Tests EXPECT_NO_THROW. 1.4549 +TEST(ExpectTest, EXPECT_NO_THROW) { 1.4550 + EXPECT_NO_THROW(ThrowNothing()); 1.4551 + EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()), 1.4552 + "Expected: ThrowAnInteger() doesn't throw an " 1.4553 + "exception.\n Actual: it throws."); 1.4554 +} 1.4555 + 1.4556 +// Tests EXPECT_ANY_THROW. 1.4557 +TEST(ExpectTest, EXPECT_ANY_THROW) { 1.4558 + EXPECT_ANY_THROW(ThrowAnInteger()); 1.4559 + EXPECT_NONFATAL_FAILURE( 1.4560 + EXPECT_ANY_THROW(ThrowNothing()), 1.4561 + "Expected: ThrowNothing() throws an exception.\n" 1.4562 + " Actual: it doesn't."); 1.4563 +} 1.4564 + 1.4565 +#endif // GTEST_HAS_EXCEPTIONS 1.4566 + 1.4567 +// Make sure we deal with the precedence of <<. 1.4568 +TEST(ExpectTest, ExpectPrecedence) { 1.4569 + EXPECT_EQ(1 < 2, true); 1.4570 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(true, true && false), 1.4571 + "Value of: true && false"); 1.4572 +} 1.4573 + 1.4574 + 1.4575 +// Tests the StreamableToString() function. 1.4576 + 1.4577 +// Tests using StreamableToString() on a scalar. 1.4578 +TEST(StreamableToStringTest, Scalar) { 1.4579 + EXPECT_STREQ("5", StreamableToString(5).c_str()); 1.4580 +} 1.4581 + 1.4582 +// Tests using StreamableToString() on a non-char pointer. 1.4583 +TEST(StreamableToStringTest, Pointer) { 1.4584 + int n = 0; 1.4585 + int* p = &n; 1.4586 + EXPECT_STRNE("(null)", StreamableToString(p).c_str()); 1.4587 +} 1.4588 + 1.4589 +// Tests using StreamableToString() on a NULL non-char pointer. 1.4590 +TEST(StreamableToStringTest, NullPointer) { 1.4591 + int* p = NULL; 1.4592 + EXPECT_STREQ("(null)", StreamableToString(p).c_str()); 1.4593 +} 1.4594 + 1.4595 +// Tests using StreamableToString() on a C string. 1.4596 +TEST(StreamableToStringTest, CString) { 1.4597 + EXPECT_STREQ("Foo", StreamableToString("Foo").c_str()); 1.4598 +} 1.4599 + 1.4600 +// Tests using StreamableToString() on a NULL C string. 1.4601 +TEST(StreamableToStringTest, NullCString) { 1.4602 + char* p = NULL; 1.4603 + EXPECT_STREQ("(null)", StreamableToString(p).c_str()); 1.4604 +} 1.4605 + 1.4606 +// Tests using streamable values as assertion messages. 1.4607 + 1.4608 +// Tests using std::string as an assertion message. 1.4609 +TEST(StreamableTest, string) { 1.4610 + static const std::string str( 1.4611 + "This failure message is a std::string, and is expected."); 1.4612 + EXPECT_FATAL_FAILURE(FAIL() << str, 1.4613 + str.c_str()); 1.4614 +} 1.4615 + 1.4616 +// Tests that we can output strings containing embedded NULs. 1.4617 +// Limited to Linux because we can only do this with std::string's. 1.4618 +TEST(StreamableTest, stringWithEmbeddedNUL) { 1.4619 + static const char char_array_with_nul[] = 1.4620 + "Here's a NUL\0 and some more string"; 1.4621 + static const std::string string_with_nul(char_array_with_nul, 1.4622 + sizeof(char_array_with_nul) 1.4623 + - 1); // drops the trailing NUL 1.4624 + EXPECT_FATAL_FAILURE(FAIL() << string_with_nul, 1.4625 + "Here's a NUL\\0 and some more string"); 1.4626 +} 1.4627 + 1.4628 +// Tests that we can output a NUL char. 1.4629 +TEST(StreamableTest, NULChar) { 1.4630 + EXPECT_FATAL_FAILURE({ // NOLINT 1.4631 + FAIL() << "A NUL" << '\0' << " and some more string"; 1.4632 + }, "A NUL\\0 and some more string"); 1.4633 +} 1.4634 + 1.4635 +// Tests using int as an assertion message. 1.4636 +TEST(StreamableTest, int) { 1.4637 + EXPECT_FATAL_FAILURE(FAIL() << 900913, 1.4638 + "900913"); 1.4639 +} 1.4640 + 1.4641 +// Tests using NULL char pointer as an assertion message. 1.4642 +// 1.4643 +// In MSVC, streaming a NULL char * causes access violation. Google Test 1.4644 +// implemented a workaround (substituting "(null)" for NULL). This 1.4645 +// tests whether the workaround works. 1.4646 +TEST(StreamableTest, NullCharPtr) { 1.4647 + EXPECT_FATAL_FAILURE(FAIL() << static_cast<const char*>(NULL), 1.4648 + "(null)"); 1.4649 +} 1.4650 + 1.4651 +// Tests that basic IO manipulators (endl, ends, and flush) can be 1.4652 +// streamed to testing::Message. 1.4653 +TEST(StreamableTest, BasicIoManip) { 1.4654 + EXPECT_FATAL_FAILURE({ // NOLINT 1.4655 + FAIL() << "Line 1." << std::endl 1.4656 + << "A NUL char " << std::ends << std::flush << " in line 2."; 1.4657 + }, "Line 1.\nA NUL char \\0 in line 2."); 1.4658 +} 1.4659 + 1.4660 +// Tests the macros that haven't been covered so far. 1.4661 + 1.4662 +void AddFailureHelper(bool* aborted) { 1.4663 + *aborted = true; 1.4664 + ADD_FAILURE() << "Intentional failure."; 1.4665 + *aborted = false; 1.4666 +} 1.4667 + 1.4668 +// Tests ADD_FAILURE. 1.4669 +TEST(MacroTest, ADD_FAILURE) { 1.4670 + bool aborted = true; 1.4671 + EXPECT_NONFATAL_FAILURE(AddFailureHelper(&aborted), 1.4672 + "Intentional failure."); 1.4673 + EXPECT_FALSE(aborted); 1.4674 +} 1.4675 + 1.4676 +// Tests ADD_FAILURE_AT. 1.4677 +TEST(MacroTest, ADD_FAILURE_AT) { 1.4678 + // Verifies that ADD_FAILURE_AT does generate a nonfatal failure and 1.4679 + // the failure message contains the user-streamed part. 1.4680 + EXPECT_NONFATAL_FAILURE(ADD_FAILURE_AT("foo.cc", 42) << "Wrong!", "Wrong!"); 1.4681 + 1.4682 + // Verifies that the user-streamed part is optional. 1.4683 + EXPECT_NONFATAL_FAILURE(ADD_FAILURE_AT("foo.cc", 42), "Failed"); 1.4684 + 1.4685 + // Unfortunately, we cannot verify that the failure message contains 1.4686 + // the right file path and line number the same way, as 1.4687 + // EXPECT_NONFATAL_FAILURE() doesn't get to see the file path and 1.4688 + // line number. Instead, we do that in gtest_output_test_.cc. 1.4689 +} 1.4690 + 1.4691 +// Tests FAIL. 1.4692 +TEST(MacroTest, FAIL) { 1.4693 + EXPECT_FATAL_FAILURE(FAIL(), 1.4694 + "Failed"); 1.4695 + EXPECT_FATAL_FAILURE(FAIL() << "Intentional failure.", 1.4696 + "Intentional failure."); 1.4697 +} 1.4698 + 1.4699 +// Tests SUCCEED 1.4700 +TEST(MacroTest, SUCCEED) { 1.4701 + SUCCEED(); 1.4702 + SUCCEED() << "Explicit success."; 1.4703 +} 1.4704 + 1.4705 +// Tests for EXPECT_EQ() and ASSERT_EQ(). 1.4706 +// 1.4707 +// These tests fail *intentionally*, s.t. the failure messages can be 1.4708 +// generated and tested. 1.4709 +// 1.4710 +// We have different tests for different argument types. 1.4711 + 1.4712 +// Tests using bool values in {EXPECT|ASSERT}_EQ. 1.4713 +TEST(EqAssertionTest, Bool) { 1.4714 + EXPECT_EQ(true, true); 1.4715 + EXPECT_FATAL_FAILURE({ 1.4716 + bool false_value = false; 1.4717 + ASSERT_EQ(false_value, true); 1.4718 + }, "Value of: true"); 1.4719 +} 1.4720 + 1.4721 +// Tests using int values in {EXPECT|ASSERT}_EQ. 1.4722 +TEST(EqAssertionTest, Int) { 1.4723 + ASSERT_EQ(32, 32); 1.4724 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(32, 33), 1.4725 + "33"); 1.4726 +} 1.4727 + 1.4728 +// Tests using time_t values in {EXPECT|ASSERT}_EQ. 1.4729 +TEST(EqAssertionTest, Time_T) { 1.4730 + EXPECT_EQ(static_cast<time_t>(0), 1.4731 + static_cast<time_t>(0)); 1.4732 + EXPECT_FATAL_FAILURE(ASSERT_EQ(static_cast<time_t>(0), 1.4733 + static_cast<time_t>(1234)), 1.4734 + "1234"); 1.4735 +} 1.4736 + 1.4737 +// Tests using char values in {EXPECT|ASSERT}_EQ. 1.4738 +TEST(EqAssertionTest, Char) { 1.4739 + ASSERT_EQ('z', 'z'); 1.4740 + const char ch = 'b'; 1.4741 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ('\0', ch), 1.4742 + "ch"); 1.4743 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ('a', ch), 1.4744 + "ch"); 1.4745 +} 1.4746 + 1.4747 +// Tests using wchar_t values in {EXPECT|ASSERT}_EQ. 1.4748 +TEST(EqAssertionTest, WideChar) { 1.4749 + EXPECT_EQ(L'b', L'b'); 1.4750 + 1.4751 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(L'\0', L'x'), 1.4752 + "Value of: L'x'\n" 1.4753 + " Actual: L'x' (120, 0x78)\n" 1.4754 + "Expected: L'\0'\n" 1.4755 + "Which is: L'\0' (0, 0x0)"); 1.4756 + 1.4757 + static wchar_t wchar; 1.4758 + wchar = L'b'; 1.4759 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(L'a', wchar), 1.4760 + "wchar"); 1.4761 + wchar = 0x8119; 1.4762 + EXPECT_FATAL_FAILURE(ASSERT_EQ(static_cast<wchar_t>(0x8120), wchar), 1.4763 + "Value of: wchar"); 1.4764 +} 1.4765 + 1.4766 +// Tests using ::std::string values in {EXPECT|ASSERT}_EQ. 1.4767 +TEST(EqAssertionTest, StdString) { 1.4768 + // Compares a const char* to an std::string that has identical 1.4769 + // content. 1.4770 + ASSERT_EQ("Test", ::std::string("Test")); 1.4771 + 1.4772 + // Compares two identical std::strings. 1.4773 + static const ::std::string str1("A * in the middle"); 1.4774 + static const ::std::string str2(str1); 1.4775 + EXPECT_EQ(str1, str2); 1.4776 + 1.4777 + // Compares a const char* to an std::string that has different 1.4778 + // content 1.4779 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ("Test", ::std::string("test")), 1.4780 + "::std::string(\"test\")"); 1.4781 + 1.4782 + // Compares an std::string to a char* that has different content. 1.4783 + char* const p1 = const_cast<char*>("foo"); 1.4784 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(::std::string("bar"), p1), 1.4785 + "p1"); 1.4786 + 1.4787 + // Compares two std::strings that have different contents, one of 1.4788 + // which having a NUL character in the middle. This should fail. 1.4789 + static ::std::string str3(str1); 1.4790 + str3.at(2) = '\0'; 1.4791 + EXPECT_FATAL_FAILURE(ASSERT_EQ(str1, str3), 1.4792 + "Value of: str3\n" 1.4793 + " Actual: \"A \\0 in the middle\""); 1.4794 +} 1.4795 + 1.4796 +#if GTEST_HAS_STD_WSTRING 1.4797 + 1.4798 +// Tests using ::std::wstring values in {EXPECT|ASSERT}_EQ. 1.4799 +TEST(EqAssertionTest, StdWideString) { 1.4800 + // Compares two identical std::wstrings. 1.4801 + const ::std::wstring wstr1(L"A * in the middle"); 1.4802 + const ::std::wstring wstr2(wstr1); 1.4803 + ASSERT_EQ(wstr1, wstr2); 1.4804 + 1.4805 + // Compares an std::wstring to a const wchar_t* that has identical 1.4806 + // content. 1.4807 + const wchar_t kTestX8119[] = { 'T', 'e', 's', 't', 0x8119, '\0' }; 1.4808 + EXPECT_EQ(::std::wstring(kTestX8119), kTestX8119); 1.4809 + 1.4810 + // Compares an std::wstring to a const wchar_t* that has different 1.4811 + // content. 1.4812 + const wchar_t kTestX8120[] = { 'T', 'e', 's', 't', 0x8120, '\0' }; 1.4813 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.4814 + EXPECT_EQ(::std::wstring(kTestX8119), kTestX8120); 1.4815 + }, "kTestX8120"); 1.4816 + 1.4817 + // Compares two std::wstrings that have different contents, one of 1.4818 + // which having a NUL character in the middle. 1.4819 + ::std::wstring wstr3(wstr1); 1.4820 + wstr3.at(2) = L'\0'; 1.4821 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(wstr1, wstr3), 1.4822 + "wstr3"); 1.4823 + 1.4824 + // Compares a wchar_t* to an std::wstring that has different 1.4825 + // content. 1.4826 + EXPECT_FATAL_FAILURE({ // NOLINT 1.4827 + ASSERT_EQ(const_cast<wchar_t*>(L"foo"), ::std::wstring(L"bar")); 1.4828 + }, ""); 1.4829 +} 1.4830 + 1.4831 +#endif // GTEST_HAS_STD_WSTRING 1.4832 + 1.4833 +#if GTEST_HAS_GLOBAL_STRING 1.4834 +// Tests using ::string values in {EXPECT|ASSERT}_EQ. 1.4835 +TEST(EqAssertionTest, GlobalString) { 1.4836 + // Compares a const char* to a ::string that has identical content. 1.4837 + EXPECT_EQ("Test", ::string("Test")); 1.4838 + 1.4839 + // Compares two identical ::strings. 1.4840 + const ::string str1("A * in the middle"); 1.4841 + const ::string str2(str1); 1.4842 + ASSERT_EQ(str1, str2); 1.4843 + 1.4844 + // Compares a ::string to a const char* that has different content. 1.4845 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(::string("Test"), "test"), 1.4846 + "test"); 1.4847 + 1.4848 + // Compares two ::strings that have different contents, one of which 1.4849 + // having a NUL character in the middle. 1.4850 + ::string str3(str1); 1.4851 + str3.at(2) = '\0'; 1.4852 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(str1, str3), 1.4853 + "str3"); 1.4854 + 1.4855 + // Compares a ::string to a char* that has different content. 1.4856 + EXPECT_FATAL_FAILURE({ // NOLINT 1.4857 + ASSERT_EQ(::string("bar"), const_cast<char*>("foo")); 1.4858 + }, ""); 1.4859 +} 1.4860 + 1.4861 +#endif // GTEST_HAS_GLOBAL_STRING 1.4862 + 1.4863 +#if GTEST_HAS_GLOBAL_WSTRING 1.4864 + 1.4865 +// Tests using ::wstring values in {EXPECT|ASSERT}_EQ. 1.4866 +TEST(EqAssertionTest, GlobalWideString) { 1.4867 + // Compares two identical ::wstrings. 1.4868 + static const ::wstring wstr1(L"A * in the middle"); 1.4869 + static const ::wstring wstr2(wstr1); 1.4870 + EXPECT_EQ(wstr1, wstr2); 1.4871 + 1.4872 + // Compares a const wchar_t* to a ::wstring that has identical content. 1.4873 + const wchar_t kTestX8119[] = { 'T', 'e', 's', 't', 0x8119, '\0' }; 1.4874 + ASSERT_EQ(kTestX8119, ::wstring(kTestX8119)); 1.4875 + 1.4876 + // Compares a const wchar_t* to a ::wstring that has different 1.4877 + // content. 1.4878 + const wchar_t kTestX8120[] = { 'T', 'e', 's', 't', 0x8120, '\0' }; 1.4879 + EXPECT_NONFATAL_FAILURE({ // NOLINT 1.4880 + EXPECT_EQ(kTestX8120, ::wstring(kTestX8119)); 1.4881 + }, "Test\\x8119"); 1.4882 + 1.4883 + // Compares a wchar_t* to a ::wstring that has different content. 1.4884 + wchar_t* const p1 = const_cast<wchar_t*>(L"foo"); 1.4885 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p1, ::wstring(L"bar")), 1.4886 + "bar"); 1.4887 + 1.4888 + // Compares two ::wstrings that have different contents, one of which 1.4889 + // having a NUL character in the middle. 1.4890 + static ::wstring wstr3; 1.4891 + wstr3 = wstr1; 1.4892 + wstr3.at(2) = L'\0'; 1.4893 + EXPECT_FATAL_FAILURE(ASSERT_EQ(wstr1, wstr3), 1.4894 + "wstr3"); 1.4895 +} 1.4896 + 1.4897 +#endif // GTEST_HAS_GLOBAL_WSTRING 1.4898 + 1.4899 +// Tests using char pointers in {EXPECT|ASSERT}_EQ. 1.4900 +TEST(EqAssertionTest, CharPointer) { 1.4901 + char* const p0 = NULL; 1.4902 + // Only way to get the Nokia compiler to compile the cast 1.4903 + // is to have a separate void* variable first. Putting 1.4904 + // the two casts on the same line doesn't work, neither does 1.4905 + // a direct C-style to char*. 1.4906 + void* pv1 = (void*)0x1234; // NOLINT 1.4907 + void* pv2 = (void*)0xABC0; // NOLINT 1.4908 + char* const p1 = reinterpret_cast<char*>(pv1); 1.4909 + char* const p2 = reinterpret_cast<char*>(pv2); 1.4910 + ASSERT_EQ(p1, p1); 1.4911 + 1.4912 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p0, p2), 1.4913 + "Value of: p2"); 1.4914 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p1, p2), 1.4915 + "p2"); 1.4916 + EXPECT_FATAL_FAILURE(ASSERT_EQ(reinterpret_cast<char*>(0x1234), 1.4917 + reinterpret_cast<char*>(0xABC0)), 1.4918 + "ABC0"); 1.4919 +} 1.4920 + 1.4921 +// Tests using wchar_t pointers in {EXPECT|ASSERT}_EQ. 1.4922 +TEST(EqAssertionTest, WideCharPointer) { 1.4923 + wchar_t* const p0 = NULL; 1.4924 + // Only way to get the Nokia compiler to compile the cast 1.4925 + // is to have a separate void* variable first. Putting 1.4926 + // the two casts on the same line doesn't work, neither does 1.4927 + // a direct C-style to char*. 1.4928 + void* pv1 = (void*)0x1234; // NOLINT 1.4929 + void* pv2 = (void*)0xABC0; // NOLINT 1.4930 + wchar_t* const p1 = reinterpret_cast<wchar_t*>(pv1); 1.4931 + wchar_t* const p2 = reinterpret_cast<wchar_t*>(pv2); 1.4932 + EXPECT_EQ(p0, p0); 1.4933 + 1.4934 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p0, p2), 1.4935 + "Value of: p2"); 1.4936 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p1, p2), 1.4937 + "p2"); 1.4938 + void* pv3 = (void*)0x1234; // NOLINT 1.4939 + void* pv4 = (void*)0xABC0; // NOLINT 1.4940 + const wchar_t* p3 = reinterpret_cast<const wchar_t*>(pv3); 1.4941 + const wchar_t* p4 = reinterpret_cast<const wchar_t*>(pv4); 1.4942 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p3, p4), 1.4943 + "p4"); 1.4944 +} 1.4945 + 1.4946 +// Tests using other types of pointers in {EXPECT|ASSERT}_EQ. 1.4947 +TEST(EqAssertionTest, OtherPointer) { 1.4948 + ASSERT_EQ(static_cast<const int*>(NULL), 1.4949 + static_cast<const int*>(NULL)); 1.4950 + EXPECT_FATAL_FAILURE(ASSERT_EQ(static_cast<const int*>(NULL), 1.4951 + reinterpret_cast<const int*>(0x1234)), 1.4952 + "0x1234"); 1.4953 +} 1.4954 + 1.4955 +// A class that supports binary comparison operators but not streaming. 1.4956 +class UnprintableChar { 1.4957 + public: 1.4958 + explicit UnprintableChar(char ch) : char_(ch) {} 1.4959 + 1.4960 + bool operator==(const UnprintableChar& rhs) const { 1.4961 + return char_ == rhs.char_; 1.4962 + } 1.4963 + bool operator!=(const UnprintableChar& rhs) const { 1.4964 + return char_ != rhs.char_; 1.4965 + } 1.4966 + bool operator<(const UnprintableChar& rhs) const { 1.4967 + return char_ < rhs.char_; 1.4968 + } 1.4969 + bool operator<=(const UnprintableChar& rhs) const { 1.4970 + return char_ <= rhs.char_; 1.4971 + } 1.4972 + bool operator>(const UnprintableChar& rhs) const { 1.4973 + return char_ > rhs.char_; 1.4974 + } 1.4975 + bool operator>=(const UnprintableChar& rhs) const { 1.4976 + return char_ >= rhs.char_; 1.4977 + } 1.4978 + 1.4979 + private: 1.4980 + char char_; 1.4981 +}; 1.4982 + 1.4983 +// Tests that ASSERT_EQ() and friends don't require the arguments to 1.4984 +// be printable. 1.4985 +TEST(ComparisonAssertionTest, AcceptsUnprintableArgs) { 1.4986 + const UnprintableChar x('x'), y('y'); 1.4987 + ASSERT_EQ(x, x); 1.4988 + EXPECT_NE(x, y); 1.4989 + ASSERT_LT(x, y); 1.4990 + EXPECT_LE(x, y); 1.4991 + ASSERT_GT(y, x); 1.4992 + EXPECT_GE(x, x); 1.4993 + 1.4994 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(x, y), "1-byte object <78>"); 1.4995 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(x, y), "1-byte object <79>"); 1.4996 + EXPECT_NONFATAL_FAILURE(EXPECT_LT(y, y), "1-byte object <79>"); 1.4997 + EXPECT_NONFATAL_FAILURE(EXPECT_GT(x, y), "1-byte object <78>"); 1.4998 + EXPECT_NONFATAL_FAILURE(EXPECT_GT(x, y), "1-byte object <79>"); 1.4999 + 1.5000 + // Code tested by EXPECT_FATAL_FAILURE cannot reference local 1.5001 + // variables, so we have to write UnprintableChar('x') instead of x. 1.5002 +#ifndef __BORLANDC__ 1.5003 + // ICE's in C++Builder. 1.5004 + EXPECT_FATAL_FAILURE(ASSERT_NE(UnprintableChar('x'), UnprintableChar('x')), 1.5005 + "1-byte object <78>"); 1.5006 + EXPECT_FATAL_FAILURE(ASSERT_LE(UnprintableChar('y'), UnprintableChar('x')), 1.5007 + "1-byte object <78>"); 1.5008 +#endif 1.5009 + EXPECT_FATAL_FAILURE(ASSERT_LE(UnprintableChar('y'), UnprintableChar('x')), 1.5010 + "1-byte object <79>"); 1.5011 + EXPECT_FATAL_FAILURE(ASSERT_GE(UnprintableChar('x'), UnprintableChar('y')), 1.5012 + "1-byte object <78>"); 1.5013 + EXPECT_FATAL_FAILURE(ASSERT_GE(UnprintableChar('x'), UnprintableChar('y')), 1.5014 + "1-byte object <79>"); 1.5015 +} 1.5016 + 1.5017 +// Tests the FRIEND_TEST macro. 1.5018 + 1.5019 +// This class has a private member we want to test. We will test it 1.5020 +// both in a TEST and in a TEST_F. 1.5021 +class Foo { 1.5022 + public: 1.5023 + Foo() {} 1.5024 + 1.5025 + private: 1.5026 + int Bar() const { return 1; } 1.5027 + 1.5028 + // Declares the friend tests that can access the private member 1.5029 + // Bar(). 1.5030 + FRIEND_TEST(FRIEND_TEST_Test, TEST); 1.5031 + FRIEND_TEST(FRIEND_TEST_Test2, TEST_F); 1.5032 +}; 1.5033 + 1.5034 +// Tests that the FRIEND_TEST declaration allows a TEST to access a 1.5035 +// class's private members. This should compile. 1.5036 +TEST(FRIEND_TEST_Test, TEST) { 1.5037 + ASSERT_EQ(1, Foo().Bar()); 1.5038 +} 1.5039 + 1.5040 +// The fixture needed to test using FRIEND_TEST with TEST_F. 1.5041 +class FRIEND_TEST_Test2 : public Test { 1.5042 + protected: 1.5043 + Foo foo; 1.5044 +}; 1.5045 + 1.5046 +// Tests that the FRIEND_TEST declaration allows a TEST_F to access a 1.5047 +// class's private members. This should compile. 1.5048 +TEST_F(FRIEND_TEST_Test2, TEST_F) { 1.5049 + ASSERT_EQ(1, foo.Bar()); 1.5050 +} 1.5051 + 1.5052 +// Tests the life cycle of Test objects. 1.5053 + 1.5054 +// The test fixture for testing the life cycle of Test objects. 1.5055 +// 1.5056 +// This class counts the number of live test objects that uses this 1.5057 +// fixture. 1.5058 +class TestLifeCycleTest : public Test { 1.5059 + protected: 1.5060 + // Constructor. Increments the number of test objects that uses 1.5061 + // this fixture. 1.5062 + TestLifeCycleTest() { count_++; } 1.5063 + 1.5064 + // Destructor. Decrements the number of test objects that uses this 1.5065 + // fixture. 1.5066 + ~TestLifeCycleTest() { count_--; } 1.5067 + 1.5068 + // Returns the number of live test objects that uses this fixture. 1.5069 + int count() const { return count_; } 1.5070 + 1.5071 + private: 1.5072 + static int count_; 1.5073 +}; 1.5074 + 1.5075 +int TestLifeCycleTest::count_ = 0; 1.5076 + 1.5077 +// Tests the life cycle of test objects. 1.5078 +TEST_F(TestLifeCycleTest, Test1) { 1.5079 + // There should be only one test object in this test case that's 1.5080 + // currently alive. 1.5081 + ASSERT_EQ(1, count()); 1.5082 +} 1.5083 + 1.5084 +// Tests the life cycle of test objects. 1.5085 +TEST_F(TestLifeCycleTest, Test2) { 1.5086 + // After Test1 is done and Test2 is started, there should still be 1.5087 + // only one live test object, as the object for Test1 should've been 1.5088 + // deleted. 1.5089 + ASSERT_EQ(1, count()); 1.5090 +} 1.5091 + 1.5092 +} // namespace 1.5093 + 1.5094 +// Tests that the copy constructor works when it is NOT optimized away by 1.5095 +// the compiler. 1.5096 +TEST(AssertionResultTest, CopyConstructorWorksWhenNotOptimied) { 1.5097 + // Checks that the copy constructor doesn't try to dereference NULL pointers 1.5098 + // in the source object. 1.5099 + AssertionResult r1 = AssertionSuccess(); 1.5100 + AssertionResult r2 = r1; 1.5101 + // The following line is added to prevent the compiler from optimizing 1.5102 + // away the constructor call. 1.5103 + r1 << "abc"; 1.5104 + 1.5105 + AssertionResult r3 = r1; 1.5106 + EXPECT_EQ(static_cast<bool>(r3), static_cast<bool>(r1)); 1.5107 + EXPECT_STREQ("abc", r1.message()); 1.5108 +} 1.5109 + 1.5110 +// Tests that AssertionSuccess and AssertionFailure construct 1.5111 +// AssertionResult objects as expected. 1.5112 +TEST(AssertionResultTest, ConstructionWorks) { 1.5113 + AssertionResult r1 = AssertionSuccess(); 1.5114 + EXPECT_TRUE(r1); 1.5115 + EXPECT_STREQ("", r1.message()); 1.5116 + 1.5117 + AssertionResult r2 = AssertionSuccess() << "abc"; 1.5118 + EXPECT_TRUE(r2); 1.5119 + EXPECT_STREQ("abc", r2.message()); 1.5120 + 1.5121 + AssertionResult r3 = AssertionFailure(); 1.5122 + EXPECT_FALSE(r3); 1.5123 + EXPECT_STREQ("", r3.message()); 1.5124 + 1.5125 + AssertionResult r4 = AssertionFailure() << "def"; 1.5126 + EXPECT_FALSE(r4); 1.5127 + EXPECT_STREQ("def", r4.message()); 1.5128 + 1.5129 + AssertionResult r5 = AssertionFailure(Message() << "ghi"); 1.5130 + EXPECT_FALSE(r5); 1.5131 + EXPECT_STREQ("ghi", r5.message()); 1.5132 +} 1.5133 + 1.5134 +// Tests that the negation flips the predicate result but keeps the message. 1.5135 +TEST(AssertionResultTest, NegationWorks) { 1.5136 + AssertionResult r1 = AssertionSuccess() << "abc"; 1.5137 + EXPECT_FALSE(!r1); 1.5138 + EXPECT_STREQ("abc", (!r1).message()); 1.5139 + 1.5140 + AssertionResult r2 = AssertionFailure() << "def"; 1.5141 + EXPECT_TRUE(!r2); 1.5142 + EXPECT_STREQ("def", (!r2).message()); 1.5143 +} 1.5144 + 1.5145 +TEST(AssertionResultTest, StreamingWorks) { 1.5146 + AssertionResult r = AssertionSuccess(); 1.5147 + r << "abc" << 'd' << 0 << true; 1.5148 + EXPECT_STREQ("abcd0true", r.message()); 1.5149 +} 1.5150 + 1.5151 +TEST(AssertionResultTest, CanStreamOstreamManipulators) { 1.5152 + AssertionResult r = AssertionSuccess(); 1.5153 + r << "Data" << std::endl << std::flush << std::ends << "Will be visible"; 1.5154 + EXPECT_STREQ("Data\n\\0Will be visible", r.message()); 1.5155 +} 1.5156 + 1.5157 +// Tests streaming a user type whose definition and operator << are 1.5158 +// both in the global namespace. 1.5159 +class Base { 1.5160 + public: 1.5161 + explicit Base(int an_x) : x_(an_x) {} 1.5162 + int x() const { return x_; } 1.5163 + private: 1.5164 + int x_; 1.5165 +}; 1.5166 +std::ostream& operator<<(std::ostream& os, 1.5167 + const Base& val) { 1.5168 + return os << val.x(); 1.5169 +} 1.5170 +std::ostream& operator<<(std::ostream& os, 1.5171 + const Base* pointer) { 1.5172 + return os << "(" << pointer->x() << ")"; 1.5173 +} 1.5174 + 1.5175 +TEST(MessageTest, CanStreamUserTypeInGlobalNameSpace) { 1.5176 + Message msg; 1.5177 + Base a(1); 1.5178 + 1.5179 + msg << a << &a; // Uses ::operator<<. 1.5180 + EXPECT_STREQ("1(1)", msg.GetString().c_str()); 1.5181 +} 1.5182 + 1.5183 +// Tests streaming a user type whose definition and operator<< are 1.5184 +// both in an unnamed namespace. 1.5185 +namespace { 1.5186 +class MyTypeInUnnamedNameSpace : public Base { 1.5187 + public: 1.5188 + explicit MyTypeInUnnamedNameSpace(int an_x): Base(an_x) {} 1.5189 +}; 1.5190 +std::ostream& operator<<(std::ostream& os, 1.5191 + const MyTypeInUnnamedNameSpace& val) { 1.5192 + return os << val.x(); 1.5193 +} 1.5194 +std::ostream& operator<<(std::ostream& os, 1.5195 + const MyTypeInUnnamedNameSpace* pointer) { 1.5196 + return os << "(" << pointer->x() << ")"; 1.5197 +} 1.5198 +} // namespace 1.5199 + 1.5200 +TEST(MessageTest, CanStreamUserTypeInUnnamedNameSpace) { 1.5201 + Message msg; 1.5202 + MyTypeInUnnamedNameSpace a(1); 1.5203 + 1.5204 + msg << a << &a; // Uses <unnamed_namespace>::operator<<. 1.5205 + EXPECT_STREQ("1(1)", msg.GetString().c_str()); 1.5206 +} 1.5207 + 1.5208 +// Tests streaming a user type whose definition and operator<< are 1.5209 +// both in a user namespace. 1.5210 +namespace namespace1 { 1.5211 +class MyTypeInNameSpace1 : public Base { 1.5212 + public: 1.5213 + explicit MyTypeInNameSpace1(int an_x): Base(an_x) {} 1.5214 +}; 1.5215 +std::ostream& operator<<(std::ostream& os, 1.5216 + const MyTypeInNameSpace1& val) { 1.5217 + return os << val.x(); 1.5218 +} 1.5219 +std::ostream& operator<<(std::ostream& os, 1.5220 + const MyTypeInNameSpace1* pointer) { 1.5221 + return os << "(" << pointer->x() << ")"; 1.5222 +} 1.5223 +} // namespace namespace1 1.5224 + 1.5225 +TEST(MessageTest, CanStreamUserTypeInUserNameSpace) { 1.5226 + Message msg; 1.5227 + namespace1::MyTypeInNameSpace1 a(1); 1.5228 + 1.5229 + msg << a << &a; // Uses namespace1::operator<<. 1.5230 + EXPECT_STREQ("1(1)", msg.GetString().c_str()); 1.5231 +} 1.5232 + 1.5233 +// Tests streaming a user type whose definition is in a user namespace 1.5234 +// but whose operator<< is in the global namespace. 1.5235 +namespace namespace2 { 1.5236 +class MyTypeInNameSpace2 : public ::Base { 1.5237 + public: 1.5238 + explicit MyTypeInNameSpace2(int an_x): Base(an_x) {} 1.5239 +}; 1.5240 +} // namespace namespace2 1.5241 +std::ostream& operator<<(std::ostream& os, 1.5242 + const namespace2::MyTypeInNameSpace2& val) { 1.5243 + return os << val.x(); 1.5244 +} 1.5245 +std::ostream& operator<<(std::ostream& os, 1.5246 + const namespace2::MyTypeInNameSpace2* pointer) { 1.5247 + return os << "(" << pointer->x() << ")"; 1.5248 +} 1.5249 + 1.5250 +TEST(MessageTest, CanStreamUserTypeInUserNameSpaceWithStreamOperatorInGlobal) { 1.5251 + Message msg; 1.5252 + namespace2::MyTypeInNameSpace2 a(1); 1.5253 + 1.5254 + msg << a << &a; // Uses ::operator<<. 1.5255 + EXPECT_STREQ("1(1)", msg.GetString().c_str()); 1.5256 +} 1.5257 + 1.5258 +// Tests streaming NULL pointers to testing::Message. 1.5259 +TEST(MessageTest, NullPointers) { 1.5260 + Message msg; 1.5261 + char* const p1 = NULL; 1.5262 + unsigned char* const p2 = NULL; 1.5263 + int* p3 = NULL; 1.5264 + double* p4 = NULL; 1.5265 + bool* p5 = NULL; 1.5266 + Message* p6 = NULL; 1.5267 + 1.5268 + msg << p1 << p2 << p3 << p4 << p5 << p6; 1.5269 + ASSERT_STREQ("(null)(null)(null)(null)(null)(null)", 1.5270 + msg.GetString().c_str()); 1.5271 +} 1.5272 + 1.5273 +// Tests streaming wide strings to testing::Message. 1.5274 +TEST(MessageTest, WideStrings) { 1.5275 + // Streams a NULL of type const wchar_t*. 1.5276 + const wchar_t* const_wstr = NULL; 1.5277 + EXPECT_STREQ("(null)", 1.5278 + (Message() << const_wstr).GetString().c_str()); 1.5279 + 1.5280 + // Streams a NULL of type wchar_t*. 1.5281 + wchar_t* wstr = NULL; 1.5282 + EXPECT_STREQ("(null)", 1.5283 + (Message() << wstr).GetString().c_str()); 1.5284 + 1.5285 + // Streams a non-NULL of type const wchar_t*. 1.5286 + const_wstr = L"abc\x8119"; 1.5287 + EXPECT_STREQ("abc\xe8\x84\x99", 1.5288 + (Message() << const_wstr).GetString().c_str()); 1.5289 + 1.5290 + // Streams a non-NULL of type wchar_t*. 1.5291 + wstr = const_cast<wchar_t*>(const_wstr); 1.5292 + EXPECT_STREQ("abc\xe8\x84\x99", 1.5293 + (Message() << wstr).GetString().c_str()); 1.5294 +} 1.5295 + 1.5296 + 1.5297 +// This line tests that we can define tests in the testing namespace. 1.5298 +namespace testing { 1.5299 + 1.5300 +// Tests the TestInfo class. 1.5301 + 1.5302 +class TestInfoTest : public Test { 1.5303 + protected: 1.5304 + static const TestInfo* GetTestInfo(const char* test_name) { 1.5305 + const TestCase* const test_case = GetUnitTestImpl()-> 1.5306 + GetTestCase("TestInfoTest", "", NULL, NULL); 1.5307 + 1.5308 + for (int i = 0; i < test_case->total_test_count(); ++i) { 1.5309 + const TestInfo* const test_info = test_case->GetTestInfo(i); 1.5310 + if (strcmp(test_name, test_info->name()) == 0) 1.5311 + return test_info; 1.5312 + } 1.5313 + return NULL; 1.5314 + } 1.5315 + 1.5316 + static const TestResult* GetTestResult( 1.5317 + const TestInfo* test_info) { 1.5318 + return test_info->result(); 1.5319 + } 1.5320 +}; 1.5321 + 1.5322 +// Tests TestInfo::test_case_name() and TestInfo::name(). 1.5323 +TEST_F(TestInfoTest, Names) { 1.5324 + const TestInfo* const test_info = GetTestInfo("Names"); 1.5325 + 1.5326 + ASSERT_STREQ("TestInfoTest", test_info->test_case_name()); 1.5327 + ASSERT_STREQ("Names", test_info->name()); 1.5328 +} 1.5329 + 1.5330 +// Tests TestInfo::result(). 1.5331 +TEST_F(TestInfoTest, result) { 1.5332 + const TestInfo* const test_info = GetTestInfo("result"); 1.5333 + 1.5334 + // Initially, there is no TestPartResult for this test. 1.5335 + ASSERT_EQ(0, GetTestResult(test_info)->total_part_count()); 1.5336 + 1.5337 + // After the previous assertion, there is still none. 1.5338 + ASSERT_EQ(0, GetTestResult(test_info)->total_part_count()); 1.5339 +} 1.5340 + 1.5341 +// Tests setting up and tearing down a test case. 1.5342 + 1.5343 +class SetUpTestCaseTest : public Test { 1.5344 + protected: 1.5345 + // This will be called once before the first test in this test case 1.5346 + // is run. 1.5347 + static void SetUpTestCase() { 1.5348 + printf("Setting up the test case . . .\n"); 1.5349 + 1.5350 + // Initializes some shared resource. In this simple example, we 1.5351 + // just create a C string. More complex stuff can be done if 1.5352 + // desired. 1.5353 + shared_resource_ = "123"; 1.5354 + 1.5355 + // Increments the number of test cases that have been set up. 1.5356 + counter_++; 1.5357 + 1.5358 + // SetUpTestCase() should be called only once. 1.5359 + EXPECT_EQ(1, counter_); 1.5360 + } 1.5361 + 1.5362 + // This will be called once after the last test in this test case is 1.5363 + // run. 1.5364 + static void TearDownTestCase() { 1.5365 + printf("Tearing down the test case . . .\n"); 1.5366 + 1.5367 + // Decrements the number of test cases that have been set up. 1.5368 + counter_--; 1.5369 + 1.5370 + // TearDownTestCase() should be called only once. 1.5371 + EXPECT_EQ(0, counter_); 1.5372 + 1.5373 + // Cleans up the shared resource. 1.5374 + shared_resource_ = NULL; 1.5375 + } 1.5376 + 1.5377 + // This will be called before each test in this test case. 1.5378 + virtual void SetUp() { 1.5379 + // SetUpTestCase() should be called only once, so counter_ should 1.5380 + // always be 1. 1.5381 + EXPECT_EQ(1, counter_); 1.5382 + } 1.5383 + 1.5384 + // Number of test cases that have been set up. 1.5385 + static int counter_; 1.5386 + 1.5387 + // Some resource to be shared by all tests in this test case. 1.5388 + static const char* shared_resource_; 1.5389 +}; 1.5390 + 1.5391 +int SetUpTestCaseTest::counter_ = 0; 1.5392 +const char* SetUpTestCaseTest::shared_resource_ = NULL; 1.5393 + 1.5394 +// A test that uses the shared resource. 1.5395 +TEST_F(SetUpTestCaseTest, Test1) { 1.5396 + EXPECT_STRNE(NULL, shared_resource_); 1.5397 +} 1.5398 + 1.5399 +// Another test that uses the shared resource. 1.5400 +TEST_F(SetUpTestCaseTest, Test2) { 1.5401 + EXPECT_STREQ("123", shared_resource_); 1.5402 +} 1.5403 + 1.5404 +// The InitGoogleTestTest test case tests testing::InitGoogleTest(). 1.5405 + 1.5406 +// The Flags struct stores a copy of all Google Test flags. 1.5407 +struct Flags { 1.5408 + // Constructs a Flags struct where each flag has its default value. 1.5409 + Flags() : also_run_disabled_tests(false), 1.5410 + break_on_failure(false), 1.5411 + catch_exceptions(false), 1.5412 + death_test_use_fork(false), 1.5413 + filter(""), 1.5414 + list_tests(false), 1.5415 + output(""), 1.5416 + print_time(true), 1.5417 + random_seed(0), 1.5418 + repeat(1), 1.5419 + shuffle(false), 1.5420 + stack_trace_depth(kMaxStackTraceDepth), 1.5421 + stream_result_to(""), 1.5422 + throw_on_failure(false) {} 1.5423 + 1.5424 + // Factory methods. 1.5425 + 1.5426 + // Creates a Flags struct where the gtest_also_run_disabled_tests flag has 1.5427 + // the given value. 1.5428 + static Flags AlsoRunDisabledTests(bool also_run_disabled_tests) { 1.5429 + Flags flags; 1.5430 + flags.also_run_disabled_tests = also_run_disabled_tests; 1.5431 + return flags; 1.5432 + } 1.5433 + 1.5434 + // Creates a Flags struct where the gtest_break_on_failure flag has 1.5435 + // the given value. 1.5436 + static Flags BreakOnFailure(bool break_on_failure) { 1.5437 + Flags flags; 1.5438 + flags.break_on_failure = break_on_failure; 1.5439 + return flags; 1.5440 + } 1.5441 + 1.5442 + // Creates a Flags struct where the gtest_catch_exceptions flag has 1.5443 + // the given value. 1.5444 + static Flags CatchExceptions(bool catch_exceptions) { 1.5445 + Flags flags; 1.5446 + flags.catch_exceptions = catch_exceptions; 1.5447 + return flags; 1.5448 + } 1.5449 + 1.5450 + // Creates a Flags struct where the gtest_death_test_use_fork flag has 1.5451 + // the given value. 1.5452 + static Flags DeathTestUseFork(bool death_test_use_fork) { 1.5453 + Flags flags; 1.5454 + flags.death_test_use_fork = death_test_use_fork; 1.5455 + return flags; 1.5456 + } 1.5457 + 1.5458 + // Creates a Flags struct where the gtest_filter flag has the given 1.5459 + // value. 1.5460 + static Flags Filter(const char* filter) { 1.5461 + Flags flags; 1.5462 + flags.filter = filter; 1.5463 + return flags; 1.5464 + } 1.5465 + 1.5466 + // Creates a Flags struct where the gtest_list_tests flag has the 1.5467 + // given value. 1.5468 + static Flags ListTests(bool list_tests) { 1.5469 + Flags flags; 1.5470 + flags.list_tests = list_tests; 1.5471 + return flags; 1.5472 + } 1.5473 + 1.5474 + // Creates a Flags struct where the gtest_output flag has the given 1.5475 + // value. 1.5476 + static Flags Output(const char* output) { 1.5477 + Flags flags; 1.5478 + flags.output = output; 1.5479 + return flags; 1.5480 + } 1.5481 + 1.5482 + // Creates a Flags struct where the gtest_print_time flag has the given 1.5483 + // value. 1.5484 + static Flags PrintTime(bool print_time) { 1.5485 + Flags flags; 1.5486 + flags.print_time = print_time; 1.5487 + return flags; 1.5488 + } 1.5489 + 1.5490 + // Creates a Flags struct where the gtest_random_seed flag has 1.5491 + // the given value. 1.5492 + static Flags RandomSeed(Int32 random_seed) { 1.5493 + Flags flags; 1.5494 + flags.random_seed = random_seed; 1.5495 + return flags; 1.5496 + } 1.5497 + 1.5498 + // Creates a Flags struct where the gtest_repeat flag has the given 1.5499 + // value. 1.5500 + static Flags Repeat(Int32 repeat) { 1.5501 + Flags flags; 1.5502 + flags.repeat = repeat; 1.5503 + return flags; 1.5504 + } 1.5505 + 1.5506 + // Creates a Flags struct where the gtest_shuffle flag has 1.5507 + // the given value. 1.5508 + static Flags Shuffle(bool shuffle) { 1.5509 + Flags flags; 1.5510 + flags.shuffle = shuffle; 1.5511 + return flags; 1.5512 + } 1.5513 + 1.5514 + // Creates a Flags struct where the GTEST_FLAG(stack_trace_depth) flag has 1.5515 + // the given value. 1.5516 + static Flags StackTraceDepth(Int32 stack_trace_depth) { 1.5517 + Flags flags; 1.5518 + flags.stack_trace_depth = stack_trace_depth; 1.5519 + return flags; 1.5520 + } 1.5521 + 1.5522 + // Creates a Flags struct where the GTEST_FLAG(stream_result_to) flag has 1.5523 + // the given value. 1.5524 + static Flags StreamResultTo(const char* stream_result_to) { 1.5525 + Flags flags; 1.5526 + flags.stream_result_to = stream_result_to; 1.5527 + return flags; 1.5528 + } 1.5529 + 1.5530 + // Creates a Flags struct where the gtest_throw_on_failure flag has 1.5531 + // the given value. 1.5532 + static Flags ThrowOnFailure(bool throw_on_failure) { 1.5533 + Flags flags; 1.5534 + flags.throw_on_failure = throw_on_failure; 1.5535 + return flags; 1.5536 + } 1.5537 + 1.5538 + // These fields store the flag values. 1.5539 + bool also_run_disabled_tests; 1.5540 + bool break_on_failure; 1.5541 + bool catch_exceptions; 1.5542 + bool death_test_use_fork; 1.5543 + const char* filter; 1.5544 + bool list_tests; 1.5545 + const char* output; 1.5546 + bool print_time; 1.5547 + Int32 random_seed; 1.5548 + Int32 repeat; 1.5549 + bool shuffle; 1.5550 + Int32 stack_trace_depth; 1.5551 + const char* stream_result_to; 1.5552 + bool throw_on_failure; 1.5553 +}; 1.5554 + 1.5555 +// Fixture for testing InitGoogleTest(). 1.5556 +class InitGoogleTestTest : public Test { 1.5557 + protected: 1.5558 + // Clears the flags before each test. 1.5559 + virtual void SetUp() { 1.5560 + GTEST_FLAG(also_run_disabled_tests) = false; 1.5561 + GTEST_FLAG(break_on_failure) = false; 1.5562 + GTEST_FLAG(catch_exceptions) = false; 1.5563 + GTEST_FLAG(death_test_use_fork) = false; 1.5564 + GTEST_FLAG(filter) = ""; 1.5565 + GTEST_FLAG(list_tests) = false; 1.5566 + GTEST_FLAG(output) = ""; 1.5567 + GTEST_FLAG(print_time) = true; 1.5568 + GTEST_FLAG(random_seed) = 0; 1.5569 + GTEST_FLAG(repeat) = 1; 1.5570 + GTEST_FLAG(shuffle) = false; 1.5571 + GTEST_FLAG(stack_trace_depth) = kMaxStackTraceDepth; 1.5572 + GTEST_FLAG(stream_result_to) = ""; 1.5573 + GTEST_FLAG(throw_on_failure) = false; 1.5574 + } 1.5575 + 1.5576 + // Asserts that two narrow or wide string arrays are equal. 1.5577 + template <typename CharType> 1.5578 + static void AssertStringArrayEq(size_t size1, CharType** array1, 1.5579 + size_t size2, CharType** array2) { 1.5580 + ASSERT_EQ(size1, size2) << " Array sizes different."; 1.5581 + 1.5582 + for (size_t i = 0; i != size1; i++) { 1.5583 + ASSERT_STREQ(array1[i], array2[i]) << " where i == " << i; 1.5584 + } 1.5585 + } 1.5586 + 1.5587 + // Verifies that the flag values match the expected values. 1.5588 + static void CheckFlags(const Flags& expected) { 1.5589 + EXPECT_EQ(expected.also_run_disabled_tests, 1.5590 + GTEST_FLAG(also_run_disabled_tests)); 1.5591 + EXPECT_EQ(expected.break_on_failure, GTEST_FLAG(break_on_failure)); 1.5592 + EXPECT_EQ(expected.catch_exceptions, GTEST_FLAG(catch_exceptions)); 1.5593 + EXPECT_EQ(expected.death_test_use_fork, GTEST_FLAG(death_test_use_fork)); 1.5594 + EXPECT_STREQ(expected.filter, GTEST_FLAG(filter).c_str()); 1.5595 + EXPECT_EQ(expected.list_tests, GTEST_FLAG(list_tests)); 1.5596 + EXPECT_STREQ(expected.output, GTEST_FLAG(output).c_str()); 1.5597 + EXPECT_EQ(expected.print_time, GTEST_FLAG(print_time)); 1.5598 + EXPECT_EQ(expected.random_seed, GTEST_FLAG(random_seed)); 1.5599 + EXPECT_EQ(expected.repeat, GTEST_FLAG(repeat)); 1.5600 + EXPECT_EQ(expected.shuffle, GTEST_FLAG(shuffle)); 1.5601 + EXPECT_EQ(expected.stack_trace_depth, GTEST_FLAG(stack_trace_depth)); 1.5602 + EXPECT_STREQ(expected.stream_result_to, 1.5603 + GTEST_FLAG(stream_result_to).c_str()); 1.5604 + EXPECT_EQ(expected.throw_on_failure, GTEST_FLAG(throw_on_failure)); 1.5605 + } 1.5606 + 1.5607 + // Parses a command line (specified by argc1 and argv1), then 1.5608 + // verifies that the flag values are expected and that the 1.5609 + // recognized flags are removed from the command line. 1.5610 + template <typename CharType> 1.5611 + static void TestParsingFlags(int argc1, const CharType** argv1, 1.5612 + int argc2, const CharType** argv2, 1.5613 + const Flags& expected, bool should_print_help) { 1.5614 + const bool saved_help_flag = ::testing::internal::g_help_flag; 1.5615 + ::testing::internal::g_help_flag = false; 1.5616 + 1.5617 +#if GTEST_HAS_STREAM_REDIRECTION 1.5618 + CaptureStdout(); 1.5619 +#endif 1.5620 + 1.5621 + // Parses the command line. 1.5622 + internal::ParseGoogleTestFlagsOnly(&argc1, const_cast<CharType**>(argv1)); 1.5623 + 1.5624 +#if GTEST_HAS_STREAM_REDIRECTION 1.5625 + const String captured_stdout = GetCapturedStdout(); 1.5626 +#endif 1.5627 + 1.5628 + // Verifies the flag values. 1.5629 + CheckFlags(expected); 1.5630 + 1.5631 + // Verifies that the recognized flags are removed from the command 1.5632 + // line. 1.5633 + AssertStringArrayEq(argc1 + 1, argv1, argc2 + 1, argv2); 1.5634 + 1.5635 + // ParseGoogleTestFlagsOnly should neither set g_help_flag nor print the 1.5636 + // help message for the flags it recognizes. 1.5637 + EXPECT_EQ(should_print_help, ::testing::internal::g_help_flag); 1.5638 + 1.5639 +#if GTEST_HAS_STREAM_REDIRECTION 1.5640 + const char* const expected_help_fragment = 1.5641 + "This program contains tests written using"; 1.5642 + if (should_print_help) { 1.5643 + EXPECT_PRED_FORMAT2(IsSubstring, expected_help_fragment, captured_stdout); 1.5644 + } else { 1.5645 + EXPECT_PRED_FORMAT2(IsNotSubstring, 1.5646 + expected_help_fragment, captured_stdout); 1.5647 + } 1.5648 +#endif // GTEST_HAS_STREAM_REDIRECTION 1.5649 + 1.5650 + ::testing::internal::g_help_flag = saved_help_flag; 1.5651 + } 1.5652 + 1.5653 + // This macro wraps TestParsingFlags s.t. the user doesn't need 1.5654 + // to specify the array sizes. 1.5655 + 1.5656 +#define GTEST_TEST_PARSING_FLAGS_(argv1, argv2, expected, should_print_help) \ 1.5657 + TestParsingFlags(sizeof(argv1)/sizeof(*argv1) - 1, argv1, \ 1.5658 + sizeof(argv2)/sizeof(*argv2) - 1, argv2, \ 1.5659 + expected, should_print_help) 1.5660 +}; 1.5661 + 1.5662 +// Tests parsing an empty command line. 1.5663 +TEST_F(InitGoogleTestTest, Empty) { 1.5664 + const char* argv[] = { 1.5665 + NULL 1.5666 + }; 1.5667 + 1.5668 + const char* argv2[] = { 1.5669 + NULL 1.5670 + }; 1.5671 + 1.5672 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), false); 1.5673 +} 1.5674 + 1.5675 +// Tests parsing a command line that has no flag. 1.5676 +TEST_F(InitGoogleTestTest, NoFlag) { 1.5677 + const char* argv[] = { 1.5678 + "foo.exe", 1.5679 + NULL 1.5680 + }; 1.5681 + 1.5682 + const char* argv2[] = { 1.5683 + "foo.exe", 1.5684 + NULL 1.5685 + }; 1.5686 + 1.5687 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), false); 1.5688 +} 1.5689 + 1.5690 +// Tests parsing a bad --gtest_filter flag. 1.5691 +TEST_F(InitGoogleTestTest, FilterBad) { 1.5692 + const char* argv[] = { 1.5693 + "foo.exe", 1.5694 + "--gtest_filter", 1.5695 + NULL 1.5696 + }; 1.5697 + 1.5698 + const char* argv2[] = { 1.5699 + "foo.exe", 1.5700 + "--gtest_filter", 1.5701 + NULL 1.5702 + }; 1.5703 + 1.5704 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter(""), true); 1.5705 +} 1.5706 + 1.5707 +// Tests parsing an empty --gtest_filter flag. 1.5708 +TEST_F(InitGoogleTestTest, FilterEmpty) { 1.5709 + const char* argv[] = { 1.5710 + "foo.exe", 1.5711 + "--gtest_filter=", 1.5712 + NULL 1.5713 + }; 1.5714 + 1.5715 + const char* argv2[] = { 1.5716 + "foo.exe", 1.5717 + NULL 1.5718 + }; 1.5719 + 1.5720 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter(""), false); 1.5721 +} 1.5722 + 1.5723 +// Tests parsing a non-empty --gtest_filter flag. 1.5724 +TEST_F(InitGoogleTestTest, FilterNonEmpty) { 1.5725 + const char* argv[] = { 1.5726 + "foo.exe", 1.5727 + "--gtest_filter=abc", 1.5728 + NULL 1.5729 + }; 1.5730 + 1.5731 + const char* argv2[] = { 1.5732 + "foo.exe", 1.5733 + NULL 1.5734 + }; 1.5735 + 1.5736 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter("abc"), false); 1.5737 +} 1.5738 + 1.5739 +// Tests parsing --gtest_break_on_failure. 1.5740 +TEST_F(InitGoogleTestTest, BreakOnFailureWithoutValue) { 1.5741 + const char* argv[] = { 1.5742 + "foo.exe", 1.5743 + "--gtest_break_on_failure", 1.5744 + NULL 1.5745 +}; 1.5746 + 1.5747 + const char* argv2[] = { 1.5748 + "foo.exe", 1.5749 + NULL 1.5750 + }; 1.5751 + 1.5752 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(true), false); 1.5753 +} 1.5754 + 1.5755 +// Tests parsing --gtest_break_on_failure=0. 1.5756 +TEST_F(InitGoogleTestTest, BreakOnFailureFalse_0) { 1.5757 + const char* argv[] = { 1.5758 + "foo.exe", 1.5759 + "--gtest_break_on_failure=0", 1.5760 + NULL 1.5761 + }; 1.5762 + 1.5763 + const char* argv2[] = { 1.5764 + "foo.exe", 1.5765 + NULL 1.5766 + }; 1.5767 + 1.5768 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(false), false); 1.5769 +} 1.5770 + 1.5771 +// Tests parsing --gtest_break_on_failure=f. 1.5772 +TEST_F(InitGoogleTestTest, BreakOnFailureFalse_f) { 1.5773 + const char* argv[] = { 1.5774 + "foo.exe", 1.5775 + "--gtest_break_on_failure=f", 1.5776 + NULL 1.5777 + }; 1.5778 + 1.5779 + const char* argv2[] = { 1.5780 + "foo.exe", 1.5781 + NULL 1.5782 + }; 1.5783 + 1.5784 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(false), false); 1.5785 +} 1.5786 + 1.5787 +// Tests parsing --gtest_break_on_failure=F. 1.5788 +TEST_F(InitGoogleTestTest, BreakOnFailureFalse_F) { 1.5789 + const char* argv[] = { 1.5790 + "foo.exe", 1.5791 + "--gtest_break_on_failure=F", 1.5792 + NULL 1.5793 + }; 1.5794 + 1.5795 + const char* argv2[] = { 1.5796 + "foo.exe", 1.5797 + NULL 1.5798 + }; 1.5799 + 1.5800 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(false), false); 1.5801 +} 1.5802 + 1.5803 +// Tests parsing a --gtest_break_on_failure flag that has a "true" 1.5804 +// definition. 1.5805 +TEST_F(InitGoogleTestTest, BreakOnFailureTrue) { 1.5806 + const char* argv[] = { 1.5807 + "foo.exe", 1.5808 + "--gtest_break_on_failure=1", 1.5809 + NULL 1.5810 + }; 1.5811 + 1.5812 + const char* argv2[] = { 1.5813 + "foo.exe", 1.5814 + NULL 1.5815 + }; 1.5816 + 1.5817 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(true), false); 1.5818 +} 1.5819 + 1.5820 +// Tests parsing --gtest_catch_exceptions. 1.5821 +TEST_F(InitGoogleTestTest, CatchExceptions) { 1.5822 + const char* argv[] = { 1.5823 + "foo.exe", 1.5824 + "--gtest_catch_exceptions", 1.5825 + NULL 1.5826 + }; 1.5827 + 1.5828 + const char* argv2[] = { 1.5829 + "foo.exe", 1.5830 + NULL 1.5831 + }; 1.5832 + 1.5833 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::CatchExceptions(true), false); 1.5834 +} 1.5835 + 1.5836 +// Tests parsing --gtest_death_test_use_fork. 1.5837 +TEST_F(InitGoogleTestTest, DeathTestUseFork) { 1.5838 + const char* argv[] = { 1.5839 + "foo.exe", 1.5840 + "--gtest_death_test_use_fork", 1.5841 + NULL 1.5842 + }; 1.5843 + 1.5844 + const char* argv2[] = { 1.5845 + "foo.exe", 1.5846 + NULL 1.5847 + }; 1.5848 + 1.5849 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::DeathTestUseFork(true), false); 1.5850 +} 1.5851 + 1.5852 +// Tests having the same flag twice with different values. The 1.5853 +// expected behavior is that the one coming last takes precedence. 1.5854 +TEST_F(InitGoogleTestTest, DuplicatedFlags) { 1.5855 + const char* argv[] = { 1.5856 + "foo.exe", 1.5857 + "--gtest_filter=a", 1.5858 + "--gtest_filter=b", 1.5859 + NULL 1.5860 + }; 1.5861 + 1.5862 + const char* argv2[] = { 1.5863 + "foo.exe", 1.5864 + NULL 1.5865 + }; 1.5866 + 1.5867 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter("b"), false); 1.5868 +} 1.5869 + 1.5870 +// Tests having an unrecognized flag on the command line. 1.5871 +TEST_F(InitGoogleTestTest, UnrecognizedFlag) { 1.5872 + const char* argv[] = { 1.5873 + "foo.exe", 1.5874 + "--gtest_break_on_failure", 1.5875 + "bar", // Unrecognized by Google Test. 1.5876 + "--gtest_filter=b", 1.5877 + NULL 1.5878 + }; 1.5879 + 1.5880 + const char* argv2[] = { 1.5881 + "foo.exe", 1.5882 + "bar", 1.5883 + NULL 1.5884 + }; 1.5885 + 1.5886 + Flags flags; 1.5887 + flags.break_on_failure = true; 1.5888 + flags.filter = "b"; 1.5889 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, flags, false); 1.5890 +} 1.5891 + 1.5892 +// Tests having a --gtest_list_tests flag 1.5893 +TEST_F(InitGoogleTestTest, ListTestsFlag) { 1.5894 + const char* argv[] = { 1.5895 + "foo.exe", 1.5896 + "--gtest_list_tests", 1.5897 + NULL 1.5898 + }; 1.5899 + 1.5900 + const char* argv2[] = { 1.5901 + "foo.exe", 1.5902 + NULL 1.5903 + }; 1.5904 + 1.5905 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(true), false); 1.5906 +} 1.5907 + 1.5908 +// Tests having a --gtest_list_tests flag with a "true" value 1.5909 +TEST_F(InitGoogleTestTest, ListTestsTrue) { 1.5910 + const char* argv[] = { 1.5911 + "foo.exe", 1.5912 + "--gtest_list_tests=1", 1.5913 + NULL 1.5914 + }; 1.5915 + 1.5916 + const char* argv2[] = { 1.5917 + "foo.exe", 1.5918 + NULL 1.5919 + }; 1.5920 + 1.5921 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(true), false); 1.5922 +} 1.5923 + 1.5924 +// Tests having a --gtest_list_tests flag with a "false" value 1.5925 +TEST_F(InitGoogleTestTest, ListTestsFalse) { 1.5926 + const char* argv[] = { 1.5927 + "foo.exe", 1.5928 + "--gtest_list_tests=0", 1.5929 + NULL 1.5930 + }; 1.5931 + 1.5932 + const char* argv2[] = { 1.5933 + "foo.exe", 1.5934 + NULL 1.5935 + }; 1.5936 + 1.5937 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(false), false); 1.5938 +} 1.5939 + 1.5940 +// Tests parsing --gtest_list_tests=f. 1.5941 +TEST_F(InitGoogleTestTest, ListTestsFalse_f) { 1.5942 + const char* argv[] = { 1.5943 + "foo.exe", 1.5944 + "--gtest_list_tests=f", 1.5945 + NULL 1.5946 + }; 1.5947 + 1.5948 + const char* argv2[] = { 1.5949 + "foo.exe", 1.5950 + NULL 1.5951 + }; 1.5952 + 1.5953 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(false), false); 1.5954 +} 1.5955 + 1.5956 +// Tests parsing --gtest_list_tests=F. 1.5957 +TEST_F(InitGoogleTestTest, ListTestsFalse_F) { 1.5958 + const char* argv[] = { 1.5959 + "foo.exe", 1.5960 + "--gtest_list_tests=F", 1.5961 + NULL 1.5962 + }; 1.5963 + 1.5964 + const char* argv2[] = { 1.5965 + "foo.exe", 1.5966 + NULL 1.5967 + }; 1.5968 + 1.5969 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(false), false); 1.5970 +} 1.5971 + 1.5972 +// Tests parsing --gtest_output (invalid). 1.5973 +TEST_F(InitGoogleTestTest, OutputEmpty) { 1.5974 + const char* argv[] = { 1.5975 + "foo.exe", 1.5976 + "--gtest_output", 1.5977 + NULL 1.5978 + }; 1.5979 + 1.5980 + const char* argv2[] = { 1.5981 + "foo.exe", 1.5982 + "--gtest_output", 1.5983 + NULL 1.5984 + }; 1.5985 + 1.5986 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), true); 1.5987 +} 1.5988 + 1.5989 +// Tests parsing --gtest_output=xml 1.5990 +TEST_F(InitGoogleTestTest, OutputXml) { 1.5991 + const char* argv[] = { 1.5992 + "foo.exe", 1.5993 + "--gtest_output=xml", 1.5994 + NULL 1.5995 + }; 1.5996 + 1.5997 + const char* argv2[] = { 1.5998 + "foo.exe", 1.5999 + NULL 1.6000 + }; 1.6001 + 1.6002 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Output("xml"), false); 1.6003 +} 1.6004 + 1.6005 +// Tests parsing --gtest_output=xml:file 1.6006 +TEST_F(InitGoogleTestTest, OutputXmlFile) { 1.6007 + const char* argv[] = { 1.6008 + "foo.exe", 1.6009 + "--gtest_output=xml:file", 1.6010 + NULL 1.6011 + }; 1.6012 + 1.6013 + const char* argv2[] = { 1.6014 + "foo.exe", 1.6015 + NULL 1.6016 + }; 1.6017 + 1.6018 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Output("xml:file"), false); 1.6019 +} 1.6020 + 1.6021 +// Tests parsing --gtest_output=xml:directory/path/ 1.6022 +TEST_F(InitGoogleTestTest, OutputXmlDirectory) { 1.6023 + const char* argv[] = { 1.6024 + "foo.exe", 1.6025 + "--gtest_output=xml:directory/path/", 1.6026 + NULL 1.6027 + }; 1.6028 + 1.6029 + const char* argv2[] = { 1.6030 + "foo.exe", 1.6031 + NULL 1.6032 + }; 1.6033 + 1.6034 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, 1.6035 + Flags::Output("xml:directory/path/"), false); 1.6036 +} 1.6037 + 1.6038 +// Tests having a --gtest_print_time flag 1.6039 +TEST_F(InitGoogleTestTest, PrintTimeFlag) { 1.6040 + const char* argv[] = { 1.6041 + "foo.exe", 1.6042 + "--gtest_print_time", 1.6043 + NULL 1.6044 + }; 1.6045 + 1.6046 + const char* argv2[] = { 1.6047 + "foo.exe", 1.6048 + NULL 1.6049 + }; 1.6050 + 1.6051 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(true), false); 1.6052 +} 1.6053 + 1.6054 +// Tests having a --gtest_print_time flag with a "true" value 1.6055 +TEST_F(InitGoogleTestTest, PrintTimeTrue) { 1.6056 + const char* argv[] = { 1.6057 + "foo.exe", 1.6058 + "--gtest_print_time=1", 1.6059 + NULL 1.6060 + }; 1.6061 + 1.6062 + const char* argv2[] = { 1.6063 + "foo.exe", 1.6064 + NULL 1.6065 + }; 1.6066 + 1.6067 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(true), false); 1.6068 +} 1.6069 + 1.6070 +// Tests having a --gtest_print_time flag with a "false" value 1.6071 +TEST_F(InitGoogleTestTest, PrintTimeFalse) { 1.6072 + const char* argv[] = { 1.6073 + "foo.exe", 1.6074 + "--gtest_print_time=0", 1.6075 + NULL 1.6076 + }; 1.6077 + 1.6078 + const char* argv2[] = { 1.6079 + "foo.exe", 1.6080 + NULL 1.6081 + }; 1.6082 + 1.6083 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(false), false); 1.6084 +} 1.6085 + 1.6086 +// Tests parsing --gtest_print_time=f. 1.6087 +TEST_F(InitGoogleTestTest, PrintTimeFalse_f) { 1.6088 + const char* argv[] = { 1.6089 + "foo.exe", 1.6090 + "--gtest_print_time=f", 1.6091 + NULL 1.6092 + }; 1.6093 + 1.6094 + const char* argv2[] = { 1.6095 + "foo.exe", 1.6096 + NULL 1.6097 + }; 1.6098 + 1.6099 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(false), false); 1.6100 +} 1.6101 + 1.6102 +// Tests parsing --gtest_print_time=F. 1.6103 +TEST_F(InitGoogleTestTest, PrintTimeFalse_F) { 1.6104 + const char* argv[] = { 1.6105 + "foo.exe", 1.6106 + "--gtest_print_time=F", 1.6107 + NULL 1.6108 + }; 1.6109 + 1.6110 + const char* argv2[] = { 1.6111 + "foo.exe", 1.6112 + NULL 1.6113 + }; 1.6114 + 1.6115 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(false), false); 1.6116 +} 1.6117 + 1.6118 +// Tests parsing --gtest_random_seed=number 1.6119 +TEST_F(InitGoogleTestTest, RandomSeed) { 1.6120 + const char* argv[] = { 1.6121 + "foo.exe", 1.6122 + "--gtest_random_seed=1000", 1.6123 + NULL 1.6124 + }; 1.6125 + 1.6126 + const char* argv2[] = { 1.6127 + "foo.exe", 1.6128 + NULL 1.6129 + }; 1.6130 + 1.6131 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::RandomSeed(1000), false); 1.6132 +} 1.6133 + 1.6134 +// Tests parsing --gtest_repeat=number 1.6135 +TEST_F(InitGoogleTestTest, Repeat) { 1.6136 + const char* argv[] = { 1.6137 + "foo.exe", 1.6138 + "--gtest_repeat=1000", 1.6139 + NULL 1.6140 + }; 1.6141 + 1.6142 + const char* argv2[] = { 1.6143 + "foo.exe", 1.6144 + NULL 1.6145 + }; 1.6146 + 1.6147 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Repeat(1000), false); 1.6148 +} 1.6149 + 1.6150 +// Tests having a --gtest_also_run_disabled_tests flag 1.6151 +TEST_F(InitGoogleTestTest, AlsoRunDisabledTestsFlag) { 1.6152 + const char* argv[] = { 1.6153 + "foo.exe", 1.6154 + "--gtest_also_run_disabled_tests", 1.6155 + NULL 1.6156 + }; 1.6157 + 1.6158 + const char* argv2[] = { 1.6159 + "foo.exe", 1.6160 + NULL 1.6161 + }; 1.6162 + 1.6163 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, 1.6164 + Flags::AlsoRunDisabledTests(true), false); 1.6165 +} 1.6166 + 1.6167 +// Tests having a --gtest_also_run_disabled_tests flag with a "true" value 1.6168 +TEST_F(InitGoogleTestTest, AlsoRunDisabledTestsTrue) { 1.6169 + const char* argv[] = { 1.6170 + "foo.exe", 1.6171 + "--gtest_also_run_disabled_tests=1", 1.6172 + NULL 1.6173 + }; 1.6174 + 1.6175 + const char* argv2[] = { 1.6176 + "foo.exe", 1.6177 + NULL 1.6178 + }; 1.6179 + 1.6180 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, 1.6181 + Flags::AlsoRunDisabledTests(true), false); 1.6182 +} 1.6183 + 1.6184 +// Tests having a --gtest_also_run_disabled_tests flag with a "false" value 1.6185 +TEST_F(InitGoogleTestTest, AlsoRunDisabledTestsFalse) { 1.6186 + const char* argv[] = { 1.6187 + "foo.exe", 1.6188 + "--gtest_also_run_disabled_tests=0", 1.6189 + NULL 1.6190 + }; 1.6191 + 1.6192 + const char* argv2[] = { 1.6193 + "foo.exe", 1.6194 + NULL 1.6195 + }; 1.6196 + 1.6197 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, 1.6198 + Flags::AlsoRunDisabledTests(false), false); 1.6199 +} 1.6200 + 1.6201 +// Tests parsing --gtest_shuffle. 1.6202 +TEST_F(InitGoogleTestTest, ShuffleWithoutValue) { 1.6203 + const char* argv[] = { 1.6204 + "foo.exe", 1.6205 + "--gtest_shuffle", 1.6206 + NULL 1.6207 +}; 1.6208 + 1.6209 + const char* argv2[] = { 1.6210 + "foo.exe", 1.6211 + NULL 1.6212 + }; 1.6213 + 1.6214 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Shuffle(true), false); 1.6215 +} 1.6216 + 1.6217 +// Tests parsing --gtest_shuffle=0. 1.6218 +TEST_F(InitGoogleTestTest, ShuffleFalse_0) { 1.6219 + const char* argv[] = { 1.6220 + "foo.exe", 1.6221 + "--gtest_shuffle=0", 1.6222 + NULL 1.6223 + }; 1.6224 + 1.6225 + const char* argv2[] = { 1.6226 + "foo.exe", 1.6227 + NULL 1.6228 + }; 1.6229 + 1.6230 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Shuffle(false), false); 1.6231 +} 1.6232 + 1.6233 +// Tests parsing a --gtest_shuffle flag that has a "true" 1.6234 +// definition. 1.6235 +TEST_F(InitGoogleTestTest, ShuffleTrue) { 1.6236 + const char* argv[] = { 1.6237 + "foo.exe", 1.6238 + "--gtest_shuffle=1", 1.6239 + NULL 1.6240 + }; 1.6241 + 1.6242 + const char* argv2[] = { 1.6243 + "foo.exe", 1.6244 + NULL 1.6245 + }; 1.6246 + 1.6247 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Shuffle(true), false); 1.6248 +} 1.6249 + 1.6250 +// Tests parsing --gtest_stack_trace_depth=number. 1.6251 +TEST_F(InitGoogleTestTest, StackTraceDepth) { 1.6252 + const char* argv[] = { 1.6253 + "foo.exe", 1.6254 + "--gtest_stack_trace_depth=5", 1.6255 + NULL 1.6256 + }; 1.6257 + 1.6258 + const char* argv2[] = { 1.6259 + "foo.exe", 1.6260 + NULL 1.6261 + }; 1.6262 + 1.6263 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::StackTraceDepth(5), false); 1.6264 +} 1.6265 + 1.6266 +TEST_F(InitGoogleTestTest, StreamResultTo) { 1.6267 + const char* argv[] = { 1.6268 + "foo.exe", 1.6269 + "--gtest_stream_result_to=localhost:1234", 1.6270 + NULL 1.6271 + }; 1.6272 + 1.6273 + const char* argv2[] = { 1.6274 + "foo.exe", 1.6275 + NULL 1.6276 + }; 1.6277 + 1.6278 + GTEST_TEST_PARSING_FLAGS_( 1.6279 + argv, argv2, Flags::StreamResultTo("localhost:1234"), false); 1.6280 +} 1.6281 + 1.6282 +// Tests parsing --gtest_throw_on_failure. 1.6283 +TEST_F(InitGoogleTestTest, ThrowOnFailureWithoutValue) { 1.6284 + const char* argv[] = { 1.6285 + "foo.exe", 1.6286 + "--gtest_throw_on_failure", 1.6287 + NULL 1.6288 +}; 1.6289 + 1.6290 + const char* argv2[] = { 1.6291 + "foo.exe", 1.6292 + NULL 1.6293 + }; 1.6294 + 1.6295 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ThrowOnFailure(true), false); 1.6296 +} 1.6297 + 1.6298 +// Tests parsing --gtest_throw_on_failure=0. 1.6299 +TEST_F(InitGoogleTestTest, ThrowOnFailureFalse_0) { 1.6300 + const char* argv[] = { 1.6301 + "foo.exe", 1.6302 + "--gtest_throw_on_failure=0", 1.6303 + NULL 1.6304 + }; 1.6305 + 1.6306 + const char* argv2[] = { 1.6307 + "foo.exe", 1.6308 + NULL 1.6309 + }; 1.6310 + 1.6311 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ThrowOnFailure(false), false); 1.6312 +} 1.6313 + 1.6314 +// Tests parsing a --gtest_throw_on_failure flag that has a "true" 1.6315 +// definition. 1.6316 +TEST_F(InitGoogleTestTest, ThrowOnFailureTrue) { 1.6317 + const char* argv[] = { 1.6318 + "foo.exe", 1.6319 + "--gtest_throw_on_failure=1", 1.6320 + NULL 1.6321 + }; 1.6322 + 1.6323 + const char* argv2[] = { 1.6324 + "foo.exe", 1.6325 + NULL 1.6326 + }; 1.6327 + 1.6328 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ThrowOnFailure(true), false); 1.6329 +} 1.6330 + 1.6331 +#if GTEST_OS_WINDOWS 1.6332 +// Tests parsing wide strings. 1.6333 +TEST_F(InitGoogleTestTest, WideStrings) { 1.6334 + const wchar_t* argv[] = { 1.6335 + L"foo.exe", 1.6336 + L"--gtest_filter=Foo*", 1.6337 + L"--gtest_list_tests=1", 1.6338 + L"--gtest_break_on_failure", 1.6339 + L"--non_gtest_flag", 1.6340 + NULL 1.6341 + }; 1.6342 + 1.6343 + const wchar_t* argv2[] = { 1.6344 + L"foo.exe", 1.6345 + L"--non_gtest_flag", 1.6346 + NULL 1.6347 + }; 1.6348 + 1.6349 + Flags expected_flags; 1.6350 + expected_flags.break_on_failure = true; 1.6351 + expected_flags.filter = "Foo*"; 1.6352 + expected_flags.list_tests = true; 1.6353 + 1.6354 + GTEST_TEST_PARSING_FLAGS_(argv, argv2, expected_flags, false); 1.6355 +} 1.6356 +#endif // GTEST_OS_WINDOWS 1.6357 + 1.6358 +// Tests current_test_info() in UnitTest. 1.6359 +class CurrentTestInfoTest : public Test { 1.6360 + protected: 1.6361 + // Tests that current_test_info() returns NULL before the first test in 1.6362 + // the test case is run. 1.6363 + static void SetUpTestCase() { 1.6364 + // There should be no tests running at this point. 1.6365 + const TestInfo* test_info = 1.6366 + UnitTest::GetInstance()->current_test_info(); 1.6367 + EXPECT_TRUE(test_info == NULL) 1.6368 + << "There should be no tests running at this point."; 1.6369 + } 1.6370 + 1.6371 + // Tests that current_test_info() returns NULL after the last test in 1.6372 + // the test case has run. 1.6373 + static void TearDownTestCase() { 1.6374 + const TestInfo* test_info = 1.6375 + UnitTest::GetInstance()->current_test_info(); 1.6376 + EXPECT_TRUE(test_info == NULL) 1.6377 + << "There should be no tests running at this point."; 1.6378 + } 1.6379 +}; 1.6380 + 1.6381 +// Tests that current_test_info() returns TestInfo for currently running 1.6382 +// test by checking the expected test name against the actual one. 1.6383 +TEST_F(CurrentTestInfoTest, WorksForFirstTestInATestCase) { 1.6384 + const TestInfo* test_info = 1.6385 + UnitTest::GetInstance()->current_test_info(); 1.6386 + ASSERT_TRUE(NULL != test_info) 1.6387 + << "There is a test running so we should have a valid TestInfo."; 1.6388 + EXPECT_STREQ("CurrentTestInfoTest", test_info->test_case_name()) 1.6389 + << "Expected the name of the currently running test case."; 1.6390 + EXPECT_STREQ("WorksForFirstTestInATestCase", test_info->name()) 1.6391 + << "Expected the name of the currently running test."; 1.6392 +} 1.6393 + 1.6394 +// Tests that current_test_info() returns TestInfo for currently running 1.6395 +// test by checking the expected test name against the actual one. We 1.6396 +// use this test to see that the TestInfo object actually changed from 1.6397 +// the previous invocation. 1.6398 +TEST_F(CurrentTestInfoTest, WorksForSecondTestInATestCase) { 1.6399 + const TestInfo* test_info = 1.6400 + UnitTest::GetInstance()->current_test_info(); 1.6401 + ASSERT_TRUE(NULL != test_info) 1.6402 + << "There is a test running so we should have a valid TestInfo."; 1.6403 + EXPECT_STREQ("CurrentTestInfoTest", test_info->test_case_name()) 1.6404 + << "Expected the name of the currently running test case."; 1.6405 + EXPECT_STREQ("WorksForSecondTestInATestCase", test_info->name()) 1.6406 + << "Expected the name of the currently running test."; 1.6407 +} 1.6408 + 1.6409 +} // namespace testing 1.6410 + 1.6411 +// These two lines test that we can define tests in a namespace that 1.6412 +// has the name "testing" and is nested in another namespace. 1.6413 +namespace my_namespace { 1.6414 +namespace testing { 1.6415 + 1.6416 +// Makes sure that TEST knows to use ::testing::Test instead of 1.6417 +// ::my_namespace::testing::Test. 1.6418 +class Test {}; 1.6419 + 1.6420 +// Makes sure that an assertion knows to use ::testing::Message instead of 1.6421 +// ::my_namespace::testing::Message. 1.6422 +class Message {}; 1.6423 + 1.6424 +// Makes sure that an assertion knows to use 1.6425 +// ::testing::AssertionResult instead of 1.6426 +// ::my_namespace::testing::AssertionResult. 1.6427 +class AssertionResult {}; 1.6428 + 1.6429 +// Tests that an assertion that should succeed works as expected. 1.6430 +TEST(NestedTestingNamespaceTest, Success) { 1.6431 + EXPECT_EQ(1, 1) << "This shouldn't fail."; 1.6432 +} 1.6433 + 1.6434 +// Tests that an assertion that should fail works as expected. 1.6435 +TEST(NestedTestingNamespaceTest, Failure) { 1.6436 + EXPECT_FATAL_FAILURE(FAIL() << "This failure is expected.", 1.6437 + "This failure is expected."); 1.6438 +} 1.6439 + 1.6440 +} // namespace testing 1.6441 +} // namespace my_namespace 1.6442 + 1.6443 +// Tests that one can call superclass SetUp and TearDown methods-- 1.6444 +// that is, that they are not private. 1.6445 +// No tests are based on this fixture; the test "passes" if it compiles 1.6446 +// successfully. 1.6447 +class ProtectedFixtureMethodsTest : public Test { 1.6448 + protected: 1.6449 + virtual void SetUp() { 1.6450 + Test::SetUp(); 1.6451 + } 1.6452 + virtual void TearDown() { 1.6453 + Test::TearDown(); 1.6454 + } 1.6455 +}; 1.6456 + 1.6457 +// StreamingAssertionsTest tests the streaming versions of a representative 1.6458 +// sample of assertions. 1.6459 +TEST(StreamingAssertionsTest, Unconditional) { 1.6460 + SUCCEED() << "expected success"; 1.6461 + EXPECT_NONFATAL_FAILURE(ADD_FAILURE() << "expected failure", 1.6462 + "expected failure"); 1.6463 + EXPECT_FATAL_FAILURE(FAIL() << "expected failure", 1.6464 + "expected failure"); 1.6465 +} 1.6466 + 1.6467 +#ifdef __BORLANDC__ 1.6468 +// Silences warnings: "Condition is always true", "Unreachable code" 1.6469 +# pragma option push -w-ccc -w-rch 1.6470 +#endif 1.6471 + 1.6472 +TEST(StreamingAssertionsTest, Truth) { 1.6473 + EXPECT_TRUE(true) << "unexpected failure"; 1.6474 + ASSERT_TRUE(true) << "unexpected failure"; 1.6475 + EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(false) << "expected failure", 1.6476 + "expected failure"); 1.6477 + EXPECT_FATAL_FAILURE(ASSERT_TRUE(false) << "expected failure", 1.6478 + "expected failure"); 1.6479 +} 1.6480 + 1.6481 +TEST(StreamingAssertionsTest, Truth2) { 1.6482 + EXPECT_FALSE(false) << "unexpected failure"; 1.6483 + ASSERT_FALSE(false) << "unexpected failure"; 1.6484 + EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(true) << "expected failure", 1.6485 + "expected failure"); 1.6486 + EXPECT_FATAL_FAILURE(ASSERT_FALSE(true) << "expected failure", 1.6487 + "expected failure"); 1.6488 +} 1.6489 + 1.6490 +#ifdef __BORLANDC__ 1.6491 +// Restores warnings after previous "#pragma option push" supressed them 1.6492 +# pragma option pop 1.6493 +#endif 1.6494 + 1.6495 +TEST(StreamingAssertionsTest, IntegerEquals) { 1.6496 + EXPECT_EQ(1, 1) << "unexpected failure"; 1.6497 + ASSERT_EQ(1, 1) << "unexpected failure"; 1.6498 + EXPECT_NONFATAL_FAILURE(EXPECT_EQ(1, 2) << "expected failure", 1.6499 + "expected failure"); 1.6500 + EXPECT_FATAL_FAILURE(ASSERT_EQ(1, 2) << "expected failure", 1.6501 + "expected failure"); 1.6502 +} 1.6503 + 1.6504 +TEST(StreamingAssertionsTest, IntegerLessThan) { 1.6505 + EXPECT_LT(1, 2) << "unexpected failure"; 1.6506 + ASSERT_LT(1, 2) << "unexpected failure"; 1.6507 + EXPECT_NONFATAL_FAILURE(EXPECT_LT(2, 1) << "expected failure", 1.6508 + "expected failure"); 1.6509 + EXPECT_FATAL_FAILURE(ASSERT_LT(2, 1) << "expected failure", 1.6510 + "expected failure"); 1.6511 +} 1.6512 + 1.6513 +TEST(StreamingAssertionsTest, StringsEqual) { 1.6514 + EXPECT_STREQ("foo", "foo") << "unexpected failure"; 1.6515 + ASSERT_STREQ("foo", "foo") << "unexpected failure"; 1.6516 + EXPECT_NONFATAL_FAILURE(EXPECT_STREQ("foo", "bar") << "expected failure", 1.6517 + "expected failure"); 1.6518 + EXPECT_FATAL_FAILURE(ASSERT_STREQ("foo", "bar") << "expected failure", 1.6519 + "expected failure"); 1.6520 +} 1.6521 + 1.6522 +TEST(StreamingAssertionsTest, StringsNotEqual) { 1.6523 + EXPECT_STRNE("foo", "bar") << "unexpected failure"; 1.6524 + ASSERT_STRNE("foo", "bar") << "unexpected failure"; 1.6525 + EXPECT_NONFATAL_FAILURE(EXPECT_STRNE("foo", "foo") << "expected failure", 1.6526 + "expected failure"); 1.6527 + EXPECT_FATAL_FAILURE(ASSERT_STRNE("foo", "foo") << "expected failure", 1.6528 + "expected failure"); 1.6529 +} 1.6530 + 1.6531 +TEST(StreamingAssertionsTest, StringsEqualIgnoringCase) { 1.6532 + EXPECT_STRCASEEQ("foo", "FOO") << "unexpected failure"; 1.6533 + ASSERT_STRCASEEQ("foo", "FOO") << "unexpected failure"; 1.6534 + EXPECT_NONFATAL_FAILURE(EXPECT_STRCASEEQ("foo", "bar") << "expected failure", 1.6535 + "expected failure"); 1.6536 + EXPECT_FATAL_FAILURE(ASSERT_STRCASEEQ("foo", "bar") << "expected failure", 1.6537 + "expected failure"); 1.6538 +} 1.6539 + 1.6540 +TEST(StreamingAssertionsTest, StringNotEqualIgnoringCase) { 1.6541 + EXPECT_STRCASENE("foo", "bar") << "unexpected failure"; 1.6542 + ASSERT_STRCASENE("foo", "bar") << "unexpected failure"; 1.6543 + EXPECT_NONFATAL_FAILURE(EXPECT_STRCASENE("foo", "FOO") << "expected failure", 1.6544 + "expected failure"); 1.6545 + EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("bar", "BAR") << "expected failure", 1.6546 + "expected failure"); 1.6547 +} 1.6548 + 1.6549 +TEST(StreamingAssertionsTest, FloatingPointEquals) { 1.6550 + EXPECT_FLOAT_EQ(1.0, 1.0) << "unexpected failure"; 1.6551 + ASSERT_FLOAT_EQ(1.0, 1.0) << "unexpected failure"; 1.6552 + EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(0.0, 1.0) << "expected failure", 1.6553 + "expected failure"); 1.6554 + EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(0.0, 1.0) << "expected failure", 1.6555 + "expected failure"); 1.6556 +} 1.6557 + 1.6558 +#if GTEST_HAS_EXCEPTIONS 1.6559 + 1.6560 +TEST(StreamingAssertionsTest, Throw) { 1.6561 + EXPECT_THROW(ThrowAnInteger(), int) << "unexpected failure"; 1.6562 + ASSERT_THROW(ThrowAnInteger(), int) << "unexpected failure"; 1.6563 + EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnInteger(), bool) << 1.6564 + "expected failure", "expected failure"); 1.6565 + EXPECT_FATAL_FAILURE(ASSERT_THROW(ThrowAnInteger(), bool) << 1.6566 + "expected failure", "expected failure"); 1.6567 +} 1.6568 + 1.6569 +TEST(StreamingAssertionsTest, NoThrow) { 1.6570 + EXPECT_NO_THROW(ThrowNothing()) << "unexpected failure"; 1.6571 + ASSERT_NO_THROW(ThrowNothing()) << "unexpected failure"; 1.6572 + EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()) << 1.6573 + "expected failure", "expected failure"); 1.6574 + EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()) << 1.6575 + "expected failure", "expected failure"); 1.6576 +} 1.6577 + 1.6578 +TEST(StreamingAssertionsTest, AnyThrow) { 1.6579 + EXPECT_ANY_THROW(ThrowAnInteger()) << "unexpected failure"; 1.6580 + ASSERT_ANY_THROW(ThrowAnInteger()) << "unexpected failure"; 1.6581 + EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(ThrowNothing()) << 1.6582 + "expected failure", "expected failure"); 1.6583 + EXPECT_FATAL_FAILURE(ASSERT_ANY_THROW(ThrowNothing()) << 1.6584 + "expected failure", "expected failure"); 1.6585 +} 1.6586 + 1.6587 +#endif // GTEST_HAS_EXCEPTIONS 1.6588 + 1.6589 +// Tests that Google Test correctly decides whether to use colors in the output. 1.6590 + 1.6591 +TEST(ColoredOutputTest, UsesColorsWhenGTestColorFlagIsYes) { 1.6592 + GTEST_FLAG(color) = "yes"; 1.6593 + 1.6594 + SetEnv("TERM", "xterm"); // TERM supports colors. 1.6595 + EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. 1.6596 + EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. 1.6597 + 1.6598 + SetEnv("TERM", "dumb"); // TERM doesn't support colors. 1.6599 + EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. 1.6600 + EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. 1.6601 +} 1.6602 + 1.6603 +TEST(ColoredOutputTest, UsesColorsWhenGTestColorFlagIsAliasOfYes) { 1.6604 + SetEnv("TERM", "dumb"); // TERM doesn't support colors. 1.6605 + 1.6606 + GTEST_FLAG(color) = "True"; 1.6607 + EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. 1.6608 + 1.6609 + GTEST_FLAG(color) = "t"; 1.6610 + EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. 1.6611 + 1.6612 + GTEST_FLAG(color) = "1"; 1.6613 + EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. 1.6614 +} 1.6615 + 1.6616 +TEST(ColoredOutputTest, UsesNoColorWhenGTestColorFlagIsNo) { 1.6617 + GTEST_FLAG(color) = "no"; 1.6618 + 1.6619 + SetEnv("TERM", "xterm"); // TERM supports colors. 1.6620 + EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. 1.6621 + EXPECT_FALSE(ShouldUseColor(false)); // Stdout is not a TTY. 1.6622 + 1.6623 + SetEnv("TERM", "dumb"); // TERM doesn't support colors. 1.6624 + EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. 1.6625 + EXPECT_FALSE(ShouldUseColor(false)); // Stdout is not a TTY. 1.6626 +} 1.6627 + 1.6628 +TEST(ColoredOutputTest, UsesNoColorWhenGTestColorFlagIsInvalid) { 1.6629 + SetEnv("TERM", "xterm"); // TERM supports colors. 1.6630 + 1.6631 + GTEST_FLAG(color) = "F"; 1.6632 + EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. 1.6633 + 1.6634 + GTEST_FLAG(color) = "0"; 1.6635 + EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. 1.6636 + 1.6637 + GTEST_FLAG(color) = "unknown"; 1.6638 + EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. 1.6639 +} 1.6640 + 1.6641 +TEST(ColoredOutputTest, UsesColorsWhenStdoutIsTty) { 1.6642 + GTEST_FLAG(color) = "auto"; 1.6643 + 1.6644 + SetEnv("TERM", "xterm"); // TERM supports colors. 1.6645 + EXPECT_FALSE(ShouldUseColor(false)); // Stdout is not a TTY. 1.6646 + EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. 1.6647 +} 1.6648 + 1.6649 +TEST(ColoredOutputTest, UsesColorsWhenTermSupportsColors) { 1.6650 + GTEST_FLAG(color) = "auto"; 1.6651 + 1.6652 +#if GTEST_OS_WINDOWS 1.6653 + // On Windows, we ignore the TERM variable as it's usually not set. 1.6654 + 1.6655 + SetEnv("TERM", "dumb"); 1.6656 + EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. 1.6657 + 1.6658 + SetEnv("TERM", ""); 1.6659 + EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. 1.6660 + 1.6661 + SetEnv("TERM", "xterm"); 1.6662 + EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. 1.6663 +#else 1.6664 + // On non-Windows platforms, we rely on TERM to determine if the 1.6665 + // terminal supports colors. 1.6666 + 1.6667 + SetEnv("TERM", "dumb"); // TERM doesn't support colors. 1.6668 + EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. 1.6669 + 1.6670 + SetEnv("TERM", "emacs"); // TERM doesn't support colors. 1.6671 + EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. 1.6672 + 1.6673 + SetEnv("TERM", "vt100"); // TERM doesn't support colors. 1.6674 + EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. 1.6675 + 1.6676 + SetEnv("TERM", "xterm-mono"); // TERM doesn't support colors. 1.6677 + EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. 1.6678 + 1.6679 + SetEnv("TERM", "xterm"); // TERM supports colors. 1.6680 + EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. 1.6681 + 1.6682 + SetEnv("TERM", "xterm-color"); // TERM supports colors. 1.6683 + EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. 1.6684 + 1.6685 + SetEnv("TERM", "xterm-256color"); // TERM supports colors. 1.6686 + EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. 1.6687 + 1.6688 + SetEnv("TERM", "screen"); // TERM supports colors. 1.6689 + EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. 1.6690 + 1.6691 + SetEnv("TERM", "linux"); // TERM supports colors. 1.6692 + EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. 1.6693 + 1.6694 + SetEnv("TERM", "cygwin"); // TERM supports colors. 1.6695 + EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. 1.6696 +#endif // GTEST_OS_WINDOWS 1.6697 +} 1.6698 + 1.6699 +// Verifies that StaticAssertTypeEq works in a namespace scope. 1.6700 + 1.6701 +static bool dummy1 GTEST_ATTRIBUTE_UNUSED_ = StaticAssertTypeEq<bool, bool>(); 1.6702 +static bool dummy2 GTEST_ATTRIBUTE_UNUSED_ = 1.6703 + StaticAssertTypeEq<const int, const int>(); 1.6704 + 1.6705 +// Verifies that StaticAssertTypeEq works in a class. 1.6706 + 1.6707 +template <typename T> 1.6708 +class StaticAssertTypeEqTestHelper { 1.6709 + public: 1.6710 + StaticAssertTypeEqTestHelper() { StaticAssertTypeEq<bool, T>(); } 1.6711 +}; 1.6712 + 1.6713 +TEST(StaticAssertTypeEqTest, WorksInClass) { 1.6714 + StaticAssertTypeEqTestHelper<bool>(); 1.6715 +} 1.6716 + 1.6717 +// Verifies that StaticAssertTypeEq works inside a function. 1.6718 + 1.6719 +typedef int IntAlias; 1.6720 + 1.6721 +TEST(StaticAssertTypeEqTest, CompilesForEqualTypes) { 1.6722 + StaticAssertTypeEq<int, IntAlias>(); 1.6723 + StaticAssertTypeEq<int*, IntAlias*>(); 1.6724 +} 1.6725 + 1.6726 +TEST(GetCurrentOsStackTraceExceptTopTest, ReturnsTheStackTrace) { 1.6727 + testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); 1.6728 + 1.6729 + // We don't have a stack walker in Google Test yet. 1.6730 + EXPECT_STREQ("", GetCurrentOsStackTraceExceptTop(unit_test, 0).c_str()); 1.6731 + EXPECT_STREQ("", GetCurrentOsStackTraceExceptTop(unit_test, 1).c_str()); 1.6732 +} 1.6733 + 1.6734 +TEST(HasNonfatalFailureTest, ReturnsFalseWhenThereIsNoFailure) { 1.6735 + EXPECT_FALSE(HasNonfatalFailure()); 1.6736 +} 1.6737 + 1.6738 +static void FailFatally() { FAIL(); } 1.6739 + 1.6740 +TEST(HasNonfatalFailureTest, ReturnsFalseWhenThereIsOnlyFatalFailure) { 1.6741 + FailFatally(); 1.6742 + const bool has_nonfatal_failure = HasNonfatalFailure(); 1.6743 + ClearCurrentTestPartResults(); 1.6744 + EXPECT_FALSE(has_nonfatal_failure); 1.6745 +} 1.6746 + 1.6747 +TEST(HasNonfatalFailureTest, ReturnsTrueWhenThereIsNonfatalFailure) { 1.6748 + ADD_FAILURE(); 1.6749 + const bool has_nonfatal_failure = HasNonfatalFailure(); 1.6750 + ClearCurrentTestPartResults(); 1.6751 + EXPECT_TRUE(has_nonfatal_failure); 1.6752 +} 1.6753 + 1.6754 +TEST(HasNonfatalFailureTest, ReturnsTrueWhenThereAreFatalAndNonfatalFailures) { 1.6755 + FailFatally(); 1.6756 + ADD_FAILURE(); 1.6757 + const bool has_nonfatal_failure = HasNonfatalFailure(); 1.6758 + ClearCurrentTestPartResults(); 1.6759 + EXPECT_TRUE(has_nonfatal_failure); 1.6760 +} 1.6761 + 1.6762 +// A wrapper for calling HasNonfatalFailure outside of a test body. 1.6763 +static bool HasNonfatalFailureHelper() { 1.6764 + return testing::Test::HasNonfatalFailure(); 1.6765 +} 1.6766 + 1.6767 +TEST(HasNonfatalFailureTest, WorksOutsideOfTestBody) { 1.6768 + EXPECT_FALSE(HasNonfatalFailureHelper()); 1.6769 +} 1.6770 + 1.6771 +TEST(HasNonfatalFailureTest, WorksOutsideOfTestBody2) { 1.6772 + ADD_FAILURE(); 1.6773 + const bool has_nonfatal_failure = HasNonfatalFailureHelper(); 1.6774 + ClearCurrentTestPartResults(); 1.6775 + EXPECT_TRUE(has_nonfatal_failure); 1.6776 +} 1.6777 + 1.6778 +TEST(HasFailureTest, ReturnsFalseWhenThereIsNoFailure) { 1.6779 + EXPECT_FALSE(HasFailure()); 1.6780 +} 1.6781 + 1.6782 +TEST(HasFailureTest, ReturnsTrueWhenThereIsFatalFailure) { 1.6783 + FailFatally(); 1.6784 + const bool has_failure = HasFailure(); 1.6785 + ClearCurrentTestPartResults(); 1.6786 + EXPECT_TRUE(has_failure); 1.6787 +} 1.6788 + 1.6789 +TEST(HasFailureTest, ReturnsTrueWhenThereIsNonfatalFailure) { 1.6790 + ADD_FAILURE(); 1.6791 + const bool has_failure = HasFailure(); 1.6792 + ClearCurrentTestPartResults(); 1.6793 + EXPECT_TRUE(has_failure); 1.6794 +} 1.6795 + 1.6796 +TEST(HasFailureTest, ReturnsTrueWhenThereAreFatalAndNonfatalFailures) { 1.6797 + FailFatally(); 1.6798 + ADD_FAILURE(); 1.6799 + const bool has_failure = HasFailure(); 1.6800 + ClearCurrentTestPartResults(); 1.6801 + EXPECT_TRUE(has_failure); 1.6802 +} 1.6803 + 1.6804 +// A wrapper for calling HasFailure outside of a test body. 1.6805 +static bool HasFailureHelper() { return testing::Test::HasFailure(); } 1.6806 + 1.6807 +TEST(HasFailureTest, WorksOutsideOfTestBody) { 1.6808 + EXPECT_FALSE(HasFailureHelper()); 1.6809 +} 1.6810 + 1.6811 +TEST(HasFailureTest, WorksOutsideOfTestBody2) { 1.6812 + ADD_FAILURE(); 1.6813 + const bool has_failure = HasFailureHelper(); 1.6814 + ClearCurrentTestPartResults(); 1.6815 + EXPECT_TRUE(has_failure); 1.6816 +} 1.6817 + 1.6818 +class TestListener : public EmptyTestEventListener { 1.6819 + public: 1.6820 + TestListener() : on_start_counter_(NULL), is_destroyed_(NULL) {} 1.6821 + TestListener(int* on_start_counter, bool* is_destroyed) 1.6822 + : on_start_counter_(on_start_counter), 1.6823 + is_destroyed_(is_destroyed) {} 1.6824 + 1.6825 + virtual ~TestListener() { 1.6826 + if (is_destroyed_) 1.6827 + *is_destroyed_ = true; 1.6828 + } 1.6829 + 1.6830 + protected: 1.6831 + virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) { 1.6832 + if (on_start_counter_ != NULL) 1.6833 + (*on_start_counter_)++; 1.6834 + } 1.6835 + 1.6836 + private: 1.6837 + int* on_start_counter_; 1.6838 + bool* is_destroyed_; 1.6839 +}; 1.6840 + 1.6841 +// Tests the constructor. 1.6842 +TEST(TestEventListenersTest, ConstructionWorks) { 1.6843 + TestEventListeners listeners; 1.6844 + 1.6845 + EXPECT_TRUE(TestEventListenersAccessor::GetRepeater(&listeners) != NULL); 1.6846 + EXPECT_TRUE(listeners.default_result_printer() == NULL); 1.6847 + EXPECT_TRUE(listeners.default_xml_generator() == NULL); 1.6848 +} 1.6849 + 1.6850 +// Tests that the TestEventListeners destructor deletes all the listeners it 1.6851 +// owns. 1.6852 +TEST(TestEventListenersTest, DestructionWorks) { 1.6853 + bool default_result_printer_is_destroyed = false; 1.6854 + bool default_xml_printer_is_destroyed = false; 1.6855 + bool extra_listener_is_destroyed = false; 1.6856 + TestListener* default_result_printer = new TestListener( 1.6857 + NULL, &default_result_printer_is_destroyed); 1.6858 + TestListener* default_xml_printer = new TestListener( 1.6859 + NULL, &default_xml_printer_is_destroyed); 1.6860 + TestListener* extra_listener = new TestListener( 1.6861 + NULL, &extra_listener_is_destroyed); 1.6862 + 1.6863 + { 1.6864 + TestEventListeners listeners; 1.6865 + TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, 1.6866 + default_result_printer); 1.6867 + TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, 1.6868 + default_xml_printer); 1.6869 + listeners.Append(extra_listener); 1.6870 + } 1.6871 + EXPECT_TRUE(default_result_printer_is_destroyed); 1.6872 + EXPECT_TRUE(default_xml_printer_is_destroyed); 1.6873 + EXPECT_TRUE(extra_listener_is_destroyed); 1.6874 +} 1.6875 + 1.6876 +// Tests that a listener Append'ed to a TestEventListeners list starts 1.6877 +// receiving events. 1.6878 +TEST(TestEventListenersTest, Append) { 1.6879 + int on_start_counter = 0; 1.6880 + bool is_destroyed = false; 1.6881 + TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); 1.6882 + { 1.6883 + TestEventListeners listeners; 1.6884 + listeners.Append(listener); 1.6885 + TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( 1.6886 + *UnitTest::GetInstance()); 1.6887 + EXPECT_EQ(1, on_start_counter); 1.6888 + } 1.6889 + EXPECT_TRUE(is_destroyed); 1.6890 +} 1.6891 + 1.6892 +// Tests that listeners receive events in the order they were appended to 1.6893 +// the list, except for *End requests, which must be received in the reverse 1.6894 +// order. 1.6895 +class SequenceTestingListener : public EmptyTestEventListener { 1.6896 + public: 1.6897 + SequenceTestingListener(std::vector<String>* vector, const char* id) 1.6898 + : vector_(vector), id_(id) {} 1.6899 + 1.6900 + protected: 1.6901 + virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) { 1.6902 + vector_->push_back(GetEventDescription("OnTestProgramStart")); 1.6903 + } 1.6904 + 1.6905 + virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) { 1.6906 + vector_->push_back(GetEventDescription("OnTestProgramEnd")); 1.6907 + } 1.6908 + 1.6909 + virtual void OnTestIterationStart(const UnitTest& /*unit_test*/, 1.6910 + int /*iteration*/) { 1.6911 + vector_->push_back(GetEventDescription("OnTestIterationStart")); 1.6912 + } 1.6913 + 1.6914 + virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/, 1.6915 + int /*iteration*/) { 1.6916 + vector_->push_back(GetEventDescription("OnTestIterationEnd")); 1.6917 + } 1.6918 + 1.6919 + private: 1.6920 + String GetEventDescription(const char* method) { 1.6921 + Message message; 1.6922 + message << id_ << "." << method; 1.6923 + return message.GetString(); 1.6924 + } 1.6925 + 1.6926 + std::vector<String>* vector_; 1.6927 + const char* const id_; 1.6928 + 1.6929 + GTEST_DISALLOW_COPY_AND_ASSIGN_(SequenceTestingListener); 1.6930 +}; 1.6931 + 1.6932 +TEST(EventListenerTest, AppendKeepsOrder) { 1.6933 + std::vector<String> vec; 1.6934 + TestEventListeners listeners; 1.6935 + listeners.Append(new SequenceTestingListener(&vec, "1st")); 1.6936 + listeners.Append(new SequenceTestingListener(&vec, "2nd")); 1.6937 + listeners.Append(new SequenceTestingListener(&vec, "3rd")); 1.6938 + 1.6939 + TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( 1.6940 + *UnitTest::GetInstance()); 1.6941 + ASSERT_EQ(3U, vec.size()); 1.6942 + EXPECT_STREQ("1st.OnTestProgramStart", vec[0].c_str()); 1.6943 + EXPECT_STREQ("2nd.OnTestProgramStart", vec[1].c_str()); 1.6944 + EXPECT_STREQ("3rd.OnTestProgramStart", vec[2].c_str()); 1.6945 + 1.6946 + vec.clear(); 1.6947 + TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramEnd( 1.6948 + *UnitTest::GetInstance()); 1.6949 + ASSERT_EQ(3U, vec.size()); 1.6950 + EXPECT_STREQ("3rd.OnTestProgramEnd", vec[0].c_str()); 1.6951 + EXPECT_STREQ("2nd.OnTestProgramEnd", vec[1].c_str()); 1.6952 + EXPECT_STREQ("1st.OnTestProgramEnd", vec[2].c_str()); 1.6953 + 1.6954 + vec.clear(); 1.6955 + TestEventListenersAccessor::GetRepeater(&listeners)->OnTestIterationStart( 1.6956 + *UnitTest::GetInstance(), 0); 1.6957 + ASSERT_EQ(3U, vec.size()); 1.6958 + EXPECT_STREQ("1st.OnTestIterationStart", vec[0].c_str()); 1.6959 + EXPECT_STREQ("2nd.OnTestIterationStart", vec[1].c_str()); 1.6960 + EXPECT_STREQ("3rd.OnTestIterationStart", vec[2].c_str()); 1.6961 + 1.6962 + vec.clear(); 1.6963 + TestEventListenersAccessor::GetRepeater(&listeners)->OnTestIterationEnd( 1.6964 + *UnitTest::GetInstance(), 0); 1.6965 + ASSERT_EQ(3U, vec.size()); 1.6966 + EXPECT_STREQ("3rd.OnTestIterationEnd", vec[0].c_str()); 1.6967 + EXPECT_STREQ("2nd.OnTestIterationEnd", vec[1].c_str()); 1.6968 + EXPECT_STREQ("1st.OnTestIterationEnd", vec[2].c_str()); 1.6969 +} 1.6970 + 1.6971 +// Tests that a listener removed from a TestEventListeners list stops receiving 1.6972 +// events and is not deleted when the list is destroyed. 1.6973 +TEST(TestEventListenersTest, Release) { 1.6974 + int on_start_counter = 0; 1.6975 + bool is_destroyed = false; 1.6976 + // Although Append passes the ownership of this object to the list, 1.6977 + // the following calls release it, and we need to delete it before the 1.6978 + // test ends. 1.6979 + TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); 1.6980 + { 1.6981 + TestEventListeners listeners; 1.6982 + listeners.Append(listener); 1.6983 + EXPECT_EQ(listener, listeners.Release(listener)); 1.6984 + TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( 1.6985 + *UnitTest::GetInstance()); 1.6986 + EXPECT_TRUE(listeners.Release(listener) == NULL); 1.6987 + } 1.6988 + EXPECT_EQ(0, on_start_counter); 1.6989 + EXPECT_FALSE(is_destroyed); 1.6990 + delete listener; 1.6991 +} 1.6992 + 1.6993 +// Tests that no events are forwarded when event forwarding is disabled. 1.6994 +TEST(EventListenerTest, SuppressEventForwarding) { 1.6995 + int on_start_counter = 0; 1.6996 + TestListener* listener = new TestListener(&on_start_counter, NULL); 1.6997 + 1.6998 + TestEventListeners listeners; 1.6999 + listeners.Append(listener); 1.7000 + ASSERT_TRUE(TestEventListenersAccessor::EventForwardingEnabled(listeners)); 1.7001 + TestEventListenersAccessor::SuppressEventForwarding(&listeners); 1.7002 + ASSERT_FALSE(TestEventListenersAccessor::EventForwardingEnabled(listeners)); 1.7003 + TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( 1.7004 + *UnitTest::GetInstance()); 1.7005 + EXPECT_EQ(0, on_start_counter); 1.7006 +} 1.7007 + 1.7008 +// Tests that events generated by Google Test are not forwarded in 1.7009 +// death test subprocesses. 1.7010 +TEST(EventListenerDeathTest, EventsNotForwardedInDeathTestSubprecesses) { 1.7011 + EXPECT_DEATH_IF_SUPPORTED({ 1.7012 + GTEST_CHECK_(TestEventListenersAccessor::EventForwardingEnabled( 1.7013 + *GetUnitTestImpl()->listeners())) << "expected failure";}, 1.7014 + "expected failure"); 1.7015 +} 1.7016 + 1.7017 +// Tests that a listener installed via SetDefaultResultPrinter() starts 1.7018 +// receiving events and is returned via default_result_printer() and that 1.7019 +// the previous default_result_printer is removed from the list and deleted. 1.7020 +TEST(EventListenerTest, default_result_printer) { 1.7021 + int on_start_counter = 0; 1.7022 + bool is_destroyed = false; 1.7023 + TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); 1.7024 + 1.7025 + TestEventListeners listeners; 1.7026 + TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, listener); 1.7027 + 1.7028 + EXPECT_EQ(listener, listeners.default_result_printer()); 1.7029 + 1.7030 + TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( 1.7031 + *UnitTest::GetInstance()); 1.7032 + 1.7033 + EXPECT_EQ(1, on_start_counter); 1.7034 + 1.7035 + // Replacing default_result_printer with something else should remove it 1.7036 + // from the list and destroy it. 1.7037 + TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, NULL); 1.7038 + 1.7039 + EXPECT_TRUE(listeners.default_result_printer() == NULL); 1.7040 + EXPECT_TRUE(is_destroyed); 1.7041 + 1.7042 + // After broadcasting an event the counter is still the same, indicating 1.7043 + // the listener is not in the list anymore. 1.7044 + TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( 1.7045 + *UnitTest::GetInstance()); 1.7046 + EXPECT_EQ(1, on_start_counter); 1.7047 +} 1.7048 + 1.7049 +// Tests that the default_result_printer listener stops receiving events 1.7050 +// when removed via Release and that is not owned by the list anymore. 1.7051 +TEST(EventListenerTest, RemovingDefaultResultPrinterWorks) { 1.7052 + int on_start_counter = 0; 1.7053 + bool is_destroyed = false; 1.7054 + // Although Append passes the ownership of this object to the list, 1.7055 + // the following calls release it, and we need to delete it before the 1.7056 + // test ends. 1.7057 + TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); 1.7058 + { 1.7059 + TestEventListeners listeners; 1.7060 + TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, listener); 1.7061 + 1.7062 + EXPECT_EQ(listener, listeners.Release(listener)); 1.7063 + EXPECT_TRUE(listeners.default_result_printer() == NULL); 1.7064 + EXPECT_FALSE(is_destroyed); 1.7065 + 1.7066 + // Broadcasting events now should not affect default_result_printer. 1.7067 + TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( 1.7068 + *UnitTest::GetInstance()); 1.7069 + EXPECT_EQ(0, on_start_counter); 1.7070 + } 1.7071 + // Destroying the list should not affect the listener now, too. 1.7072 + EXPECT_FALSE(is_destroyed); 1.7073 + delete listener; 1.7074 +} 1.7075 + 1.7076 +// Tests that a listener installed via SetDefaultXmlGenerator() starts 1.7077 +// receiving events and is returned via default_xml_generator() and that 1.7078 +// the previous default_xml_generator is removed from the list and deleted. 1.7079 +TEST(EventListenerTest, default_xml_generator) { 1.7080 + int on_start_counter = 0; 1.7081 + bool is_destroyed = false; 1.7082 + TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); 1.7083 + 1.7084 + TestEventListeners listeners; 1.7085 + TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, listener); 1.7086 + 1.7087 + EXPECT_EQ(listener, listeners.default_xml_generator()); 1.7088 + 1.7089 + TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( 1.7090 + *UnitTest::GetInstance()); 1.7091 + 1.7092 + EXPECT_EQ(1, on_start_counter); 1.7093 + 1.7094 + // Replacing default_xml_generator with something else should remove it 1.7095 + // from the list and destroy it. 1.7096 + TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, NULL); 1.7097 + 1.7098 + EXPECT_TRUE(listeners.default_xml_generator() == NULL); 1.7099 + EXPECT_TRUE(is_destroyed); 1.7100 + 1.7101 + // After broadcasting an event the counter is still the same, indicating 1.7102 + // the listener is not in the list anymore. 1.7103 + TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( 1.7104 + *UnitTest::GetInstance()); 1.7105 + EXPECT_EQ(1, on_start_counter); 1.7106 +} 1.7107 + 1.7108 +// Tests that the default_xml_generator listener stops receiving events 1.7109 +// when removed via Release and that is not owned by the list anymore. 1.7110 +TEST(EventListenerTest, RemovingDefaultXmlGeneratorWorks) { 1.7111 + int on_start_counter = 0; 1.7112 + bool is_destroyed = false; 1.7113 + // Although Append passes the ownership of this object to the list, 1.7114 + // the following calls release it, and we need to delete it before the 1.7115 + // test ends. 1.7116 + TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); 1.7117 + { 1.7118 + TestEventListeners listeners; 1.7119 + TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, listener); 1.7120 + 1.7121 + EXPECT_EQ(listener, listeners.Release(listener)); 1.7122 + EXPECT_TRUE(listeners.default_xml_generator() == NULL); 1.7123 + EXPECT_FALSE(is_destroyed); 1.7124 + 1.7125 + // Broadcasting events now should not affect default_xml_generator. 1.7126 + TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( 1.7127 + *UnitTest::GetInstance()); 1.7128 + EXPECT_EQ(0, on_start_counter); 1.7129 + } 1.7130 + // Destroying the list should not affect the listener now, too. 1.7131 + EXPECT_FALSE(is_destroyed); 1.7132 + delete listener; 1.7133 +} 1.7134 + 1.7135 +// Sanity tests to ensure that the alternative, verbose spellings of 1.7136 +// some of the macros work. We don't test them thoroughly as that 1.7137 +// would be quite involved. Since their implementations are 1.7138 +// straightforward, and they are rarely used, we'll just rely on the 1.7139 +// users to tell us when they are broken. 1.7140 +GTEST_TEST(AlternativeNameTest, Works) { // GTEST_TEST is the same as TEST. 1.7141 + GTEST_SUCCEED() << "OK"; // GTEST_SUCCEED is the same as SUCCEED. 1.7142 + 1.7143 + // GTEST_FAIL is the same as FAIL. 1.7144 + EXPECT_FATAL_FAILURE(GTEST_FAIL() << "An expected failure", 1.7145 + "An expected failure"); 1.7146 + 1.7147 + // GTEST_ASSERT_XY is the same as ASSERT_XY. 1.7148 + 1.7149 + GTEST_ASSERT_EQ(0, 0); 1.7150 + EXPECT_FATAL_FAILURE(GTEST_ASSERT_EQ(0, 1) << "An expected failure", 1.7151 + "An expected failure"); 1.7152 + EXPECT_FATAL_FAILURE(GTEST_ASSERT_EQ(1, 0) << "An expected failure", 1.7153 + "An expected failure"); 1.7154 + 1.7155 + GTEST_ASSERT_NE(0, 1); 1.7156 + GTEST_ASSERT_NE(1, 0); 1.7157 + EXPECT_FATAL_FAILURE(GTEST_ASSERT_NE(0, 0) << "An expected failure", 1.7158 + "An expected failure"); 1.7159 + 1.7160 + GTEST_ASSERT_LE(0, 0); 1.7161 + GTEST_ASSERT_LE(0, 1); 1.7162 + EXPECT_FATAL_FAILURE(GTEST_ASSERT_LE(1, 0) << "An expected failure", 1.7163 + "An expected failure"); 1.7164 + 1.7165 + GTEST_ASSERT_LT(0, 1); 1.7166 + EXPECT_FATAL_FAILURE(GTEST_ASSERT_LT(0, 0) << "An expected failure", 1.7167 + "An expected failure"); 1.7168 + EXPECT_FATAL_FAILURE(GTEST_ASSERT_LT(1, 0) << "An expected failure", 1.7169 + "An expected failure"); 1.7170 + 1.7171 + GTEST_ASSERT_GE(0, 0); 1.7172 + GTEST_ASSERT_GE(1, 0); 1.7173 + EXPECT_FATAL_FAILURE(GTEST_ASSERT_GE(0, 1) << "An expected failure", 1.7174 + "An expected failure"); 1.7175 + 1.7176 + GTEST_ASSERT_GT(1, 0); 1.7177 + EXPECT_FATAL_FAILURE(GTEST_ASSERT_GT(0, 1) << "An expected failure", 1.7178 + "An expected failure"); 1.7179 + EXPECT_FATAL_FAILURE(GTEST_ASSERT_GT(1, 1) << "An expected failure", 1.7180 + "An expected failure"); 1.7181 +} 1.7182 + 1.7183 +// Tests for internal utilities necessary for implementation of the universal 1.7184 +// printing. 1.7185 +// TODO(vladl@google.com): Find a better home for them. 1.7186 + 1.7187 +class ConversionHelperBase {}; 1.7188 +class ConversionHelperDerived : public ConversionHelperBase {}; 1.7189 + 1.7190 +// Tests that IsAProtocolMessage<T>::value is a compile-time constant. 1.7191 +TEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) { 1.7192 + GTEST_COMPILE_ASSERT_(IsAProtocolMessage<ProtocolMessage>::value, 1.7193 + const_true); 1.7194 + GTEST_COMPILE_ASSERT_(!IsAProtocolMessage<int>::value, const_false); 1.7195 +} 1.7196 + 1.7197 +// Tests that IsAProtocolMessage<T>::value is true when T is 1.7198 +// proto2::Message or a sub-class of it. 1.7199 +TEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) { 1.7200 + EXPECT_TRUE(IsAProtocolMessage< ::proto2::Message>::value); 1.7201 + EXPECT_TRUE(IsAProtocolMessage<ProtocolMessage>::value); 1.7202 +} 1.7203 + 1.7204 +// Tests that IsAProtocolMessage<T>::value is false when T is neither 1.7205 +// ProtocolMessage nor a sub-class of it. 1.7206 +TEST(IsAProtocolMessageTest, ValueIsFalseWhenTypeIsNotAProtocolMessage) { 1.7207 + EXPECT_FALSE(IsAProtocolMessage<int>::value); 1.7208 + EXPECT_FALSE(IsAProtocolMessage<const ConversionHelperBase>::value); 1.7209 +} 1.7210 + 1.7211 +// Tests that CompileAssertTypesEqual compiles when the type arguments are 1.7212 +// equal. 1.7213 +TEST(CompileAssertTypesEqual, CompilesWhenTypesAreEqual) { 1.7214 + CompileAssertTypesEqual<void, void>(); 1.7215 + CompileAssertTypesEqual<int*, int*>(); 1.7216 +} 1.7217 + 1.7218 +// Tests that RemoveReference does not affect non-reference types. 1.7219 +TEST(RemoveReferenceTest, DoesNotAffectNonReferenceType) { 1.7220 + CompileAssertTypesEqual<int, RemoveReference<int>::type>(); 1.7221 + CompileAssertTypesEqual<const char, RemoveReference<const char>::type>(); 1.7222 +} 1.7223 + 1.7224 +// Tests that RemoveReference removes reference from reference types. 1.7225 +TEST(RemoveReferenceTest, RemovesReference) { 1.7226 + CompileAssertTypesEqual<int, RemoveReference<int&>::type>(); 1.7227 + CompileAssertTypesEqual<const char, RemoveReference<const char&>::type>(); 1.7228 +} 1.7229 + 1.7230 +// Tests GTEST_REMOVE_REFERENCE_. 1.7231 + 1.7232 +template <typename T1, typename T2> 1.7233 +void TestGTestRemoveReference() { 1.7234 + CompileAssertTypesEqual<T1, GTEST_REMOVE_REFERENCE_(T2)>(); 1.7235 +} 1.7236 + 1.7237 +TEST(RemoveReferenceTest, MacroVersion) { 1.7238 + TestGTestRemoveReference<int, int>(); 1.7239 + TestGTestRemoveReference<const char, const char&>(); 1.7240 +} 1.7241 + 1.7242 + 1.7243 +// Tests that RemoveConst does not affect non-const types. 1.7244 +TEST(RemoveConstTest, DoesNotAffectNonConstType) { 1.7245 + CompileAssertTypesEqual<int, RemoveConst<int>::type>(); 1.7246 + CompileAssertTypesEqual<char&, RemoveConst<char&>::type>(); 1.7247 +} 1.7248 + 1.7249 +// Tests that RemoveConst removes const from const types. 1.7250 +TEST(RemoveConstTest, RemovesConst) { 1.7251 + CompileAssertTypesEqual<int, RemoveConst<const int>::type>(); 1.7252 + CompileAssertTypesEqual<char[2], RemoveConst<const char[2]>::type>(); 1.7253 + CompileAssertTypesEqual<char[2][3], RemoveConst<const char[2][3]>::type>(); 1.7254 +} 1.7255 + 1.7256 +// Tests GTEST_REMOVE_CONST_. 1.7257 + 1.7258 +template <typename T1, typename T2> 1.7259 +void TestGTestRemoveConst() { 1.7260 + CompileAssertTypesEqual<T1, GTEST_REMOVE_CONST_(T2)>(); 1.7261 +} 1.7262 + 1.7263 +TEST(RemoveConstTest, MacroVersion) { 1.7264 + TestGTestRemoveConst<int, int>(); 1.7265 + TestGTestRemoveConst<double&, double&>(); 1.7266 + TestGTestRemoveConst<char, const char>(); 1.7267 +} 1.7268 + 1.7269 +// Tests GTEST_REMOVE_REFERENCE_AND_CONST_. 1.7270 + 1.7271 +template <typename T1, typename T2> 1.7272 +void TestGTestRemoveReferenceAndConst() { 1.7273 + CompileAssertTypesEqual<T1, GTEST_REMOVE_REFERENCE_AND_CONST_(T2)>(); 1.7274 +} 1.7275 + 1.7276 +TEST(RemoveReferenceToConstTest, Works) { 1.7277 + TestGTestRemoveReferenceAndConst<int, int>(); 1.7278 + TestGTestRemoveReferenceAndConst<double, double&>(); 1.7279 + TestGTestRemoveReferenceAndConst<char, const char>(); 1.7280 + TestGTestRemoveReferenceAndConst<char, const char&>(); 1.7281 + TestGTestRemoveReferenceAndConst<const char*, const char*>(); 1.7282 +} 1.7283 + 1.7284 +// Tests that AddReference does not affect reference types. 1.7285 +TEST(AddReferenceTest, DoesNotAffectReferenceType) { 1.7286 + CompileAssertTypesEqual<int&, AddReference<int&>::type>(); 1.7287 + CompileAssertTypesEqual<const char&, AddReference<const char&>::type>(); 1.7288 +} 1.7289 + 1.7290 +// Tests that AddReference adds reference to non-reference types. 1.7291 +TEST(AddReferenceTest, AddsReference) { 1.7292 + CompileAssertTypesEqual<int&, AddReference<int>::type>(); 1.7293 + CompileAssertTypesEqual<const char&, AddReference<const char>::type>(); 1.7294 +} 1.7295 + 1.7296 +// Tests GTEST_ADD_REFERENCE_. 1.7297 + 1.7298 +template <typename T1, typename T2> 1.7299 +void TestGTestAddReference() { 1.7300 + CompileAssertTypesEqual<T1, GTEST_ADD_REFERENCE_(T2)>(); 1.7301 +} 1.7302 + 1.7303 +TEST(AddReferenceTest, MacroVersion) { 1.7304 + TestGTestAddReference<int&, int>(); 1.7305 + TestGTestAddReference<const char&, const char&>(); 1.7306 +} 1.7307 + 1.7308 +// Tests GTEST_REFERENCE_TO_CONST_. 1.7309 + 1.7310 +template <typename T1, typename T2> 1.7311 +void TestGTestReferenceToConst() { 1.7312 + CompileAssertTypesEqual<T1, GTEST_REFERENCE_TO_CONST_(T2)>(); 1.7313 +} 1.7314 + 1.7315 +TEST(GTestReferenceToConstTest, Works) { 1.7316 + TestGTestReferenceToConst<const char&, char>(); 1.7317 + TestGTestReferenceToConst<const int&, const int>(); 1.7318 + TestGTestReferenceToConst<const double&, double>(); 1.7319 + TestGTestReferenceToConst<const String&, const String&>(); 1.7320 +} 1.7321 + 1.7322 +// Tests that ImplicitlyConvertible<T1, T2>::value is a compile-time constant. 1.7323 +TEST(ImplicitlyConvertibleTest, ValueIsCompileTimeConstant) { 1.7324 + GTEST_COMPILE_ASSERT_((ImplicitlyConvertible<int, int>::value), const_true); 1.7325 + GTEST_COMPILE_ASSERT_((!ImplicitlyConvertible<void*, int*>::value), 1.7326 + const_false); 1.7327 +} 1.7328 + 1.7329 +// Tests that ImplicitlyConvertible<T1, T2>::value is true when T1 can 1.7330 +// be implicitly converted to T2. 1.7331 +TEST(ImplicitlyConvertibleTest, ValueIsTrueWhenConvertible) { 1.7332 + EXPECT_TRUE((ImplicitlyConvertible<int, double>::value)); 1.7333 + EXPECT_TRUE((ImplicitlyConvertible<double, int>::value)); 1.7334 + EXPECT_TRUE((ImplicitlyConvertible<int*, void*>::value)); 1.7335 + EXPECT_TRUE((ImplicitlyConvertible<int*, const int*>::value)); 1.7336 + EXPECT_TRUE((ImplicitlyConvertible<ConversionHelperDerived&, 1.7337 + const ConversionHelperBase&>::value)); 1.7338 + EXPECT_TRUE((ImplicitlyConvertible<const ConversionHelperBase, 1.7339 + ConversionHelperBase>::value)); 1.7340 +} 1.7341 + 1.7342 +// Tests that ImplicitlyConvertible<T1, T2>::value is false when T1 1.7343 +// cannot be implicitly converted to T2. 1.7344 +TEST(ImplicitlyConvertibleTest, ValueIsFalseWhenNotConvertible) { 1.7345 + EXPECT_FALSE((ImplicitlyConvertible<double, int*>::value)); 1.7346 + EXPECT_FALSE((ImplicitlyConvertible<void*, int*>::value)); 1.7347 + EXPECT_FALSE((ImplicitlyConvertible<const int*, int*>::value)); 1.7348 + EXPECT_FALSE((ImplicitlyConvertible<ConversionHelperBase&, 1.7349 + ConversionHelperDerived&>::value)); 1.7350 +} 1.7351 + 1.7352 +// Tests IsContainerTest. 1.7353 + 1.7354 +class NonContainer {}; 1.7355 + 1.7356 +TEST(IsContainerTestTest, WorksForNonContainer) { 1.7357 + EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<int>(0))); 1.7358 + EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<char[5]>(0))); 1.7359 + EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<NonContainer>(0))); 1.7360 +} 1.7361 + 1.7362 +TEST(IsContainerTestTest, WorksForContainer) { 1.7363 + EXPECT_EQ(sizeof(IsContainer), 1.7364 + sizeof(IsContainerTest<std::vector<bool> >(0))); 1.7365 + EXPECT_EQ(sizeof(IsContainer), 1.7366 + sizeof(IsContainerTest<std::map<int, double> >(0))); 1.7367 +} 1.7368 + 1.7369 +// Tests ArrayEq(). 1.7370 + 1.7371 +TEST(ArrayEqTest, WorksForDegeneratedArrays) { 1.7372 + EXPECT_TRUE(ArrayEq(5, 5L)); 1.7373 + EXPECT_FALSE(ArrayEq('a', 0)); 1.7374 +} 1.7375 + 1.7376 +TEST(ArrayEqTest, WorksForOneDimensionalArrays) { 1.7377 + // Note that a and b are distinct but compatible types. 1.7378 + const int a[] = { 0, 1 }; 1.7379 + long b[] = { 0, 1 }; 1.7380 + EXPECT_TRUE(ArrayEq(a, b)); 1.7381 + EXPECT_TRUE(ArrayEq(a, 2, b)); 1.7382 + 1.7383 + b[0] = 2; 1.7384 + EXPECT_FALSE(ArrayEq(a, b)); 1.7385 + EXPECT_FALSE(ArrayEq(a, 1, b)); 1.7386 +} 1.7387 + 1.7388 +TEST(ArrayEqTest, WorksForTwoDimensionalArrays) { 1.7389 + const char a[][3] = { "hi", "lo" }; 1.7390 + const char b[][3] = { "hi", "lo" }; 1.7391 + const char c[][3] = { "hi", "li" }; 1.7392 + 1.7393 + EXPECT_TRUE(ArrayEq(a, b)); 1.7394 + EXPECT_TRUE(ArrayEq(a, 2, b)); 1.7395 + 1.7396 + EXPECT_FALSE(ArrayEq(a, c)); 1.7397 + EXPECT_FALSE(ArrayEq(a, 2, c)); 1.7398 +} 1.7399 + 1.7400 +// Tests ArrayAwareFind(). 1.7401 + 1.7402 +TEST(ArrayAwareFindTest, WorksForOneDimensionalArray) { 1.7403 + const char a[] = "hello"; 1.7404 + EXPECT_EQ(a + 4, ArrayAwareFind(a, a + 5, 'o')); 1.7405 + EXPECT_EQ(a + 5, ArrayAwareFind(a, a + 5, 'x')); 1.7406 +} 1.7407 + 1.7408 +TEST(ArrayAwareFindTest, WorksForTwoDimensionalArray) { 1.7409 + int a[][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; 1.7410 + const int b[2] = { 2, 3 }; 1.7411 + EXPECT_EQ(a + 1, ArrayAwareFind(a, a + 3, b)); 1.7412 + 1.7413 + const int c[2] = { 6, 7 }; 1.7414 + EXPECT_EQ(a + 3, ArrayAwareFind(a, a + 3, c)); 1.7415 +} 1.7416 + 1.7417 +// Tests CopyArray(). 1.7418 + 1.7419 +TEST(CopyArrayTest, WorksForDegeneratedArrays) { 1.7420 + int n = 0; 1.7421 + CopyArray('a', &n); 1.7422 + EXPECT_EQ('a', n); 1.7423 +} 1.7424 + 1.7425 +TEST(CopyArrayTest, WorksForOneDimensionalArrays) { 1.7426 + const char a[3] = "hi"; 1.7427 + int b[3]; 1.7428 +#ifndef __BORLANDC__ // C++Builder cannot compile some array size deductions. 1.7429 + CopyArray(a, &b); 1.7430 + EXPECT_TRUE(ArrayEq(a, b)); 1.7431 +#endif 1.7432 + 1.7433 + int c[3]; 1.7434 + CopyArray(a, 3, c); 1.7435 + EXPECT_TRUE(ArrayEq(a, c)); 1.7436 +} 1.7437 + 1.7438 +TEST(CopyArrayTest, WorksForTwoDimensionalArrays) { 1.7439 + const int a[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } }; 1.7440 + int b[2][3]; 1.7441 +#ifndef __BORLANDC__ // C++Builder cannot compile some array size deductions. 1.7442 + CopyArray(a, &b); 1.7443 + EXPECT_TRUE(ArrayEq(a, b)); 1.7444 +#endif 1.7445 + 1.7446 + int c[2][3]; 1.7447 + CopyArray(a, 2, c); 1.7448 + EXPECT_TRUE(ArrayEq(a, c)); 1.7449 +} 1.7450 + 1.7451 +// Tests NativeArray. 1.7452 + 1.7453 +TEST(NativeArrayTest, ConstructorFromArrayWorks) { 1.7454 + const int a[3] = { 0, 1, 2 }; 1.7455 + NativeArray<int> na(a, 3, kReference); 1.7456 + EXPECT_EQ(3U, na.size()); 1.7457 + EXPECT_EQ(a, na.begin()); 1.7458 +} 1.7459 + 1.7460 +TEST(NativeArrayTest, CreatesAndDeletesCopyOfArrayWhenAskedTo) { 1.7461 + typedef int Array[2]; 1.7462 + Array* a = new Array[1]; 1.7463 + (*a)[0] = 0; 1.7464 + (*a)[1] = 1; 1.7465 + NativeArray<int> na(*a, 2, kCopy); 1.7466 + EXPECT_NE(*a, na.begin()); 1.7467 + delete[] a; 1.7468 + EXPECT_EQ(0, na.begin()[0]); 1.7469 + EXPECT_EQ(1, na.begin()[1]); 1.7470 + 1.7471 + // We rely on the heap checker to verify that na deletes the copy of 1.7472 + // array. 1.7473 +} 1.7474 + 1.7475 +TEST(NativeArrayTest, TypeMembersAreCorrect) { 1.7476 + StaticAssertTypeEq<char, NativeArray<char>::value_type>(); 1.7477 + StaticAssertTypeEq<int[2], NativeArray<int[2]>::value_type>(); 1.7478 + 1.7479 + StaticAssertTypeEq<const char*, NativeArray<char>::const_iterator>(); 1.7480 + StaticAssertTypeEq<const bool(*)[2], NativeArray<bool[2]>::const_iterator>(); 1.7481 +} 1.7482 + 1.7483 +TEST(NativeArrayTest, MethodsWork) { 1.7484 + const int a[3] = { 0, 1, 2 }; 1.7485 + NativeArray<int> na(a, 3, kCopy); 1.7486 + ASSERT_EQ(3U, na.size()); 1.7487 + EXPECT_EQ(3, na.end() - na.begin()); 1.7488 + 1.7489 + NativeArray<int>::const_iterator it = na.begin(); 1.7490 + EXPECT_EQ(0, *it); 1.7491 + ++it; 1.7492 + EXPECT_EQ(1, *it); 1.7493 + it++; 1.7494 + EXPECT_EQ(2, *it); 1.7495 + ++it; 1.7496 + EXPECT_EQ(na.end(), it); 1.7497 + 1.7498 + EXPECT_TRUE(na == na); 1.7499 + 1.7500 + NativeArray<int> na2(a, 3, kReference); 1.7501 + EXPECT_TRUE(na == na2); 1.7502 + 1.7503 + const int b1[3] = { 0, 1, 1 }; 1.7504 + const int b2[4] = { 0, 1, 2, 3 }; 1.7505 + EXPECT_FALSE(na == NativeArray<int>(b1, 3, kReference)); 1.7506 + EXPECT_FALSE(na == NativeArray<int>(b2, 4, kCopy)); 1.7507 +} 1.7508 + 1.7509 +TEST(NativeArrayTest, WorksForTwoDimensionalArray) { 1.7510 + const char a[2][3] = { "hi", "lo" }; 1.7511 + NativeArray<char[3]> na(a, 2, kReference); 1.7512 + ASSERT_EQ(2U, na.size()); 1.7513 + EXPECT_EQ(a, na.begin()); 1.7514 +} 1.7515 + 1.7516 +// Tests SkipPrefix(). 1.7517 + 1.7518 +TEST(SkipPrefixTest, SkipsWhenPrefixMatches) { 1.7519 + const char* const str = "hello"; 1.7520 + 1.7521 + const char* p = str; 1.7522 + EXPECT_TRUE(SkipPrefix("", &p)); 1.7523 + EXPECT_EQ(str, p); 1.7524 + 1.7525 + p = str; 1.7526 + EXPECT_TRUE(SkipPrefix("hell", &p)); 1.7527 + EXPECT_EQ(str + 4, p); 1.7528 +} 1.7529 + 1.7530 +TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) { 1.7531 + const char* const str = "world"; 1.7532 + 1.7533 + const char* p = str; 1.7534 + EXPECT_FALSE(SkipPrefix("W", &p)); 1.7535 + EXPECT_EQ(str, p); 1.7536 + 1.7537 + p = str; 1.7538 + EXPECT_FALSE(SkipPrefix("world!", &p)); 1.7539 + EXPECT_EQ(str, p); 1.7540 +}