shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1 | // Copyright 2005, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | // |
| 32 | // Tests for Google Test itself. This verifies that the basic constructs of |
| 33 | // Google Test work. |
| 34 | |
| 35 | #include <gtest/gtest.h> |
zhanyong.wan | 0ebc16a | 2009-02-02 06:37:03 +0000 | [diff] [blame] | 36 | |
| 37 | // Verifies that the command line flag variables can be accessed |
| 38 | // in code once <gtest/gtest.h> has been #included. |
| 39 | // Do not move it after other #includes. |
| 40 | TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) { |
| 41 | bool dummy = testing::GTEST_FLAG(also_run_disabled_tests) |
| 42 | || testing::GTEST_FLAG(break_on_failure) |
| 43 | || testing::GTEST_FLAG(catch_exceptions) |
| 44 | || testing::GTEST_FLAG(color) != "unknown" |
| 45 | || testing::GTEST_FLAG(filter) != "unknown" |
| 46 | || testing::GTEST_FLAG(list_tests) |
| 47 | || testing::GTEST_FLAG(output) != "unknown" |
| 48 | || testing::GTEST_FLAG(print_time) |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 49 | || testing::GTEST_FLAG(random_seed) |
zhanyong.wan | 0ebc16a | 2009-02-02 06:37:03 +0000 | [diff] [blame] | 50 | || testing::GTEST_FLAG(repeat) > 0 |
| 51 | || testing::GTEST_FLAG(show_internal_stack_frames) |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 52 | || testing::GTEST_FLAG(shuffle) |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 53 | || testing::GTEST_FLAG(stack_trace_depth) > 0 |
| 54 | || testing::GTEST_FLAG(throw_on_failure); |
zhanyong.wan | 0ebc16a | 2009-02-02 06:37:03 +0000 | [diff] [blame] | 55 | EXPECT_TRUE(dummy || !dummy); // Suppresses warning that dummy is unused. |
| 56 | } |
| 57 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 58 | #include <gtest/gtest-spi.h> |
| 59 | |
| 60 | // Indicates that this translation unit is part of Google Test's |
| 61 | // implementation. It must come before gtest-internal-inl.h is |
| 62 | // included, or there will be a compiler error. This trick is to |
| 63 | // prevent a user from accidentally including gtest-internal-inl.h in |
| 64 | // his code. |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 65 | #define GTEST_IMPLEMENTATION_ 1 |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 66 | #include "src/gtest-internal-inl.h" |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 67 | #undef GTEST_IMPLEMENTATION_ |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 68 | |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 69 | #include <limits.h> // For INT_MAX. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 70 | #include <stdlib.h> |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 71 | #include <time.h> |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 72 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 73 | #if GTEST_HAS_PTHREAD |
| 74 | #include <pthread.h> |
| 75 | #endif // GTEST_HAS_PTHREAD |
| 76 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 77 | #ifdef __BORLANDC__ |
| 78 | #include <map> |
| 79 | #endif |
| 80 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 81 | namespace testing { |
| 82 | namespace internal { |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 83 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 84 | bool ShouldUseColor(bool stdout_is_tty); |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 85 | ::std::string FormatTimeInMillisAsSeconds(TimeInMillis ms); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 86 | bool ParseInt32Flag(const char* str, const char* flag, Int32* value); |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 87 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 88 | // Used for testing the flag parsing. |
| 89 | extern bool g_help_flag; |
| 90 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 91 | // Provides access to otherwise private parts of the TestEventListeners class |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 92 | // that are needed to test it. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 93 | class TestEventListenersAccessor { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 94 | public: |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 95 | static TestEventListener* GetRepeater(TestEventListeners* listeners) { |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 96 | return listeners->repeater(); |
| 97 | } |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 98 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 99 | static void SetDefaultResultPrinter(TestEventListeners* listeners, |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 100 | TestEventListener* listener) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 101 | listeners->SetDefaultResultPrinter(listener); |
| 102 | } |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 103 | static void SetDefaultXmlGenerator(TestEventListeners* listeners, |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 104 | TestEventListener* listener) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 105 | listeners->SetDefaultXmlGenerator(listener); |
| 106 | } |
| 107 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 108 | static bool EventForwardingEnabled(const TestEventListeners& listeners) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 109 | return listeners.EventForwardingEnabled(); |
| 110 | } |
| 111 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 112 | static void SuppressEventForwarding(TestEventListeners* listeners) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 113 | listeners->SuppressEventForwarding(); |
| 114 | } |
| 115 | }; |
| 116 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 117 | } // namespace internal |
| 118 | } // namespace testing |
| 119 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 120 | using testing::AssertionFailure; |
| 121 | using testing::AssertionResult; |
| 122 | using testing::AssertionSuccess; |
| 123 | using testing::DoubleLE; |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 124 | using testing::EmptyTestEventListener; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 125 | using testing::FloatLE; |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 126 | using testing::GTEST_FLAG(also_run_disabled_tests); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 127 | using testing::GTEST_FLAG(break_on_failure); |
| 128 | using testing::GTEST_FLAG(catch_exceptions); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 129 | using testing::GTEST_FLAG(color); |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 130 | using testing::GTEST_FLAG(death_test_use_fork); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 131 | using testing::GTEST_FLAG(filter); |
| 132 | using testing::GTEST_FLAG(list_tests); |
| 133 | using testing::GTEST_FLAG(output); |
| 134 | using testing::GTEST_FLAG(print_time); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 135 | using testing::GTEST_FLAG(random_seed); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 136 | using testing::GTEST_FLAG(repeat); |
| 137 | using testing::GTEST_FLAG(show_internal_stack_frames); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 138 | using testing::GTEST_FLAG(shuffle); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 139 | using testing::GTEST_FLAG(stack_trace_depth); |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 140 | using testing::GTEST_FLAG(throw_on_failure); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 141 | using testing::IsNotSubstring; |
| 142 | using testing::IsSubstring; |
| 143 | using testing::Message; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 144 | using testing::ScopedFakeTestPartResultReporter; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 145 | using testing::StaticAssertTypeEq; |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 146 | using testing::Test; |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 147 | using testing::TestEventListeners; |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 148 | using testing::TestCase; |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 149 | using testing::TestPartResult; |
| 150 | using testing::TestPartResultArray; |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 151 | using testing::TestProperty; |
| 152 | using testing::TestResult; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 153 | using testing::UnitTest; |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 154 | using testing::kMaxStackTraceDepth; |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 155 | using testing::internal::AlwaysFalse; |
| 156 | using testing::internal::AlwaysTrue; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 157 | using testing::internal::AppendUserMessage; |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 158 | using testing::internal::CodePointToUtf8; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 159 | using testing::internal::EqFailure; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 160 | using testing::internal::FloatingPoint; |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 161 | using testing::internal::FormatTimeInMillisAsSeconds; |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 162 | using testing::internal::GTestFlagSaver; |
vladlosev | f904a61 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 163 | using testing::internal::GetCurrentOsStackTraceExceptTop; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 164 | using testing::internal::GetNextRandomSeed; |
| 165 | using testing::internal::GetRandomSeedFromFlag; |
shiqian | fe6a9a4 | 2008-11-24 20:13:22 +0000 | [diff] [blame] | 166 | using testing::internal::GetTestTypeId; |
| 167 | using testing::internal::GetTypeId; |
zhanyong.wan | a80f23f | 2009-06-25 20:49:23 +0000 | [diff] [blame] | 168 | using testing::internal::GetUnitTestImpl; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 169 | using testing::internal::Int32; |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 170 | using testing::internal::Int32FromEnvOrDie; |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 171 | using testing::internal::ParseInt32Flag; |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 172 | using testing::internal::ShouldRunTestOnShard; |
| 173 | using testing::internal::ShouldShard; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 174 | using testing::internal::ShouldUseColor; |
| 175 | using testing::internal::StreamableToString; |
| 176 | using testing::internal::String; |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 177 | using testing::internal::TestEventListenersAccessor; |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 178 | using testing::internal::TestResultAccessor; |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 179 | using testing::internal::ThreadLocal; |
zhanyong.wan | 85f555a | 2009-09-21 19:42:03 +0000 | [diff] [blame] | 180 | using testing::internal::UInt32; |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 181 | using testing::internal::Vector; |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 182 | using testing::internal::WideStringToUtf8; |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 183 | using testing::internal::kMaxRandomSeed; |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 184 | using testing::internal::kTestTypeIdInGoogleTest; |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 185 | using testing::internal::scoped_ptr; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 186 | |
zhanyong.wan | cf8a584 | 2010-01-28 21:50:29 +0000 | [diff] [blame^] | 187 | #if GTEST_HAS_STREAM_REDIRECTION_ |
| 188 | using testing::internal::CaptureStdout; |
| 189 | using testing::internal::GetCapturedStdout; |
| 190 | #endif // GTEST_HAS_STREAM_REDIRECTION_ |
| 191 | |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 192 | class TestingVector : public Vector<int> { |
| 193 | }; |
| 194 | |
| 195 | ::std::ostream& operator<<(::std::ostream& os, |
| 196 | const TestingVector& vector) { |
| 197 | os << "{ "; |
| 198 | for (int i = 0; i < vector.size(); i++) { |
| 199 | os << vector.GetElement(i) << " "; |
| 200 | } |
| 201 | os << "}"; |
| 202 | return os; |
| 203 | } |
| 204 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 205 | // This line tests that we can define tests in an unnamed namespace. |
| 206 | namespace { |
| 207 | |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 208 | TEST(GetRandomSeedFromFlagTest, HandlesZero) { |
| 209 | const int seed = GetRandomSeedFromFlag(0); |
| 210 | EXPECT_LE(1, seed); |
| 211 | EXPECT_LE(seed, static_cast<int>(kMaxRandomSeed)); |
| 212 | } |
| 213 | |
| 214 | TEST(GetRandomSeedFromFlagTest, PreservesValidSeed) { |
| 215 | EXPECT_EQ(1, GetRandomSeedFromFlag(1)); |
| 216 | EXPECT_EQ(2, GetRandomSeedFromFlag(2)); |
| 217 | EXPECT_EQ(kMaxRandomSeed - 1, GetRandomSeedFromFlag(kMaxRandomSeed - 1)); |
| 218 | EXPECT_EQ(static_cast<int>(kMaxRandomSeed), |
| 219 | GetRandomSeedFromFlag(kMaxRandomSeed)); |
| 220 | } |
| 221 | |
| 222 | TEST(GetRandomSeedFromFlagTest, NormalizesInvalidSeed) { |
| 223 | const int seed1 = GetRandomSeedFromFlag(-1); |
| 224 | EXPECT_LE(1, seed1); |
| 225 | EXPECT_LE(seed1, static_cast<int>(kMaxRandomSeed)); |
| 226 | |
| 227 | const int seed2 = GetRandomSeedFromFlag(kMaxRandomSeed + 1); |
| 228 | EXPECT_LE(1, seed2); |
| 229 | EXPECT_LE(seed2, static_cast<int>(kMaxRandomSeed)); |
| 230 | } |
| 231 | |
| 232 | TEST(GetNextRandomSeedTest, WorksForValidInput) { |
| 233 | EXPECT_EQ(2, GetNextRandomSeed(1)); |
| 234 | EXPECT_EQ(3, GetNextRandomSeed(2)); |
| 235 | EXPECT_EQ(static_cast<int>(kMaxRandomSeed), |
| 236 | GetNextRandomSeed(kMaxRandomSeed - 1)); |
| 237 | EXPECT_EQ(1, GetNextRandomSeed(kMaxRandomSeed)); |
| 238 | |
| 239 | // We deliberately don't test GetNextRandomSeed() with invalid |
| 240 | // inputs, as that requires death tests, which are expensive. This |
| 241 | // is fine as GetNextRandomSeed() is internal and has a |
| 242 | // straightforward definition. |
| 243 | } |
| 244 | |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 245 | static void ClearCurrentTestPartResults() { |
| 246 | TestResultAccessor::ClearTestPartResults( |
| 247 | GetUnitTestImpl()->current_test_result()); |
| 248 | } |
| 249 | |
shiqian | fe6a9a4 | 2008-11-24 20:13:22 +0000 | [diff] [blame] | 250 | // Tests GetTypeId. |
| 251 | |
| 252 | TEST(GetTypeIdTest, ReturnsSameValueForSameType) { |
| 253 | EXPECT_EQ(GetTypeId<int>(), GetTypeId<int>()); |
| 254 | EXPECT_EQ(GetTypeId<Test>(), GetTypeId<Test>()); |
| 255 | } |
| 256 | |
| 257 | class SubClassOfTest : public Test {}; |
| 258 | class AnotherSubClassOfTest : public Test {}; |
| 259 | |
| 260 | TEST(GetTypeIdTest, ReturnsDifferentValuesForDifferentTypes) { |
| 261 | EXPECT_NE(GetTypeId<int>(), GetTypeId<const int>()); |
| 262 | EXPECT_NE(GetTypeId<int>(), GetTypeId<char>()); |
| 263 | EXPECT_NE(GetTypeId<int>(), GetTestTypeId()); |
| 264 | EXPECT_NE(GetTypeId<SubClassOfTest>(), GetTestTypeId()); |
| 265 | EXPECT_NE(GetTypeId<AnotherSubClassOfTest>(), GetTestTypeId()); |
| 266 | EXPECT_NE(GetTypeId<AnotherSubClassOfTest>(), GetTypeId<SubClassOfTest>()); |
| 267 | } |
| 268 | |
| 269 | // Verifies that GetTestTypeId() returns the same value, no matter it |
| 270 | // is called from inside Google Test or outside of it. |
| 271 | TEST(GetTestTypeIdTest, ReturnsTheSameValueInsideOrOutsideOfGoogleTest) { |
| 272 | EXPECT_EQ(kTestTypeIdInGoogleTest, GetTestTypeId()); |
| 273 | } |
| 274 | |
shiqian | f0e809a | 2008-09-26 16:08:30 +0000 | [diff] [blame] | 275 | // Tests FormatTimeInMillisAsSeconds(). |
| 276 | |
| 277 | TEST(FormatTimeInMillisAsSecondsTest, FormatsZero) { |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 278 | EXPECT_EQ("0", FormatTimeInMillisAsSeconds(0)); |
shiqian | f0e809a | 2008-09-26 16:08:30 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | TEST(FormatTimeInMillisAsSecondsTest, FormatsPositiveNumber) { |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 282 | EXPECT_EQ("0.003", FormatTimeInMillisAsSeconds(3)); |
| 283 | EXPECT_EQ("0.01", FormatTimeInMillisAsSeconds(10)); |
| 284 | EXPECT_EQ("0.2", FormatTimeInMillisAsSeconds(200)); |
| 285 | EXPECT_EQ("1.2", FormatTimeInMillisAsSeconds(1200)); |
| 286 | EXPECT_EQ("3", FormatTimeInMillisAsSeconds(3000)); |
shiqian | f0e809a | 2008-09-26 16:08:30 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | TEST(FormatTimeInMillisAsSecondsTest, FormatsNegativeNumber) { |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 290 | EXPECT_EQ("-0.003", FormatTimeInMillisAsSeconds(-3)); |
| 291 | EXPECT_EQ("-0.01", FormatTimeInMillisAsSeconds(-10)); |
| 292 | EXPECT_EQ("-0.2", FormatTimeInMillisAsSeconds(-200)); |
| 293 | EXPECT_EQ("-1.2", FormatTimeInMillisAsSeconds(-1200)); |
| 294 | EXPECT_EQ("-3", FormatTimeInMillisAsSeconds(-3000)); |
shiqian | f0e809a | 2008-09-26 16:08:30 +0000 | [diff] [blame] | 295 | } |
| 296 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 297 | #if !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 298 | // NULL testing does not work with Symbian compilers. |
| 299 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 300 | #ifdef __BORLANDC__ |
| 301 | // Silences warnings: "Condition is always true", "Unreachable code" |
| 302 | #pragma option push -w-ccc -w-rch |
| 303 | #endif |
| 304 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 305 | // Tests that GTEST_IS_NULL_LITERAL_(x) is true when x is a null |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 306 | // pointer literal. |
| 307 | TEST(NullLiteralTest, IsTrueForNullLiterals) { |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 308 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(NULL)); |
| 309 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0)); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 310 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0U)); |
| 311 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0L)); |
| 312 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(false)); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 313 | #ifndef __BORLANDC__ |
| 314 | // Some compilers may fail to detect some null pointer literals; |
| 315 | // as long as users of the framework don't use such literals, this |
| 316 | // is harmless. |
| 317 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(1 - 1)); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 318 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(true && false)); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 319 | #endif |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 320 | } |
| 321 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 322 | // Tests that GTEST_IS_NULL_LITERAL_(x) is false when x is not a null |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 323 | // pointer literal. |
| 324 | TEST(NullLiteralTest, IsFalseForNonNullLiterals) { |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 325 | EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(1)); |
| 326 | EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(0.0)); |
| 327 | EXPECT_FALSE(GTEST_IS_NULL_LITERAL_('a')); |
| 328 | EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(static_cast<void*>(NULL))); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 329 | } |
| 330 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 331 | #ifdef __BORLANDC__ |
| 332 | // Restores warnings after previous "#pragma option push" supressed them |
| 333 | #pragma option pop |
| 334 | #endif |
| 335 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 336 | #endif // !GTEST_OS_SYMBIAN |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 337 | // |
| 338 | // Tests CodePointToUtf8(). |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 339 | |
| 340 | // Tests that the NUL character L'\0' is encoded correctly. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 341 | TEST(CodePointToUtf8Test, CanEncodeNul) { |
| 342 | char buffer[32]; |
| 343 | EXPECT_STREQ("", CodePointToUtf8(L'\0', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // Tests that ASCII characters are encoded correctly. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 347 | TEST(CodePointToUtf8Test, CanEncodeAscii) { |
| 348 | char buffer[32]; |
| 349 | EXPECT_STREQ("a", CodePointToUtf8(L'a', buffer)); |
| 350 | EXPECT_STREQ("Z", CodePointToUtf8(L'Z', buffer)); |
| 351 | EXPECT_STREQ("&", CodePointToUtf8(L'&', buffer)); |
| 352 | EXPECT_STREQ("\x7F", CodePointToUtf8(L'\x7F', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Tests that Unicode code-points that have 8 to 11 bits are encoded |
| 356 | // as 110xxxxx 10xxxxxx. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 357 | TEST(CodePointToUtf8Test, CanEncode8To11Bits) { |
| 358 | char buffer[32]; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 359 | // 000 1101 0011 => 110-00011 10-010011 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 360 | EXPECT_STREQ("\xC3\x93", CodePointToUtf8(L'\xD3', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 361 | |
| 362 | // 101 0111 0110 => 110-10101 10-110110 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 363 | EXPECT_STREQ("\xD5\xB6", CodePointToUtf8(L'\x576', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | // Tests that Unicode code-points that have 12 to 16 bits are encoded |
| 367 | // as 1110xxxx 10xxxxxx 10xxxxxx. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 368 | TEST(CodePointToUtf8Test, CanEncode12To16Bits) { |
| 369 | char buffer[32]; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 370 | // 0000 1000 1101 0011 => 1110-0000 10-100011 10-010011 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 371 | EXPECT_STREQ("\xE0\xA3\x93", CodePointToUtf8(L'\x8D3', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 372 | |
| 373 | // 1100 0111 0100 1101 => 1110-1100 10-011101 10-001101 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 374 | EXPECT_STREQ("\xEC\x9D\x8D", CodePointToUtf8(L'\xC74D', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 375 | } |
| 376 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 377 | #if !GTEST_WIDE_STRING_USES_UTF16_ |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 378 | // Tests in this group require a wchar_t to hold > 16 bits, and thus |
shiqian | 4f1d72e | 2008-07-09 20:58:26 +0000 | [diff] [blame] | 379 | // are skipped on Windows, Cygwin, and Symbian, where a wchar_t is |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 380 | // 16-bit wide. This code may not compile on those systems. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 381 | |
| 382 | // Tests that Unicode code-points that have 17 to 21 bits are encoded |
| 383 | // as 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 384 | TEST(CodePointToUtf8Test, CanEncode17To21Bits) { |
| 385 | char buffer[32]; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 386 | // 0 0001 0000 1000 1101 0011 => 11110-000 10-010000 10-100011 10-010011 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 387 | EXPECT_STREQ("\xF0\x90\xA3\x93", CodePointToUtf8(L'\x108D3', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 388 | |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 389 | // 0 0001 0000 0100 0000 0000 => 11110-000 10-010000 10-010000 10-000000 |
| 390 | EXPECT_STREQ("\xF0\x90\x90\x80", CodePointToUtf8(L'\x10400', buffer)); |
| 391 | |
| 392 | // 1 0000 1000 0110 0011 0100 => 11110-100 10-001000 10-011000 10-110100 |
| 393 | EXPECT_STREQ("\xF4\x88\x98\xB4", CodePointToUtf8(L'\x108634', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // Tests that encoding an invalid code-point generates the expected result. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 397 | TEST(CodePointToUtf8Test, CanEncodeInvalidCodePoint) { |
| 398 | char buffer[32]; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 399 | EXPECT_STREQ("(Invalid Unicode 0x1234ABCD)", |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 400 | CodePointToUtf8(L'\x1234ABCD', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 401 | } |
| 402 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 403 | #endif // !GTEST_WIDE_STRING_USES_UTF16_ |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 404 | |
| 405 | // Tests WideStringToUtf8(). |
| 406 | |
| 407 | // Tests that the NUL character L'\0' is encoded correctly. |
| 408 | TEST(WideStringToUtf8Test, CanEncodeNul) { |
| 409 | EXPECT_STREQ("", WideStringToUtf8(L"", 0).c_str()); |
| 410 | EXPECT_STREQ("", WideStringToUtf8(L"", -1).c_str()); |
| 411 | } |
| 412 | |
| 413 | // Tests that ASCII strings are encoded correctly. |
| 414 | TEST(WideStringToUtf8Test, CanEncodeAscii) { |
| 415 | EXPECT_STREQ("a", WideStringToUtf8(L"a", 1).c_str()); |
| 416 | EXPECT_STREQ("ab", WideStringToUtf8(L"ab", 2).c_str()); |
| 417 | EXPECT_STREQ("a", WideStringToUtf8(L"a", -1).c_str()); |
| 418 | EXPECT_STREQ("ab", WideStringToUtf8(L"ab", -1).c_str()); |
| 419 | } |
| 420 | |
| 421 | // Tests that Unicode code-points that have 8 to 11 bits are encoded |
| 422 | // as 110xxxxx 10xxxxxx. |
| 423 | TEST(WideStringToUtf8Test, CanEncode8To11Bits) { |
| 424 | // 000 1101 0011 => 110-00011 10-010011 |
| 425 | EXPECT_STREQ("\xC3\x93", WideStringToUtf8(L"\xD3", 1).c_str()); |
| 426 | EXPECT_STREQ("\xC3\x93", WideStringToUtf8(L"\xD3", -1).c_str()); |
| 427 | |
| 428 | // 101 0111 0110 => 110-10101 10-110110 |
| 429 | EXPECT_STREQ("\xD5\xB6", WideStringToUtf8(L"\x576", 1).c_str()); |
| 430 | EXPECT_STREQ("\xD5\xB6", WideStringToUtf8(L"\x576", -1).c_str()); |
| 431 | } |
| 432 | |
| 433 | // Tests that Unicode code-points that have 12 to 16 bits are encoded |
| 434 | // as 1110xxxx 10xxxxxx 10xxxxxx. |
| 435 | TEST(WideStringToUtf8Test, CanEncode12To16Bits) { |
| 436 | // 0000 1000 1101 0011 => 1110-0000 10-100011 10-010011 |
| 437 | EXPECT_STREQ("\xE0\xA3\x93", WideStringToUtf8(L"\x8D3", 1).c_str()); |
| 438 | EXPECT_STREQ("\xE0\xA3\x93", WideStringToUtf8(L"\x8D3", -1).c_str()); |
| 439 | |
| 440 | // 1100 0111 0100 1101 => 1110-1100 10-011101 10-001101 |
| 441 | EXPECT_STREQ("\xEC\x9D\x8D", WideStringToUtf8(L"\xC74D", 1).c_str()); |
| 442 | EXPECT_STREQ("\xEC\x9D\x8D", WideStringToUtf8(L"\xC74D", -1).c_str()); |
| 443 | } |
| 444 | |
| 445 | // Tests that the conversion stops when the function encounters \0 character. |
| 446 | TEST(WideStringToUtf8Test, StopsOnNulCharacter) { |
| 447 | EXPECT_STREQ("ABC", WideStringToUtf8(L"ABC\0XYZ", 100).c_str()); |
| 448 | } |
| 449 | |
| 450 | // Tests that the conversion stops when the function reaches the limit |
| 451 | // specified by the 'length' parameter. |
| 452 | TEST(WideStringToUtf8Test, StopsWhenLengthLimitReached) { |
| 453 | EXPECT_STREQ("ABC", WideStringToUtf8(L"ABCDEF", 3).c_str()); |
| 454 | } |
| 455 | |
| 456 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 457 | #if !GTEST_WIDE_STRING_USES_UTF16_ |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 458 | // Tests that Unicode code-points that have 17 to 21 bits are encoded |
| 459 | // as 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx. This code may not compile |
| 460 | // on the systems using UTF-16 encoding. |
| 461 | TEST(WideStringToUtf8Test, CanEncode17To21Bits) { |
| 462 | // 0 0001 0000 1000 1101 0011 => 11110-000 10-010000 10-100011 10-010011 |
| 463 | EXPECT_STREQ("\xF0\x90\xA3\x93", WideStringToUtf8(L"\x108D3", 1).c_str()); |
| 464 | EXPECT_STREQ("\xF0\x90\xA3\x93", WideStringToUtf8(L"\x108D3", -1).c_str()); |
| 465 | |
| 466 | // 1 0000 1000 0110 0011 0100 => 11110-100 10-001000 10-011000 10-110100 |
| 467 | EXPECT_STREQ("\xF4\x88\x98\xB4", WideStringToUtf8(L"\x108634", 1).c_str()); |
| 468 | EXPECT_STREQ("\xF4\x88\x98\xB4", WideStringToUtf8(L"\x108634", -1).c_str()); |
| 469 | } |
| 470 | |
| 471 | // Tests that encoding an invalid code-point generates the expected result. |
| 472 | TEST(WideStringToUtf8Test, CanEncodeInvalidCodePoint) { |
| 473 | EXPECT_STREQ("(Invalid Unicode 0xABCDFF)", |
| 474 | WideStringToUtf8(L"\xABCDFF", -1).c_str()); |
| 475 | } |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 476 | #else // !GTEST_WIDE_STRING_USES_UTF16_ |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 477 | // Tests that surrogate pairs are encoded correctly on the systems using |
| 478 | // UTF-16 encoding in the wide strings. |
| 479 | TEST(WideStringToUtf8Test, CanEncodeValidUtf16SUrrogatePairs) { |
| 480 | EXPECT_STREQ("\xF0\x90\x90\x80", |
| 481 | WideStringToUtf8(L"\xD801\xDC00", -1).c_str()); |
| 482 | } |
| 483 | |
| 484 | // Tests that encoding an invalid UTF-16 surrogate pair |
| 485 | // generates the expected result. |
| 486 | TEST(WideStringToUtf8Test, CanEncodeInvalidUtf16SurrogatePair) { |
| 487 | // Leading surrogate is at the end of the string. |
| 488 | EXPECT_STREQ("\xED\xA0\x80", WideStringToUtf8(L"\xD800", -1).c_str()); |
| 489 | // Leading surrogate is not followed by the trailing surrogate. |
| 490 | EXPECT_STREQ("\xED\xA0\x80$", WideStringToUtf8(L"\xD800$", -1).c_str()); |
| 491 | // Trailing surrogate appearas without a leading surrogate. |
| 492 | EXPECT_STREQ("\xED\xB0\x80PQR", WideStringToUtf8(L"\xDC00PQR", -1).c_str()); |
| 493 | } |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 494 | #endif // !GTEST_WIDE_STRING_USES_UTF16_ |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 495 | |
| 496 | // Tests that codepoint concatenation works correctly. |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 497 | #if !GTEST_WIDE_STRING_USES_UTF16_ |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 498 | TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) { |
| 499 | EXPECT_STREQ( |
| 500 | "\xF4\x88\x98\xB4" |
| 501 | "\xEC\x9D\x8D" |
| 502 | "\n" |
| 503 | "\xD5\xB6" |
| 504 | "\xE0\xA3\x93" |
| 505 | "\xF4\x88\x98\xB4", |
| 506 | WideStringToUtf8(L"\x108634\xC74D\n\x576\x8D3\x108634", -1).c_str()); |
| 507 | } |
| 508 | #else |
| 509 | TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) { |
| 510 | EXPECT_STREQ( |
| 511 | "\xEC\x9D\x8D" "\n" "\xD5\xB6" "\xE0\xA3\x93", |
| 512 | WideStringToUtf8(L"\xC74D\n\x576\x8D3", -1).c_str()); |
| 513 | } |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 514 | #endif // !GTEST_WIDE_STRING_USES_UTF16_ |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 515 | |
zhanyong.wan | 85f555a | 2009-09-21 19:42:03 +0000 | [diff] [blame] | 516 | // Tests the Random class. |
| 517 | |
| 518 | TEST(RandomDeathTest, GeneratesCrashesOnInvalidRange) { |
| 519 | testing::internal::Random random(42); |
| 520 | EXPECT_DEATH_IF_SUPPORTED( |
| 521 | random.Generate(0), |
| 522 | "Cannot generate a number in the range \\[0, 0\\)"); |
| 523 | EXPECT_DEATH_IF_SUPPORTED( |
| 524 | random.Generate(testing::internal::Random::kMaxRange + 1), |
| 525 | "Generation of a number in \\[0, 2147483649\\) was requested, " |
| 526 | "but this can only generate numbers in \\[0, 2147483648\\)"); |
| 527 | } |
| 528 | |
| 529 | TEST(RandomTest, GeneratesNumbersWithinRange) { |
| 530 | const UInt32 kRange = 10000; |
| 531 | testing::internal::Random random(12345); |
| 532 | for (int i = 0; i < 10; i++) { |
| 533 | EXPECT_LT(random.Generate(kRange), kRange) << " for iteration " << i; |
| 534 | } |
| 535 | |
| 536 | testing::internal::Random random2(testing::internal::Random::kMaxRange); |
| 537 | for (int i = 0; i < 10; i++) { |
| 538 | EXPECT_LT(random2.Generate(kRange), kRange) << " for iteration " << i; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | TEST(RandomTest, RepeatsWhenReseeded) { |
| 543 | const int kSeed = 123; |
| 544 | const int kArraySize = 10; |
| 545 | const UInt32 kRange = 10000; |
| 546 | UInt32 values[kArraySize]; |
| 547 | |
| 548 | testing::internal::Random random(kSeed); |
| 549 | for (int i = 0; i < kArraySize; i++) { |
| 550 | values[i] = random.Generate(kRange); |
| 551 | } |
| 552 | |
| 553 | random.Reseed(kSeed); |
| 554 | for (int i = 0; i < kArraySize; i++) { |
| 555 | EXPECT_EQ(values[i], random.Generate(kRange)) << " for iteration " << i; |
| 556 | } |
| 557 | } |
| 558 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 559 | // Tests the Vector class template. |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 560 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 561 | // Tests Vector::Clear(). |
| 562 | TEST(VectorTest, Clear) { |
| 563 | Vector<int> a; |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 564 | a.PushBack(1); |
| 565 | a.Clear(); |
| 566 | EXPECT_EQ(0, a.size()); |
| 567 | |
| 568 | a.PushBack(2); |
| 569 | a.PushBack(3); |
| 570 | a.Clear(); |
| 571 | EXPECT_EQ(0, a.size()); |
| 572 | } |
| 573 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 574 | // Tests Vector::PushBack(). |
| 575 | TEST(VectorTest, PushBack) { |
| 576 | Vector<char> a; |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 577 | a.PushBack('a'); |
| 578 | ASSERT_EQ(1, a.size()); |
| 579 | EXPECT_EQ('a', a.GetElement(0)); |
| 580 | |
| 581 | a.PushBack('b'); |
| 582 | ASSERT_EQ(2, a.size()); |
| 583 | EXPECT_EQ('a', a.GetElement(0)); |
| 584 | EXPECT_EQ('b', a.GetElement(1)); |
| 585 | } |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 586 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 587 | // Tests Vector::PushFront(). |
| 588 | TEST(VectorTest, PushFront) { |
| 589 | Vector<int> a; |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 590 | ASSERT_EQ(0, a.size()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 591 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 592 | // Calls PushFront() on an empty Vector. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 593 | a.PushFront(1); |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 594 | ASSERT_EQ(1, a.size()); |
| 595 | EXPECT_EQ(1, a.GetElement(0)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 596 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 597 | // Calls PushFront() on a singleton Vector. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 598 | a.PushFront(2); |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 599 | ASSERT_EQ(2, a.size()); |
| 600 | EXPECT_EQ(2, a.GetElement(0)); |
| 601 | EXPECT_EQ(1, a.GetElement(1)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 602 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 603 | // Calls PushFront() on a Vector with more than one elements. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 604 | a.PushFront(3); |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 605 | ASSERT_EQ(3, a.size()); |
| 606 | EXPECT_EQ(3, a.GetElement(0)); |
| 607 | EXPECT_EQ(2, a.GetElement(1)); |
| 608 | EXPECT_EQ(1, a.GetElement(2)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 609 | } |
| 610 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 611 | // Tests Vector::PopFront(). |
| 612 | TEST(VectorTest, PopFront) { |
| 613 | Vector<int> a; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 614 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 615 | // Popping on an empty Vector should fail. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 616 | EXPECT_FALSE(a.PopFront(NULL)); |
| 617 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 618 | // Popping again on an empty Vector should fail, and the result element |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 619 | // shouldn't be overwritten. |
| 620 | int element = 1; |
| 621 | EXPECT_FALSE(a.PopFront(&element)); |
| 622 | EXPECT_EQ(1, element); |
| 623 | |
| 624 | a.PushFront(2); |
| 625 | a.PushFront(3); |
| 626 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 627 | // PopFront() should pop the element in the front of the Vector. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 628 | EXPECT_TRUE(a.PopFront(&element)); |
| 629 | EXPECT_EQ(3, element); |
| 630 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 631 | // After popping the last element, the Vector should be empty. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 632 | EXPECT_TRUE(a.PopFront(NULL)); |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 633 | EXPECT_EQ(0, a.size()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 634 | } |
| 635 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 636 | // Tests inserting at the beginning using Vector::Insert(). |
| 637 | TEST(VectorTest, InsertAtBeginning) { |
| 638 | Vector<int> a; |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 639 | ASSERT_EQ(0, a.size()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 640 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 641 | // Inserts into an empty Vector. |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 642 | a.Insert(1, 0); |
| 643 | ASSERT_EQ(1, a.size()); |
| 644 | EXPECT_EQ(1, a.GetElement(0)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 645 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 646 | // Inserts at the beginning of a singleton Vector. |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 647 | a.Insert(2, 0); |
| 648 | ASSERT_EQ(2, a.size()); |
| 649 | EXPECT_EQ(2, a.GetElement(0)); |
| 650 | EXPECT_EQ(1, a.GetElement(1)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 651 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 652 | // Inserts at the beginning of a Vector with more than one elements. |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 653 | a.Insert(3, 0); |
| 654 | ASSERT_EQ(3, a.size()); |
| 655 | EXPECT_EQ(3, a.GetElement(0)); |
| 656 | EXPECT_EQ(2, a.GetElement(1)); |
| 657 | EXPECT_EQ(1, a.GetElement(2)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | // Tests inserting at a location other than the beginning using |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 661 | // Vector::Insert(). |
| 662 | TEST(VectorTest, InsertNotAtBeginning) { |
| 663 | // Prepares a singleton Vector. |
| 664 | Vector<int> a; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 665 | a.PushBack(1); |
| 666 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 667 | // Inserts at the end of a singleton Vector. |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 668 | a.Insert(2, a.size()); |
| 669 | ASSERT_EQ(2, a.size()); |
| 670 | EXPECT_EQ(1, a.GetElement(0)); |
| 671 | EXPECT_EQ(2, a.GetElement(1)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 672 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 673 | // Inserts at the end of a Vector with more than one elements. |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 674 | a.Insert(3, a.size()); |
| 675 | ASSERT_EQ(3, a.size()); |
| 676 | EXPECT_EQ(1, a.GetElement(0)); |
| 677 | EXPECT_EQ(2, a.GetElement(1)); |
| 678 | EXPECT_EQ(3, a.GetElement(2)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 679 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 680 | // Inserts in the middle of a Vector. |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 681 | a.Insert(4, 1); |
| 682 | ASSERT_EQ(4, a.size()); |
| 683 | EXPECT_EQ(1, a.GetElement(0)); |
| 684 | EXPECT_EQ(4, a.GetElement(1)); |
| 685 | EXPECT_EQ(2, a.GetElement(2)); |
| 686 | EXPECT_EQ(3, a.GetElement(3)); |
| 687 | } |
| 688 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 689 | // Tests Vector::GetElementOr(). |
| 690 | TEST(VectorTest, GetElementOr) { |
| 691 | Vector<char> a; |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 692 | EXPECT_EQ('x', a.GetElementOr(0, 'x')); |
| 693 | |
| 694 | a.PushBack('a'); |
| 695 | a.PushBack('b'); |
| 696 | EXPECT_EQ('a', a.GetElementOr(0, 'x')); |
| 697 | EXPECT_EQ('b', a.GetElementOr(1, 'x')); |
| 698 | EXPECT_EQ('x', a.GetElementOr(-2, 'x')); |
| 699 | EXPECT_EQ('x', a.GetElementOr(2, 'x')); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 700 | } |
| 701 | |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 702 | TEST(VectorTest, Swap) { |
| 703 | Vector<int> a; |
| 704 | a.PushBack(0); |
| 705 | a.PushBack(1); |
| 706 | a.PushBack(2); |
| 707 | |
| 708 | // Swaps an element with itself. |
| 709 | a.Swap(0, 0); |
| 710 | ASSERT_EQ(0, a.GetElement(0)); |
| 711 | ASSERT_EQ(1, a.GetElement(1)); |
| 712 | ASSERT_EQ(2, a.GetElement(2)); |
| 713 | |
| 714 | // Swaps two different elements where the indices go up. |
| 715 | a.Swap(0, 1); |
| 716 | ASSERT_EQ(1, a.GetElement(0)); |
| 717 | ASSERT_EQ(0, a.GetElement(1)); |
| 718 | ASSERT_EQ(2, a.GetElement(2)); |
| 719 | |
| 720 | // Swaps two different elements where the indices go down. |
| 721 | a.Swap(2, 0); |
| 722 | ASSERT_EQ(2, a.GetElement(0)); |
| 723 | ASSERT_EQ(0, a.GetElement(1)); |
| 724 | ASSERT_EQ(1, a.GetElement(2)); |
| 725 | } |
| 726 | |
| 727 | TEST(VectorTest, Clone) { |
| 728 | // Clones an empty Vector. |
| 729 | Vector<int> a; |
| 730 | scoped_ptr<Vector<int> > empty(a.Clone()); |
| 731 | EXPECT_EQ(0, empty->size()); |
| 732 | |
| 733 | // Clones a singleton. |
| 734 | a.PushBack(42); |
| 735 | scoped_ptr<Vector<int> > singleton(a.Clone()); |
| 736 | ASSERT_EQ(1, singleton->size()); |
| 737 | EXPECT_EQ(42, singleton->GetElement(0)); |
| 738 | |
| 739 | // Clones a Vector with more elements. |
| 740 | a.PushBack(43); |
| 741 | a.PushBack(44); |
| 742 | scoped_ptr<Vector<int> > big(a.Clone()); |
| 743 | ASSERT_EQ(3, big->size()); |
| 744 | EXPECT_EQ(42, big->GetElement(0)); |
| 745 | EXPECT_EQ(43, big->GetElement(1)); |
| 746 | EXPECT_EQ(44, big->GetElement(2)); |
| 747 | } |
| 748 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 749 | // Tests Vector::Erase(). |
| 750 | TEST(VectorDeathTest, Erase) { |
| 751 | Vector<int> a; |
| 752 | |
| 753 | // Tests erasing from an empty vector. |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 754 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 755 | a.Erase(0), |
| 756 | "Invalid Vector index 0: must be in range \\[0, -1\\]\\."); |
| 757 | |
| 758 | // Tests erasing from a singleton vector. |
| 759 | a.PushBack(0); |
| 760 | |
| 761 | a.Erase(0); |
| 762 | EXPECT_EQ(0, a.size()); |
| 763 | |
| 764 | // Tests Erase parameters beyond the bounds of the vector. |
| 765 | Vector<int> a1; |
| 766 | a1.PushBack(0); |
| 767 | a1.PushBack(1); |
| 768 | a1.PushBack(2); |
| 769 | |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 770 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 771 | a1.Erase(3), |
| 772 | "Invalid Vector index 3: must be in range \\[0, 2\\]\\."); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 773 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 774 | a1.Erase(-1), |
| 775 | "Invalid Vector index -1: must be in range \\[0, 2\\]\\."); |
| 776 | |
| 777 | // Tests erasing at the end of the vector. |
| 778 | Vector<int> a2; |
| 779 | a2.PushBack(0); |
| 780 | a2.PushBack(1); |
| 781 | a2.PushBack(2); |
| 782 | |
| 783 | a2.Erase(2); |
| 784 | ASSERT_EQ(2, a2.size()); |
| 785 | EXPECT_EQ(0, a2.GetElement(0)); |
| 786 | EXPECT_EQ(1, a2.GetElement(1)); |
| 787 | |
| 788 | // Tests erasing in the middle of the vector. |
| 789 | Vector<int> a3; |
| 790 | a3.PushBack(0); |
| 791 | a3.PushBack(1); |
| 792 | a3.PushBack(2); |
| 793 | |
| 794 | a3.Erase(1); |
| 795 | ASSERT_EQ(2, a3.size()); |
| 796 | EXPECT_EQ(0, a3.GetElement(0)); |
| 797 | EXPECT_EQ(2, a3.GetElement(1)); |
| 798 | |
| 799 | // Tests erasing at the beginning of the vector. |
| 800 | Vector<int> a4; |
| 801 | a4.PushBack(0); |
| 802 | a4.PushBack(1); |
| 803 | a4.PushBack(2); |
| 804 | |
| 805 | a4.Erase(0); |
| 806 | ASSERT_EQ(2, a4.size()); |
| 807 | EXPECT_EQ(1, a4.GetElement(0)); |
| 808 | EXPECT_EQ(2, a4.GetElement(1)); |
| 809 | } |
| 810 | |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 811 | // Tests the GetElement accessor. |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 812 | TEST(VectorDeathTest, GetElement) { |
| 813 | Vector<int> a; |
| 814 | a.PushBack(0); |
| 815 | a.PushBack(1); |
| 816 | a.PushBack(2); |
| 817 | const Vector<int>& b = a; |
| 818 | |
| 819 | EXPECT_EQ(0, b.GetElement(0)); |
| 820 | EXPECT_EQ(1, b.GetElement(1)); |
| 821 | EXPECT_EQ(2, b.GetElement(2)); |
| 822 | EXPECT_DEATH_IF_SUPPORTED( |
| 823 | b.GetElement(3), |
| 824 | "Invalid Vector index 3: must be in range \\[0, 2\\]\\."); |
| 825 | EXPECT_DEATH_IF_SUPPORTED( |
| 826 | b.GetElement(-1), |
| 827 | "Invalid Vector index -1: must be in range \\[0, 2\\]\\."); |
| 828 | } |
| 829 | |
| 830 | // Tests the GetMutableElement accessor. |
| 831 | TEST(VectorDeathTest, GetMutableElement) { |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 832 | Vector<int> a; |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 833 | a.PushBack(0); |
| 834 | a.PushBack(1); |
| 835 | a.PushBack(2); |
| 836 | |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 837 | EXPECT_EQ(0, a.GetMutableElement(0)); |
| 838 | EXPECT_EQ(1, a.GetMutableElement(1)); |
| 839 | EXPECT_EQ(2, a.GetMutableElement(2)); |
| 840 | |
| 841 | a.GetMutableElement(0) = 42; |
| 842 | EXPECT_EQ(42, a.GetMutableElement(0)); |
| 843 | EXPECT_EQ(1, a.GetMutableElement(1)); |
| 844 | EXPECT_EQ(2, a.GetMutableElement(2)); |
| 845 | |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 846 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 847 | a.GetMutableElement(3), |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 848 | "Invalid Vector index 3: must be in range \\[0, 2\\]\\."); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 849 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 850 | a.GetMutableElement(-1), |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 851 | "Invalid Vector index -1: must be in range \\[0, 2\\]\\."); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 852 | } |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 853 | |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 854 | TEST(VectorDeathTest, Swap) { |
| 855 | Vector<int> a; |
| 856 | a.PushBack(0); |
| 857 | a.PushBack(1); |
| 858 | a.PushBack(2); |
| 859 | |
| 860 | EXPECT_DEATH_IF_SUPPORTED( |
| 861 | a.Swap(-1, 1), |
| 862 | "Invalid first swap element -1: must be in range \\[0, 2\\]"); |
| 863 | EXPECT_DEATH_IF_SUPPORTED( |
| 864 | a.Swap(3, 1), |
| 865 | "Invalid first swap element 3: must be in range \\[0, 2\\]"); |
| 866 | EXPECT_DEATH_IF_SUPPORTED( |
| 867 | a.Swap(1, -1), |
| 868 | "Invalid second swap element -1: must be in range \\[0, 2\\]"); |
| 869 | EXPECT_DEATH_IF_SUPPORTED( |
| 870 | a.Swap(1, 3), |
| 871 | "Invalid second swap element 3: must be in range \\[0, 2\\]"); |
| 872 | } |
| 873 | |
| 874 | TEST(VectorDeathTest, ShuffleRange) { |
| 875 | Vector<int> a; |
| 876 | a.PushBack(0); |
| 877 | a.PushBack(1); |
| 878 | a.PushBack(2); |
| 879 | testing::internal::Random random(1); |
| 880 | |
| 881 | EXPECT_DEATH_IF_SUPPORTED( |
| 882 | a.ShuffleRange(&random, -1, 1), |
| 883 | "Invalid shuffle range start -1: must be in range \\[0, 3\\]"); |
| 884 | EXPECT_DEATH_IF_SUPPORTED( |
| 885 | a.ShuffleRange(&random, 4, 4), |
| 886 | "Invalid shuffle range start 4: must be in range \\[0, 3\\]"); |
| 887 | EXPECT_DEATH_IF_SUPPORTED( |
| 888 | a.ShuffleRange(&random, 3, 2), |
| 889 | "Invalid shuffle range finish 2: must be in range \\[3, 3\\]"); |
| 890 | EXPECT_DEATH_IF_SUPPORTED( |
| 891 | a.ShuffleRange(&random, 3, 4), |
| 892 | "Invalid shuffle range finish 4: must be in range \\[3, 3\\]"); |
| 893 | } |
| 894 | |
| 895 | class VectorShuffleTest : public Test { |
| 896 | protected: |
| 897 | static const int kVectorSize = 20; |
| 898 | |
| 899 | VectorShuffleTest() : random_(1) { |
| 900 | for (int i = 0; i < kVectorSize; i++) { |
| 901 | vector_.PushBack(i); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | static bool VectorIsCorrupt(const TestingVector& vector) { |
| 906 | if (kVectorSize != vector.size()) { |
| 907 | return true; |
| 908 | } |
| 909 | |
| 910 | bool found_in_vector[kVectorSize] = { false }; |
| 911 | for (int i = 0; i < vector.size(); i++) { |
| 912 | const int e = vector.GetElement(i); |
| 913 | if (e < 0 || e >= kVectorSize || found_in_vector[e]) { |
| 914 | return true; |
| 915 | } |
| 916 | found_in_vector[e] = true; |
| 917 | } |
| 918 | |
| 919 | // Vector size is correct, elements' range is correct, no |
| 920 | // duplicate elements. Therefore no corruption has occurred. |
| 921 | return false; |
| 922 | } |
| 923 | |
| 924 | static bool VectorIsNotCorrupt(const TestingVector& vector) { |
| 925 | return !VectorIsCorrupt(vector); |
| 926 | } |
| 927 | |
| 928 | static bool RangeIsShuffled(const TestingVector& vector, int begin, int end) { |
| 929 | for (int i = begin; i < end; i++) { |
| 930 | if (i != vector.GetElement(i)) { |
| 931 | return true; |
| 932 | } |
| 933 | } |
| 934 | return false; |
| 935 | } |
| 936 | |
| 937 | static bool RangeIsUnshuffled( |
| 938 | const TestingVector& vector, int begin, int end) { |
| 939 | return !RangeIsShuffled(vector, begin, end); |
| 940 | } |
| 941 | |
| 942 | static bool VectorIsShuffled(const TestingVector& vector) { |
| 943 | return RangeIsShuffled(vector, 0, vector.size()); |
| 944 | } |
| 945 | |
| 946 | static bool VectorIsUnshuffled(const TestingVector& vector) { |
| 947 | return !VectorIsShuffled(vector); |
| 948 | } |
| 949 | |
| 950 | testing::internal::Random random_; |
| 951 | TestingVector vector_; |
| 952 | }; // class VectorShuffleTest |
| 953 | |
| 954 | const int VectorShuffleTest::kVectorSize; |
| 955 | |
| 956 | TEST_F(VectorShuffleTest, HandlesEmptyRange) { |
| 957 | // Tests an empty range at the beginning... |
| 958 | vector_.ShuffleRange(&random_, 0, 0); |
| 959 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 960 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 961 | |
| 962 | // ...in the middle... |
| 963 | vector_.ShuffleRange(&random_, kVectorSize/2, kVectorSize/2); |
| 964 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 965 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 966 | |
| 967 | // ...at the end... |
| 968 | vector_.ShuffleRange(&random_, kVectorSize - 1, kVectorSize - 1); |
| 969 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 970 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 971 | |
| 972 | // ...and past the end. |
| 973 | vector_.ShuffleRange(&random_, kVectorSize, kVectorSize); |
| 974 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 975 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 976 | } |
| 977 | |
| 978 | TEST_F(VectorShuffleTest, HandlesRangeOfSizeOne) { |
| 979 | // Tests a size one range at the beginning... |
| 980 | vector_.ShuffleRange(&random_, 0, 1); |
| 981 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 982 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 983 | |
| 984 | // ...in the middle... |
| 985 | vector_.ShuffleRange(&random_, kVectorSize/2, kVectorSize/2 + 1); |
| 986 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 987 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 988 | |
| 989 | // ...and at the end. |
| 990 | vector_.ShuffleRange(&random_, kVectorSize - 1, kVectorSize); |
| 991 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 992 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 993 | } |
| 994 | |
| 995 | // Because we use our own random number generator and a fixed seed, |
| 996 | // we can guarantee that the following "random" tests will succeed. |
| 997 | |
| 998 | TEST_F(VectorShuffleTest, ShufflesEntireVector) { |
| 999 | vector_.Shuffle(&random_); |
| 1000 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 1001 | EXPECT_FALSE(VectorIsUnshuffled(vector_)) << vector_; |
| 1002 | |
| 1003 | // Tests the first and last elements in particular to ensure that |
| 1004 | // there are no off-by-one problems in our shuffle algorithm. |
| 1005 | EXPECT_NE(0, vector_.GetElement(0)); |
| 1006 | EXPECT_NE(kVectorSize - 1, vector_.GetElement(kVectorSize - 1)); |
| 1007 | } |
| 1008 | |
| 1009 | TEST_F(VectorShuffleTest, ShufflesStartOfVector) { |
| 1010 | const int kRangeSize = kVectorSize/2; |
| 1011 | |
| 1012 | vector_.ShuffleRange(&random_, 0, kRangeSize); |
| 1013 | |
| 1014 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 1015 | EXPECT_PRED3(RangeIsShuffled, vector_, 0, kRangeSize); |
| 1016 | EXPECT_PRED3(RangeIsUnshuffled, vector_, kRangeSize, kVectorSize); |
| 1017 | } |
| 1018 | |
| 1019 | TEST_F(VectorShuffleTest, ShufflesEndOfVector) { |
| 1020 | const int kRangeSize = kVectorSize / 2; |
| 1021 | vector_.ShuffleRange(&random_, kRangeSize, kVectorSize); |
| 1022 | |
| 1023 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 1024 | EXPECT_PRED3(RangeIsUnshuffled, vector_, 0, kRangeSize); |
| 1025 | EXPECT_PRED3(RangeIsShuffled, vector_, kRangeSize, kVectorSize); |
| 1026 | } |
| 1027 | |
| 1028 | TEST_F(VectorShuffleTest, ShufflesMiddleOfVector) { |
| 1029 | int kRangeSize = kVectorSize/3; |
| 1030 | vector_.ShuffleRange(&random_, kRangeSize, 2*kRangeSize); |
| 1031 | |
| 1032 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 1033 | EXPECT_PRED3(RangeIsUnshuffled, vector_, 0, kRangeSize); |
| 1034 | EXPECT_PRED3(RangeIsShuffled, vector_, kRangeSize, 2*kRangeSize); |
| 1035 | EXPECT_PRED3(RangeIsUnshuffled, vector_, 2*kRangeSize, kVectorSize); |
| 1036 | } |
| 1037 | |
| 1038 | TEST_F(VectorShuffleTest, ShufflesRepeatably) { |
| 1039 | TestingVector vector2; |
| 1040 | for (int i = 0; i < kVectorSize; i++) { |
| 1041 | vector2.PushBack(i); |
| 1042 | } |
| 1043 | |
| 1044 | random_.Reseed(1234); |
| 1045 | vector_.Shuffle(&random_); |
| 1046 | random_.Reseed(1234); |
| 1047 | vector2.Shuffle(&random_); |
| 1048 | |
| 1049 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 1050 | ASSERT_PRED1(VectorIsNotCorrupt, vector2); |
| 1051 | |
| 1052 | for (int i = 0; i < kVectorSize; i++) { |
| 1053 | EXPECT_EQ(vector_.GetElement(i), vector2.GetElement(i)) |
| 1054 | << " where i is " << i; |
| 1055 | } |
| 1056 | } |
| 1057 | |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 1058 | // Tests the size of the AssertHelper class. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1059 | |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 1060 | TEST(AssertHelperTest, AssertHelperIsSmall) { |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1061 | // To avoid breaking clients that use lots of assertions in one |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 1062 | // function, we cannot grow the size of AssertHelper. |
| 1063 | EXPECT_LE(sizeof(testing::internal::AssertHelper), sizeof(void*)); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 1066 | // Tests the String class. |
| 1067 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1068 | // Tests String's constructors. |
| 1069 | TEST(StringTest, Constructors) { |
| 1070 | // Default ctor. |
| 1071 | String s1; |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 1072 | // We aren't using EXPECT_EQ(NULL, s1.c_str()) because comparing |
| 1073 | // pointers with NULL isn't supported on all platforms. |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1074 | EXPECT_EQ(0U, s1.length()); |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 1075 | EXPECT_TRUE(NULL == s1.c_str()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1076 | |
| 1077 | // Implicitly constructs from a C-string. |
| 1078 | String s2 = "Hi"; |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1079 | EXPECT_EQ(2U, s2.length()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1080 | EXPECT_STREQ("Hi", s2.c_str()); |
| 1081 | |
| 1082 | // Constructs from a C-string and a length. |
| 1083 | String s3("hello", 3); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1084 | EXPECT_EQ(3U, s3.length()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1085 | EXPECT_STREQ("hel", s3.c_str()); |
| 1086 | |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1087 | // The empty String should be created when String is constructed with |
| 1088 | // a NULL pointer and length 0. |
| 1089 | EXPECT_EQ(0U, String(NULL, 0).length()); |
| 1090 | EXPECT_FALSE(String(NULL, 0).c_str() == NULL); |
| 1091 | |
| 1092 | // Constructs a String that contains '\0'. |
| 1093 | String s4("a\0bcd", 4); |
| 1094 | EXPECT_EQ(4U, s4.length()); |
| 1095 | EXPECT_EQ('a', s4.c_str()[0]); |
| 1096 | EXPECT_EQ('\0', s4.c_str()[1]); |
| 1097 | EXPECT_EQ('b', s4.c_str()[2]); |
| 1098 | EXPECT_EQ('c', s4.c_str()[3]); |
| 1099 | |
| 1100 | // Copy ctor where the source is NULL. |
| 1101 | const String null_str; |
| 1102 | String s5 = null_str; |
| 1103 | EXPECT_TRUE(s5.c_str() == NULL); |
| 1104 | |
| 1105 | // Copy ctor where the source isn't NULL. |
| 1106 | String s6 = s3; |
| 1107 | EXPECT_EQ(3U, s6.length()); |
| 1108 | EXPECT_STREQ("hel", s6.c_str()); |
| 1109 | |
| 1110 | // Copy ctor where the source contains '\0'. |
| 1111 | String s7 = s4; |
| 1112 | EXPECT_EQ(4U, s7.length()); |
| 1113 | EXPECT_EQ('a', s7.c_str()[0]); |
| 1114 | EXPECT_EQ('\0', s7.c_str()[1]); |
| 1115 | EXPECT_EQ('b', s7.c_str()[2]); |
| 1116 | EXPECT_EQ('c', s7.c_str()[3]); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 1119 | TEST(StringTest, ConvertsFromStdString) { |
| 1120 | // An empty std::string. |
| 1121 | const std::string src1(""); |
| 1122 | const String dest1 = src1; |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1123 | EXPECT_EQ(0U, dest1.length()); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 1124 | EXPECT_STREQ("", dest1.c_str()); |
| 1125 | |
| 1126 | // A normal std::string. |
| 1127 | const std::string src2("Hi"); |
| 1128 | const String dest2 = src2; |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1129 | EXPECT_EQ(2U, dest2.length()); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 1130 | EXPECT_STREQ("Hi", dest2.c_str()); |
| 1131 | |
| 1132 | // An std::string with an embedded NUL character. |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1133 | const char src3[] = "a\0b"; |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 1134 | const String dest3 = std::string(src3, sizeof(src3)); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1135 | EXPECT_EQ(sizeof(src3), dest3.length()); |
| 1136 | EXPECT_EQ('a', dest3.c_str()[0]); |
| 1137 | EXPECT_EQ('\0', dest3.c_str()[1]); |
| 1138 | EXPECT_EQ('b', dest3.c_str()[2]); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | TEST(StringTest, ConvertsToStdString) { |
| 1142 | // An empty String. |
| 1143 | const String src1(""); |
| 1144 | const std::string dest1 = src1; |
| 1145 | EXPECT_EQ("", dest1); |
| 1146 | |
| 1147 | // A normal String. |
| 1148 | const String src2("Hi"); |
| 1149 | const std::string dest2 = src2; |
| 1150 | EXPECT_EQ("Hi", dest2); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1151 | |
| 1152 | // A String containing a '\0'. |
| 1153 | const String src3("x\0y", 3); |
| 1154 | const std::string dest3 = src3; |
| 1155 | EXPECT_EQ(std::string("x\0y", 3), dest3); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 1158 | #if GTEST_HAS_GLOBAL_STRING |
| 1159 | |
| 1160 | TEST(StringTest, ConvertsFromGlobalString) { |
| 1161 | // An empty ::string. |
| 1162 | const ::string src1(""); |
| 1163 | const String dest1 = src1; |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1164 | EXPECT_EQ(0U, dest1.length()); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 1165 | EXPECT_STREQ("", dest1.c_str()); |
| 1166 | |
| 1167 | // A normal ::string. |
| 1168 | const ::string src2("Hi"); |
| 1169 | const String dest2 = src2; |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1170 | EXPECT_EQ(2U, dest2.length()); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 1171 | EXPECT_STREQ("Hi", dest2.c_str()); |
| 1172 | |
| 1173 | // An ::string with an embedded NUL character. |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1174 | const char src3[] = "x\0y"; |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 1175 | const String dest3 = ::string(src3, sizeof(src3)); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1176 | EXPECT_EQ(sizeof(src3), dest3.length()); |
| 1177 | EXPECT_EQ('x', dest3.c_str()[0]); |
| 1178 | EXPECT_EQ('\0', dest3.c_str()[1]); |
| 1179 | EXPECT_EQ('y', dest3.c_str()[2]); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | TEST(StringTest, ConvertsToGlobalString) { |
| 1183 | // An empty String. |
| 1184 | const String src1(""); |
| 1185 | const ::string dest1 = src1; |
| 1186 | EXPECT_EQ("", dest1); |
| 1187 | |
| 1188 | // A normal String. |
| 1189 | const String src2("Hi"); |
| 1190 | const ::string dest2 = src2; |
| 1191 | EXPECT_EQ("Hi", dest2); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1192 | |
| 1193 | const String src3("x\0y", 3); |
| 1194 | const ::string dest3 = src3; |
| 1195 | EXPECT_EQ(::string("x\0y", 3), dest3); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | #endif // GTEST_HAS_GLOBAL_STRING |
| 1199 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1200 | // Tests String::ShowCStringQuoted(). |
| 1201 | TEST(StringTest, ShowCStringQuoted) { |
| 1202 | EXPECT_STREQ("(null)", |
| 1203 | String::ShowCStringQuoted(NULL).c_str()); |
| 1204 | EXPECT_STREQ("\"\"", |
| 1205 | String::ShowCStringQuoted("").c_str()); |
| 1206 | EXPECT_STREQ("\"foo\"", |
| 1207 | String::ShowCStringQuoted("foo").c_str()); |
| 1208 | } |
| 1209 | |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1210 | // Tests String::empty(). |
| 1211 | TEST(StringTest, Empty) { |
| 1212 | EXPECT_TRUE(String("").empty()); |
| 1213 | EXPECT_FALSE(String().empty()); |
| 1214 | EXPECT_FALSE(String(NULL).empty()); |
| 1215 | EXPECT_FALSE(String("a").empty()); |
| 1216 | EXPECT_FALSE(String("\0", 1).empty()); |
| 1217 | } |
| 1218 | |
| 1219 | // Tests String::Compare(). |
| 1220 | TEST(StringTest, Compare) { |
| 1221 | // NULL vs NULL. |
| 1222 | EXPECT_EQ(0, String().Compare(String())); |
| 1223 | |
| 1224 | // NULL vs non-NULL. |
| 1225 | EXPECT_EQ(-1, String().Compare(String(""))); |
| 1226 | |
| 1227 | // Non-NULL vs NULL. |
| 1228 | EXPECT_EQ(1, String("").Compare(String())); |
| 1229 | |
| 1230 | // The following covers non-NULL vs non-NULL. |
| 1231 | |
| 1232 | // "" vs "". |
| 1233 | EXPECT_EQ(0, String("").Compare(String(""))); |
| 1234 | |
| 1235 | // "" vs non-"". |
| 1236 | EXPECT_EQ(-1, String("").Compare(String("\0", 1))); |
| 1237 | EXPECT_EQ(-1, String("").Compare(" ")); |
| 1238 | |
| 1239 | // Non-"" vs "". |
| 1240 | EXPECT_EQ(1, String("a").Compare(String(""))); |
| 1241 | |
| 1242 | // The following covers non-"" vs non-"". |
| 1243 | |
| 1244 | // Same length and equal. |
| 1245 | EXPECT_EQ(0, String("a").Compare(String("a"))); |
| 1246 | |
| 1247 | // Same length and different. |
| 1248 | EXPECT_EQ(-1, String("a\0b", 3).Compare(String("a\0c", 3))); |
| 1249 | EXPECT_EQ(1, String("b").Compare(String("a"))); |
| 1250 | |
| 1251 | // Different lengths. |
| 1252 | EXPECT_EQ(-1, String("a").Compare(String("ab"))); |
| 1253 | EXPECT_EQ(-1, String("a").Compare(String("a\0", 2))); |
| 1254 | EXPECT_EQ(1, String("abc").Compare(String("aacd"))); |
| 1255 | } |
| 1256 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1257 | // Tests String::operator==(). |
| 1258 | TEST(StringTest, Equals) { |
| 1259 | const String null(NULL); |
| 1260 | EXPECT_TRUE(null == NULL); // NOLINT |
| 1261 | EXPECT_FALSE(null == ""); // NOLINT |
| 1262 | EXPECT_FALSE(null == "bar"); // NOLINT |
| 1263 | |
| 1264 | const String empty(""); |
| 1265 | EXPECT_FALSE(empty == NULL); // NOLINT |
| 1266 | EXPECT_TRUE(empty == ""); // NOLINT |
| 1267 | EXPECT_FALSE(empty == "bar"); // NOLINT |
| 1268 | |
| 1269 | const String foo("foo"); |
| 1270 | EXPECT_FALSE(foo == NULL); // NOLINT |
| 1271 | EXPECT_FALSE(foo == ""); // NOLINT |
| 1272 | EXPECT_FALSE(foo == "bar"); // NOLINT |
| 1273 | EXPECT_TRUE(foo == "foo"); // NOLINT |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1274 | |
| 1275 | const String bar("x\0y", 3); |
| 1276 | EXPECT_FALSE(bar == "x"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | // Tests String::operator!=(). |
| 1280 | TEST(StringTest, NotEquals) { |
| 1281 | const String null(NULL); |
| 1282 | EXPECT_FALSE(null != NULL); // NOLINT |
| 1283 | EXPECT_TRUE(null != ""); // NOLINT |
| 1284 | EXPECT_TRUE(null != "bar"); // NOLINT |
| 1285 | |
| 1286 | const String empty(""); |
| 1287 | EXPECT_TRUE(empty != NULL); // NOLINT |
| 1288 | EXPECT_FALSE(empty != ""); // NOLINT |
| 1289 | EXPECT_TRUE(empty != "bar"); // NOLINT |
| 1290 | |
| 1291 | const String foo("foo"); |
| 1292 | EXPECT_TRUE(foo != NULL); // NOLINT |
| 1293 | EXPECT_TRUE(foo != ""); // NOLINT |
| 1294 | EXPECT_TRUE(foo != "bar"); // NOLINT |
| 1295 | EXPECT_FALSE(foo != "foo"); // NOLINT |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1296 | |
| 1297 | const String bar("x\0y", 3); |
| 1298 | EXPECT_TRUE(bar != "x"); |
| 1299 | } |
| 1300 | |
| 1301 | // Tests String::length(). |
| 1302 | TEST(StringTest, Length) { |
| 1303 | EXPECT_EQ(0U, String().length()); |
| 1304 | EXPECT_EQ(0U, String("").length()); |
| 1305 | EXPECT_EQ(2U, String("ab").length()); |
| 1306 | EXPECT_EQ(3U, String("a\0b", 3).length()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | // Tests String::EndsWith(). |
| 1310 | TEST(StringTest, EndsWith) { |
| 1311 | EXPECT_TRUE(String("foobar").EndsWith("bar")); |
| 1312 | EXPECT_TRUE(String("foobar").EndsWith("")); |
| 1313 | EXPECT_TRUE(String("").EndsWith("")); |
| 1314 | |
| 1315 | EXPECT_FALSE(String("foobar").EndsWith("foo")); |
| 1316 | EXPECT_FALSE(String("").EndsWith("foo")); |
| 1317 | } |
| 1318 | |
| 1319 | // Tests String::EndsWithCaseInsensitive(). |
| 1320 | TEST(StringTest, EndsWithCaseInsensitive) { |
| 1321 | EXPECT_TRUE(String("foobar").EndsWithCaseInsensitive("BAR")); |
| 1322 | EXPECT_TRUE(String("foobaR").EndsWithCaseInsensitive("bar")); |
| 1323 | EXPECT_TRUE(String("foobar").EndsWithCaseInsensitive("")); |
| 1324 | EXPECT_TRUE(String("").EndsWithCaseInsensitive("")); |
| 1325 | |
| 1326 | EXPECT_FALSE(String("Foobar").EndsWithCaseInsensitive("foo")); |
| 1327 | EXPECT_FALSE(String("foobar").EndsWithCaseInsensitive("Foo")); |
| 1328 | EXPECT_FALSE(String("").EndsWithCaseInsensitive("foo")); |
| 1329 | } |
| 1330 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1331 | // C++Builder's preprocessor is buggy; it fails to expand macros that |
| 1332 | // appear in macro parameters after wide char literals. Provide an alias |
| 1333 | // for NULL as a workaround. |
| 1334 | static const wchar_t* const kNull = NULL; |
| 1335 | |
shiqian | e8ff148 | 2008-09-08 17:55:52 +0000 | [diff] [blame] | 1336 | // Tests String::CaseInsensitiveWideCStringEquals |
| 1337 | TEST(StringTest, CaseInsensitiveWideCStringEquals) { |
| 1338 | EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(NULL, NULL)); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1339 | EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(kNull, L"")); |
| 1340 | EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(L"", kNull)); |
| 1341 | EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(kNull, L"foobar")); |
| 1342 | EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(L"foobar", kNull)); |
shiqian | e8ff148 | 2008-09-08 17:55:52 +0000 | [diff] [blame] | 1343 | EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"foobar", L"foobar")); |
| 1344 | EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"foobar", L"FOOBAR")); |
| 1345 | EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"FOOBAR", L"foobar")); |
| 1346 | } |
| 1347 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1348 | // Tests that NULL can be assigned to a String. |
| 1349 | TEST(StringTest, CanBeAssignedNULL) { |
| 1350 | const String src(NULL); |
| 1351 | String dest; |
| 1352 | |
| 1353 | dest = src; |
| 1354 | EXPECT_STREQ(NULL, dest.c_str()); |
| 1355 | } |
| 1356 | |
| 1357 | // Tests that the empty string "" can be assigned to a String. |
| 1358 | TEST(StringTest, CanBeAssignedEmpty) { |
| 1359 | const String src(""); |
| 1360 | String dest; |
| 1361 | |
| 1362 | dest = src; |
| 1363 | EXPECT_STREQ("", dest.c_str()); |
| 1364 | } |
| 1365 | |
| 1366 | // Tests that a non-empty string can be assigned to a String. |
| 1367 | TEST(StringTest, CanBeAssignedNonEmpty) { |
| 1368 | const String src("hello"); |
| 1369 | String dest; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1370 | dest = src; |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1371 | EXPECT_EQ(5U, dest.length()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1372 | EXPECT_STREQ("hello", dest.c_str()); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1373 | |
| 1374 | const String src2("x\0y", 3); |
| 1375 | String dest2; |
| 1376 | dest2 = src2; |
| 1377 | EXPECT_EQ(3U, dest2.length()); |
| 1378 | EXPECT_EQ('x', dest2.c_str()[0]); |
| 1379 | EXPECT_EQ('\0', dest2.c_str()[1]); |
| 1380 | EXPECT_EQ('y', dest2.c_str()[2]); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | // Tests that a String can be assigned to itself. |
| 1384 | TEST(StringTest, CanBeAssignedSelf) { |
| 1385 | String dest("hello"); |
| 1386 | |
| 1387 | dest = dest; |
| 1388 | EXPECT_STREQ("hello", dest.c_str()); |
| 1389 | } |
| 1390 | |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1391 | // Tests streaming a String. |
| 1392 | TEST(StringTest, Streams) { |
| 1393 | EXPECT_EQ(StreamableToString(String()), "(null)"); |
| 1394 | EXPECT_EQ(StreamableToString(String("")), ""); |
| 1395 | EXPECT_EQ(StreamableToString(String("a\0b", 3)), "a\\0b"); |
| 1396 | } |
| 1397 | |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 1398 | // Tests that String::Format() works. |
| 1399 | TEST(StringTest, FormatWorks) { |
| 1400 | // Normal case: the format spec is valid, the arguments match the |
| 1401 | // spec, and the result is < 4095 characters. |
| 1402 | EXPECT_STREQ("Hello, 42", String::Format("%s, %d", "Hello", 42).c_str()); |
| 1403 | |
| 1404 | // Edge case: the result is 4095 characters. |
| 1405 | char buffer[4096]; |
| 1406 | const size_t kSize = sizeof(buffer); |
| 1407 | memset(buffer, 'a', kSize - 1); |
| 1408 | buffer[kSize - 1] = '\0'; |
| 1409 | EXPECT_STREQ(buffer, String::Format("%s", buffer).c_str()); |
| 1410 | |
| 1411 | // The result needs to be 4096 characters, exceeding Format()'s limit. |
| 1412 | EXPECT_STREQ("<formatting error or buffer exceeded>", |
| 1413 | String::Format("x%s", buffer).c_str()); |
| 1414 | |
| 1415 | #if GTEST_OS_LINUX |
| 1416 | // On Linux, invalid format spec should lead to an error message. |
| 1417 | // In other environment (e.g. MSVC on Windows), String::Format() may |
| 1418 | // simply ignore a bad format spec, so this assertion is run on |
| 1419 | // Linux only. |
| 1420 | EXPECT_STREQ("<formatting error or buffer exceeded>", |
| 1421 | String::Format("%").c_str()); |
| 1422 | #endif |
| 1423 | } |
| 1424 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1425 | #if GTEST_OS_WINDOWS |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1426 | |
| 1427 | // Tests String::ShowWideCString(). |
| 1428 | TEST(StringTest, ShowWideCString) { |
| 1429 | EXPECT_STREQ("(null)", |
| 1430 | String::ShowWideCString(NULL).c_str()); |
| 1431 | EXPECT_STREQ("", String::ShowWideCString(L"").c_str()); |
| 1432 | EXPECT_STREQ("foo", String::ShowWideCString(L"foo").c_str()); |
| 1433 | } |
| 1434 | |
| 1435 | // Tests String::ShowWideCStringQuoted(). |
| 1436 | TEST(StringTest, ShowWideCStringQuoted) { |
| 1437 | EXPECT_STREQ("(null)", |
| 1438 | String::ShowWideCStringQuoted(NULL).c_str()); |
| 1439 | EXPECT_STREQ("L\"\"", |
| 1440 | String::ShowWideCStringQuoted(L"").c_str()); |
| 1441 | EXPECT_STREQ("L\"foo\"", |
| 1442 | String::ShowWideCStringQuoted(L"foo").c_str()); |
| 1443 | } |
| 1444 | |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 1445 | #if GTEST_OS_WINDOWS_MOBILE |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 1446 | TEST(StringTest, AnsiAndUtf16Null) { |
| 1447 | EXPECT_EQ(NULL, String::AnsiToUtf16(NULL)); |
| 1448 | EXPECT_EQ(NULL, String::Utf16ToAnsi(NULL)); |
| 1449 | } |
| 1450 | |
| 1451 | TEST(StringTest, AnsiAndUtf16ConvertBasic) { |
| 1452 | const char* ansi = String::Utf16ToAnsi(L"str"); |
| 1453 | EXPECT_STREQ("str", ansi); |
| 1454 | delete [] ansi; |
| 1455 | const WCHAR* utf16 = String::AnsiToUtf16("str"); |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 1456 | EXPECT_EQ(0, wcsncmp(L"str", utf16, 3)); |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 1457 | delete [] utf16; |
| 1458 | } |
| 1459 | |
| 1460 | TEST(StringTest, AnsiAndUtf16ConvertPathChars) { |
| 1461 | const char* ansi = String::Utf16ToAnsi(L".:\\ \"*?"); |
| 1462 | EXPECT_STREQ(".:\\ \"*?", ansi); |
| 1463 | delete [] ansi; |
| 1464 | const WCHAR* utf16 = String::AnsiToUtf16(".:\\ \"*?"); |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 1465 | EXPECT_EQ(0, wcsncmp(L".:\\ \"*?", utf16, 3)); |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 1466 | delete [] utf16; |
| 1467 | } |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 1468 | #endif // GTEST_OS_WINDOWS_MOBILE |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 1469 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1470 | #endif // GTEST_OS_WINDOWS |
| 1471 | |
| 1472 | // Tests TestProperty construction. |
| 1473 | TEST(TestPropertyTest, StringValue) { |
| 1474 | TestProperty property("key", "1"); |
| 1475 | EXPECT_STREQ("key", property.key()); |
| 1476 | EXPECT_STREQ("1", property.value()); |
| 1477 | } |
| 1478 | |
| 1479 | // Tests TestProperty replacing a value. |
| 1480 | TEST(TestPropertyTest, ReplaceStringValue) { |
| 1481 | TestProperty property("key", "1"); |
| 1482 | EXPECT_STREQ("1", property.value()); |
| 1483 | property.SetValue("2"); |
| 1484 | EXPECT_STREQ("2", property.value()); |
| 1485 | } |
| 1486 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1487 | // AddFatalFailure() and AddNonfatalFailure() must be stand-alone |
| 1488 | // functions (i.e. their definitions cannot be inlined at the call |
| 1489 | // sites), or C++Builder won't compile the code. |
| 1490 | static void AddFatalFailure() { |
| 1491 | FAIL() << "Expected fatal failure."; |
| 1492 | } |
| 1493 | |
| 1494 | static void AddNonfatalFailure() { |
| 1495 | ADD_FAILURE() << "Expected non-fatal failure."; |
| 1496 | } |
| 1497 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1498 | class ScopedFakeTestPartResultReporterTest : public Test { |
tsunanet | acd0f32 | 2009-05-18 20:53:57 +0000 | [diff] [blame] | 1499 | public: // Must be public and not protected due to a bug in g++ 3.4.2. |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1500 | enum FailureMode { |
| 1501 | FATAL_FAILURE, |
| 1502 | NONFATAL_FAILURE |
| 1503 | }; |
| 1504 | static void AddFailure(FailureMode failure) { |
| 1505 | if (failure == FATAL_FAILURE) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1506 | AddFatalFailure(); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1507 | } else { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1508 | AddNonfatalFailure(); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1509 | } |
| 1510 | } |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1511 | }; |
| 1512 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1513 | // Tests that ScopedFakeTestPartResultReporter intercepts test |
| 1514 | // failures. |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1515 | TEST_F(ScopedFakeTestPartResultReporterTest, InterceptsTestFailures) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1516 | TestPartResultArray results; |
| 1517 | { |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1518 | ScopedFakeTestPartResultReporter reporter( |
| 1519 | ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD, |
| 1520 | &results); |
| 1521 | AddFailure(NONFATAL_FAILURE); |
| 1522 | AddFailure(FATAL_FAILURE); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | EXPECT_EQ(2, results.size()); |
| 1526 | EXPECT_TRUE(results.GetTestPartResult(0).nonfatally_failed()); |
| 1527 | EXPECT_TRUE(results.GetTestPartResult(1).fatally_failed()); |
| 1528 | } |
| 1529 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1530 | TEST_F(ScopedFakeTestPartResultReporterTest, DeprecatedConstructor) { |
| 1531 | TestPartResultArray results; |
| 1532 | { |
| 1533 | // Tests, that the deprecated constructor still works. |
| 1534 | ScopedFakeTestPartResultReporter reporter(&results); |
| 1535 | AddFailure(NONFATAL_FAILURE); |
| 1536 | } |
| 1537 | EXPECT_EQ(1, results.size()); |
| 1538 | } |
| 1539 | |
| 1540 | #if GTEST_IS_THREADSAFE && GTEST_HAS_PTHREAD |
| 1541 | |
| 1542 | class ScopedFakeTestPartResultReporterWithThreadsTest |
| 1543 | : public ScopedFakeTestPartResultReporterTest { |
| 1544 | protected: |
| 1545 | static void AddFailureInOtherThread(FailureMode failure) { |
| 1546 | pthread_t tid; |
| 1547 | pthread_create(&tid, |
| 1548 | NULL, |
| 1549 | ScopedFakeTestPartResultReporterWithThreadsTest:: |
| 1550 | FailureThread, |
| 1551 | &failure); |
| 1552 | pthread_join(tid, NULL); |
| 1553 | } |
| 1554 | private: |
| 1555 | static void* FailureThread(void* attr) { |
| 1556 | FailureMode* failure = static_cast<FailureMode*>(attr); |
| 1557 | AddFailure(*failure); |
| 1558 | return NULL; |
| 1559 | } |
| 1560 | }; |
| 1561 | |
| 1562 | TEST_F(ScopedFakeTestPartResultReporterWithThreadsTest, |
| 1563 | InterceptsTestFailuresInAllThreads) { |
| 1564 | TestPartResultArray results; |
| 1565 | { |
| 1566 | ScopedFakeTestPartResultReporter reporter( |
| 1567 | ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, &results); |
| 1568 | AddFailure(NONFATAL_FAILURE); |
| 1569 | AddFailure(FATAL_FAILURE); |
| 1570 | AddFailureInOtherThread(NONFATAL_FAILURE); |
| 1571 | AddFailureInOtherThread(FATAL_FAILURE); |
| 1572 | } |
| 1573 | |
| 1574 | EXPECT_EQ(4, results.size()); |
| 1575 | EXPECT_TRUE(results.GetTestPartResult(0).nonfatally_failed()); |
| 1576 | EXPECT_TRUE(results.GetTestPartResult(1).fatally_failed()); |
| 1577 | EXPECT_TRUE(results.GetTestPartResult(2).nonfatally_failed()); |
| 1578 | EXPECT_TRUE(results.GetTestPartResult(3).fatally_failed()); |
| 1579 | } |
| 1580 | |
| 1581 | #endif // GTEST_IS_THREADSAFE && GTEST_HAS_PTHREAD |
| 1582 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1583 | // Tests EXPECT_FATAL_FAILURE{,ON_ALL_THREADS}. Makes sure that they |
| 1584 | // work even if the failure is generated in a called function rather than |
| 1585 | // the current context. |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1586 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1587 | typedef ScopedFakeTestPartResultReporterTest ExpectFatalFailureTest; |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1588 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1589 | TEST_F(ExpectFatalFailureTest, CatchesFatalFaliure) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1590 | EXPECT_FATAL_FAILURE(AddFatalFailure(), "Expected fatal failure."); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1593 | TEST_F(ExpectFatalFailureTest, CatchesFatalFailureOnAllThreads) { |
| 1594 | // We have another test below to verify that the macro catches fatal |
| 1595 | // failures generated on another thread. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1596 | EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFatalFailure(), |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1597 | "Expected fatal failure."); |
| 1598 | } |
| 1599 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1600 | #ifdef __BORLANDC__ |
| 1601 | // Silences warnings: "Condition is always true" |
| 1602 | #pragma option push -w-ccc |
| 1603 | #endif |
| 1604 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1605 | // Tests that EXPECT_FATAL_FAILURE() can be used in a non-void |
| 1606 | // function even when the statement in it contains ASSERT_*. |
| 1607 | |
| 1608 | int NonVoidFunction() { |
| 1609 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(false), ""); |
| 1610 | EXPECT_FATAL_FAILURE_ON_ALL_THREADS(FAIL(), ""); |
| 1611 | return 0; |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1614 | TEST_F(ExpectFatalFailureTest, CanBeUsedInNonVoidFunction) { |
| 1615 | NonVoidFunction(); |
| 1616 | } |
| 1617 | |
| 1618 | // Tests that EXPECT_FATAL_FAILURE(statement, ...) doesn't abort the |
| 1619 | // current function even though 'statement' generates a fatal failure. |
| 1620 | |
| 1621 | void DoesNotAbortHelper(bool* aborted) { |
| 1622 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(false), ""); |
| 1623 | EXPECT_FATAL_FAILURE_ON_ALL_THREADS(FAIL(), ""); |
| 1624 | |
| 1625 | *aborted = false; |
| 1626 | } |
| 1627 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1628 | #ifdef __BORLANDC__ |
| 1629 | // Restores warnings after previous "#pragma option push" supressed them |
| 1630 | #pragma option pop |
| 1631 | #endif |
| 1632 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1633 | TEST_F(ExpectFatalFailureTest, DoesNotAbort) { |
| 1634 | bool aborted = true; |
| 1635 | DoesNotAbortHelper(&aborted); |
| 1636 | EXPECT_FALSE(aborted); |
| 1637 | } |
| 1638 | |
| 1639 | // Tests that the EXPECT_FATAL_FAILURE{,_ON_ALL_THREADS} accepts a |
| 1640 | // statement that contains a macro which expands to code containing an |
| 1641 | // unprotected comma. |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1642 | |
| 1643 | static int global_var = 0; |
| 1644 | #define GTEST_USE_UNPROTECTED_COMMA_ global_var++, global_var++ |
| 1645 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1646 | TEST_F(ExpectFatalFailureTest, AcceptsMacroThatExpandsToUnprotectedComma) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1647 | #ifndef __BORLANDC__ |
| 1648 | // ICE's in C++Builder 2007. |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1649 | EXPECT_FATAL_FAILURE({ |
| 1650 | GTEST_USE_UNPROTECTED_COMMA_; |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1651 | AddFatalFailure(); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1652 | }, ""); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1653 | #endif |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1654 | |
| 1655 | EXPECT_FATAL_FAILURE_ON_ALL_THREADS({ |
| 1656 | GTEST_USE_UNPROTECTED_COMMA_; |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1657 | AddFatalFailure(); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1658 | }, ""); |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1659 | } |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1660 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1661 | // Tests EXPECT_NONFATAL_FAILURE{,ON_ALL_THREADS}. |
| 1662 | |
| 1663 | typedef ScopedFakeTestPartResultReporterTest ExpectNonfatalFailureTest; |
| 1664 | |
| 1665 | TEST_F(ExpectNonfatalFailureTest, CatchesNonfatalFailure) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1666 | EXPECT_NONFATAL_FAILURE(AddNonfatalFailure(), |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1667 | "Expected non-fatal failure."); |
| 1668 | } |
| 1669 | |
| 1670 | TEST_F(ExpectNonfatalFailureTest, CatchesNonfatalFailureOnAllThreads) { |
| 1671 | // We have another test below to verify that the macro catches |
| 1672 | // non-fatal failures generated on another thread. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1673 | EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddNonfatalFailure(), |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1674 | "Expected non-fatal failure."); |
| 1675 | } |
| 1676 | |
| 1677 | // Tests that the EXPECT_NONFATAL_FAILURE{,_ON_ALL_THREADS} accepts a |
| 1678 | // statement that contains a macro which expands to code containing an |
| 1679 | // unprotected comma. |
| 1680 | TEST_F(ExpectNonfatalFailureTest, AcceptsMacroThatExpandsToUnprotectedComma) { |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1681 | EXPECT_NONFATAL_FAILURE({ |
| 1682 | GTEST_USE_UNPROTECTED_COMMA_; |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1683 | AddNonfatalFailure(); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1684 | }, ""); |
| 1685 | |
| 1686 | EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS({ |
| 1687 | GTEST_USE_UNPROTECTED_COMMA_; |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1688 | AddNonfatalFailure(); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1689 | }, ""); |
| 1690 | } |
| 1691 | |
| 1692 | #if GTEST_IS_THREADSAFE && GTEST_HAS_PTHREAD |
| 1693 | |
| 1694 | typedef ScopedFakeTestPartResultReporterWithThreadsTest |
| 1695 | ExpectFailureWithThreadsTest; |
| 1696 | |
| 1697 | TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailureOnAllThreads) { |
| 1698 | EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailureInOtherThread(FATAL_FAILURE), |
| 1699 | "Expected fatal failure."); |
| 1700 | } |
| 1701 | |
| 1702 | TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailureOnAllThreads) { |
| 1703 | EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS( |
| 1704 | AddFailureInOtherThread(NONFATAL_FAILURE), "Expected non-fatal failure."); |
| 1705 | } |
| 1706 | |
| 1707 | #endif // GTEST_IS_THREADSAFE && GTEST_HAS_PTHREAD |
| 1708 | |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 1709 | // Tests the TestProperty class. |
| 1710 | |
| 1711 | TEST(TestPropertyTest, ConstructorWorks) { |
| 1712 | const TestProperty property("key", "value"); |
| 1713 | EXPECT_STREQ("key", property.key()); |
| 1714 | EXPECT_STREQ("value", property.value()); |
| 1715 | } |
| 1716 | |
| 1717 | TEST(TestPropertyTest, SetValue) { |
| 1718 | TestProperty property("key", "value_1"); |
| 1719 | EXPECT_STREQ("key", property.key()); |
| 1720 | property.SetValue("value_2"); |
| 1721 | EXPECT_STREQ("key", property.key()); |
| 1722 | EXPECT_STREQ("value_2", property.value()); |
| 1723 | } |
| 1724 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1725 | // Tests the TestResult class |
| 1726 | |
| 1727 | // The test fixture for testing TestResult. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1728 | class TestResultTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1729 | protected: |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 1730 | typedef Vector<TestPartResult> TPRVector; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1731 | |
| 1732 | // We make use of 2 TestPartResult objects, |
| 1733 | TestPartResult * pr1, * pr2; |
| 1734 | |
| 1735 | // ... and 3 TestResult objects. |
| 1736 | TestResult * r0, * r1, * r2; |
| 1737 | |
| 1738 | virtual void SetUp() { |
| 1739 | // pr1 is for success. |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 1740 | pr1 = new TestPartResult(TestPartResult::kSuccess, |
| 1741 | "foo/bar.cc", |
| 1742 | 10, |
| 1743 | "Success!"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1744 | |
| 1745 | // pr2 is for fatal failure. |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 1746 | pr2 = new TestPartResult(TestPartResult::kFatalFailure, |
| 1747 | "foo/bar.cc", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1748 | -1, // This line number means "unknown" |
| 1749 | "Failure!"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1750 | |
| 1751 | // Creates the TestResult objects. |
| 1752 | r0 = new TestResult(); |
| 1753 | r1 = new TestResult(); |
| 1754 | r2 = new TestResult(); |
| 1755 | |
| 1756 | // In order to test TestResult, we need to modify its internal |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 1757 | // state, in particular the TestPartResult Vector it holds. |
| 1758 | // test_part_results() returns a const reference to this Vector. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1759 | // We cast it to a non-const object s.t. it can be modified (yes, |
| 1760 | // this is a hack). |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 1761 | TPRVector* results1 = const_cast<Vector<TestPartResult> *>( |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1762 | &TestResultAccessor::test_part_results(*r1)); |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 1763 | TPRVector* results2 = const_cast<Vector<TestPartResult> *>( |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1764 | &TestResultAccessor::test_part_results(*r2)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1765 | |
| 1766 | // r0 is an empty TestResult. |
| 1767 | |
| 1768 | // r1 contains a single SUCCESS TestPartResult. |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 1769 | results1->PushBack(*pr1); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1770 | |
| 1771 | // r2 contains a SUCCESS, and a FAILURE. |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 1772 | results2->PushBack(*pr1); |
| 1773 | results2->PushBack(*pr2); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | virtual void TearDown() { |
| 1777 | delete pr1; |
| 1778 | delete pr2; |
| 1779 | |
| 1780 | delete r0; |
| 1781 | delete r1; |
| 1782 | delete r2; |
| 1783 | } |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1784 | |
| 1785 | // Helper that compares two two TestPartResults. |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1786 | static void CompareTestPartResult(const TestPartResult& expected, |
| 1787 | const TestPartResult& actual) { |
| 1788 | EXPECT_EQ(expected.type(), actual.type()); |
| 1789 | EXPECT_STREQ(expected.file_name(), actual.file_name()); |
| 1790 | EXPECT_EQ(expected.line_number(), actual.line_number()); |
| 1791 | EXPECT_STREQ(expected.summary(), actual.summary()); |
| 1792 | EXPECT_STREQ(expected.message(), actual.message()); |
| 1793 | EXPECT_EQ(expected.passed(), actual.passed()); |
| 1794 | EXPECT_EQ(expected.failed(), actual.failed()); |
| 1795 | EXPECT_EQ(expected.nonfatally_failed(), actual.nonfatally_failed()); |
| 1796 | EXPECT_EQ(expected.fatally_failed(), actual.fatally_failed()); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1797 | } |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1798 | }; |
| 1799 | |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1800 | // Tests TestResult::total_part_count(). |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1801 | TEST_F(TestResultTest, total_part_count) { |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1802 | ASSERT_EQ(0, r0->total_part_count()); |
| 1803 | ASSERT_EQ(1, r1->total_part_count()); |
| 1804 | ASSERT_EQ(2, r2->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1807 | // Tests TestResult::Passed(). |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1808 | TEST_F(TestResultTest, Passed) { |
| 1809 | ASSERT_TRUE(r0->Passed()); |
| 1810 | ASSERT_TRUE(r1->Passed()); |
| 1811 | ASSERT_FALSE(r2->Passed()); |
| 1812 | } |
| 1813 | |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1814 | // Tests TestResult::Failed(). |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1815 | TEST_F(TestResultTest, Failed) { |
| 1816 | ASSERT_FALSE(r0->Failed()); |
| 1817 | ASSERT_FALSE(r1->Failed()); |
| 1818 | ASSERT_TRUE(r2->Failed()); |
| 1819 | } |
| 1820 | |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1821 | // Tests TestResult::GetTestPartResult(). |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1822 | |
| 1823 | typedef TestResultTest TestResultDeathTest; |
| 1824 | |
| 1825 | TEST_F(TestResultDeathTest, GetTestPartResult) { |
| 1826 | CompareTestPartResult(*pr1, r2->GetTestPartResult(0)); |
| 1827 | CompareTestPartResult(*pr2, r2->GetTestPartResult(1)); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 1828 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1829 | r2->GetTestPartResult(2), |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 1830 | "Invalid Vector index 2: must be in range \\[0, 1\\]\\."); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 1831 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1832 | r2->GetTestPartResult(-1), |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 1833 | "Invalid Vector index -1: must be in range \\[0, 1\\]\\."); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1836 | // Tests TestResult has no properties when none are added. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1837 | TEST(TestResultPropertyTest, NoPropertiesFoundWhenNoneAreAdded) { |
| 1838 | TestResult test_result; |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1839 | ASSERT_EQ(0, test_result.test_property_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1842 | // Tests TestResult has the expected property when added. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1843 | TEST(TestResultPropertyTest, OnePropertyFoundWhenAdded) { |
| 1844 | TestResult test_result; |
| 1845 | TestProperty property("key_1", "1"); |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1846 | TestResultAccessor::RecordProperty(&test_result, property); |
| 1847 | ASSERT_EQ(1, test_result.test_property_count()); |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1848 | const TestProperty& actual_property = test_result.GetTestProperty(0); |
| 1849 | EXPECT_STREQ("key_1", actual_property.key()); |
| 1850 | EXPECT_STREQ("1", actual_property.value()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1853 | // Tests TestResult has multiple properties when added. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1854 | TEST(TestResultPropertyTest, MultiplePropertiesFoundWhenAdded) { |
| 1855 | TestResult test_result; |
| 1856 | TestProperty property_1("key_1", "1"); |
| 1857 | TestProperty property_2("key_2", "2"); |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1858 | TestResultAccessor::RecordProperty(&test_result, property_1); |
| 1859 | TestResultAccessor::RecordProperty(&test_result, property_2); |
| 1860 | ASSERT_EQ(2, test_result.test_property_count()); |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1861 | const TestProperty& actual_property_1 = test_result.GetTestProperty(0); |
| 1862 | EXPECT_STREQ("key_1", actual_property_1.key()); |
| 1863 | EXPECT_STREQ("1", actual_property_1.value()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1864 | |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1865 | const TestProperty& actual_property_2 = test_result.GetTestProperty(1); |
| 1866 | EXPECT_STREQ("key_2", actual_property_2.key()); |
| 1867 | EXPECT_STREQ("2", actual_property_2.value()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1870 | // Tests TestResult::RecordProperty() overrides values for duplicate keys. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1871 | TEST(TestResultPropertyTest, OverridesValuesForDuplicateKeys) { |
| 1872 | TestResult test_result; |
| 1873 | TestProperty property_1_1("key_1", "1"); |
| 1874 | TestProperty property_2_1("key_2", "2"); |
| 1875 | TestProperty property_1_2("key_1", "12"); |
| 1876 | TestProperty property_2_2("key_2", "22"); |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1877 | TestResultAccessor::RecordProperty(&test_result, property_1_1); |
| 1878 | TestResultAccessor::RecordProperty(&test_result, property_2_1); |
| 1879 | TestResultAccessor::RecordProperty(&test_result, property_1_2); |
| 1880 | TestResultAccessor::RecordProperty(&test_result, property_2_2); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1881 | |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1882 | ASSERT_EQ(2, test_result.test_property_count()); |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1883 | const TestProperty& actual_property_1 = test_result.GetTestProperty(0); |
| 1884 | EXPECT_STREQ("key_1", actual_property_1.key()); |
| 1885 | EXPECT_STREQ("12", actual_property_1.value()); |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1886 | |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1887 | const TestProperty& actual_property_2 = test_result.GetTestProperty(1); |
| 1888 | EXPECT_STREQ("key_2", actual_property_2.key()); |
| 1889 | EXPECT_STREQ("22", actual_property_2.value()); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | // Tests TestResult::GetTestProperty(). |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1893 | TEST(TestResultPropertyDeathTest, GetTestProperty) { |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1894 | TestResult test_result; |
| 1895 | TestProperty property_1("key_1", "1"); |
| 1896 | TestProperty property_2("key_2", "2"); |
| 1897 | TestProperty property_3("key_3", "3"); |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1898 | TestResultAccessor::RecordProperty(&test_result, property_1); |
| 1899 | TestResultAccessor::RecordProperty(&test_result, property_2); |
| 1900 | TestResultAccessor::RecordProperty(&test_result, property_3); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1901 | |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1902 | const TestProperty& fetched_property_1 = test_result.GetTestProperty(0); |
| 1903 | const TestProperty& fetched_property_2 = test_result.GetTestProperty(1); |
| 1904 | const TestProperty& fetched_property_3 = test_result.GetTestProperty(2); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1905 | |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1906 | EXPECT_STREQ("key_1", fetched_property_1.key()); |
| 1907 | EXPECT_STREQ("1", fetched_property_1.value()); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1908 | |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1909 | EXPECT_STREQ("key_2", fetched_property_2.key()); |
| 1910 | EXPECT_STREQ("2", fetched_property_2.value()); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1911 | |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1912 | EXPECT_STREQ("key_3", fetched_property_3.key()); |
| 1913 | EXPECT_STREQ("3", fetched_property_3.value()); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1914 | |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 1915 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1916 | test_result.GetTestProperty(3), |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 1917 | "Invalid Vector index 3: must be in range \\[0, 2\\]\\."); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 1918 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1919 | test_result.GetTestProperty(-1), |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 1920 | "Invalid Vector index -1: must be in range \\[0, 2\\]\\."); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1923 | // When a property using a reserved key is supplied to this function, it tests |
| 1924 | // that a non-fatal failure is added, a fatal failure is not added, and that the |
| 1925 | // property is not recorded. |
| 1926 | void ExpectNonFatalFailureRecordingPropertyWithReservedKey(const char* key) { |
| 1927 | TestResult test_result; |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame] | 1928 | TestProperty property(key, "1"); |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1929 | EXPECT_NONFATAL_FAILURE( |
| 1930 | TestResultAccessor::RecordProperty(&test_result, property), |
| 1931 | "Reserved key"); |
| 1932 | ASSERT_EQ(0, test_result.test_property_count()) << "Not recorded"; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | // Attempting to recording a property with the Reserved literal "name" |
| 1936 | // should add a non-fatal failure and the property should not be recorded. |
| 1937 | TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledName) { |
| 1938 | ExpectNonFatalFailureRecordingPropertyWithReservedKey("name"); |
| 1939 | } |
| 1940 | |
| 1941 | // Attempting to recording a property with the Reserved literal "status" |
| 1942 | // should add a non-fatal failure and the property should not be recorded. |
| 1943 | TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledStatus) { |
| 1944 | ExpectNonFatalFailureRecordingPropertyWithReservedKey("status"); |
| 1945 | } |
| 1946 | |
| 1947 | // Attempting to recording a property with the Reserved literal "time" |
| 1948 | // should add a non-fatal failure and the property should not be recorded. |
| 1949 | TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledTime) { |
| 1950 | ExpectNonFatalFailureRecordingPropertyWithReservedKey("time"); |
| 1951 | } |
| 1952 | |
| 1953 | // Attempting to recording a property with the Reserved literal "classname" |
| 1954 | // should add a non-fatal failure and the property should not be recorded. |
| 1955 | TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledClassname) { |
| 1956 | ExpectNonFatalFailureRecordingPropertyWithReservedKey("classname"); |
| 1957 | } |
| 1958 | |
| 1959 | // Tests that GTestFlagSaver works on Windows and Mac. |
| 1960 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1961 | class GTestFlagSaverTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1962 | protected: |
| 1963 | // Saves the Google Test flags such that we can restore them later, and |
| 1964 | // then sets them to their default values. This will be called |
| 1965 | // before the first test in this test case is run. |
| 1966 | static void SetUpTestCase() { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1967 | saver_ = new GTestFlagSaver; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1968 | |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 1969 | GTEST_FLAG(also_run_disabled_tests) = false; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1970 | GTEST_FLAG(break_on_failure) = false; |
| 1971 | GTEST_FLAG(catch_exceptions) = false; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 1972 | GTEST_FLAG(death_test_use_fork) = false; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1973 | GTEST_FLAG(color) = "auto"; |
| 1974 | GTEST_FLAG(filter) = ""; |
| 1975 | GTEST_FLAG(list_tests) = false; |
| 1976 | GTEST_FLAG(output) = ""; |
zhanyong.wan | 73ad5a3 | 2009-04-14 23:19:22 +0000 | [diff] [blame] | 1977 | GTEST_FLAG(print_time) = true; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 1978 | GTEST_FLAG(random_seed) = 0; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1979 | GTEST_FLAG(repeat) = 1; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 1980 | GTEST_FLAG(shuffle) = false; |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 1981 | GTEST_FLAG(stack_trace_depth) = kMaxStackTraceDepth; |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 1982 | GTEST_FLAG(throw_on_failure) = false; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | // Restores the Google Test flags that the tests have modified. This will |
| 1986 | // be called after the last test in this test case is run. |
| 1987 | static void TearDownTestCase() { |
| 1988 | delete saver_; |
| 1989 | saver_ = NULL; |
| 1990 | } |
| 1991 | |
| 1992 | // Verifies that the Google Test flags have their default values, and then |
| 1993 | // modifies each of them. |
| 1994 | void VerifyAndModifyFlags() { |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 1995 | EXPECT_FALSE(GTEST_FLAG(also_run_disabled_tests)); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1996 | EXPECT_FALSE(GTEST_FLAG(break_on_failure)); |
| 1997 | EXPECT_FALSE(GTEST_FLAG(catch_exceptions)); |
| 1998 | EXPECT_STREQ("auto", GTEST_FLAG(color).c_str()); |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 1999 | EXPECT_FALSE(GTEST_FLAG(death_test_use_fork)); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2000 | EXPECT_STREQ("", GTEST_FLAG(filter).c_str()); |
| 2001 | EXPECT_FALSE(GTEST_FLAG(list_tests)); |
| 2002 | EXPECT_STREQ("", GTEST_FLAG(output).c_str()); |
zhanyong.wan | 73ad5a3 | 2009-04-14 23:19:22 +0000 | [diff] [blame] | 2003 | EXPECT_TRUE(GTEST_FLAG(print_time)); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 2004 | EXPECT_EQ(0, GTEST_FLAG(random_seed)); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2005 | EXPECT_EQ(1, GTEST_FLAG(repeat)); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 2006 | EXPECT_FALSE(GTEST_FLAG(shuffle)); |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 2007 | EXPECT_EQ(kMaxStackTraceDepth, GTEST_FLAG(stack_trace_depth)); |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 2008 | EXPECT_FALSE(GTEST_FLAG(throw_on_failure)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2009 | |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 2010 | GTEST_FLAG(also_run_disabled_tests) = true; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2011 | GTEST_FLAG(break_on_failure) = true; |
| 2012 | GTEST_FLAG(catch_exceptions) = true; |
| 2013 | GTEST_FLAG(color) = "no"; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 2014 | GTEST_FLAG(death_test_use_fork) = true; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2015 | GTEST_FLAG(filter) = "abc"; |
| 2016 | GTEST_FLAG(list_tests) = true; |
| 2017 | GTEST_FLAG(output) = "xml:foo.xml"; |
zhanyong.wan | 73ad5a3 | 2009-04-14 23:19:22 +0000 | [diff] [blame] | 2018 | GTEST_FLAG(print_time) = false; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 2019 | GTEST_FLAG(random_seed) = 1; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2020 | GTEST_FLAG(repeat) = 100; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 2021 | GTEST_FLAG(shuffle) = true; |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 2022 | GTEST_FLAG(stack_trace_depth) = 1; |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 2023 | GTEST_FLAG(throw_on_failure) = true; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2024 | } |
| 2025 | private: |
| 2026 | // For saving Google Test flags during this test case. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2027 | static GTestFlagSaver* saver_; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2028 | }; |
| 2029 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2030 | GTestFlagSaver* GTestFlagSaverTest::saver_ = NULL; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2031 | |
| 2032 | // Google Test doesn't guarantee the order of tests. The following two |
| 2033 | // tests are designed to work regardless of their order. |
| 2034 | |
| 2035 | // Modifies the Google Test flags in the test body. |
| 2036 | TEST_F(GTestFlagSaverTest, ModifyGTestFlags) { |
| 2037 | VerifyAndModifyFlags(); |
| 2038 | } |
| 2039 | |
| 2040 | // Verifies that the Google Test flags in the body of the previous test were |
| 2041 | // restored to their original values. |
| 2042 | TEST_F(GTestFlagSaverTest, VerifyGTestFlags) { |
| 2043 | VerifyAndModifyFlags(); |
| 2044 | } |
| 2045 | |
| 2046 | // Sets an environment variable with the given name to the given |
| 2047 | // value. If the value argument is "", unsets the environment |
| 2048 | // variable. The caller must ensure that both arguments are not NULL. |
| 2049 | static void SetEnv(const char* name, const char* value) { |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 2050 | #if GTEST_OS_WINDOWS_MOBILE |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2051 | // Environment variables are not supported on Windows CE. |
| 2052 | return; |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2053 | #elif defined(__BORLANDC__) |
| 2054 | // C++Builder's putenv only stores a pointer to its parameter; we have to |
| 2055 | // ensure that the string remains valid as long as it might be needed. |
| 2056 | // We use an std::map to do so. |
| 2057 | static std::map<String, String*> added_env; |
| 2058 | |
| 2059 | // Because putenv stores a pointer to the string buffer, we can't delete the |
| 2060 | // previous string (if present) until after it's replaced. |
| 2061 | String *prev_env = NULL; |
| 2062 | if (added_env.find(name) != added_env.end()) { |
| 2063 | prev_env = added_env[name]; |
| 2064 | } |
| 2065 | added_env[name] = new String((Message() << name << "=" << value).GetString()); |
| 2066 | putenv(added_env[name]->c_str()); |
| 2067 | delete prev_env; |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2068 | #elif GTEST_OS_WINDOWS // If we are on Windows proper. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2069 | _putenv((Message() << name << "=" << value).GetString().c_str()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2070 | #else |
| 2071 | if (*value == '\0') { |
| 2072 | unsetenv(name); |
| 2073 | } else { |
| 2074 | setenv(name, value, 1); |
| 2075 | } |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 2076 | #endif // GTEST_OS_WINDOWS_MOBILE |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 2079 | #if !GTEST_OS_WINDOWS_MOBILE |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2080 | // Environment variables are not supported on Windows CE. |
| 2081 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2082 | using testing::internal::Int32FromGTestEnv; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2083 | |
| 2084 | // Tests Int32FromGTestEnv(). |
| 2085 | |
| 2086 | // Tests that Int32FromGTestEnv() returns the default value when the |
| 2087 | // environment variable is not set. |
| 2088 | TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenVariableIsNotSet) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2089 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", ""); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2090 | EXPECT_EQ(10, Int32FromGTestEnv("temp", 10)); |
| 2091 | } |
| 2092 | |
| 2093 | // Tests that Int32FromGTestEnv() returns the default value when the |
| 2094 | // environment variable overflows as an Int32. |
| 2095 | TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenValueOverflows) { |
| 2096 | printf("(expecting 2 warnings)\n"); |
| 2097 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2098 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "12345678987654321"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2099 | EXPECT_EQ(20, Int32FromGTestEnv("temp", 20)); |
| 2100 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2101 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "-12345678987654321"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2102 | EXPECT_EQ(30, Int32FromGTestEnv("temp", 30)); |
| 2103 | } |
| 2104 | |
| 2105 | // Tests that Int32FromGTestEnv() returns the default value when the |
| 2106 | // environment variable does not represent a valid decimal integer. |
| 2107 | TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenValueIsInvalid) { |
| 2108 | printf("(expecting 2 warnings)\n"); |
| 2109 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2110 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "A1"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2111 | EXPECT_EQ(40, Int32FromGTestEnv("temp", 40)); |
| 2112 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2113 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "12X"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2114 | EXPECT_EQ(50, Int32FromGTestEnv("temp", 50)); |
| 2115 | } |
| 2116 | |
| 2117 | // Tests that Int32FromGTestEnv() parses and returns the value of the |
| 2118 | // environment variable when it represents a valid decimal integer in |
| 2119 | // the range of an Int32. |
| 2120 | TEST(Int32FromGTestEnvTest, ParsesAndReturnsValidValue) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2121 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "123"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2122 | EXPECT_EQ(123, Int32FromGTestEnv("temp", 0)); |
| 2123 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2124 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "-321"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2125 | EXPECT_EQ(-321, Int32FromGTestEnv("temp", 0)); |
| 2126 | } |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 2127 | #endif // !GTEST_OS_WINDOWS_MOBILE |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2128 | |
| 2129 | // Tests ParseInt32Flag(). |
| 2130 | |
| 2131 | // Tests that ParseInt32Flag() returns false and doesn't change the |
| 2132 | // output value when the flag has wrong format |
| 2133 | TEST(ParseInt32FlagTest, ReturnsFalseForInvalidFlag) { |
| 2134 | Int32 value = 123; |
| 2135 | EXPECT_FALSE(ParseInt32Flag("--a=100", "b", &value)); |
| 2136 | EXPECT_EQ(123, value); |
| 2137 | |
| 2138 | EXPECT_FALSE(ParseInt32Flag("a=100", "a", &value)); |
| 2139 | EXPECT_EQ(123, value); |
| 2140 | } |
| 2141 | |
| 2142 | // Tests that ParseInt32Flag() returns false and doesn't change the |
| 2143 | // output value when the flag overflows as an Int32. |
| 2144 | TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueOverflows) { |
| 2145 | printf("(expecting 2 warnings)\n"); |
| 2146 | |
| 2147 | Int32 value = 123; |
| 2148 | EXPECT_FALSE(ParseInt32Flag("--abc=12345678987654321", "abc", &value)); |
| 2149 | EXPECT_EQ(123, value); |
| 2150 | |
| 2151 | EXPECT_FALSE(ParseInt32Flag("--abc=-12345678987654321", "abc", &value)); |
| 2152 | EXPECT_EQ(123, value); |
| 2153 | } |
| 2154 | |
| 2155 | // Tests that ParseInt32Flag() returns false and doesn't change the |
| 2156 | // output value when the flag does not represent a valid decimal |
| 2157 | // integer. |
| 2158 | TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueIsInvalid) { |
| 2159 | printf("(expecting 2 warnings)\n"); |
| 2160 | |
| 2161 | Int32 value = 123; |
| 2162 | EXPECT_FALSE(ParseInt32Flag("--abc=A1", "abc", &value)); |
| 2163 | EXPECT_EQ(123, value); |
| 2164 | |
| 2165 | EXPECT_FALSE(ParseInt32Flag("--abc=12X", "abc", &value)); |
| 2166 | EXPECT_EQ(123, value); |
| 2167 | } |
| 2168 | |
| 2169 | // Tests that ParseInt32Flag() parses the value of the flag and |
| 2170 | // returns true when the flag represents a valid decimal integer in |
| 2171 | // the range of an Int32. |
| 2172 | TEST(ParseInt32FlagTest, ParsesAndReturnsValidValue) { |
| 2173 | Int32 value = 123; |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2174 | EXPECT_TRUE(ParseInt32Flag("--" GTEST_FLAG_PREFIX_ "abc=456", "abc", &value)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2175 | EXPECT_EQ(456, value); |
| 2176 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2177 | EXPECT_TRUE(ParseInt32Flag("--" GTEST_FLAG_PREFIX_ "abc=-789", |
| 2178 | "abc", &value)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2179 | EXPECT_EQ(-789, value); |
| 2180 | } |
| 2181 | |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2182 | // Tests that Int32FromEnvOrDie() parses the value of the var or |
| 2183 | // returns the correct default. |
zhanyong.wan | c427f5e | 2009-06-19 17:23:54 +0000 | [diff] [blame] | 2184 | // Environment variables are not supported on Windows CE. |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 2185 | #if !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2186 | TEST(Int32FromEnvOrDieTest, ParsesAndReturnsValidValue) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2187 | EXPECT_EQ(333, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333)); |
| 2188 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "123"); |
| 2189 | EXPECT_EQ(123, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333)); |
| 2190 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "-123"); |
| 2191 | EXPECT_EQ(-123, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333)); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2192 | } |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 2193 | #endif // !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2194 | |
| 2195 | // Tests that Int32FromEnvOrDie() aborts with an error message |
| 2196 | // if the variable is not an Int32. |
| 2197 | TEST(Int32FromEnvOrDieDeathTest, AbortsOnFailure) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2198 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "xxx"); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 2199 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 2200 | Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123), |
| 2201 | ".*"); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | // Tests that Int32FromEnvOrDie() aborts with an error message |
| 2205 | // if the variable cannot be represnted by an Int32. |
| 2206 | TEST(Int32FromEnvOrDieDeathTest, AbortsOnInt32Overflow) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2207 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "1234567891234567891234"); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 2208 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 2209 | Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123), |
| 2210 | ".*"); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2213 | // Tests that ShouldRunTestOnShard() selects all tests |
| 2214 | // where there is 1 shard. |
| 2215 | TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereIsOneShard) { |
| 2216 | EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 0)); |
| 2217 | EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 1)); |
| 2218 | EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 2)); |
| 2219 | EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 3)); |
| 2220 | EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 4)); |
| 2221 | } |
| 2222 | |
| 2223 | class ShouldShardTest : public testing::Test { |
| 2224 | protected: |
| 2225 | virtual void SetUp() { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2226 | index_var_ = GTEST_FLAG_PREFIX_UPPER_ "INDEX"; |
| 2227 | total_var_ = GTEST_FLAG_PREFIX_UPPER_ "TOTAL"; |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | virtual void TearDown() { |
| 2231 | SetEnv(index_var_, ""); |
| 2232 | SetEnv(total_var_, ""); |
| 2233 | } |
| 2234 | |
| 2235 | const char* index_var_; |
| 2236 | const char* total_var_; |
| 2237 | }; |
| 2238 | |
| 2239 | // Tests that sharding is disabled if neither of the environment variables |
| 2240 | // are set. |
| 2241 | TEST_F(ShouldShardTest, ReturnsFalseWhenNeitherEnvVarIsSet) { |
| 2242 | SetEnv(index_var_, ""); |
| 2243 | SetEnv(total_var_, ""); |
| 2244 | |
| 2245 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, false)); |
| 2246 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); |
| 2247 | } |
| 2248 | |
| 2249 | // Tests that sharding is not enabled if total_shards == 1. |
| 2250 | TEST_F(ShouldShardTest, ReturnsFalseWhenTotalShardIsOne) { |
| 2251 | SetEnv(index_var_, "0"); |
| 2252 | SetEnv(total_var_, "1"); |
| 2253 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, false)); |
| 2254 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); |
| 2255 | } |
| 2256 | |
| 2257 | // Tests that sharding is enabled if total_shards > 1 and |
| 2258 | // we are not in a death test subprocess. |
zhanyong.wan | c427f5e | 2009-06-19 17:23:54 +0000 | [diff] [blame] | 2259 | // Environment variables are not supported on Windows CE. |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 2260 | #if !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2261 | TEST_F(ShouldShardTest, WorksWhenShardEnvVarsAreValid) { |
| 2262 | SetEnv(index_var_, "4"); |
| 2263 | SetEnv(total_var_, "22"); |
| 2264 | EXPECT_TRUE(ShouldShard(total_var_, index_var_, false)); |
| 2265 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); |
| 2266 | |
| 2267 | SetEnv(index_var_, "8"); |
| 2268 | SetEnv(total_var_, "9"); |
| 2269 | EXPECT_TRUE(ShouldShard(total_var_, index_var_, false)); |
| 2270 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); |
| 2271 | |
| 2272 | SetEnv(index_var_, "0"); |
| 2273 | SetEnv(total_var_, "9"); |
| 2274 | EXPECT_TRUE(ShouldShard(total_var_, index_var_, false)); |
| 2275 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); |
| 2276 | } |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 2277 | #endif // !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2278 | |
| 2279 | // Tests that we exit in error if the sharding values are not valid. |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 2280 | |
| 2281 | typedef ShouldShardTest ShouldShardDeathTest; |
| 2282 | |
| 2283 | TEST_F(ShouldShardDeathTest, AbortsWhenShardingEnvVarsAreInvalid) { |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2284 | SetEnv(index_var_, "4"); |
| 2285 | SetEnv(total_var_, "4"); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 2286 | EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2287 | |
| 2288 | SetEnv(index_var_, "4"); |
| 2289 | SetEnv(total_var_, "-2"); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 2290 | EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2291 | |
| 2292 | SetEnv(index_var_, "5"); |
| 2293 | SetEnv(total_var_, ""); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 2294 | EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2295 | |
| 2296 | SetEnv(index_var_, ""); |
| 2297 | SetEnv(total_var_, "5"); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 2298 | EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2301 | // Tests that ShouldRunTestOnShard is a partition when 5 |
| 2302 | // shards are used. |
| 2303 | TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereAreFiveShards) { |
| 2304 | // Choose an arbitrary number of tests and shards. |
| 2305 | const int num_tests = 17; |
| 2306 | const int num_shards = 5; |
| 2307 | |
| 2308 | // Check partitioning: each test should be on exactly 1 shard. |
| 2309 | for (int test_id = 0; test_id < num_tests; test_id++) { |
| 2310 | int prev_selected_shard_index = -1; |
| 2311 | for (int shard_index = 0; shard_index < num_shards; shard_index++) { |
| 2312 | if (ShouldRunTestOnShard(num_shards, shard_index, test_id)) { |
| 2313 | if (prev_selected_shard_index < 0) { |
| 2314 | prev_selected_shard_index = shard_index; |
| 2315 | } else { |
| 2316 | ADD_FAILURE() << "Shard " << prev_selected_shard_index << " and " |
| 2317 | << shard_index << " are both selected to run test " << test_id; |
| 2318 | } |
| 2319 | } |
| 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | // Check balance: This is not required by the sharding protocol, but is a |
| 2324 | // desirable property for performance. |
| 2325 | for (int shard_index = 0; shard_index < num_shards; shard_index++) { |
| 2326 | int num_tests_on_shard = 0; |
| 2327 | for (int test_id = 0; test_id < num_tests; test_id++) { |
| 2328 | num_tests_on_shard += |
| 2329 | ShouldRunTestOnShard(num_shards, shard_index, test_id); |
| 2330 | } |
| 2331 | EXPECT_GE(num_tests_on_shard, num_tests / num_shards); |
| 2332 | } |
| 2333 | } |
| 2334 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2335 | // For the same reason we are not explicitly testing everything in the |
shiqian | c3b4de3 | 2008-09-12 04:01:37 +0000 | [diff] [blame] | 2336 | // Test class, there are no separate tests for the following classes |
| 2337 | // (except for some trivial cases): |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2338 | // |
| 2339 | // TestCase, UnitTest, UnitTestResultPrinter. |
| 2340 | // |
| 2341 | // Similarly, there are no separate tests for the following macros: |
| 2342 | // |
| 2343 | // TEST, TEST_F, RUN_ALL_TESTS |
| 2344 | |
shiqian | c3b4de3 | 2008-09-12 04:01:37 +0000 | [diff] [blame] | 2345 | TEST(UnitTestTest, CanGetOriginalWorkingDir) { |
| 2346 | ASSERT_TRUE(UnitTest::GetInstance()->original_working_dir() != NULL); |
| 2347 | EXPECT_STRNE(UnitTest::GetInstance()->original_working_dir(), ""); |
| 2348 | } |
| 2349 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2350 | // This group of tests is for predicate assertions (ASSERT_PRED*, etc) |
| 2351 | // of various arities. They do not attempt to be exhaustive. Rather, |
| 2352 | // view them as smoke tests that can be easily reviewed and verified. |
| 2353 | // A more complete set of tests for predicate assertions can be found |
| 2354 | // in gtest_pred_impl_unittest.cc. |
| 2355 | |
| 2356 | // First, some predicates and predicate-formatters needed by the tests. |
| 2357 | |
| 2358 | // Returns true iff the argument is an even number. |
| 2359 | bool IsEven(int n) { |
| 2360 | return (n % 2) == 0; |
| 2361 | } |
| 2362 | |
| 2363 | // A functor that returns true iff the argument is an even number. |
| 2364 | struct IsEvenFunctor { |
| 2365 | bool operator()(int n) { return IsEven(n); } |
| 2366 | }; |
| 2367 | |
| 2368 | // A predicate-formatter function that asserts the argument is an even |
| 2369 | // number. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2370 | AssertionResult AssertIsEven(const char* expr, int n) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2371 | if (IsEven(n)) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2372 | return AssertionSuccess(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2375 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2376 | msg << expr << " evaluates to " << n << ", which is not even."; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2377 | return AssertionFailure(msg); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 2380 | // A predicate function that returns AssertionResult for use in |
| 2381 | // EXPECT/ASSERT_TRUE/FALSE. |
| 2382 | AssertionResult ResultIsEven(int n) { |
| 2383 | if (IsEven(n)) |
| 2384 | return AssertionSuccess() << n << " is even"; |
| 2385 | else |
| 2386 | return AssertionFailure() << n << " is odd"; |
| 2387 | } |
| 2388 | |
| 2389 | // A predicate function that returns AssertionResult but gives no |
| 2390 | // explanation why it succeeds. Needed for testing that |
| 2391 | // EXPECT/ASSERT_FALSE handles such functions correctly. |
| 2392 | AssertionResult ResultIsEvenNoExplanation(int n) { |
| 2393 | if (IsEven(n)) |
| 2394 | return AssertionSuccess(); |
| 2395 | else |
| 2396 | return AssertionFailure() << n << " is odd"; |
| 2397 | } |
| 2398 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2399 | // A predicate-formatter functor that asserts the argument is an even |
| 2400 | // number. |
| 2401 | struct AssertIsEvenFunctor { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2402 | AssertionResult operator()(const char* expr, int n) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2403 | return AssertIsEven(expr, n); |
| 2404 | } |
| 2405 | }; |
| 2406 | |
| 2407 | // Returns true iff the sum of the arguments is an even number. |
| 2408 | bool SumIsEven2(int n1, int n2) { |
| 2409 | return IsEven(n1 + n2); |
| 2410 | } |
| 2411 | |
| 2412 | // A functor that returns true iff the sum of the arguments is an even |
| 2413 | // number. |
| 2414 | struct SumIsEven3Functor { |
| 2415 | bool operator()(int n1, int n2, int n3) { |
| 2416 | return IsEven(n1 + n2 + n3); |
| 2417 | } |
| 2418 | }; |
| 2419 | |
| 2420 | // A predicate-formatter function that asserts the sum of the |
| 2421 | // arguments is an even number. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2422 | AssertionResult AssertSumIsEven4( |
| 2423 | const char* e1, const char* e2, const char* e3, const char* e4, |
| 2424 | int n1, int n2, int n3, int n4) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2425 | const int sum = n1 + n2 + n3 + n4; |
| 2426 | if (IsEven(sum)) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2427 | return AssertionSuccess(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2430 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2431 | msg << e1 << " + " << e2 << " + " << e3 << " + " << e4 |
| 2432 | << " (" << n1 << " + " << n2 << " + " << n3 << " + " << n4 |
| 2433 | << ") evaluates to " << sum << ", which is not even."; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2434 | return AssertionFailure(msg); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | // A predicate-formatter functor that asserts the sum of the arguments |
| 2438 | // is an even number. |
| 2439 | struct AssertSumIsEven5Functor { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2440 | AssertionResult operator()( |
| 2441 | const char* e1, const char* e2, const char* e3, const char* e4, |
| 2442 | const char* e5, int n1, int n2, int n3, int n4, int n5) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2443 | const int sum = n1 + n2 + n3 + n4 + n5; |
| 2444 | if (IsEven(sum)) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2445 | return AssertionSuccess(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2448 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2449 | msg << e1 << " + " << e2 << " + " << e3 << " + " << e4 << " + " << e5 |
| 2450 | << " (" |
| 2451 | << n1 << " + " << n2 << " + " << n3 << " + " << n4 << " + " << n5 |
| 2452 | << ") evaluates to " << sum << ", which is not even."; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2453 | return AssertionFailure(msg); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2454 | } |
| 2455 | }; |
| 2456 | |
| 2457 | |
| 2458 | // Tests unary predicate assertions. |
| 2459 | |
| 2460 | // Tests unary predicate assertions that don't use a custom formatter. |
| 2461 | TEST(Pred1Test, WithoutFormat) { |
| 2462 | // Success cases. |
| 2463 | EXPECT_PRED1(IsEvenFunctor(), 2) << "This failure is UNEXPECTED!"; |
| 2464 | ASSERT_PRED1(IsEven, 4); |
| 2465 | |
| 2466 | // Failure cases. |
| 2467 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 2468 | EXPECT_PRED1(IsEven, 5) << "This failure is expected."; |
| 2469 | }, "This failure is expected."); |
| 2470 | EXPECT_FATAL_FAILURE(ASSERT_PRED1(IsEvenFunctor(), 5), |
| 2471 | "evaluates to false"); |
| 2472 | } |
| 2473 | |
| 2474 | // Tests unary predicate assertions that use a custom formatter. |
| 2475 | TEST(Pred1Test, WithFormat) { |
| 2476 | // Success cases. |
| 2477 | EXPECT_PRED_FORMAT1(AssertIsEven, 2); |
| 2478 | ASSERT_PRED_FORMAT1(AssertIsEvenFunctor(), 4) |
| 2479 | << "This failure is UNEXPECTED!"; |
| 2480 | |
| 2481 | // Failure cases. |
| 2482 | const int n = 5; |
| 2483 | EXPECT_NONFATAL_FAILURE(EXPECT_PRED_FORMAT1(AssertIsEvenFunctor(), n), |
| 2484 | "n evaluates to 5, which is not even."); |
| 2485 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 2486 | ASSERT_PRED_FORMAT1(AssertIsEven, 5) << "This failure is expected."; |
| 2487 | }, "This failure is expected."); |
| 2488 | } |
| 2489 | |
| 2490 | // Tests that unary predicate assertions evaluates their arguments |
| 2491 | // exactly once. |
| 2492 | TEST(Pred1Test, SingleEvaluationOnFailure) { |
| 2493 | // A success case. |
| 2494 | static int n = 0; |
| 2495 | EXPECT_PRED1(IsEven, n++); |
| 2496 | EXPECT_EQ(1, n) << "The argument is not evaluated exactly once."; |
| 2497 | |
| 2498 | // A failure case. |
| 2499 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 2500 | ASSERT_PRED_FORMAT1(AssertIsEvenFunctor(), n++) |
| 2501 | << "This failure is expected."; |
| 2502 | }, "This failure is expected."); |
| 2503 | EXPECT_EQ(2, n) << "The argument is not evaluated exactly once."; |
| 2504 | } |
| 2505 | |
| 2506 | |
| 2507 | // Tests predicate assertions whose arity is >= 2. |
| 2508 | |
| 2509 | // Tests predicate assertions that don't use a custom formatter. |
| 2510 | TEST(PredTest, WithoutFormat) { |
| 2511 | // Success cases. |
| 2512 | ASSERT_PRED2(SumIsEven2, 2, 4) << "This failure is UNEXPECTED!"; |
| 2513 | EXPECT_PRED3(SumIsEven3Functor(), 4, 6, 8); |
| 2514 | |
| 2515 | // Failure cases. |
| 2516 | const int n1 = 1; |
| 2517 | const int n2 = 2; |
| 2518 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 2519 | EXPECT_PRED2(SumIsEven2, n1, n2) << "This failure is expected."; |
| 2520 | }, "This failure is expected."); |
| 2521 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 2522 | ASSERT_PRED3(SumIsEven3Functor(), 1, 2, 4); |
| 2523 | }, "evaluates to false"); |
| 2524 | } |
| 2525 | |
| 2526 | // Tests predicate assertions that use a custom formatter. |
| 2527 | TEST(PredTest, WithFormat) { |
| 2528 | // Success cases. |
| 2529 | ASSERT_PRED_FORMAT4(AssertSumIsEven4, 4, 6, 8, 10) << |
| 2530 | "This failure is UNEXPECTED!"; |
| 2531 | EXPECT_PRED_FORMAT5(AssertSumIsEven5Functor(), 2, 4, 6, 8, 10); |
| 2532 | |
| 2533 | // Failure cases. |
| 2534 | const int n1 = 1; |
| 2535 | const int n2 = 2; |
| 2536 | const int n3 = 4; |
| 2537 | const int n4 = 6; |
| 2538 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 2539 | EXPECT_PRED_FORMAT4(AssertSumIsEven4, n1, n2, n3, n4); |
| 2540 | }, "evaluates to 13, which is not even."); |
| 2541 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 2542 | ASSERT_PRED_FORMAT5(AssertSumIsEven5Functor(), 1, 2, 4, 6, 8) |
| 2543 | << "This failure is expected."; |
| 2544 | }, "This failure is expected."); |
| 2545 | } |
| 2546 | |
| 2547 | // Tests that predicate assertions evaluates their arguments |
| 2548 | // exactly once. |
| 2549 | TEST(PredTest, SingleEvaluationOnFailure) { |
| 2550 | // A success case. |
| 2551 | int n1 = 0; |
| 2552 | int n2 = 0; |
| 2553 | EXPECT_PRED2(SumIsEven2, n1++, n2++); |
| 2554 | EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; |
| 2555 | EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; |
| 2556 | |
| 2557 | // Another success case. |
| 2558 | n1 = n2 = 0; |
| 2559 | int n3 = 0; |
| 2560 | int n4 = 0; |
| 2561 | int n5 = 0; |
| 2562 | ASSERT_PRED_FORMAT5(AssertSumIsEven5Functor(), |
| 2563 | n1++, n2++, n3++, n4++, n5++) |
| 2564 | << "This failure is UNEXPECTED!"; |
| 2565 | EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; |
| 2566 | EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; |
| 2567 | EXPECT_EQ(1, n3) << "Argument 3 is not evaluated exactly once."; |
| 2568 | EXPECT_EQ(1, n4) << "Argument 4 is not evaluated exactly once."; |
| 2569 | EXPECT_EQ(1, n5) << "Argument 5 is not evaluated exactly once."; |
| 2570 | |
| 2571 | // A failure case. |
| 2572 | n1 = n2 = n3 = 0; |
| 2573 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 2574 | EXPECT_PRED3(SumIsEven3Functor(), ++n1, n2++, n3++) |
| 2575 | << "This failure is expected."; |
| 2576 | }, "This failure is expected."); |
| 2577 | EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; |
| 2578 | EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; |
| 2579 | EXPECT_EQ(1, n3) << "Argument 3 is not evaluated exactly once."; |
| 2580 | |
| 2581 | // Another failure case. |
| 2582 | n1 = n2 = n3 = n4 = 0; |
| 2583 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 2584 | EXPECT_PRED_FORMAT4(AssertSumIsEven4, ++n1, n2++, n3++, n4++); |
| 2585 | }, "evaluates to 1, which is not even."); |
| 2586 | EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; |
| 2587 | EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; |
| 2588 | EXPECT_EQ(1, n3) << "Argument 3 is not evaluated exactly once."; |
| 2589 | EXPECT_EQ(1, n4) << "Argument 4 is not evaluated exactly once."; |
| 2590 | } |
| 2591 | |
| 2592 | |
| 2593 | // Some helper functions for testing using overloaded/template |
| 2594 | // functions with ASSERT_PREDn and EXPECT_PREDn. |
| 2595 | |
| 2596 | bool IsPositive(int n) { |
| 2597 | return n > 0; |
| 2598 | } |
| 2599 | |
| 2600 | bool IsPositive(double x) { |
| 2601 | return x > 0; |
| 2602 | } |
| 2603 | |
| 2604 | template <typename T> |
| 2605 | bool IsNegative(T x) { |
| 2606 | return x < 0; |
| 2607 | } |
| 2608 | |
| 2609 | template <typename T1, typename T2> |
| 2610 | bool GreaterThan(T1 x1, T2 x2) { |
| 2611 | return x1 > x2; |
| 2612 | } |
| 2613 | |
| 2614 | // Tests that overloaded functions can be used in *_PRED* as long as |
| 2615 | // their types are explicitly specified. |
| 2616 | TEST(PredicateAssertionTest, AcceptsOverloadedFunction) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2617 | // C++Builder requires C-style casts rather than static_cast. |
| 2618 | EXPECT_PRED1((bool (*)(int))(IsPositive), 5); // NOLINT |
| 2619 | ASSERT_PRED1((bool (*)(double))(IsPositive), 6.0); // NOLINT |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | // Tests that template functions can be used in *_PRED* as long as |
| 2623 | // their types are explicitly specified. |
| 2624 | TEST(PredicateAssertionTest, AcceptsTemplateFunction) { |
| 2625 | EXPECT_PRED1(IsNegative<int>, -5); |
| 2626 | // Makes sure that we can handle templates with more than one |
| 2627 | // parameter. |
| 2628 | ASSERT_PRED2((GreaterThan<int, int>), 5, 0); |
| 2629 | } |
| 2630 | |
| 2631 | |
| 2632 | // Some helper functions for testing using overloaded/template |
| 2633 | // functions with ASSERT_PRED_FORMATn and EXPECT_PRED_FORMATn. |
| 2634 | |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame] | 2635 | AssertionResult IsPositiveFormat(const char* /* expr */, int n) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2636 | return n > 0 ? AssertionSuccess() : |
| 2637 | AssertionFailure(Message() << "Failure"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame] | 2640 | AssertionResult IsPositiveFormat(const char* /* expr */, double x) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2641 | return x > 0 ? AssertionSuccess() : |
| 2642 | AssertionFailure(Message() << "Failure"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | template <typename T> |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame] | 2646 | AssertionResult IsNegativeFormat(const char* /* expr */, T x) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2647 | return x < 0 ? AssertionSuccess() : |
| 2648 | AssertionFailure(Message() << "Failure"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | template <typename T1, typename T2> |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame] | 2652 | AssertionResult EqualsFormat(const char* /* expr1 */, const char* /* expr2 */, |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2653 | const T1& x1, const T2& x2) { |
| 2654 | return x1 == x2 ? AssertionSuccess() : |
| 2655 | AssertionFailure(Message() << "Failure"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | // Tests that overloaded functions can be used in *_PRED_FORMAT* |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame] | 2659 | // without explicitly specifying their types. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2660 | TEST(PredicateFormatAssertionTest, AcceptsOverloadedFunction) { |
| 2661 | EXPECT_PRED_FORMAT1(IsPositiveFormat, 5); |
| 2662 | ASSERT_PRED_FORMAT1(IsPositiveFormat, 6.0); |
| 2663 | } |
| 2664 | |
| 2665 | // Tests that template functions can be used in *_PRED_FORMAT* without |
| 2666 | // explicitly specifying their types. |
| 2667 | TEST(PredicateFormatAssertionTest, AcceptsTemplateFunction) { |
| 2668 | EXPECT_PRED_FORMAT1(IsNegativeFormat, -5); |
| 2669 | ASSERT_PRED_FORMAT2(EqualsFormat, 3, 3); |
| 2670 | } |
| 2671 | |
| 2672 | |
| 2673 | // Tests string assertions. |
| 2674 | |
| 2675 | // Tests ASSERT_STREQ with non-NULL arguments. |
| 2676 | TEST(StringAssertionTest, ASSERT_STREQ) { |
| 2677 | const char * const p1 = "good"; |
| 2678 | ASSERT_STREQ(p1, p1); |
| 2679 | |
| 2680 | // Let p2 have the same content as p1, but be at a different address. |
| 2681 | const char p2[] = "good"; |
| 2682 | ASSERT_STREQ(p1, p2); |
| 2683 | |
| 2684 | EXPECT_FATAL_FAILURE(ASSERT_STREQ("bad", "good"), |
| 2685 | "Expected: \"bad\""); |
| 2686 | } |
| 2687 | |
| 2688 | // Tests ASSERT_STREQ with NULL arguments. |
| 2689 | TEST(StringAssertionTest, ASSERT_STREQ_Null) { |
| 2690 | ASSERT_STREQ(static_cast<const char *>(NULL), NULL); |
| 2691 | EXPECT_FATAL_FAILURE(ASSERT_STREQ(NULL, "non-null"), |
| 2692 | "non-null"); |
| 2693 | } |
| 2694 | |
| 2695 | // Tests ASSERT_STREQ with NULL arguments. |
| 2696 | TEST(StringAssertionTest, ASSERT_STREQ_Null2) { |
| 2697 | EXPECT_FATAL_FAILURE(ASSERT_STREQ("non-null", NULL), |
| 2698 | "non-null"); |
| 2699 | } |
| 2700 | |
| 2701 | // Tests ASSERT_STRNE. |
| 2702 | TEST(StringAssertionTest, ASSERT_STRNE) { |
| 2703 | ASSERT_STRNE("hi", "Hi"); |
| 2704 | ASSERT_STRNE("Hi", NULL); |
| 2705 | ASSERT_STRNE(NULL, "Hi"); |
| 2706 | ASSERT_STRNE("", NULL); |
| 2707 | ASSERT_STRNE(NULL, ""); |
| 2708 | ASSERT_STRNE("", "Hi"); |
| 2709 | ASSERT_STRNE("Hi", ""); |
| 2710 | EXPECT_FATAL_FAILURE(ASSERT_STRNE("Hi", "Hi"), |
| 2711 | "\"Hi\" vs \"Hi\""); |
| 2712 | } |
| 2713 | |
| 2714 | // Tests ASSERT_STRCASEEQ. |
| 2715 | TEST(StringAssertionTest, ASSERT_STRCASEEQ) { |
| 2716 | ASSERT_STRCASEEQ("hi", "Hi"); |
| 2717 | ASSERT_STRCASEEQ(static_cast<const char *>(NULL), NULL); |
| 2718 | |
| 2719 | ASSERT_STRCASEEQ("", ""); |
| 2720 | EXPECT_FATAL_FAILURE(ASSERT_STRCASEEQ("Hi", "hi2"), |
| 2721 | "(ignoring case)"); |
| 2722 | } |
| 2723 | |
| 2724 | // Tests ASSERT_STRCASENE. |
| 2725 | TEST(StringAssertionTest, ASSERT_STRCASENE) { |
| 2726 | ASSERT_STRCASENE("hi1", "Hi2"); |
| 2727 | ASSERT_STRCASENE("Hi", NULL); |
| 2728 | ASSERT_STRCASENE(NULL, "Hi"); |
| 2729 | ASSERT_STRCASENE("", NULL); |
| 2730 | ASSERT_STRCASENE(NULL, ""); |
| 2731 | ASSERT_STRCASENE("", "Hi"); |
| 2732 | ASSERT_STRCASENE("Hi", ""); |
| 2733 | EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("Hi", "hi"), |
| 2734 | "(ignoring case)"); |
| 2735 | } |
| 2736 | |
| 2737 | // Tests *_STREQ on wide strings. |
| 2738 | TEST(StringAssertionTest, STREQ_Wide) { |
| 2739 | // NULL strings. |
| 2740 | ASSERT_STREQ(static_cast<const wchar_t *>(NULL), NULL); |
| 2741 | |
| 2742 | // Empty strings. |
| 2743 | ASSERT_STREQ(L"", L""); |
| 2744 | |
| 2745 | // Non-null vs NULL. |
| 2746 | EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"non-null", NULL), |
| 2747 | "non-null"); |
| 2748 | |
| 2749 | // Equal strings. |
| 2750 | EXPECT_STREQ(L"Hi", L"Hi"); |
| 2751 | |
| 2752 | // Unequal strings. |
| 2753 | EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"abc", L"Abc"), |
| 2754 | "Abc"); |
| 2755 | |
| 2756 | // Strings containing wide characters. |
| 2757 | EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"abc\x8119", L"abc\x8120"), |
| 2758 | "abc"); |
| 2759 | } |
| 2760 | |
| 2761 | // Tests *_STRNE on wide strings. |
| 2762 | TEST(StringAssertionTest, STRNE_Wide) { |
| 2763 | // NULL strings. |
| 2764 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 2765 | EXPECT_STRNE(static_cast<const wchar_t *>(NULL), NULL); |
| 2766 | }, ""); |
| 2767 | |
| 2768 | // Empty strings. |
| 2769 | EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"", L""), |
| 2770 | "L\"\""); |
| 2771 | |
| 2772 | // Non-null vs NULL. |
| 2773 | ASSERT_STRNE(L"non-null", NULL); |
| 2774 | |
| 2775 | // Equal strings. |
| 2776 | EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"Hi", L"Hi"), |
| 2777 | "L\"Hi\""); |
| 2778 | |
| 2779 | // Unequal strings. |
| 2780 | EXPECT_STRNE(L"abc", L"Abc"); |
| 2781 | |
| 2782 | // Strings containing wide characters. |
| 2783 | EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"abc\x8119", L"abc\x8119"), |
| 2784 | "abc"); |
| 2785 | } |
| 2786 | |
| 2787 | // Tests for ::testing::IsSubstring(). |
| 2788 | |
| 2789 | // Tests that IsSubstring() returns the correct result when the input |
| 2790 | // argument type is const char*. |
| 2791 | TEST(IsSubstringTest, ReturnsCorrectResultForCString) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2792 | EXPECT_FALSE(IsSubstring("", "", NULL, "a")); |
| 2793 | EXPECT_FALSE(IsSubstring("", "", "b", NULL)); |
| 2794 | EXPECT_FALSE(IsSubstring("", "", "needle", "haystack")); |
| 2795 | |
| 2796 | EXPECT_TRUE(IsSubstring("", "", static_cast<const char*>(NULL), NULL)); |
| 2797 | EXPECT_TRUE(IsSubstring("", "", "needle", "two needles")); |
| 2798 | } |
| 2799 | |
| 2800 | // Tests that IsSubstring() returns the correct result when the input |
| 2801 | // argument type is const wchar_t*. |
| 2802 | TEST(IsSubstringTest, ReturnsCorrectResultForWideCString) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2803 | EXPECT_FALSE(IsSubstring("", "", kNull, L"a")); |
| 2804 | EXPECT_FALSE(IsSubstring("", "", L"b", kNull)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2805 | EXPECT_FALSE(IsSubstring("", "", L"needle", L"haystack")); |
| 2806 | |
| 2807 | EXPECT_TRUE(IsSubstring("", "", static_cast<const wchar_t*>(NULL), NULL)); |
| 2808 | EXPECT_TRUE(IsSubstring("", "", L"needle", L"two needles")); |
| 2809 | } |
| 2810 | |
| 2811 | // Tests that IsSubstring() generates the correct message when the input |
| 2812 | // argument type is const char*. |
| 2813 | TEST(IsSubstringTest, GeneratesCorrectMessageForCString) { |
| 2814 | EXPECT_STREQ("Value of: needle_expr\n" |
| 2815 | " Actual: \"needle\"\n" |
| 2816 | "Expected: a substring of haystack_expr\n" |
| 2817 | "Which is: \"haystack\"", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2818 | IsSubstring("needle_expr", "haystack_expr", |
| 2819 | "needle", "haystack").failure_message()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2822 | // Tests that IsSubstring returns the correct result when the input |
| 2823 | // argument type is ::std::string. |
| 2824 | TEST(IsSubstringTest, ReturnsCorrectResultsForStdString) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2825 | EXPECT_TRUE(IsSubstring("", "", std::string("hello"), "ahellob")); |
| 2826 | EXPECT_FALSE(IsSubstring("", "", "hello", std::string("world"))); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2829 | #if GTEST_HAS_STD_WSTRING |
| 2830 | // Tests that IsSubstring returns the correct result when the input |
| 2831 | // argument type is ::std::wstring. |
| 2832 | TEST(IsSubstringTest, ReturnsCorrectResultForStdWstring) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2833 | EXPECT_TRUE(IsSubstring("", "", ::std::wstring(L"needle"), L"two needles")); |
| 2834 | EXPECT_FALSE(IsSubstring("", "", L"needle", ::std::wstring(L"haystack"))); |
| 2835 | } |
| 2836 | |
| 2837 | // Tests that IsSubstring() generates the correct message when the input |
| 2838 | // argument type is ::std::wstring. |
| 2839 | TEST(IsSubstringTest, GeneratesCorrectMessageForWstring) { |
| 2840 | EXPECT_STREQ("Value of: needle_expr\n" |
| 2841 | " Actual: L\"needle\"\n" |
| 2842 | "Expected: a substring of haystack_expr\n" |
| 2843 | "Which is: L\"haystack\"", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2844 | IsSubstring( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2845 | "needle_expr", "haystack_expr", |
| 2846 | ::std::wstring(L"needle"), L"haystack").failure_message()); |
| 2847 | } |
| 2848 | |
| 2849 | #endif // GTEST_HAS_STD_WSTRING |
| 2850 | |
| 2851 | // Tests for ::testing::IsNotSubstring(). |
| 2852 | |
| 2853 | // Tests that IsNotSubstring() returns the correct result when the input |
| 2854 | // argument type is const char*. |
| 2855 | TEST(IsNotSubstringTest, ReturnsCorrectResultForCString) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2856 | EXPECT_TRUE(IsNotSubstring("", "", "needle", "haystack")); |
| 2857 | EXPECT_FALSE(IsNotSubstring("", "", "needle", "two needles")); |
| 2858 | } |
| 2859 | |
| 2860 | // Tests that IsNotSubstring() returns the correct result when the input |
| 2861 | // argument type is const wchar_t*. |
| 2862 | TEST(IsNotSubstringTest, ReturnsCorrectResultForWideCString) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2863 | EXPECT_TRUE(IsNotSubstring("", "", L"needle", L"haystack")); |
| 2864 | EXPECT_FALSE(IsNotSubstring("", "", L"needle", L"two needles")); |
| 2865 | } |
| 2866 | |
| 2867 | // Tests that IsNotSubstring() generates the correct message when the input |
| 2868 | // argument type is const wchar_t*. |
| 2869 | TEST(IsNotSubstringTest, GeneratesCorrectMessageForWideCString) { |
| 2870 | EXPECT_STREQ("Value of: needle_expr\n" |
| 2871 | " Actual: L\"needle\"\n" |
| 2872 | "Expected: not a substring of haystack_expr\n" |
| 2873 | "Which is: L\"two needles\"", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2874 | IsNotSubstring( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2875 | "needle_expr", "haystack_expr", |
| 2876 | L"needle", L"two needles").failure_message()); |
| 2877 | } |
| 2878 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2879 | // Tests that IsNotSubstring returns the correct result when the input |
| 2880 | // argument type is ::std::string. |
| 2881 | TEST(IsNotSubstringTest, ReturnsCorrectResultsForStdString) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2882 | EXPECT_FALSE(IsNotSubstring("", "", std::string("hello"), "ahellob")); |
| 2883 | EXPECT_TRUE(IsNotSubstring("", "", "hello", std::string("world"))); |
| 2884 | } |
| 2885 | |
| 2886 | // Tests that IsNotSubstring() generates the correct message when the input |
| 2887 | // argument type is ::std::string. |
| 2888 | TEST(IsNotSubstringTest, GeneratesCorrectMessageForStdString) { |
| 2889 | EXPECT_STREQ("Value of: needle_expr\n" |
| 2890 | " Actual: \"needle\"\n" |
| 2891 | "Expected: not a substring of haystack_expr\n" |
| 2892 | "Which is: \"two needles\"", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2893 | IsNotSubstring( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2894 | "needle_expr", "haystack_expr", |
| 2895 | ::std::string("needle"), "two needles").failure_message()); |
| 2896 | } |
| 2897 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2898 | #if GTEST_HAS_STD_WSTRING |
| 2899 | |
| 2900 | // Tests that IsNotSubstring returns the correct result when the input |
| 2901 | // argument type is ::std::wstring. |
| 2902 | TEST(IsNotSubstringTest, ReturnsCorrectResultForStdWstring) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2903 | EXPECT_FALSE( |
| 2904 | IsNotSubstring("", "", ::std::wstring(L"needle"), L"two needles")); |
| 2905 | EXPECT_TRUE(IsNotSubstring("", "", L"needle", ::std::wstring(L"haystack"))); |
| 2906 | } |
| 2907 | |
| 2908 | #endif // GTEST_HAS_STD_WSTRING |
| 2909 | |
| 2910 | // Tests floating-point assertions. |
| 2911 | |
| 2912 | template <typename RawType> |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2913 | class FloatingPointTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2914 | protected: |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2915 | |
| 2916 | // Pre-calculated numbers to be used by the tests. |
| 2917 | struct TestValues { |
| 2918 | RawType close_to_positive_zero; |
| 2919 | RawType close_to_negative_zero; |
| 2920 | RawType further_from_negative_zero; |
| 2921 | |
| 2922 | RawType close_to_one; |
| 2923 | RawType further_from_one; |
| 2924 | |
| 2925 | RawType infinity; |
| 2926 | RawType close_to_infinity; |
| 2927 | RawType further_from_infinity; |
| 2928 | |
| 2929 | RawType nan1; |
| 2930 | RawType nan2; |
| 2931 | }; |
| 2932 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2933 | typedef typename testing::internal::FloatingPoint<RawType> Floating; |
| 2934 | typedef typename Floating::Bits Bits; |
| 2935 | |
| 2936 | virtual void SetUp() { |
| 2937 | const size_t max_ulps = Floating::kMaxUlps; |
| 2938 | |
| 2939 | // The bits that represent 0.0. |
| 2940 | const Bits zero_bits = Floating(0).bits(); |
| 2941 | |
| 2942 | // Makes some numbers close to 0.0. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2943 | values_.close_to_positive_zero = Floating::ReinterpretBits( |
| 2944 | zero_bits + max_ulps/2); |
| 2945 | values_.close_to_negative_zero = -Floating::ReinterpretBits( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2946 | zero_bits + max_ulps - max_ulps/2); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2947 | values_.further_from_negative_zero = -Floating::ReinterpretBits( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2948 | zero_bits + max_ulps + 1 - max_ulps/2); |
| 2949 | |
| 2950 | // The bits that represent 1.0. |
| 2951 | const Bits one_bits = Floating(1).bits(); |
| 2952 | |
| 2953 | // Makes some numbers close to 1.0. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2954 | values_.close_to_one = Floating::ReinterpretBits(one_bits + max_ulps); |
| 2955 | values_.further_from_one = Floating::ReinterpretBits( |
| 2956 | one_bits + max_ulps + 1); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2957 | |
| 2958 | // +infinity. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2959 | values_.infinity = Floating::Infinity(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2960 | |
| 2961 | // The bits that represent +infinity. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2962 | const Bits infinity_bits = Floating(values_.infinity).bits(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2963 | |
| 2964 | // Makes some numbers close to infinity. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2965 | values_.close_to_infinity = Floating::ReinterpretBits( |
| 2966 | infinity_bits - max_ulps); |
| 2967 | values_.further_from_infinity = Floating::ReinterpretBits( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2968 | infinity_bits - max_ulps - 1); |
| 2969 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2970 | // Makes some NAN's. Sets the most significant bit of the fraction so that |
| 2971 | // our NaN's are quiet; trying to process a signaling NaN would raise an |
| 2972 | // exception if our environment enables floating point exceptions. |
| 2973 | values_.nan1 = Floating::ReinterpretBits(Floating::kExponentBitMask |
| 2974 | | (static_cast<Bits>(1) << (Floating::kFractionBitCount - 1)) | 1); |
| 2975 | values_.nan2 = Floating::ReinterpretBits(Floating::kExponentBitMask |
| 2976 | | (static_cast<Bits>(1) << (Floating::kFractionBitCount - 1)) | 200); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | void TestSize() { |
| 2980 | EXPECT_EQ(sizeof(RawType), sizeof(Bits)); |
| 2981 | } |
| 2982 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2983 | static TestValues values_; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2984 | }; |
| 2985 | |
| 2986 | template <typename RawType> |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2987 | typename FloatingPointTest<RawType>::TestValues |
| 2988 | FloatingPointTest<RawType>::values_; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2989 | |
| 2990 | // Instantiates FloatingPointTest for testing *_FLOAT_EQ. |
| 2991 | typedef FloatingPointTest<float> FloatTest; |
| 2992 | |
| 2993 | // Tests that the size of Float::Bits matches the size of float. |
| 2994 | TEST_F(FloatTest, Size) { |
| 2995 | TestSize(); |
| 2996 | } |
| 2997 | |
| 2998 | // Tests comparing with +0 and -0. |
| 2999 | TEST_F(FloatTest, Zeros) { |
| 3000 | EXPECT_FLOAT_EQ(0.0, -0.0); |
| 3001 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(-0.0, 1.0), |
| 3002 | "1.0"); |
| 3003 | EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(0.0, 1.5), |
| 3004 | "1.5"); |
| 3005 | } |
| 3006 | |
| 3007 | // Tests comparing numbers close to 0. |
| 3008 | // |
| 3009 | // This ensures that *_FLOAT_EQ handles the sign correctly and no |
| 3010 | // overflow occurs when comparing numbers whose absolute value is very |
| 3011 | // small. |
| 3012 | TEST_F(FloatTest, AlmostZeros) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3013 | // In C++Builder, names within local classes (such as used by |
| 3014 | // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the |
| 3015 | // scoping class. Use a static local alias as a workaround. |
| 3016 | static const FloatTest::TestValues& v(this->values_); |
| 3017 | |
| 3018 | EXPECT_FLOAT_EQ(0.0, v.close_to_positive_zero); |
| 3019 | EXPECT_FLOAT_EQ(-0.0, v.close_to_negative_zero); |
| 3020 | EXPECT_FLOAT_EQ(v.close_to_positive_zero, v.close_to_negative_zero); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3021 | |
| 3022 | EXPECT_FATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3023 | ASSERT_FLOAT_EQ(v.close_to_positive_zero, |
| 3024 | v.further_from_negative_zero); |
| 3025 | }, "v.further_from_negative_zero"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | // Tests comparing numbers close to each other. |
| 3029 | TEST_F(FloatTest, SmallDiff) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3030 | EXPECT_FLOAT_EQ(1.0, values_.close_to_one); |
| 3031 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(1.0, values_.further_from_one), |
| 3032 | "values_.further_from_one"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | // Tests comparing numbers far apart. |
| 3036 | TEST_F(FloatTest, LargeDiff) { |
| 3037 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(2.5, 3.0), |
| 3038 | "3.0"); |
| 3039 | } |
| 3040 | |
| 3041 | // Tests comparing with infinity. |
| 3042 | // |
| 3043 | // This ensures that no overflow occurs when comparing numbers whose |
| 3044 | // absolute value is very large. |
| 3045 | TEST_F(FloatTest, Infinity) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3046 | EXPECT_FLOAT_EQ(values_.infinity, values_.close_to_infinity); |
| 3047 | EXPECT_FLOAT_EQ(-values_.infinity, -values_.close_to_infinity); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3048 | #if !GTEST_OS_SYMBIAN |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3049 | // Nokia's STLport crashes if we try to output infinity or NaN. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3050 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.infinity, -values_.infinity), |
| 3051 | "-values_.infinity"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3052 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3053 | // This is interesting as the representations of infinity and nan1 |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3054 | // are only 1 DLP apart. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3055 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.infinity, values_.nan1), |
| 3056 | "values_.nan1"); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3057 | #endif // !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | // Tests that comparing with NAN always returns false. |
| 3061 | TEST_F(FloatTest, NaN) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3062 | #if !GTEST_OS_SYMBIAN |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3063 | // Nokia's STLport crashes if we try to output infinity or NaN. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3064 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3065 | // In C++Builder, names within local classes (such as used by |
| 3066 | // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the |
| 3067 | // scoping class. Use a static local alias as a workaround. |
| 3068 | static const FloatTest::TestValues& v(this->values_); |
| 3069 | |
| 3070 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(v.nan1, v.nan1), |
| 3071 | "v.nan1"); |
| 3072 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(v.nan1, v.nan2), |
| 3073 | "v.nan2"); |
| 3074 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(1.0, v.nan1), |
| 3075 | "v.nan1"); |
| 3076 | |
| 3077 | EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(v.nan1, v.infinity), |
| 3078 | "v.infinity"); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3079 | #endif // !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | // Tests that *_FLOAT_EQ are reflexive. |
| 3083 | TEST_F(FloatTest, Reflexive) { |
| 3084 | EXPECT_FLOAT_EQ(0.0, 0.0); |
| 3085 | EXPECT_FLOAT_EQ(1.0, 1.0); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3086 | ASSERT_FLOAT_EQ(values_.infinity, values_.infinity); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | // Tests that *_FLOAT_EQ are commutative. |
| 3090 | TEST_F(FloatTest, Commutative) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3091 | // We already tested EXPECT_FLOAT_EQ(1.0, values_.close_to_one). |
| 3092 | EXPECT_FLOAT_EQ(values_.close_to_one, 1.0); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3093 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3094 | // We already tested EXPECT_FLOAT_EQ(1.0, values_.further_from_one). |
| 3095 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.further_from_one, 1.0), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3096 | "1.0"); |
| 3097 | } |
| 3098 | |
| 3099 | // Tests EXPECT_NEAR. |
| 3100 | TEST_F(FloatTest, EXPECT_NEAR) { |
| 3101 | EXPECT_NEAR(-1.0f, -1.1f, 0.2f); |
| 3102 | EXPECT_NEAR(2.0f, 3.0f, 1.0f); |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 3103 | EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0f,1.5f, 0.25f), // NOLINT |
| 3104 | "The difference between 1.0f and 1.5f is 0.5, " |
| 3105 | "which exceeds 0.25f"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3106 | // To work around a bug in gcc 2.95.0, there is intentionally no |
| 3107 | // space after the first comma in the previous line. |
| 3108 | } |
| 3109 | |
| 3110 | // Tests ASSERT_NEAR. |
| 3111 | TEST_F(FloatTest, ASSERT_NEAR) { |
| 3112 | ASSERT_NEAR(-1.0f, -1.1f, 0.2f); |
| 3113 | ASSERT_NEAR(2.0f, 3.0f, 1.0f); |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 3114 | EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0f,1.5f, 0.25f), // NOLINT |
| 3115 | "The difference between 1.0f and 1.5f is 0.5, " |
| 3116 | "which exceeds 0.25f"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3117 | // To work around a bug in gcc 2.95.0, there is intentionally no |
| 3118 | // space after the first comma in the previous line. |
| 3119 | } |
| 3120 | |
| 3121 | // Tests the cases where FloatLE() should succeed. |
| 3122 | TEST_F(FloatTest, FloatLESucceeds) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3123 | EXPECT_PRED_FORMAT2(FloatLE, 1.0f, 2.0f); // When val1 < val2, |
| 3124 | ASSERT_PRED_FORMAT2(FloatLE, 1.0f, 1.0f); // val1 == val2, |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3125 | |
| 3126 | // or when val1 is greater than, but almost equals to, val2. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3127 | EXPECT_PRED_FORMAT2(FloatLE, values_.close_to_positive_zero, 0.0f); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | // Tests the cases where FloatLE() should fail. |
| 3131 | TEST_F(FloatTest, FloatLEFails) { |
| 3132 | // When val1 is greater than val2 by a large margin, |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3133 | EXPECT_NONFATAL_FAILURE(EXPECT_PRED_FORMAT2(FloatLE, 2.0f, 1.0f), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3134 | "(2.0f) <= (1.0f)"); |
| 3135 | |
| 3136 | // or by a small yet non-negligible margin, |
| 3137 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3138 | EXPECT_PRED_FORMAT2(FloatLE, values_.further_from_one, 1.0f); |
| 3139 | }, "(values_.further_from_one) <= (1.0f)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3140 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3141 | #if !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3142 | // Nokia's STLport crashes if we try to output infinity or NaN. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3143 | // C++Builder gives bad results for ordered comparisons involving NaNs |
| 3144 | // due to compiler bugs. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3145 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3146 | EXPECT_PRED_FORMAT2(FloatLE, values_.nan1, values_.infinity); |
| 3147 | }, "(values_.nan1) <= (values_.infinity)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3148 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3149 | EXPECT_PRED_FORMAT2(FloatLE, -values_.infinity, values_.nan1); |
| 3150 | }, "(-values_.infinity) <= (values_.nan1)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3151 | EXPECT_FATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3152 | ASSERT_PRED_FORMAT2(FloatLE, values_.nan1, values_.nan1); |
| 3153 | }, "(values_.nan1) <= (values_.nan1)"); |
| 3154 | #endif // !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | // Instantiates FloatingPointTest for testing *_DOUBLE_EQ. |
| 3158 | typedef FloatingPointTest<double> DoubleTest; |
| 3159 | |
| 3160 | // Tests that the size of Double::Bits matches the size of double. |
| 3161 | TEST_F(DoubleTest, Size) { |
| 3162 | TestSize(); |
| 3163 | } |
| 3164 | |
| 3165 | // Tests comparing with +0 and -0. |
| 3166 | TEST_F(DoubleTest, Zeros) { |
| 3167 | EXPECT_DOUBLE_EQ(0.0, -0.0); |
| 3168 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(-0.0, 1.0), |
| 3169 | "1.0"); |
| 3170 | EXPECT_FATAL_FAILURE(ASSERT_DOUBLE_EQ(0.0, 1.0), |
| 3171 | "1.0"); |
| 3172 | } |
| 3173 | |
| 3174 | // Tests comparing numbers close to 0. |
| 3175 | // |
| 3176 | // This ensures that *_DOUBLE_EQ handles the sign correctly and no |
| 3177 | // overflow occurs when comparing numbers whose absolute value is very |
| 3178 | // small. |
| 3179 | TEST_F(DoubleTest, AlmostZeros) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3180 | // In C++Builder, names within local classes (such as used by |
| 3181 | // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the |
| 3182 | // scoping class. Use a static local alias as a workaround. |
| 3183 | static const DoubleTest::TestValues& v(this->values_); |
| 3184 | |
| 3185 | EXPECT_DOUBLE_EQ(0.0, v.close_to_positive_zero); |
| 3186 | EXPECT_DOUBLE_EQ(-0.0, v.close_to_negative_zero); |
| 3187 | EXPECT_DOUBLE_EQ(v.close_to_positive_zero, v.close_to_negative_zero); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3188 | |
| 3189 | EXPECT_FATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3190 | ASSERT_DOUBLE_EQ(v.close_to_positive_zero, |
| 3191 | v.further_from_negative_zero); |
| 3192 | }, "v.further_from_negative_zero"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | // Tests comparing numbers close to each other. |
| 3196 | TEST_F(DoubleTest, SmallDiff) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3197 | EXPECT_DOUBLE_EQ(1.0, values_.close_to_one); |
| 3198 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(1.0, values_.further_from_one), |
| 3199 | "values_.further_from_one"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | // Tests comparing numbers far apart. |
| 3203 | TEST_F(DoubleTest, LargeDiff) { |
| 3204 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(2.0, 3.0), |
| 3205 | "3.0"); |
| 3206 | } |
| 3207 | |
| 3208 | // Tests comparing with infinity. |
| 3209 | // |
| 3210 | // This ensures that no overflow occurs when comparing numbers whose |
| 3211 | // absolute value is very large. |
| 3212 | TEST_F(DoubleTest, Infinity) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3213 | EXPECT_DOUBLE_EQ(values_.infinity, values_.close_to_infinity); |
| 3214 | EXPECT_DOUBLE_EQ(-values_.infinity, -values_.close_to_infinity); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3215 | #if !GTEST_OS_SYMBIAN |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3216 | // Nokia's STLport crashes if we try to output infinity or NaN. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3217 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(values_.infinity, -values_.infinity), |
| 3218 | "-values_.infinity"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3219 | |
| 3220 | // This is interesting as the representations of infinity_ and nan1_ |
| 3221 | // are only 1 DLP apart. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3222 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(values_.infinity, values_.nan1), |
| 3223 | "values_.nan1"); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3224 | #endif // !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | // Tests that comparing with NAN always returns false. |
| 3228 | TEST_F(DoubleTest, NaN) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3229 | #if !GTEST_OS_SYMBIAN |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3230 | // In C++Builder, names within local classes (such as used by |
| 3231 | // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the |
| 3232 | // scoping class. Use a static local alias as a workaround. |
| 3233 | static const DoubleTest::TestValues& v(this->values_); |
| 3234 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3235 | // Nokia's STLport crashes if we try to output infinity or NaN. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3236 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(v.nan1, v.nan1), |
| 3237 | "v.nan1"); |
| 3238 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(v.nan1, v.nan2), "v.nan2"); |
| 3239 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(1.0, v.nan1), "v.nan1"); |
| 3240 | EXPECT_FATAL_FAILURE(ASSERT_DOUBLE_EQ(v.nan1, v.infinity), |
| 3241 | "v.infinity"); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3242 | #endif // !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | // Tests that *_DOUBLE_EQ are reflexive. |
| 3246 | TEST_F(DoubleTest, Reflexive) { |
| 3247 | EXPECT_DOUBLE_EQ(0.0, 0.0); |
| 3248 | EXPECT_DOUBLE_EQ(1.0, 1.0); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3249 | #if !GTEST_OS_SYMBIAN |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3250 | // Nokia's STLport crashes if we try to output infinity or NaN. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3251 | ASSERT_DOUBLE_EQ(values_.infinity, values_.infinity); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3252 | #endif // !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | // Tests that *_DOUBLE_EQ are commutative. |
| 3256 | TEST_F(DoubleTest, Commutative) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3257 | // We already tested EXPECT_DOUBLE_EQ(1.0, values_.close_to_one). |
| 3258 | EXPECT_DOUBLE_EQ(values_.close_to_one, 1.0); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3259 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3260 | // We already tested EXPECT_DOUBLE_EQ(1.0, values_.further_from_one). |
| 3261 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(values_.further_from_one, 1.0), |
| 3262 | "1.0"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | // Tests EXPECT_NEAR. |
| 3266 | TEST_F(DoubleTest, EXPECT_NEAR) { |
| 3267 | EXPECT_NEAR(-1.0, -1.1, 0.2); |
| 3268 | EXPECT_NEAR(2.0, 3.0, 1.0); |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 3269 | EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0, 1.5, 0.25), // NOLINT |
| 3270 | "The difference between 1.0 and 1.5 is 0.5, " |
| 3271 | "which exceeds 0.25"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3272 | // To work around a bug in gcc 2.95.0, there is intentionally no |
| 3273 | // space after the first comma in the previous statement. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
| 3276 | // Tests ASSERT_NEAR. |
| 3277 | TEST_F(DoubleTest, ASSERT_NEAR) { |
| 3278 | ASSERT_NEAR(-1.0, -1.1, 0.2); |
| 3279 | ASSERT_NEAR(2.0, 3.0, 1.0); |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 3280 | EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0, 1.5, 0.25), // NOLINT |
| 3281 | "The difference between 1.0 and 1.5 is 0.5, " |
| 3282 | "which exceeds 0.25"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3283 | // To work around a bug in gcc 2.95.0, there is intentionally no |
| 3284 | // space after the first comma in the previous statement. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | // Tests the cases where DoubleLE() should succeed. |
| 3288 | TEST_F(DoubleTest, DoubleLESucceeds) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3289 | EXPECT_PRED_FORMAT2(DoubleLE, 1.0, 2.0); // When val1 < val2, |
| 3290 | ASSERT_PRED_FORMAT2(DoubleLE, 1.0, 1.0); // val1 == val2, |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3291 | |
| 3292 | // or when val1 is greater than, but almost equals to, val2. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3293 | EXPECT_PRED_FORMAT2(DoubleLE, values_.close_to_positive_zero, 0.0); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | // Tests the cases where DoubleLE() should fail. |
| 3297 | TEST_F(DoubleTest, DoubleLEFails) { |
| 3298 | // When val1 is greater than val2 by a large margin, |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3299 | EXPECT_NONFATAL_FAILURE(EXPECT_PRED_FORMAT2(DoubleLE, 2.0, 1.0), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3300 | "(2.0) <= (1.0)"); |
| 3301 | |
| 3302 | // or by a small yet non-negligible margin, |
| 3303 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3304 | EXPECT_PRED_FORMAT2(DoubleLE, values_.further_from_one, 1.0); |
| 3305 | }, "(values_.further_from_one) <= (1.0)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3306 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3307 | #if !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3308 | // Nokia's STLport crashes if we try to output infinity or NaN. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3309 | // C++Builder gives bad results for ordered comparisons involving NaNs |
| 3310 | // due to compiler bugs. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3311 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3312 | EXPECT_PRED_FORMAT2(DoubleLE, values_.nan1, values_.infinity); |
| 3313 | }, "(values_.nan1) <= (values_.infinity)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3314 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3315 | EXPECT_PRED_FORMAT2(DoubleLE, -values_.infinity, values_.nan1); |
| 3316 | }, " (-values_.infinity) <= (values_.nan1)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3317 | EXPECT_FATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3318 | ASSERT_PRED_FORMAT2(DoubleLE, values_.nan1, values_.nan1); |
| 3319 | }, "(values_.nan1) <= (values_.nan1)"); |
| 3320 | #endif // !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | |
| 3324 | // Verifies that a test or test case whose name starts with DISABLED_ is |
| 3325 | // not run. |
| 3326 | |
| 3327 | // A test whose name starts with DISABLED_. |
| 3328 | // Should not run. |
| 3329 | TEST(DisabledTest, DISABLED_TestShouldNotRun) { |
| 3330 | FAIL() << "Unexpected failure: Disabled test should not be run."; |
| 3331 | } |
| 3332 | |
| 3333 | // A test whose name does not start with DISABLED_. |
| 3334 | // Should run. |
| 3335 | TEST(DisabledTest, NotDISABLED_TestShouldRun) { |
| 3336 | EXPECT_EQ(1, 1); |
| 3337 | } |
| 3338 | |
| 3339 | // A test case whose name starts with DISABLED_. |
| 3340 | // Should not run. |
| 3341 | TEST(DISABLED_TestCase, TestShouldNotRun) { |
| 3342 | FAIL() << "Unexpected failure: Test in disabled test case should not be run."; |
| 3343 | } |
| 3344 | |
| 3345 | // A test case and test whose names start with DISABLED_. |
| 3346 | // Should not run. |
| 3347 | TEST(DISABLED_TestCase, DISABLED_TestShouldNotRun) { |
| 3348 | FAIL() << "Unexpected failure: Test in disabled test case should not be run."; |
| 3349 | } |
| 3350 | |
| 3351 | // Check that when all tests in a test case are disabled, SetupTestCase() and |
| 3352 | // TearDownTestCase() are not called. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3353 | class DisabledTestsTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3354 | protected: |
| 3355 | static void SetUpTestCase() { |
| 3356 | FAIL() << "Unexpected failure: All tests disabled in test case. " |
| 3357 | "SetupTestCase() should not be called."; |
| 3358 | } |
| 3359 | |
| 3360 | static void TearDownTestCase() { |
| 3361 | FAIL() << "Unexpected failure: All tests disabled in test case. " |
| 3362 | "TearDownTestCase() should not be called."; |
| 3363 | } |
| 3364 | }; |
| 3365 | |
| 3366 | TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_1) { |
| 3367 | FAIL() << "Unexpected failure: Disabled test should not be run."; |
| 3368 | } |
| 3369 | |
| 3370 | TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_2) { |
| 3371 | FAIL() << "Unexpected failure: Disabled test should not be run."; |
| 3372 | } |
| 3373 | |
shiqian | e8ff148 | 2008-09-08 17:55:52 +0000 | [diff] [blame] | 3374 | // Tests that disabled typed tests aren't run. |
| 3375 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3376 | #if GTEST_HAS_TYPED_TEST |
shiqian | e8ff148 | 2008-09-08 17:55:52 +0000 | [diff] [blame] | 3377 | |
| 3378 | template <typename T> |
| 3379 | class TypedTest : public Test { |
| 3380 | }; |
| 3381 | |
| 3382 | typedef testing::Types<int, double> NumericTypes; |
| 3383 | TYPED_TEST_CASE(TypedTest, NumericTypes); |
| 3384 | |
| 3385 | TYPED_TEST(TypedTest, DISABLED_ShouldNotRun) { |
| 3386 | FAIL() << "Unexpected failure: Disabled typed test should not run."; |
| 3387 | } |
| 3388 | |
| 3389 | template <typename T> |
| 3390 | class DISABLED_TypedTest : public Test { |
| 3391 | }; |
| 3392 | |
| 3393 | TYPED_TEST_CASE(DISABLED_TypedTest, NumericTypes); |
| 3394 | |
| 3395 | TYPED_TEST(DISABLED_TypedTest, ShouldNotRun) { |
| 3396 | FAIL() << "Unexpected failure: Disabled typed test should not run."; |
| 3397 | } |
| 3398 | |
| 3399 | #endif // GTEST_HAS_TYPED_TEST |
| 3400 | |
| 3401 | // Tests that disabled type-parameterized tests aren't run. |
| 3402 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3403 | #if GTEST_HAS_TYPED_TEST_P |
shiqian | e8ff148 | 2008-09-08 17:55:52 +0000 | [diff] [blame] | 3404 | |
| 3405 | template <typename T> |
| 3406 | class TypedTestP : public Test { |
| 3407 | }; |
| 3408 | |
| 3409 | TYPED_TEST_CASE_P(TypedTestP); |
| 3410 | |
| 3411 | TYPED_TEST_P(TypedTestP, DISABLED_ShouldNotRun) { |
| 3412 | FAIL() << "Unexpected failure: " |
| 3413 | << "Disabled type-parameterized test should not run."; |
| 3414 | } |
| 3415 | |
| 3416 | REGISTER_TYPED_TEST_CASE_P(TypedTestP, DISABLED_ShouldNotRun); |
| 3417 | |
| 3418 | INSTANTIATE_TYPED_TEST_CASE_P(My, TypedTestP, NumericTypes); |
| 3419 | |
| 3420 | template <typename T> |
| 3421 | class DISABLED_TypedTestP : public Test { |
| 3422 | }; |
| 3423 | |
| 3424 | TYPED_TEST_CASE_P(DISABLED_TypedTestP); |
| 3425 | |
| 3426 | TYPED_TEST_P(DISABLED_TypedTestP, ShouldNotRun) { |
| 3427 | FAIL() << "Unexpected failure: " |
| 3428 | << "Disabled type-parameterized test should not run."; |
| 3429 | } |
| 3430 | |
| 3431 | REGISTER_TYPED_TEST_CASE_P(DISABLED_TypedTestP, ShouldNotRun); |
| 3432 | |
| 3433 | INSTANTIATE_TYPED_TEST_CASE_P(My, DISABLED_TypedTestP, NumericTypes); |
| 3434 | |
| 3435 | #endif // GTEST_HAS_TYPED_TEST_P |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3436 | |
| 3437 | // Tests that assertion macros evaluate their arguments exactly once. |
| 3438 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3439 | class SingleEvaluationTest : public Test { |
tsunanet | acd0f32 | 2009-05-18 20:53:57 +0000 | [diff] [blame] | 3440 | public: // Must be public and not protected due to a bug in g++ 3.4.2. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3441 | // This helper function is needed by the FailedASSERT_STREQ test |
| 3442 | // below. It's public to work around C++Builder's bug with scoping local |
| 3443 | // classes. |
| 3444 | static void CompareAndIncrementCharPtrs() { |
| 3445 | ASSERT_STREQ(p1_++, p2_++); |
| 3446 | } |
| 3447 | |
| 3448 | // This helper function is needed by the FailedASSERT_NE test below. It's |
| 3449 | // public to work around C++Builder's bug with scoping local classes. |
| 3450 | static void CompareAndIncrementInts() { |
| 3451 | ASSERT_NE(a_++, b_++); |
| 3452 | } |
| 3453 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3454 | protected: |
| 3455 | SingleEvaluationTest() { |
| 3456 | p1_ = s1_; |
| 3457 | p2_ = s2_; |
| 3458 | a_ = 0; |
| 3459 | b_ = 0; |
| 3460 | } |
| 3461 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3462 | static const char* const s1_; |
| 3463 | static const char* const s2_; |
| 3464 | static const char* p1_; |
| 3465 | static const char* p2_; |
| 3466 | |
| 3467 | static int a_; |
| 3468 | static int b_; |
| 3469 | }; |
| 3470 | |
| 3471 | const char* const SingleEvaluationTest::s1_ = "01234"; |
| 3472 | const char* const SingleEvaluationTest::s2_ = "abcde"; |
| 3473 | const char* SingleEvaluationTest::p1_; |
| 3474 | const char* SingleEvaluationTest::p2_; |
| 3475 | int SingleEvaluationTest::a_; |
| 3476 | int SingleEvaluationTest::b_; |
| 3477 | |
| 3478 | // Tests that when ASSERT_STREQ fails, it evaluates its arguments |
| 3479 | // exactly once. |
| 3480 | TEST_F(SingleEvaluationTest, FailedASSERT_STREQ) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3481 | EXPECT_FATAL_FAILURE(SingleEvaluationTest::CompareAndIncrementCharPtrs(), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3482 | "p2_++"); |
| 3483 | EXPECT_EQ(s1_ + 1, p1_); |
| 3484 | EXPECT_EQ(s2_ + 1, p2_); |
| 3485 | } |
| 3486 | |
| 3487 | // Tests that string assertion arguments are evaluated exactly once. |
| 3488 | TEST_F(SingleEvaluationTest, ASSERT_STR) { |
| 3489 | // successful EXPECT_STRNE |
| 3490 | EXPECT_STRNE(p1_++, p2_++); |
| 3491 | EXPECT_EQ(s1_ + 1, p1_); |
| 3492 | EXPECT_EQ(s2_ + 1, p2_); |
| 3493 | |
| 3494 | // failed EXPECT_STRCASEEQ |
| 3495 | EXPECT_NONFATAL_FAILURE(EXPECT_STRCASEEQ(p1_++, p2_++), |
| 3496 | "ignoring case"); |
| 3497 | EXPECT_EQ(s1_ + 2, p1_); |
| 3498 | EXPECT_EQ(s2_ + 2, p2_); |
| 3499 | } |
| 3500 | |
| 3501 | // Tests that when ASSERT_NE fails, it evaluates its arguments exactly |
| 3502 | // once. |
| 3503 | TEST_F(SingleEvaluationTest, FailedASSERT_NE) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3504 | EXPECT_FATAL_FAILURE(SingleEvaluationTest::CompareAndIncrementInts(), |
| 3505 | "(a_++) != (b_++)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3506 | EXPECT_EQ(1, a_); |
| 3507 | EXPECT_EQ(1, b_); |
| 3508 | } |
| 3509 | |
| 3510 | // Tests that assertion arguments are evaluated exactly once. |
| 3511 | TEST_F(SingleEvaluationTest, OtherCases) { |
| 3512 | // successful EXPECT_TRUE |
| 3513 | EXPECT_TRUE(0 == a_++); // NOLINT |
| 3514 | EXPECT_EQ(1, a_); |
| 3515 | |
| 3516 | // failed EXPECT_TRUE |
| 3517 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(-1 == a_++), "-1 == a_++"); |
| 3518 | EXPECT_EQ(2, a_); |
| 3519 | |
| 3520 | // successful EXPECT_GT |
| 3521 | EXPECT_GT(a_++, b_++); |
| 3522 | EXPECT_EQ(3, a_); |
| 3523 | EXPECT_EQ(1, b_); |
| 3524 | |
| 3525 | // failed EXPECT_LT |
| 3526 | EXPECT_NONFATAL_FAILURE(EXPECT_LT(a_++, b_++), "(a_++) < (b_++)"); |
| 3527 | EXPECT_EQ(4, a_); |
| 3528 | EXPECT_EQ(2, b_); |
| 3529 | |
| 3530 | // successful ASSERT_TRUE |
| 3531 | ASSERT_TRUE(0 < a_++); // NOLINT |
| 3532 | EXPECT_EQ(5, a_); |
| 3533 | |
| 3534 | // successful ASSERT_GT |
| 3535 | ASSERT_GT(a_++, b_++); |
| 3536 | EXPECT_EQ(6, a_); |
| 3537 | EXPECT_EQ(3, b_); |
| 3538 | } |
| 3539 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3540 | #if GTEST_HAS_EXCEPTIONS |
| 3541 | |
| 3542 | void ThrowAnInteger() { |
| 3543 | throw 1; |
| 3544 | } |
| 3545 | |
| 3546 | // Tests that assertion arguments are evaluated exactly once. |
| 3547 | TEST_F(SingleEvaluationTest, ExceptionTests) { |
| 3548 | // successful EXPECT_THROW |
| 3549 | EXPECT_THROW({ // NOLINT |
| 3550 | a_++; |
| 3551 | ThrowAnInteger(); |
| 3552 | }, int); |
| 3553 | EXPECT_EQ(1, a_); |
| 3554 | |
| 3555 | // failed EXPECT_THROW, throws different |
| 3556 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW({ // NOLINT |
| 3557 | a_++; |
| 3558 | ThrowAnInteger(); |
| 3559 | }, bool), "throws a different type"); |
| 3560 | EXPECT_EQ(2, a_); |
| 3561 | |
| 3562 | // failed EXPECT_THROW, throws nothing |
| 3563 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(a_++, bool), "throws nothing"); |
| 3564 | EXPECT_EQ(3, a_); |
| 3565 | |
| 3566 | // successful EXPECT_NO_THROW |
| 3567 | EXPECT_NO_THROW(a_++); |
| 3568 | EXPECT_EQ(4, a_); |
| 3569 | |
| 3570 | // failed EXPECT_NO_THROW |
| 3571 | EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW({ // NOLINT |
| 3572 | a_++; |
| 3573 | ThrowAnInteger(); |
| 3574 | }), "it throws"); |
| 3575 | EXPECT_EQ(5, a_); |
| 3576 | |
| 3577 | // successful EXPECT_ANY_THROW |
| 3578 | EXPECT_ANY_THROW({ // NOLINT |
| 3579 | a_++; |
| 3580 | ThrowAnInteger(); |
| 3581 | }); |
| 3582 | EXPECT_EQ(6, a_); |
| 3583 | |
| 3584 | // failed EXPECT_ANY_THROW |
| 3585 | EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(a_++), "it doesn't"); |
| 3586 | EXPECT_EQ(7, a_); |
| 3587 | } |
| 3588 | |
| 3589 | #endif // GTEST_HAS_EXCEPTIONS |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3590 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3591 | // Tests {ASSERT|EXPECT}_NO_FATAL_FAILURE. |
| 3592 | class NoFatalFailureTest : public Test { |
| 3593 | protected: |
| 3594 | void Succeeds() {} |
| 3595 | void FailsNonFatal() { |
| 3596 | ADD_FAILURE() << "some non-fatal failure"; |
| 3597 | } |
| 3598 | void Fails() { |
| 3599 | FAIL() << "some fatal failure"; |
| 3600 | } |
| 3601 | |
| 3602 | void DoAssertNoFatalFailureOnFails() { |
| 3603 | ASSERT_NO_FATAL_FAILURE(Fails()); |
| 3604 | ADD_FAILURE() << "shold not reach here."; |
| 3605 | } |
| 3606 | |
| 3607 | void DoExpectNoFatalFailureOnFails() { |
| 3608 | EXPECT_NO_FATAL_FAILURE(Fails()); |
| 3609 | ADD_FAILURE() << "other failure"; |
| 3610 | } |
| 3611 | }; |
| 3612 | |
| 3613 | TEST_F(NoFatalFailureTest, NoFailure) { |
| 3614 | EXPECT_NO_FATAL_FAILURE(Succeeds()); |
| 3615 | ASSERT_NO_FATAL_FAILURE(Succeeds()); |
| 3616 | } |
| 3617 | |
| 3618 | TEST_F(NoFatalFailureTest, NonFatalIsNoFailure) { |
| 3619 | EXPECT_NONFATAL_FAILURE( |
| 3620 | EXPECT_NO_FATAL_FAILURE(FailsNonFatal()), |
| 3621 | "some non-fatal failure"); |
| 3622 | EXPECT_NONFATAL_FAILURE( |
| 3623 | ASSERT_NO_FATAL_FAILURE(FailsNonFatal()), |
| 3624 | "some non-fatal failure"); |
| 3625 | } |
| 3626 | |
| 3627 | TEST_F(NoFatalFailureTest, AssertNoFatalFailureOnFatalFailure) { |
| 3628 | TestPartResultArray gtest_failures; |
| 3629 | { |
| 3630 | ScopedFakeTestPartResultReporter gtest_reporter(>est_failures); |
| 3631 | DoAssertNoFatalFailureOnFails(); |
| 3632 | } |
| 3633 | ASSERT_EQ(2, gtest_failures.size()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3634 | EXPECT_EQ(TestPartResult::kFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3635 | gtest_failures.GetTestPartResult(0).type()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3636 | EXPECT_EQ(TestPartResult::kFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3637 | gtest_failures.GetTestPartResult(1).type()); |
| 3638 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "some fatal failure", |
| 3639 | gtest_failures.GetTestPartResult(0).message()); |
| 3640 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "it does", |
| 3641 | gtest_failures.GetTestPartResult(1).message()); |
| 3642 | } |
| 3643 | |
| 3644 | TEST_F(NoFatalFailureTest, ExpectNoFatalFailureOnFatalFailure) { |
| 3645 | TestPartResultArray gtest_failures; |
| 3646 | { |
| 3647 | ScopedFakeTestPartResultReporter gtest_reporter(>est_failures); |
| 3648 | DoExpectNoFatalFailureOnFails(); |
| 3649 | } |
| 3650 | ASSERT_EQ(3, gtest_failures.size()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3651 | EXPECT_EQ(TestPartResult::kFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3652 | gtest_failures.GetTestPartResult(0).type()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3653 | EXPECT_EQ(TestPartResult::kNonFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3654 | gtest_failures.GetTestPartResult(1).type()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3655 | EXPECT_EQ(TestPartResult::kNonFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3656 | gtest_failures.GetTestPartResult(2).type()); |
| 3657 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "some fatal failure", |
| 3658 | gtest_failures.GetTestPartResult(0).message()); |
| 3659 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "it does", |
| 3660 | gtest_failures.GetTestPartResult(1).message()); |
| 3661 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "other failure", |
| 3662 | gtest_failures.GetTestPartResult(2).message()); |
| 3663 | } |
| 3664 | |
| 3665 | TEST_F(NoFatalFailureTest, MessageIsStreamable) { |
| 3666 | TestPartResultArray gtest_failures; |
| 3667 | { |
| 3668 | ScopedFakeTestPartResultReporter gtest_reporter(>est_failures); |
| 3669 | EXPECT_NO_FATAL_FAILURE(FAIL() << "foo") << "my message"; |
| 3670 | } |
| 3671 | ASSERT_EQ(2, gtest_failures.size()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3672 | EXPECT_EQ(TestPartResult::kNonFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3673 | gtest_failures.GetTestPartResult(0).type()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3674 | EXPECT_EQ(TestPartResult::kNonFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3675 | gtest_failures.GetTestPartResult(1).type()); |
| 3676 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "foo", |
| 3677 | gtest_failures.GetTestPartResult(0).message()); |
| 3678 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "my message", |
| 3679 | gtest_failures.GetTestPartResult(1).message()); |
| 3680 | } |
| 3681 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3682 | // Tests non-string assertions. |
| 3683 | |
| 3684 | // Tests EqFailure(), used for implementing *EQ* assertions. |
| 3685 | TEST(AssertionTest, EqFailure) { |
| 3686 | const String foo_val("5"), bar_val("6"); |
| 3687 | const String msg1( |
| 3688 | EqFailure("foo", "bar", foo_val, bar_val, false) |
| 3689 | .failure_message()); |
| 3690 | EXPECT_STREQ( |
| 3691 | "Value of: bar\n" |
| 3692 | " Actual: 6\n" |
| 3693 | "Expected: foo\n" |
| 3694 | "Which is: 5", |
| 3695 | msg1.c_str()); |
| 3696 | |
| 3697 | const String msg2( |
| 3698 | EqFailure("foo", "6", foo_val, bar_val, false) |
| 3699 | .failure_message()); |
| 3700 | EXPECT_STREQ( |
| 3701 | "Value of: 6\n" |
| 3702 | "Expected: foo\n" |
| 3703 | "Which is: 5", |
| 3704 | msg2.c_str()); |
| 3705 | |
| 3706 | const String msg3( |
| 3707 | EqFailure("5", "bar", foo_val, bar_val, false) |
| 3708 | .failure_message()); |
| 3709 | EXPECT_STREQ( |
| 3710 | "Value of: bar\n" |
| 3711 | " Actual: 6\n" |
| 3712 | "Expected: 5", |
| 3713 | msg3.c_str()); |
| 3714 | |
| 3715 | const String msg4( |
| 3716 | EqFailure("5", "6", foo_val, bar_val, false).failure_message()); |
| 3717 | EXPECT_STREQ( |
| 3718 | "Value of: 6\n" |
| 3719 | "Expected: 5", |
| 3720 | msg4.c_str()); |
| 3721 | |
| 3722 | const String msg5( |
| 3723 | EqFailure("foo", "bar", |
| 3724 | String("\"x\""), String("\"y\""), |
| 3725 | true).failure_message()); |
| 3726 | EXPECT_STREQ( |
| 3727 | "Value of: bar\n" |
| 3728 | " Actual: \"y\"\n" |
| 3729 | "Expected: foo (ignoring case)\n" |
| 3730 | "Which is: \"x\"", |
| 3731 | msg5.c_str()); |
| 3732 | } |
| 3733 | |
| 3734 | // Tests AppendUserMessage(), used for implementing the *EQ* macros. |
| 3735 | TEST(AssertionTest, AppendUserMessage) { |
| 3736 | const String foo("foo"); |
| 3737 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3738 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3739 | EXPECT_STREQ("foo", |
| 3740 | AppendUserMessage(foo, msg).c_str()); |
| 3741 | |
| 3742 | msg << "bar"; |
| 3743 | EXPECT_STREQ("foo\nbar", |
| 3744 | AppendUserMessage(foo, msg).c_str()); |
| 3745 | } |
| 3746 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3747 | #ifdef __BORLANDC__ |
| 3748 | // Silences warnings: "Condition is always true", "Unreachable code" |
| 3749 | #pragma option push -w-ccc -w-rch |
| 3750 | #endif |
| 3751 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3752 | // Tests ASSERT_TRUE. |
| 3753 | TEST(AssertionTest, ASSERT_TRUE) { |
| 3754 | ASSERT_TRUE(2 > 1); // NOLINT |
| 3755 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(2 < 1), |
| 3756 | "2 < 1"); |
| 3757 | } |
| 3758 | |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 3759 | // Tests ASSERT_TRUE(predicate) for predicates returning AssertionResult. |
| 3760 | TEST(AssertionTest, AssertTrueWithAssertionResult) { |
| 3761 | ASSERT_TRUE(ResultIsEven(2)); |
| 3762 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(ResultIsEven(3)), |
| 3763 | "Value of: ResultIsEven(3)\n" |
| 3764 | " Actual: false (3 is odd)\n" |
| 3765 | "Expected: true"); |
| 3766 | ASSERT_TRUE(ResultIsEvenNoExplanation(2)); |
| 3767 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(ResultIsEvenNoExplanation(3)), |
| 3768 | "Value of: ResultIsEvenNoExplanation(3)\n" |
| 3769 | " Actual: false (3 is odd)\n" |
| 3770 | "Expected: true"); |
| 3771 | } |
| 3772 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3773 | // Tests ASSERT_FALSE. |
| 3774 | TEST(AssertionTest, ASSERT_FALSE) { |
| 3775 | ASSERT_FALSE(2 < 1); // NOLINT |
| 3776 | EXPECT_FATAL_FAILURE(ASSERT_FALSE(2 > 1), |
| 3777 | "Value of: 2 > 1\n" |
| 3778 | " Actual: true\n" |
| 3779 | "Expected: false"); |
| 3780 | } |
| 3781 | |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 3782 | // Tests ASSERT_FALSE(predicate) for predicates returning AssertionResult. |
| 3783 | TEST(AssertionTest, AssertFalseWithAssertionResult) { |
| 3784 | ASSERT_FALSE(ResultIsEven(3)); |
| 3785 | EXPECT_FATAL_FAILURE(ASSERT_FALSE(ResultIsEven(2)), |
| 3786 | "Value of: ResultIsEven(2)\n" |
| 3787 | " Actual: true (2 is even)\n" |
| 3788 | "Expected: false"); |
| 3789 | ASSERT_FALSE(ResultIsEvenNoExplanation(3)); |
| 3790 | EXPECT_FATAL_FAILURE(ASSERT_FALSE(ResultIsEvenNoExplanation(2)), |
| 3791 | "Value of: ResultIsEvenNoExplanation(2)\n" |
| 3792 | " Actual: true\n" |
| 3793 | "Expected: false"); |
| 3794 | } |
| 3795 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3796 | #ifdef __BORLANDC__ |
| 3797 | // Restores warnings after previous "#pragma option push" supressed them |
| 3798 | #pragma option pop |
| 3799 | #endif |
| 3800 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3801 | // Tests using ASSERT_EQ on double values. The purpose is to make |
| 3802 | // sure that the specialization we did for integer and anonymous enums |
| 3803 | // isn't used for double arguments. |
| 3804 | TEST(ExpectTest, ASSERT_EQ_Double) { |
| 3805 | // A success. |
| 3806 | ASSERT_EQ(5.6, 5.6); |
| 3807 | |
| 3808 | // A failure. |
| 3809 | EXPECT_FATAL_FAILURE(ASSERT_EQ(5.1, 5.2), |
| 3810 | "5.1"); |
| 3811 | } |
| 3812 | |
| 3813 | // Tests ASSERT_EQ. |
| 3814 | TEST(AssertionTest, ASSERT_EQ) { |
| 3815 | ASSERT_EQ(5, 2 + 3); |
| 3816 | EXPECT_FATAL_FAILURE(ASSERT_EQ(5, 2*3), |
| 3817 | "Value of: 2*3\n" |
| 3818 | " Actual: 6\n" |
| 3819 | "Expected: 5"); |
| 3820 | } |
| 3821 | |
| 3822 | // Tests ASSERT_EQ(NULL, pointer). |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3823 | #if !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3824 | // The NULL-detection template magic fails to compile with |
| 3825 | // the Nokia compiler and crashes the ARM compiler, hence |
| 3826 | // not testing on Symbian. |
| 3827 | TEST(AssertionTest, ASSERT_EQ_NULL) { |
| 3828 | // A success. |
| 3829 | const char* p = NULL; |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 3830 | // Some older GCC versions may issue a spurious waring in this or the next |
| 3831 | // assertion statement. This warning should not be suppressed with |
| 3832 | // static_cast since the test verifies the ability to use bare NULL as the |
| 3833 | // expected parameter to the macro. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3834 | ASSERT_EQ(NULL, p); |
| 3835 | |
| 3836 | // A failure. |
| 3837 | static int n = 0; |
| 3838 | EXPECT_FATAL_FAILURE(ASSERT_EQ(NULL, &n), |
| 3839 | "Value of: &n\n"); |
| 3840 | } |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3841 | #endif // !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3842 | |
| 3843 | // Tests ASSERT_EQ(0, non_pointer). Since the literal 0 can be |
| 3844 | // treated as a null pointer by the compiler, we need to make sure |
| 3845 | // that ASSERT_EQ(0, non_pointer) isn't interpreted by Google Test as |
| 3846 | // ASSERT_EQ(static_cast<void*>(NULL), non_pointer). |
| 3847 | TEST(ExpectTest, ASSERT_EQ_0) { |
| 3848 | int n = 0; |
| 3849 | |
| 3850 | // A success. |
| 3851 | ASSERT_EQ(0, n); |
| 3852 | |
| 3853 | // A failure. |
| 3854 | EXPECT_FATAL_FAILURE(ASSERT_EQ(0, 5.6), |
| 3855 | "Expected: 0"); |
| 3856 | } |
| 3857 | |
| 3858 | // Tests ASSERT_NE. |
| 3859 | TEST(AssertionTest, ASSERT_NE) { |
| 3860 | ASSERT_NE(6, 7); |
| 3861 | EXPECT_FATAL_FAILURE(ASSERT_NE('a', 'a'), |
| 3862 | "Expected: ('a') != ('a'), " |
| 3863 | "actual: 'a' (97, 0x61) vs 'a' (97, 0x61)"); |
| 3864 | } |
| 3865 | |
| 3866 | // Tests ASSERT_LE. |
| 3867 | TEST(AssertionTest, ASSERT_LE) { |
| 3868 | ASSERT_LE(2, 3); |
| 3869 | ASSERT_LE(2, 2); |
| 3870 | EXPECT_FATAL_FAILURE(ASSERT_LE(2, 0), |
| 3871 | "Expected: (2) <= (0), actual: 2 vs 0"); |
| 3872 | } |
| 3873 | |
| 3874 | // Tests ASSERT_LT. |
| 3875 | TEST(AssertionTest, ASSERT_LT) { |
| 3876 | ASSERT_LT(2, 3); |
| 3877 | EXPECT_FATAL_FAILURE(ASSERT_LT(2, 2), |
| 3878 | "Expected: (2) < (2), actual: 2 vs 2"); |
| 3879 | } |
| 3880 | |
| 3881 | // Tests ASSERT_GE. |
| 3882 | TEST(AssertionTest, ASSERT_GE) { |
| 3883 | ASSERT_GE(2, 1); |
| 3884 | ASSERT_GE(2, 2); |
| 3885 | EXPECT_FATAL_FAILURE(ASSERT_GE(2, 3), |
| 3886 | "Expected: (2) >= (3), actual: 2 vs 3"); |
| 3887 | } |
| 3888 | |
| 3889 | // Tests ASSERT_GT. |
| 3890 | TEST(AssertionTest, ASSERT_GT) { |
| 3891 | ASSERT_GT(2, 1); |
| 3892 | EXPECT_FATAL_FAILURE(ASSERT_GT(2, 2), |
| 3893 | "Expected: (2) > (2), actual: 2 vs 2"); |
| 3894 | } |
| 3895 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3896 | #if GTEST_HAS_EXCEPTIONS |
| 3897 | |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3898 | void ThrowNothing() {} |
| 3899 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3900 | // Tests ASSERT_THROW. |
| 3901 | TEST(AssertionTest, ASSERT_THROW) { |
| 3902 | ASSERT_THROW(ThrowAnInteger(), int); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3903 | #if !defined(__BORLANDC__) || __BORLANDC__ >= 0x600 || defined(_DEBUG) |
| 3904 | // ICE's in C++Builder 2007 (Release build). |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3905 | EXPECT_FATAL_FAILURE( |
| 3906 | ASSERT_THROW(ThrowAnInteger(), bool), |
| 3907 | "Expected: ThrowAnInteger() throws an exception of type bool.\n" |
| 3908 | " Actual: it throws a different type."); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3909 | #endif |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3910 | EXPECT_FATAL_FAILURE( |
| 3911 | ASSERT_THROW(ThrowNothing(), bool), |
| 3912 | "Expected: ThrowNothing() throws an exception of type bool.\n" |
| 3913 | " Actual: it throws nothing."); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
| 3916 | // Tests ASSERT_NO_THROW. |
| 3917 | TEST(AssertionTest, ASSERT_NO_THROW) { |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3918 | ASSERT_NO_THROW(ThrowNothing()); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3919 | EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()), |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3920 | "Expected: ThrowAnInteger() doesn't throw an exception." |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3921 | "\n Actual: it throws."); |
| 3922 | } |
| 3923 | |
| 3924 | // Tests ASSERT_ANY_THROW. |
| 3925 | TEST(AssertionTest, ASSERT_ANY_THROW) { |
| 3926 | ASSERT_ANY_THROW(ThrowAnInteger()); |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3927 | EXPECT_FATAL_FAILURE( |
| 3928 | ASSERT_ANY_THROW(ThrowNothing()), |
| 3929 | "Expected: ThrowNothing() throws an exception.\n" |
| 3930 | " Actual: it doesn't."); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
| 3933 | #endif // GTEST_HAS_EXCEPTIONS |
| 3934 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3935 | // Makes sure we deal with the precedence of <<. This test should |
| 3936 | // compile. |
| 3937 | TEST(AssertionTest, AssertPrecedence) { |
| 3938 | ASSERT_EQ(1 < 2, true); |
| 3939 | ASSERT_EQ(true && false, false); |
| 3940 | } |
| 3941 | |
| 3942 | // A subroutine used by the following test. |
| 3943 | void TestEq1(int x) { |
| 3944 | ASSERT_EQ(1, x); |
| 3945 | } |
| 3946 | |
| 3947 | // Tests calling a test subroutine that's not part of a fixture. |
| 3948 | TEST(AssertionTest, NonFixtureSubroutine) { |
| 3949 | EXPECT_FATAL_FAILURE(TestEq1(2), |
| 3950 | "Value of: x"); |
| 3951 | } |
| 3952 | |
| 3953 | // An uncopyable class. |
| 3954 | class Uncopyable { |
| 3955 | public: |
zhanyong.wan | 7de3401 | 2009-12-16 19:54:05 +0000 | [diff] [blame] | 3956 | explicit Uncopyable(int a_value) : value_(a_value) {} |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3957 | |
| 3958 | int value() const { return value_; } |
| 3959 | bool operator==(const Uncopyable& rhs) const { |
| 3960 | return value() == rhs.value(); |
| 3961 | } |
| 3962 | private: |
| 3963 | // This constructor deliberately has no implementation, as we don't |
| 3964 | // want this class to be copyable. |
| 3965 | Uncopyable(const Uncopyable&); // NOLINT |
| 3966 | |
| 3967 | int value_; |
| 3968 | }; |
| 3969 | |
| 3970 | ::std::ostream& operator<<(::std::ostream& os, const Uncopyable& value) { |
| 3971 | return os << value.value(); |
| 3972 | } |
| 3973 | |
| 3974 | |
| 3975 | bool IsPositiveUncopyable(const Uncopyable& x) { |
| 3976 | return x.value() > 0; |
| 3977 | } |
| 3978 | |
| 3979 | // A subroutine used by the following test. |
| 3980 | void TestAssertNonPositive() { |
| 3981 | Uncopyable y(-1); |
| 3982 | ASSERT_PRED1(IsPositiveUncopyable, y); |
| 3983 | } |
| 3984 | // A subroutine used by the following test. |
| 3985 | void TestAssertEqualsUncopyable() { |
| 3986 | Uncopyable x(5); |
| 3987 | Uncopyable y(-1); |
| 3988 | ASSERT_EQ(x, y); |
| 3989 | } |
| 3990 | |
| 3991 | // Tests that uncopyable objects can be used in assertions. |
| 3992 | TEST(AssertionTest, AssertWorksWithUncopyableObject) { |
| 3993 | Uncopyable x(5); |
| 3994 | ASSERT_PRED1(IsPositiveUncopyable, x); |
| 3995 | ASSERT_EQ(x, x); |
| 3996 | EXPECT_FATAL_FAILURE(TestAssertNonPositive(), |
| 3997 | "IsPositiveUncopyable(y) evaluates to false, where\ny evaluates to -1"); |
| 3998 | EXPECT_FATAL_FAILURE(TestAssertEqualsUncopyable(), |
| 3999 | "Value of: y\n Actual: -1\nExpected: x\nWhich is: 5"); |
| 4000 | } |
| 4001 | |
| 4002 | // Tests that uncopyable objects can be used in expects. |
| 4003 | TEST(AssertionTest, ExpectWorksWithUncopyableObject) { |
| 4004 | Uncopyable x(5); |
| 4005 | EXPECT_PRED1(IsPositiveUncopyable, x); |
| 4006 | Uncopyable y(-1); |
| 4007 | EXPECT_NONFATAL_FAILURE(EXPECT_PRED1(IsPositiveUncopyable, y), |
| 4008 | "IsPositiveUncopyable(y) evaluates to false, where\ny evaluates to -1"); |
| 4009 | EXPECT_EQ(x, x); |
| 4010 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(x, y), |
| 4011 | "Value of: y\n Actual: -1\nExpected: x\nWhich is: 5"); |
| 4012 | } |
| 4013 | |
| 4014 | |
| 4015 | // The version of gcc used in XCode 2.2 has a bug and doesn't allow |
zhanyong.wan | efa2fc7 | 2009-03-31 16:27:55 +0000 | [diff] [blame] | 4016 | // anonymous enums in assertions. Therefore the following test is not |
| 4017 | // done on Mac. |
| 4018 | #if !GTEST_OS_MAC |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4019 | |
| 4020 | // Tests using assertions with anonymous enums. |
| 4021 | enum { |
| 4022 | CASE_A = -1, |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 4023 | #if GTEST_OS_LINUX |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4024 | // We want to test the case where the size of the anonymous enum is |
| 4025 | // larger than sizeof(int), to make sure our implementation of the |
| 4026 | // assertions doesn't truncate the enums. However, MSVC |
| 4027 | // (incorrectly) doesn't allow an enum value to exceed the range of |
| 4028 | // an int, so this has to be conditionally compiled. |
| 4029 | // |
| 4030 | // On Linux, CASE_B and CASE_A have the same value when truncated to |
| 4031 | // int size. We want to test whether this will confuse the |
| 4032 | // assertions. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4033 | CASE_B = testing::internal::kMaxBiggestInt, |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4034 | #else |
| 4035 | CASE_B = INT_MAX, |
| 4036 | #endif // GTEST_OS_LINUX |
| 4037 | }; |
| 4038 | |
| 4039 | TEST(AssertionTest, AnonymousEnum) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 4040 | #if GTEST_OS_LINUX |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4041 | EXPECT_EQ(static_cast<int>(CASE_A), static_cast<int>(CASE_B)); |
| 4042 | #endif // GTEST_OS_LINUX |
| 4043 | |
| 4044 | EXPECT_EQ(CASE_A, CASE_A); |
| 4045 | EXPECT_NE(CASE_A, CASE_B); |
| 4046 | EXPECT_LT(CASE_A, CASE_B); |
| 4047 | EXPECT_LE(CASE_A, CASE_B); |
| 4048 | EXPECT_GT(CASE_B, CASE_A); |
| 4049 | EXPECT_GE(CASE_A, CASE_A); |
| 4050 | EXPECT_NONFATAL_FAILURE(EXPECT_GE(CASE_A, CASE_B), |
| 4051 | "(CASE_A) >= (CASE_B)"); |
| 4052 | |
| 4053 | ASSERT_EQ(CASE_A, CASE_A); |
| 4054 | ASSERT_NE(CASE_A, CASE_B); |
| 4055 | ASSERT_LT(CASE_A, CASE_B); |
| 4056 | ASSERT_LE(CASE_A, CASE_B); |
| 4057 | ASSERT_GT(CASE_B, CASE_A); |
| 4058 | ASSERT_GE(CASE_A, CASE_A); |
| 4059 | EXPECT_FATAL_FAILURE(ASSERT_EQ(CASE_A, CASE_B), |
| 4060 | "Value of: CASE_B"); |
| 4061 | } |
| 4062 | |
zhanyong.wan | efa2fc7 | 2009-03-31 16:27:55 +0000 | [diff] [blame] | 4063 | #endif // !GTEST_OS_MAC |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4064 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 4065 | #if GTEST_OS_WINDOWS |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4066 | |
| 4067 | static HRESULT UnexpectedHRESULTFailure() { |
| 4068 | return E_UNEXPECTED; |
| 4069 | } |
| 4070 | |
| 4071 | static HRESULT OkHRESULTSuccess() { |
| 4072 | return S_OK; |
| 4073 | } |
| 4074 | |
| 4075 | static HRESULT FalseHRESULTSuccess() { |
| 4076 | return S_FALSE; |
| 4077 | } |
| 4078 | |
| 4079 | // HRESULT assertion tests test both zero and non-zero |
| 4080 | // success codes as well as failure message for each. |
| 4081 | // |
| 4082 | // Windows CE doesn't support message texts. |
| 4083 | TEST(HRESULTAssertionTest, EXPECT_HRESULT_SUCCEEDED) { |
| 4084 | EXPECT_HRESULT_SUCCEEDED(S_OK); |
| 4085 | EXPECT_HRESULT_SUCCEEDED(S_FALSE); |
| 4086 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4087 | EXPECT_NONFATAL_FAILURE(EXPECT_HRESULT_SUCCEEDED(UnexpectedHRESULTFailure()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 4088 | "Expected: (UnexpectedHRESULTFailure()) succeeds.\n" |
| 4089 | " Actual: 0x8000FFFF"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
| 4092 | TEST(HRESULTAssertionTest, ASSERT_HRESULT_SUCCEEDED) { |
| 4093 | ASSERT_HRESULT_SUCCEEDED(S_OK); |
| 4094 | ASSERT_HRESULT_SUCCEEDED(S_FALSE); |
| 4095 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4096 | EXPECT_FATAL_FAILURE(ASSERT_HRESULT_SUCCEEDED(UnexpectedHRESULTFailure()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 4097 | "Expected: (UnexpectedHRESULTFailure()) succeeds.\n" |
| 4098 | " Actual: 0x8000FFFF"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | TEST(HRESULTAssertionTest, EXPECT_HRESULT_FAILED) { |
| 4102 | EXPECT_HRESULT_FAILED(E_UNEXPECTED); |
| 4103 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4104 | EXPECT_NONFATAL_FAILURE(EXPECT_HRESULT_FAILED(OkHRESULTSuccess()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 4105 | "Expected: (OkHRESULTSuccess()) fails.\n" |
| 4106 | " Actual: 0x00000000"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4107 | EXPECT_NONFATAL_FAILURE(EXPECT_HRESULT_FAILED(FalseHRESULTSuccess()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 4108 | "Expected: (FalseHRESULTSuccess()) fails.\n" |
| 4109 | " Actual: 0x00000001"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | TEST(HRESULTAssertionTest, ASSERT_HRESULT_FAILED) { |
| 4113 | ASSERT_HRESULT_FAILED(E_UNEXPECTED); |
| 4114 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 4115 | #ifndef __BORLANDC__ |
| 4116 | // ICE's in C++Builder 2007 and 2009. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4117 | EXPECT_FATAL_FAILURE(ASSERT_HRESULT_FAILED(OkHRESULTSuccess()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 4118 | "Expected: (OkHRESULTSuccess()) fails.\n" |
| 4119 | " Actual: 0x00000000"); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 4120 | #endif |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4121 | EXPECT_FATAL_FAILURE(ASSERT_HRESULT_FAILED(FalseHRESULTSuccess()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 4122 | "Expected: (FalseHRESULTSuccess()) fails.\n" |
| 4123 | " Actual: 0x00000001"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | // Tests that streaming to the HRESULT macros works. |
| 4127 | TEST(HRESULTAssertionTest, Streaming) { |
| 4128 | EXPECT_HRESULT_SUCCEEDED(S_OK) << "unexpected failure"; |
| 4129 | ASSERT_HRESULT_SUCCEEDED(S_OK) << "unexpected failure"; |
| 4130 | EXPECT_HRESULT_FAILED(E_UNEXPECTED) << "unexpected failure"; |
| 4131 | ASSERT_HRESULT_FAILED(E_UNEXPECTED) << "unexpected failure"; |
| 4132 | |
| 4133 | EXPECT_NONFATAL_FAILURE( |
| 4134 | EXPECT_HRESULT_SUCCEEDED(E_UNEXPECTED) << "expected failure", |
| 4135 | "expected failure"); |
| 4136 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 4137 | #ifndef __BORLANDC__ |
| 4138 | // ICE's in C++Builder 2007 and 2009. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4139 | EXPECT_FATAL_FAILURE( |
| 4140 | ASSERT_HRESULT_SUCCEEDED(E_UNEXPECTED) << "expected failure", |
| 4141 | "expected failure"); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 4142 | #endif |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4143 | |
| 4144 | EXPECT_NONFATAL_FAILURE( |
| 4145 | EXPECT_HRESULT_FAILED(S_OK) << "expected failure", |
| 4146 | "expected failure"); |
| 4147 | |
| 4148 | EXPECT_FATAL_FAILURE( |
| 4149 | ASSERT_HRESULT_FAILED(S_OK) << "expected failure", |
| 4150 | "expected failure"); |
| 4151 | } |
| 4152 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 4153 | #endif // GTEST_OS_WINDOWS |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4154 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 4155 | #ifdef __BORLANDC__ |
| 4156 | // Silences warnings: "Condition is always true", "Unreachable code" |
| 4157 | #pragma option push -w-ccc -w-rch |
| 4158 | #endif |
| 4159 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4160 | // Tests that the assertion macros behave like single statements. |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4161 | TEST(AssertionSyntaxTest, BasicAssertionsBehavesLikeSingleStatement) { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4162 | if (AlwaysFalse()) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4163 | ASSERT_TRUE(false) << "This should never be executed; " |
| 4164 | "It's a compilation test only."; |
| 4165 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4166 | if (AlwaysTrue()) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4167 | EXPECT_FALSE(false); |
| 4168 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 4169 | ; // NOLINT |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4170 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4171 | if (AlwaysFalse()) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4172 | ASSERT_LT(1, 3); |
| 4173 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4174 | if (AlwaysFalse()) |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 4175 | ; // NOLINT |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4176 | else |
| 4177 | EXPECT_GT(3, 2) << ""; |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4178 | } |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4179 | |
| 4180 | #if GTEST_HAS_EXCEPTIONS |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 4181 | // Tests that the compiler will not complain about unreachable code in the |
| 4182 | // EXPECT_THROW/EXPECT_ANY_THROW/EXPECT_NO_THROW macros. |
| 4183 | TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) { |
| 4184 | int n = 0; |
| 4185 | |
| 4186 | EXPECT_THROW(throw 1, int); |
| 4187 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(n++, int), ""); |
| 4188 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(throw 1, const char*), ""); |
| 4189 | EXPECT_NO_THROW(n++); |
| 4190 | EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(throw 1), ""); |
| 4191 | EXPECT_ANY_THROW(throw 1); |
| 4192 | EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(n++), ""); |
| 4193 | } |
| 4194 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4195 | TEST(AssertionSyntaxTest, ExceptionAssertionsBehavesLikeSingleStatement) { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4196 | if (AlwaysFalse()) |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 4197 | EXPECT_THROW(ThrowNothing(), bool); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4198 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4199 | if (AlwaysTrue()) |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4200 | EXPECT_THROW(ThrowAnInteger(), int); |
| 4201 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 4202 | ; // NOLINT |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4203 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4204 | if (AlwaysFalse()) |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4205 | EXPECT_NO_THROW(ThrowAnInteger()); |
| 4206 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4207 | if (AlwaysTrue()) |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 4208 | EXPECT_NO_THROW(ThrowNothing()); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4209 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 4210 | ; // NOLINT |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4211 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4212 | if (AlwaysFalse()) |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 4213 | EXPECT_ANY_THROW(ThrowNothing()); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4214 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4215 | if (AlwaysTrue()) |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4216 | EXPECT_ANY_THROW(ThrowAnInteger()); |
| 4217 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 4218 | ; // NOLINT |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4219 | } |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4220 | #endif // GTEST_HAS_EXCEPTIONS |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4221 | |
| 4222 | TEST(AssertionSyntaxTest, NoFatalFailureAssertionsBehavesLikeSingleStatement) { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4223 | if (AlwaysFalse()) |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4224 | EXPECT_NO_FATAL_FAILURE(FAIL()) << "This should never be executed. " |
| 4225 | << "It's a compilation test only."; |
| 4226 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 4227 | ; // NOLINT |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4228 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4229 | if (AlwaysFalse()) |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4230 | ASSERT_NO_FATAL_FAILURE(FAIL()) << ""; |
| 4231 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 4232 | ; // NOLINT |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4233 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4234 | if (AlwaysTrue()) |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4235 | EXPECT_NO_FATAL_FAILURE(SUCCEED()); |
| 4236 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 4237 | ; // NOLINT |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4238 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4239 | if (AlwaysFalse()) |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 4240 | ; // NOLINT |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4241 | else |
| 4242 | ASSERT_NO_FATAL_FAILURE(SUCCEED()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | // Tests that the assertion macros work well with switch statements. |
| 4246 | TEST(AssertionSyntaxTest, WorksWithSwitch) { |
| 4247 | switch (0) { |
| 4248 | case 1: |
| 4249 | break; |
| 4250 | default: |
| 4251 | ASSERT_TRUE(true); |
| 4252 | } |
| 4253 | |
| 4254 | switch (0) |
| 4255 | case 0: |
| 4256 | EXPECT_FALSE(false) << "EXPECT_FALSE failed in switch case"; |
| 4257 | |
| 4258 | // Binary assertions are implemented using a different code path |
| 4259 | // than the Boolean assertions. Hence we test them separately. |
| 4260 | switch (0) { |
| 4261 | case 1: |
| 4262 | default: |
| 4263 | ASSERT_EQ(1, 1) << "ASSERT_EQ failed in default switch handler"; |
| 4264 | } |
| 4265 | |
| 4266 | switch (0) |
| 4267 | case 0: |
| 4268 | EXPECT_NE(1, 2); |
| 4269 | } |
| 4270 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4271 | #if GTEST_HAS_EXCEPTIONS |
| 4272 | |
| 4273 | void ThrowAString() { |
| 4274 | throw "String"; |
| 4275 | } |
| 4276 | |
| 4277 | // Test that the exception assertion macros compile and work with const |
| 4278 | // type qualifier. |
| 4279 | TEST(AssertionSyntaxTest, WorksWithConst) { |
| 4280 | ASSERT_THROW(ThrowAString(), const char*); |
| 4281 | |
| 4282 | EXPECT_THROW(ThrowAString(), const char*); |
| 4283 | } |
| 4284 | |
| 4285 | #endif // GTEST_HAS_EXCEPTIONS |
| 4286 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4287 | } // namespace |
| 4288 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4289 | namespace testing { |
| 4290 | |
| 4291 | // Tests that Google Test tracks SUCCEED*. |
| 4292 | TEST(SuccessfulAssertionTest, SUCCEED) { |
| 4293 | SUCCEED(); |
| 4294 | SUCCEED() << "OK"; |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 4295 | EXPECT_EQ(2, GetUnitTestImpl()->current_test_result()->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4296 | } |
| 4297 | |
| 4298 | // Tests that Google Test doesn't track successful EXPECT_*. |
| 4299 | TEST(SuccessfulAssertionTest, EXPECT) { |
| 4300 | EXPECT_TRUE(true); |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 4301 | EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4302 | } |
| 4303 | |
| 4304 | // Tests that Google Test doesn't track successful EXPECT_STR*. |
| 4305 | TEST(SuccessfulAssertionTest, EXPECT_STR) { |
| 4306 | EXPECT_STREQ("", ""); |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 4307 | EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | // Tests that Google Test doesn't track successful ASSERT_*. |
| 4311 | TEST(SuccessfulAssertionTest, ASSERT) { |
| 4312 | ASSERT_TRUE(true); |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 4313 | EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | // Tests that Google Test doesn't track successful ASSERT_STR*. |
| 4317 | TEST(SuccessfulAssertionTest, ASSERT_STR) { |
| 4318 | ASSERT_STREQ("", ""); |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 4319 | EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4320 | } |
| 4321 | |
| 4322 | } // namespace testing |
| 4323 | |
| 4324 | namespace { |
| 4325 | |
| 4326 | // Tests EXPECT_TRUE. |
| 4327 | TEST(ExpectTest, EXPECT_TRUE) { |
| 4328 | EXPECT_TRUE(2 > 1); // NOLINT |
| 4329 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(2 < 1), |
| 4330 | "Value of: 2 < 1\n" |
| 4331 | " Actual: false\n" |
| 4332 | "Expected: true"); |
| 4333 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(2 > 3), |
| 4334 | "2 > 3"); |
| 4335 | } |
| 4336 | |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 4337 | // Tests EXPECT_TRUE(predicate) for predicates returning AssertionResult. |
| 4338 | TEST(ExpectTest, ExpectTrueWithAssertionResult) { |
| 4339 | EXPECT_TRUE(ResultIsEven(2)); |
| 4340 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(ResultIsEven(3)), |
| 4341 | "Value of: ResultIsEven(3)\n" |
| 4342 | " Actual: false (3 is odd)\n" |
| 4343 | "Expected: true"); |
| 4344 | EXPECT_TRUE(ResultIsEvenNoExplanation(2)); |
| 4345 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(ResultIsEvenNoExplanation(3)), |
| 4346 | "Value of: ResultIsEvenNoExplanation(3)\n" |
| 4347 | " Actual: false (3 is odd)\n" |
| 4348 | "Expected: true"); |
| 4349 | } |
| 4350 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4351 | // Tests EXPECT_FALSE. |
| 4352 | TEST(ExpectTest, EXPECT_FALSE) { |
| 4353 | EXPECT_FALSE(2 < 1); // NOLINT |
| 4354 | EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(2 > 1), |
| 4355 | "Value of: 2 > 1\n" |
| 4356 | " Actual: true\n" |
| 4357 | "Expected: false"); |
| 4358 | EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(2 < 3), |
| 4359 | "2 < 3"); |
| 4360 | } |
| 4361 | |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 4362 | // Tests EXPECT_FALSE(predicate) for predicates returning AssertionResult. |
| 4363 | TEST(ExpectTest, ExpectFalseWithAssertionResult) { |
| 4364 | EXPECT_FALSE(ResultIsEven(3)); |
| 4365 | EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(ResultIsEven(2)), |
| 4366 | "Value of: ResultIsEven(2)\n" |
| 4367 | " Actual: true (2 is even)\n" |
| 4368 | "Expected: false"); |
| 4369 | EXPECT_FALSE(ResultIsEvenNoExplanation(3)); |
| 4370 | EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(ResultIsEvenNoExplanation(2)), |
| 4371 | "Value of: ResultIsEvenNoExplanation(2)\n" |
| 4372 | " Actual: true\n" |
| 4373 | "Expected: false"); |
| 4374 | } |
| 4375 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 4376 | #ifdef __BORLANDC__ |
| 4377 | // Restores warnings after previous "#pragma option push" supressed them |
| 4378 | #pragma option pop |
| 4379 | #endif |
| 4380 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4381 | // Tests EXPECT_EQ. |
| 4382 | TEST(ExpectTest, EXPECT_EQ) { |
| 4383 | EXPECT_EQ(5, 2 + 3); |
| 4384 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5, 2*3), |
| 4385 | "Value of: 2*3\n" |
| 4386 | " Actual: 6\n" |
| 4387 | "Expected: 5"); |
| 4388 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5, 2 - 3), |
| 4389 | "2 - 3"); |
| 4390 | } |
| 4391 | |
| 4392 | // Tests using EXPECT_EQ on double values. The purpose is to make |
| 4393 | // sure that the specialization we did for integer and anonymous enums |
| 4394 | // isn't used for double arguments. |
| 4395 | TEST(ExpectTest, EXPECT_EQ_Double) { |
| 4396 | // A success. |
| 4397 | EXPECT_EQ(5.6, 5.6); |
| 4398 | |
| 4399 | // A failure. |
| 4400 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5.1, 5.2), |
| 4401 | "5.1"); |
| 4402 | } |
| 4403 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 4404 | #if !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4405 | // Tests EXPECT_EQ(NULL, pointer). |
| 4406 | TEST(ExpectTest, EXPECT_EQ_NULL) { |
| 4407 | // A success. |
| 4408 | const char* p = NULL; |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 4409 | // Some older GCC versions may issue a spurious waring in this or the next |
| 4410 | // assertion statement. This warning should not be suppressed with |
| 4411 | // static_cast since the test verifies the ability to use bare NULL as the |
| 4412 | // expected parameter to the macro. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4413 | EXPECT_EQ(NULL, p); |
| 4414 | |
| 4415 | // A failure. |
| 4416 | int n = 0; |
| 4417 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(NULL, &n), |
| 4418 | "Value of: &n\n"); |
| 4419 | } |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 4420 | #endif // !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4421 | |
| 4422 | // Tests EXPECT_EQ(0, non_pointer). Since the literal 0 can be |
| 4423 | // treated as a null pointer by the compiler, we need to make sure |
| 4424 | // that EXPECT_EQ(0, non_pointer) isn't interpreted by Google Test as |
| 4425 | // EXPECT_EQ(static_cast<void*>(NULL), non_pointer). |
| 4426 | TEST(ExpectTest, EXPECT_EQ_0) { |
| 4427 | int n = 0; |
| 4428 | |
| 4429 | // A success. |
| 4430 | EXPECT_EQ(0, n); |
| 4431 | |
| 4432 | // A failure. |
| 4433 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(0, 5.6), |
| 4434 | "Expected: 0"); |
| 4435 | } |
| 4436 | |
| 4437 | // Tests EXPECT_NE. |
| 4438 | TEST(ExpectTest, EXPECT_NE) { |
| 4439 | EXPECT_NE(6, 7); |
| 4440 | |
| 4441 | EXPECT_NONFATAL_FAILURE(EXPECT_NE('a', 'a'), |
| 4442 | "Expected: ('a') != ('a'), " |
| 4443 | "actual: 'a' (97, 0x61) vs 'a' (97, 0x61)"); |
| 4444 | EXPECT_NONFATAL_FAILURE(EXPECT_NE(2, 2), |
| 4445 | "2"); |
| 4446 | char* const p0 = NULL; |
| 4447 | EXPECT_NONFATAL_FAILURE(EXPECT_NE(p0, p0), |
| 4448 | "p0"); |
| 4449 | // Only way to get the Nokia compiler to compile the cast |
| 4450 | // is to have a separate void* variable first. Putting |
| 4451 | // the two casts on the same line doesn't work, neither does |
| 4452 | // a direct C-style to char*. |
| 4453 | void* pv1 = (void*)0x1234; // NOLINT |
| 4454 | char* const p1 = reinterpret_cast<char*>(pv1); |
| 4455 | EXPECT_NONFATAL_FAILURE(EXPECT_NE(p1, p1), |
| 4456 | "p1"); |
| 4457 | } |
| 4458 | |
| 4459 | // Tests EXPECT_LE. |
| 4460 | TEST(ExpectTest, EXPECT_LE) { |
| 4461 | EXPECT_LE(2, 3); |
| 4462 | EXPECT_LE(2, 2); |
| 4463 | EXPECT_NONFATAL_FAILURE(EXPECT_LE(2, 0), |
| 4464 | "Expected: (2) <= (0), actual: 2 vs 0"); |
| 4465 | EXPECT_NONFATAL_FAILURE(EXPECT_LE(1.1, 0.9), |
| 4466 | "(1.1) <= (0.9)"); |
| 4467 | } |
| 4468 | |
| 4469 | // Tests EXPECT_LT. |
| 4470 | TEST(ExpectTest, EXPECT_LT) { |
| 4471 | EXPECT_LT(2, 3); |
| 4472 | EXPECT_NONFATAL_FAILURE(EXPECT_LT(2, 2), |
| 4473 | "Expected: (2) < (2), actual: 2 vs 2"); |
| 4474 | EXPECT_NONFATAL_FAILURE(EXPECT_LT(2, 1), |
| 4475 | "(2) < (1)"); |
| 4476 | } |
| 4477 | |
| 4478 | // Tests EXPECT_GE. |
| 4479 | TEST(ExpectTest, EXPECT_GE) { |
| 4480 | EXPECT_GE(2, 1); |
| 4481 | EXPECT_GE(2, 2); |
| 4482 | EXPECT_NONFATAL_FAILURE(EXPECT_GE(2, 3), |
| 4483 | "Expected: (2) >= (3), actual: 2 vs 3"); |
| 4484 | EXPECT_NONFATAL_FAILURE(EXPECT_GE(0.9, 1.1), |
| 4485 | "(0.9) >= (1.1)"); |
| 4486 | } |
| 4487 | |
| 4488 | // Tests EXPECT_GT. |
| 4489 | TEST(ExpectTest, EXPECT_GT) { |
| 4490 | EXPECT_GT(2, 1); |
| 4491 | EXPECT_NONFATAL_FAILURE(EXPECT_GT(2, 2), |
| 4492 | "Expected: (2) > (2), actual: 2 vs 2"); |
| 4493 | EXPECT_NONFATAL_FAILURE(EXPECT_GT(2, 3), |
| 4494 | "(2) > (3)"); |
| 4495 | } |
| 4496 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4497 | #if GTEST_HAS_EXCEPTIONS |
| 4498 | |
| 4499 | // Tests EXPECT_THROW. |
| 4500 | TEST(ExpectTest, EXPECT_THROW) { |
| 4501 | EXPECT_THROW(ThrowAnInteger(), int); |
| 4502 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnInteger(), bool), |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 4503 | "Expected: ThrowAnInteger() throws an exception of " |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4504 | "type bool.\n Actual: it throws a different type."); |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 4505 | EXPECT_NONFATAL_FAILURE( |
| 4506 | EXPECT_THROW(ThrowNothing(), bool), |
| 4507 | "Expected: ThrowNothing() throws an exception of type bool.\n" |
| 4508 | " Actual: it throws nothing."); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
| 4511 | // Tests EXPECT_NO_THROW. |
| 4512 | TEST(ExpectTest, EXPECT_NO_THROW) { |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 4513 | EXPECT_NO_THROW(ThrowNothing()); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4514 | EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()), |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 4515 | "Expected: ThrowAnInteger() doesn't throw an " |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4516 | "exception.\n Actual: it throws."); |
| 4517 | } |
| 4518 | |
| 4519 | // Tests EXPECT_ANY_THROW. |
| 4520 | TEST(ExpectTest, EXPECT_ANY_THROW) { |
| 4521 | EXPECT_ANY_THROW(ThrowAnInteger()); |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 4522 | EXPECT_NONFATAL_FAILURE( |
| 4523 | EXPECT_ANY_THROW(ThrowNothing()), |
| 4524 | "Expected: ThrowNothing() throws an exception.\n" |
| 4525 | " Actual: it doesn't."); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | #endif // GTEST_HAS_EXCEPTIONS |
| 4529 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4530 | // Make sure we deal with the precedence of <<. |
| 4531 | TEST(ExpectTest, ExpectPrecedence) { |
| 4532 | EXPECT_EQ(1 < 2, true); |
| 4533 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(true, true && false), |
| 4534 | "Value of: true && false"); |
| 4535 | } |
| 4536 | |
| 4537 | |
| 4538 | // Tests the StreamableToString() function. |
| 4539 | |
| 4540 | // Tests using StreamableToString() on a scalar. |
| 4541 | TEST(StreamableToStringTest, Scalar) { |
| 4542 | EXPECT_STREQ("5", StreamableToString(5).c_str()); |
| 4543 | } |
| 4544 | |
| 4545 | // Tests using StreamableToString() on a non-char pointer. |
| 4546 | TEST(StreamableToStringTest, Pointer) { |
| 4547 | int n = 0; |
| 4548 | int* p = &n; |
| 4549 | EXPECT_STRNE("(null)", StreamableToString(p).c_str()); |
| 4550 | } |
| 4551 | |
| 4552 | // Tests using StreamableToString() on a NULL non-char pointer. |
| 4553 | TEST(StreamableToStringTest, NullPointer) { |
| 4554 | int* p = NULL; |
| 4555 | EXPECT_STREQ("(null)", StreamableToString(p).c_str()); |
| 4556 | } |
| 4557 | |
| 4558 | // Tests using StreamableToString() on a C string. |
| 4559 | TEST(StreamableToStringTest, CString) { |
| 4560 | EXPECT_STREQ("Foo", StreamableToString("Foo").c_str()); |
| 4561 | } |
| 4562 | |
| 4563 | // Tests using StreamableToString() on a NULL C string. |
| 4564 | TEST(StreamableToStringTest, NullCString) { |
| 4565 | char* p = NULL; |
| 4566 | EXPECT_STREQ("(null)", StreamableToString(p).c_str()); |
| 4567 | } |
| 4568 | |
| 4569 | // Tests using streamable values as assertion messages. |
| 4570 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4571 | // Tests using std::string as an assertion message. |
| 4572 | TEST(StreamableTest, string) { |
| 4573 | static const std::string str( |
| 4574 | "This failure message is a std::string, and is expected."); |
| 4575 | EXPECT_FATAL_FAILURE(FAIL() << str, |
| 4576 | str.c_str()); |
| 4577 | } |
| 4578 | |
| 4579 | // Tests that we can output strings containing embedded NULs. |
| 4580 | // Limited to Linux because we can only do this with std::string's. |
| 4581 | TEST(StreamableTest, stringWithEmbeddedNUL) { |
| 4582 | static const char char_array_with_nul[] = |
| 4583 | "Here's a NUL\0 and some more string"; |
| 4584 | static const std::string string_with_nul(char_array_with_nul, |
| 4585 | sizeof(char_array_with_nul) |
| 4586 | - 1); // drops the trailing NUL |
| 4587 | EXPECT_FATAL_FAILURE(FAIL() << string_with_nul, |
| 4588 | "Here's a NUL\\0 and some more string"); |
| 4589 | } |
| 4590 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4591 | // Tests that we can output a NUL char. |
| 4592 | TEST(StreamableTest, NULChar) { |
| 4593 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 4594 | FAIL() << "A NUL" << '\0' << " and some more string"; |
| 4595 | }, "A NUL\\0 and some more string"); |
| 4596 | } |
| 4597 | |
| 4598 | // Tests using int as an assertion message. |
| 4599 | TEST(StreamableTest, int) { |
| 4600 | EXPECT_FATAL_FAILURE(FAIL() << 900913, |
| 4601 | "900913"); |
| 4602 | } |
| 4603 | |
| 4604 | // Tests using NULL char pointer as an assertion message. |
| 4605 | // |
| 4606 | // In MSVC, streaming a NULL char * causes access violation. Google Test |
| 4607 | // implemented a workaround (substituting "(null)" for NULL). This |
| 4608 | // tests whether the workaround works. |
| 4609 | TEST(StreamableTest, NullCharPtr) { |
| 4610 | EXPECT_FATAL_FAILURE(FAIL() << static_cast<const char*>(NULL), |
| 4611 | "(null)"); |
| 4612 | } |
| 4613 | |
| 4614 | // Tests that basic IO manipulators (endl, ends, and flush) can be |
| 4615 | // streamed to testing::Message. |
| 4616 | TEST(StreamableTest, BasicIoManip) { |
| 4617 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 4618 | FAIL() << "Line 1." << std::endl |
| 4619 | << "A NUL char " << std::ends << std::flush << " in line 2."; |
| 4620 | }, "Line 1.\nA NUL char \\0 in line 2."); |
| 4621 | } |
| 4622 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4623 | // Tests the macros that haven't been covered so far. |
| 4624 | |
| 4625 | void AddFailureHelper(bool* aborted) { |
| 4626 | *aborted = true; |
| 4627 | ADD_FAILURE() << "Failure"; |
| 4628 | *aborted = false; |
| 4629 | } |
| 4630 | |
| 4631 | // Tests ADD_FAILURE. |
| 4632 | TEST(MacroTest, ADD_FAILURE) { |
| 4633 | bool aborted = true; |
| 4634 | EXPECT_NONFATAL_FAILURE(AddFailureHelper(&aborted), |
| 4635 | "Failure"); |
| 4636 | EXPECT_FALSE(aborted); |
| 4637 | } |
| 4638 | |
| 4639 | // Tests FAIL. |
| 4640 | TEST(MacroTest, FAIL) { |
| 4641 | EXPECT_FATAL_FAILURE(FAIL(), |
| 4642 | "Failed"); |
| 4643 | EXPECT_FATAL_FAILURE(FAIL() << "Intentional failure.", |
| 4644 | "Intentional failure."); |
| 4645 | } |
| 4646 | |
| 4647 | // Tests SUCCEED |
| 4648 | TEST(MacroTest, SUCCEED) { |
| 4649 | SUCCEED(); |
| 4650 | SUCCEED() << "Explicit success."; |
| 4651 | } |
| 4652 | |
| 4653 | |
| 4654 | // Tests for EXPECT_EQ() and ASSERT_EQ(). |
| 4655 | // |
| 4656 | // These tests fail *intentionally*, s.t. the failure messages can be |
| 4657 | // generated and tested. |
| 4658 | // |
| 4659 | // We have different tests for different argument types. |
| 4660 | |
| 4661 | // Tests using bool values in {EXPECT|ASSERT}_EQ. |
| 4662 | TEST(EqAssertionTest, Bool) { |
| 4663 | EXPECT_EQ(true, true); |
| 4664 | EXPECT_FATAL_FAILURE(ASSERT_EQ(false, true), |
| 4665 | "Value of: true"); |
| 4666 | } |
| 4667 | |
| 4668 | // Tests using int values in {EXPECT|ASSERT}_EQ. |
| 4669 | TEST(EqAssertionTest, Int) { |
| 4670 | ASSERT_EQ(32, 32); |
| 4671 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(32, 33), |
| 4672 | "33"); |
| 4673 | } |
| 4674 | |
| 4675 | // Tests using time_t values in {EXPECT|ASSERT}_EQ. |
| 4676 | TEST(EqAssertionTest, Time_T) { |
| 4677 | EXPECT_EQ(static_cast<time_t>(0), |
| 4678 | static_cast<time_t>(0)); |
| 4679 | EXPECT_FATAL_FAILURE(ASSERT_EQ(static_cast<time_t>(0), |
| 4680 | static_cast<time_t>(1234)), |
| 4681 | "1234"); |
| 4682 | } |
| 4683 | |
| 4684 | // Tests using char values in {EXPECT|ASSERT}_EQ. |
| 4685 | TEST(EqAssertionTest, Char) { |
| 4686 | ASSERT_EQ('z', 'z'); |
| 4687 | const char ch = 'b'; |
| 4688 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ('\0', ch), |
| 4689 | "ch"); |
| 4690 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ('a', ch), |
| 4691 | "ch"); |
| 4692 | } |
| 4693 | |
| 4694 | // Tests using wchar_t values in {EXPECT|ASSERT}_EQ. |
| 4695 | TEST(EqAssertionTest, WideChar) { |
| 4696 | EXPECT_EQ(L'b', L'b'); |
| 4697 | |
| 4698 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(L'\0', L'x'), |
| 4699 | "Value of: L'x'\n" |
| 4700 | " Actual: L'x' (120, 0x78)\n" |
| 4701 | "Expected: L'\0'\n" |
| 4702 | "Which is: L'\0' (0, 0x0)"); |
| 4703 | |
| 4704 | static wchar_t wchar; |
| 4705 | wchar = L'b'; |
| 4706 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(L'a', wchar), |
| 4707 | "wchar"); |
| 4708 | wchar = L'\x8119'; |
| 4709 | EXPECT_FATAL_FAILURE(ASSERT_EQ(L'\x8120', wchar), |
| 4710 | "Value of: wchar"); |
| 4711 | } |
| 4712 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4713 | // Tests using ::std::string values in {EXPECT|ASSERT}_EQ. |
| 4714 | TEST(EqAssertionTest, StdString) { |
| 4715 | // Compares a const char* to an std::string that has identical |
| 4716 | // content. |
| 4717 | ASSERT_EQ("Test", ::std::string("Test")); |
| 4718 | |
| 4719 | // Compares two identical std::strings. |
| 4720 | static const ::std::string str1("A * in the middle"); |
| 4721 | static const ::std::string str2(str1); |
| 4722 | EXPECT_EQ(str1, str2); |
| 4723 | |
| 4724 | // Compares a const char* to an std::string that has different |
| 4725 | // content |
| 4726 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ("Test", ::std::string("test")), |
| 4727 | "::std::string(\"test\")"); |
| 4728 | |
| 4729 | // Compares an std::string to a char* that has different content. |
| 4730 | char* const p1 = const_cast<char*>("foo"); |
| 4731 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(::std::string("bar"), p1), |
| 4732 | "p1"); |
| 4733 | |
| 4734 | // Compares two std::strings that have different contents, one of |
| 4735 | // which having a NUL character in the middle. This should fail. |
| 4736 | static ::std::string str3(str1); |
| 4737 | str3.at(2) = '\0'; |
| 4738 | EXPECT_FATAL_FAILURE(ASSERT_EQ(str1, str3), |
| 4739 | "Value of: str3\n" |
| 4740 | " Actual: \"A \\0 in the middle\""); |
| 4741 | } |
| 4742 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4743 | #if GTEST_HAS_STD_WSTRING |
| 4744 | |
| 4745 | // Tests using ::std::wstring values in {EXPECT|ASSERT}_EQ. |
| 4746 | TEST(EqAssertionTest, StdWideString) { |
| 4747 | // Compares an std::wstring to a const wchar_t* that has identical |
| 4748 | // content. |
| 4749 | EXPECT_EQ(::std::wstring(L"Test\x8119"), L"Test\x8119"); |
| 4750 | |
| 4751 | // Compares two identical std::wstrings. |
| 4752 | const ::std::wstring wstr1(L"A * in the middle"); |
| 4753 | const ::std::wstring wstr2(wstr1); |
| 4754 | ASSERT_EQ(wstr1, wstr2); |
| 4755 | |
| 4756 | // Compares an std::wstring to a const wchar_t* that has different |
| 4757 | // content. |
| 4758 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 4759 | EXPECT_EQ(::std::wstring(L"Test\x8119"), L"Test\x8120"); |
| 4760 | }, "L\"Test\\x8120\""); |
| 4761 | |
| 4762 | // Compares two std::wstrings that have different contents, one of |
| 4763 | // which having a NUL character in the middle. |
| 4764 | ::std::wstring wstr3(wstr1); |
| 4765 | wstr3.at(2) = L'\0'; |
| 4766 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(wstr1, wstr3), |
| 4767 | "wstr3"); |
| 4768 | |
| 4769 | // Compares a wchar_t* to an std::wstring that has different |
| 4770 | // content. |
| 4771 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 4772 | ASSERT_EQ(const_cast<wchar_t*>(L"foo"), ::std::wstring(L"bar")); |
| 4773 | }, ""); |
| 4774 | } |
| 4775 | |
| 4776 | #endif // GTEST_HAS_STD_WSTRING |
| 4777 | |
| 4778 | #if GTEST_HAS_GLOBAL_STRING |
| 4779 | // Tests using ::string values in {EXPECT|ASSERT}_EQ. |
| 4780 | TEST(EqAssertionTest, GlobalString) { |
| 4781 | // Compares a const char* to a ::string that has identical content. |
| 4782 | EXPECT_EQ("Test", ::string("Test")); |
| 4783 | |
| 4784 | // Compares two identical ::strings. |
| 4785 | const ::string str1("A * in the middle"); |
| 4786 | const ::string str2(str1); |
| 4787 | ASSERT_EQ(str1, str2); |
| 4788 | |
| 4789 | // Compares a ::string to a const char* that has different content. |
| 4790 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(::string("Test"), "test"), |
| 4791 | "test"); |
| 4792 | |
| 4793 | // Compares two ::strings that have different contents, one of which |
| 4794 | // having a NUL character in the middle. |
| 4795 | ::string str3(str1); |
| 4796 | str3.at(2) = '\0'; |
| 4797 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(str1, str3), |
| 4798 | "str3"); |
| 4799 | |
| 4800 | // Compares a ::string to a char* that has different content. |
| 4801 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 4802 | ASSERT_EQ(::string("bar"), const_cast<char*>("foo")); |
| 4803 | }, ""); |
| 4804 | } |
| 4805 | |
| 4806 | #endif // GTEST_HAS_GLOBAL_STRING |
| 4807 | |
| 4808 | #if GTEST_HAS_GLOBAL_WSTRING |
| 4809 | |
| 4810 | // Tests using ::wstring values in {EXPECT|ASSERT}_EQ. |
| 4811 | TEST(EqAssertionTest, GlobalWideString) { |
| 4812 | // Compares a const wchar_t* to a ::wstring that has identical content. |
| 4813 | ASSERT_EQ(L"Test\x8119", ::wstring(L"Test\x8119")); |
| 4814 | |
| 4815 | // Compares two identical ::wstrings. |
| 4816 | static const ::wstring wstr1(L"A * in the middle"); |
| 4817 | static const ::wstring wstr2(wstr1); |
| 4818 | EXPECT_EQ(wstr1, wstr2); |
| 4819 | |
| 4820 | // Compares a const wchar_t* to a ::wstring that has different |
| 4821 | // content. |
| 4822 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 4823 | EXPECT_EQ(L"Test\x8120", ::wstring(L"Test\x8119")); |
| 4824 | }, "Test\\x8119"); |
| 4825 | |
| 4826 | // Compares a wchar_t* to a ::wstring that has different content. |
| 4827 | wchar_t* const p1 = const_cast<wchar_t*>(L"foo"); |
| 4828 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p1, ::wstring(L"bar")), |
| 4829 | "bar"); |
| 4830 | |
| 4831 | // Compares two ::wstrings that have different contents, one of which |
| 4832 | // having a NUL character in the middle. |
| 4833 | static ::wstring wstr3; |
| 4834 | wstr3 = wstr1; |
| 4835 | wstr3.at(2) = L'\0'; |
| 4836 | EXPECT_FATAL_FAILURE(ASSERT_EQ(wstr1, wstr3), |
| 4837 | "wstr3"); |
| 4838 | } |
| 4839 | |
| 4840 | #endif // GTEST_HAS_GLOBAL_WSTRING |
| 4841 | |
| 4842 | // Tests using char pointers in {EXPECT|ASSERT}_EQ. |
| 4843 | TEST(EqAssertionTest, CharPointer) { |
| 4844 | char* const p0 = NULL; |
| 4845 | // Only way to get the Nokia compiler to compile the cast |
| 4846 | // is to have a separate void* variable first. Putting |
| 4847 | // the two casts on the same line doesn't work, neither does |
| 4848 | // a direct C-style to char*. |
| 4849 | void* pv1 = (void*)0x1234; // NOLINT |
| 4850 | void* pv2 = (void*)0xABC0; // NOLINT |
| 4851 | char* const p1 = reinterpret_cast<char*>(pv1); |
| 4852 | char* const p2 = reinterpret_cast<char*>(pv2); |
| 4853 | ASSERT_EQ(p1, p1); |
| 4854 | |
| 4855 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p0, p2), |
| 4856 | "Value of: p2"); |
| 4857 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p1, p2), |
| 4858 | "p2"); |
| 4859 | EXPECT_FATAL_FAILURE(ASSERT_EQ(reinterpret_cast<char*>(0x1234), |
| 4860 | reinterpret_cast<char*>(0xABC0)), |
| 4861 | "ABC0"); |
| 4862 | } |
| 4863 | |
| 4864 | // Tests using wchar_t pointers in {EXPECT|ASSERT}_EQ. |
| 4865 | TEST(EqAssertionTest, WideCharPointer) { |
| 4866 | wchar_t* const p0 = NULL; |
| 4867 | // Only way to get the Nokia compiler to compile the cast |
| 4868 | // is to have a separate void* variable first. Putting |
| 4869 | // the two casts on the same line doesn't work, neither does |
| 4870 | // a direct C-style to char*. |
| 4871 | void* pv1 = (void*)0x1234; // NOLINT |
| 4872 | void* pv2 = (void*)0xABC0; // NOLINT |
| 4873 | wchar_t* const p1 = reinterpret_cast<wchar_t*>(pv1); |
| 4874 | wchar_t* const p2 = reinterpret_cast<wchar_t*>(pv2); |
| 4875 | EXPECT_EQ(p0, p0); |
| 4876 | |
| 4877 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p0, p2), |
| 4878 | "Value of: p2"); |
| 4879 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p1, p2), |
| 4880 | "p2"); |
| 4881 | void* pv3 = (void*)0x1234; // NOLINT |
| 4882 | void* pv4 = (void*)0xABC0; // NOLINT |
| 4883 | const wchar_t* p3 = reinterpret_cast<const wchar_t*>(pv3); |
| 4884 | const wchar_t* p4 = reinterpret_cast<const wchar_t*>(pv4); |
| 4885 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p3, p4), |
| 4886 | "p4"); |
| 4887 | } |
| 4888 | |
| 4889 | // Tests using other types of pointers in {EXPECT|ASSERT}_EQ. |
| 4890 | TEST(EqAssertionTest, OtherPointer) { |
| 4891 | ASSERT_EQ(static_cast<const int*>(NULL), |
| 4892 | static_cast<const int*>(NULL)); |
| 4893 | EXPECT_FATAL_FAILURE(ASSERT_EQ(static_cast<const int*>(NULL), |
| 4894 | reinterpret_cast<const int*>(0x1234)), |
| 4895 | "0x1234"); |
| 4896 | } |
| 4897 | |
| 4898 | // Tests the FRIEND_TEST macro. |
| 4899 | |
| 4900 | // This class has a private member we want to test. We will test it |
| 4901 | // both in a TEST and in a TEST_F. |
| 4902 | class Foo { |
| 4903 | public: |
| 4904 | Foo() {} |
| 4905 | |
| 4906 | private: |
| 4907 | int Bar() const { return 1; } |
| 4908 | |
| 4909 | // Declares the friend tests that can access the private member |
| 4910 | // Bar(). |
| 4911 | FRIEND_TEST(FRIEND_TEST_Test, TEST); |
| 4912 | FRIEND_TEST(FRIEND_TEST_Test2, TEST_F); |
| 4913 | }; |
| 4914 | |
| 4915 | // Tests that the FRIEND_TEST declaration allows a TEST to access a |
| 4916 | // class's private members. This should compile. |
| 4917 | TEST(FRIEND_TEST_Test, TEST) { |
| 4918 | ASSERT_EQ(1, Foo().Bar()); |
| 4919 | } |
| 4920 | |
| 4921 | // The fixture needed to test using FRIEND_TEST with TEST_F. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4922 | class FRIEND_TEST_Test2 : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4923 | protected: |
| 4924 | Foo foo; |
| 4925 | }; |
| 4926 | |
| 4927 | // Tests that the FRIEND_TEST declaration allows a TEST_F to access a |
| 4928 | // class's private members. This should compile. |
| 4929 | TEST_F(FRIEND_TEST_Test2, TEST_F) { |
| 4930 | ASSERT_EQ(1, foo.Bar()); |
| 4931 | } |
| 4932 | |
| 4933 | // Tests the life cycle of Test objects. |
| 4934 | |
| 4935 | // The test fixture for testing the life cycle of Test objects. |
| 4936 | // |
| 4937 | // This class counts the number of live test objects that uses this |
| 4938 | // fixture. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4939 | class TestLifeCycleTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4940 | protected: |
| 4941 | // Constructor. Increments the number of test objects that uses |
| 4942 | // this fixture. |
| 4943 | TestLifeCycleTest() { count_++; } |
| 4944 | |
| 4945 | // Destructor. Decrements the number of test objects that uses this |
| 4946 | // fixture. |
| 4947 | ~TestLifeCycleTest() { count_--; } |
| 4948 | |
| 4949 | // Returns the number of live test objects that uses this fixture. |
| 4950 | int count() const { return count_; } |
| 4951 | |
| 4952 | private: |
| 4953 | static int count_; |
| 4954 | }; |
| 4955 | |
| 4956 | int TestLifeCycleTest::count_ = 0; |
| 4957 | |
| 4958 | // Tests the life cycle of test objects. |
| 4959 | TEST_F(TestLifeCycleTest, Test1) { |
| 4960 | // There should be only one test object in this test case that's |
| 4961 | // currently alive. |
| 4962 | ASSERT_EQ(1, count()); |
| 4963 | } |
| 4964 | |
| 4965 | // Tests the life cycle of test objects. |
| 4966 | TEST_F(TestLifeCycleTest, Test2) { |
| 4967 | // After Test1 is done and Test2 is started, there should still be |
| 4968 | // only one live test object, as the object for Test1 should've been |
| 4969 | // deleted. |
| 4970 | ASSERT_EQ(1, count()); |
| 4971 | } |
| 4972 | |
| 4973 | } // namespace |
| 4974 | |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 4975 | // Tests that the copy constructor works when it is NOT optimized away by |
| 4976 | // the compiler. |
| 4977 | TEST(AssertionResultTest, CopyConstructorWorksWhenNotOptimied) { |
| 4978 | // Checks that the copy constructor doesn't try to dereference NULL pointers |
| 4979 | // in the source object. |
| 4980 | AssertionResult r1 = AssertionSuccess(); |
| 4981 | AssertionResult r2 = r1; |
| 4982 | // The following line is added to prevent the compiler from optimizing |
| 4983 | // away the constructor call. |
| 4984 | r1 << "abc"; |
| 4985 | |
| 4986 | AssertionResult r3 = r1; |
| 4987 | EXPECT_EQ(static_cast<bool>(r3), static_cast<bool>(r1)); |
| 4988 | EXPECT_STREQ("abc", r1.message()); |
| 4989 | } |
| 4990 | |
| 4991 | // Tests that AssertionSuccess and AssertionFailure construct |
| 4992 | // AssertionResult objects as expected. |
| 4993 | TEST(AssertionResultTest, ConstructionWorks) { |
| 4994 | AssertionResult r1 = AssertionSuccess(); |
| 4995 | EXPECT_TRUE(r1); |
| 4996 | EXPECT_STREQ("", r1.message()); |
| 4997 | |
| 4998 | AssertionResult r2 = AssertionSuccess() << "abc"; |
| 4999 | EXPECT_TRUE(r2); |
| 5000 | EXPECT_STREQ("abc", r2.message()); |
| 5001 | |
| 5002 | AssertionResult r3 = AssertionFailure(); |
| 5003 | EXPECT_FALSE(r3); |
| 5004 | EXPECT_STREQ("", r3.message()); |
| 5005 | |
| 5006 | AssertionResult r4 = AssertionFailure() << "def"; |
| 5007 | EXPECT_FALSE(r4); |
| 5008 | EXPECT_STREQ("def", r4.message()); |
| 5009 | |
| 5010 | AssertionResult r5 = AssertionFailure(Message() << "ghi"); |
| 5011 | EXPECT_FALSE(r5); |
| 5012 | EXPECT_STREQ("ghi", r5.message()); |
| 5013 | } |
| 5014 | |
| 5015 | // Tests that the negation fips the predicate result but keeps the message. |
| 5016 | TEST(AssertionResultTest, NegationWorks) { |
| 5017 | AssertionResult r1 = AssertionSuccess() << "abc"; |
| 5018 | EXPECT_FALSE(!r1); |
| 5019 | EXPECT_STREQ("abc", (!r1).message()); |
| 5020 | |
| 5021 | AssertionResult r2 = AssertionFailure() << "def"; |
| 5022 | EXPECT_TRUE(!r2); |
| 5023 | EXPECT_STREQ("def", (!r2).message()); |
| 5024 | } |
| 5025 | |
| 5026 | TEST(AssertionResultTest, StreamingWorks) { |
| 5027 | AssertionResult r = AssertionSuccess(); |
| 5028 | r << "abc" << 'd' << 0 << true; |
| 5029 | EXPECT_STREQ("abcd0true", r.message()); |
| 5030 | } |
| 5031 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5032 | // Tests streaming a user type whose definition and operator << are |
| 5033 | // both in the global namespace. |
| 5034 | class Base { |
| 5035 | public: |
zhanyong.wan | 7de3401 | 2009-12-16 19:54:05 +0000 | [diff] [blame] | 5036 | explicit Base(int an_x) : x_(an_x) {} |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5037 | int x() const { return x_; } |
| 5038 | private: |
| 5039 | int x_; |
| 5040 | }; |
| 5041 | std::ostream& operator<<(std::ostream& os, |
| 5042 | const Base& val) { |
| 5043 | return os << val.x(); |
| 5044 | } |
| 5045 | std::ostream& operator<<(std::ostream& os, |
| 5046 | const Base* pointer) { |
| 5047 | return os << "(" << pointer->x() << ")"; |
| 5048 | } |
| 5049 | |
| 5050 | TEST(MessageTest, CanStreamUserTypeInGlobalNameSpace) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5051 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5052 | Base a(1); |
| 5053 | |
| 5054 | msg << a << &a; // Uses ::operator<<. |
| 5055 | EXPECT_STREQ("1(1)", msg.GetString().c_str()); |
| 5056 | } |
| 5057 | |
| 5058 | // Tests streaming a user type whose definition and operator<< are |
| 5059 | // both in an unnamed namespace. |
| 5060 | namespace { |
| 5061 | class MyTypeInUnnamedNameSpace : public Base { |
| 5062 | public: |
zhanyong.wan | 7de3401 | 2009-12-16 19:54:05 +0000 | [diff] [blame] | 5063 | explicit MyTypeInUnnamedNameSpace(int an_x): Base(an_x) {} |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5064 | }; |
| 5065 | std::ostream& operator<<(std::ostream& os, |
| 5066 | const MyTypeInUnnamedNameSpace& val) { |
| 5067 | return os << val.x(); |
| 5068 | } |
| 5069 | std::ostream& operator<<(std::ostream& os, |
| 5070 | const MyTypeInUnnamedNameSpace* pointer) { |
| 5071 | return os << "(" << pointer->x() << ")"; |
| 5072 | } |
| 5073 | } // namespace |
| 5074 | |
| 5075 | TEST(MessageTest, CanStreamUserTypeInUnnamedNameSpace) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5076 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5077 | MyTypeInUnnamedNameSpace a(1); |
| 5078 | |
| 5079 | msg << a << &a; // Uses <unnamed_namespace>::operator<<. |
| 5080 | EXPECT_STREQ("1(1)", msg.GetString().c_str()); |
| 5081 | } |
| 5082 | |
| 5083 | // Tests streaming a user type whose definition and operator<< are |
| 5084 | // both in a user namespace. |
| 5085 | namespace namespace1 { |
| 5086 | class MyTypeInNameSpace1 : public Base { |
| 5087 | public: |
zhanyong.wan | 7de3401 | 2009-12-16 19:54:05 +0000 | [diff] [blame] | 5088 | explicit MyTypeInNameSpace1(int an_x): Base(an_x) {} |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5089 | }; |
| 5090 | std::ostream& operator<<(std::ostream& os, |
| 5091 | const MyTypeInNameSpace1& val) { |
| 5092 | return os << val.x(); |
| 5093 | } |
| 5094 | std::ostream& operator<<(std::ostream& os, |
| 5095 | const MyTypeInNameSpace1* pointer) { |
| 5096 | return os << "(" << pointer->x() << ")"; |
| 5097 | } |
| 5098 | } // namespace namespace1 |
| 5099 | |
| 5100 | TEST(MessageTest, CanStreamUserTypeInUserNameSpace) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5101 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5102 | namespace1::MyTypeInNameSpace1 a(1); |
| 5103 | |
| 5104 | msg << a << &a; // Uses namespace1::operator<<. |
| 5105 | EXPECT_STREQ("1(1)", msg.GetString().c_str()); |
| 5106 | } |
| 5107 | |
| 5108 | // Tests streaming a user type whose definition is in a user namespace |
| 5109 | // but whose operator<< is in the global namespace. |
| 5110 | namespace namespace2 { |
| 5111 | class MyTypeInNameSpace2 : public ::Base { |
| 5112 | public: |
zhanyong.wan | 7de3401 | 2009-12-16 19:54:05 +0000 | [diff] [blame] | 5113 | explicit MyTypeInNameSpace2(int an_x): Base(an_x) {} |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5114 | }; |
| 5115 | } // namespace namespace2 |
| 5116 | std::ostream& operator<<(std::ostream& os, |
| 5117 | const namespace2::MyTypeInNameSpace2& val) { |
| 5118 | return os << val.x(); |
| 5119 | } |
| 5120 | std::ostream& operator<<(std::ostream& os, |
| 5121 | const namespace2::MyTypeInNameSpace2* pointer) { |
| 5122 | return os << "(" << pointer->x() << ")"; |
| 5123 | } |
| 5124 | |
| 5125 | TEST(MessageTest, CanStreamUserTypeInUserNameSpaceWithStreamOperatorInGlobal) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5126 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5127 | namespace2::MyTypeInNameSpace2 a(1); |
| 5128 | |
| 5129 | msg << a << &a; // Uses ::operator<<. |
| 5130 | EXPECT_STREQ("1(1)", msg.GetString().c_str()); |
| 5131 | } |
| 5132 | |
| 5133 | // Tests streaming NULL pointers to testing::Message. |
| 5134 | TEST(MessageTest, NullPointers) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5135 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5136 | char* const p1 = NULL; |
| 5137 | unsigned char* const p2 = NULL; |
| 5138 | int* p3 = NULL; |
| 5139 | double* p4 = NULL; |
| 5140 | bool* p5 = NULL; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5141 | Message* p6 = NULL; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5142 | |
| 5143 | msg << p1 << p2 << p3 << p4 << p5 << p6; |
| 5144 | ASSERT_STREQ("(null)(null)(null)(null)(null)(null)", |
| 5145 | msg.GetString().c_str()); |
| 5146 | } |
| 5147 | |
| 5148 | // Tests streaming wide strings to testing::Message. |
| 5149 | TEST(MessageTest, WideStrings) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5150 | // Streams a NULL of type const wchar_t*. |
| 5151 | const wchar_t* const_wstr = NULL; |
| 5152 | EXPECT_STREQ("(null)", |
| 5153 | (Message() << const_wstr).GetString().c_str()); |
| 5154 | |
| 5155 | // Streams a NULL of type wchar_t*. |
| 5156 | wchar_t* wstr = NULL; |
| 5157 | EXPECT_STREQ("(null)", |
| 5158 | (Message() << wstr).GetString().c_str()); |
| 5159 | |
| 5160 | // Streams a non-NULL of type const wchar_t*. |
| 5161 | const_wstr = L"abc\x8119"; |
| 5162 | EXPECT_STREQ("abc\xe8\x84\x99", |
| 5163 | (Message() << const_wstr).GetString().c_str()); |
| 5164 | |
| 5165 | // Streams a non-NULL of type wchar_t*. |
| 5166 | wstr = const_cast<wchar_t*>(const_wstr); |
| 5167 | EXPECT_STREQ("abc\xe8\x84\x99", |
| 5168 | (Message() << wstr).GetString().c_str()); |
| 5169 | } |
| 5170 | |
| 5171 | |
| 5172 | // This line tests that we can define tests in the testing namespace. |
| 5173 | namespace testing { |
| 5174 | |
| 5175 | // Tests the TestInfo class. |
| 5176 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5177 | class TestInfoTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5178 | protected: |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 5179 | static const TestInfo* GetTestInfo(const char* test_name) { |
| 5180 | const TestCase* const test_case = GetUnitTestImpl()-> |
| 5181 | GetTestCase("TestInfoTest", "", NULL, NULL); |
| 5182 | |
| 5183 | for (int i = 0; i < test_case->total_test_count(); ++i) { |
| 5184 | const TestInfo* const test_info = test_case->GetTestInfo(i); |
| 5185 | if (strcmp(test_name, test_info->name()) == 0) |
| 5186 | return test_info; |
| 5187 | } |
| 5188 | return NULL; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
| 5191 | static const TestResult* GetTestResult( |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5192 | const TestInfo* test_info) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5193 | return test_info->result(); |
| 5194 | } |
| 5195 | }; |
| 5196 | |
| 5197 | // Tests TestInfo::test_case_name() and TestInfo::name(). |
| 5198 | TEST_F(TestInfoTest, Names) { |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 5199 | const TestInfo* const test_info = GetTestInfo("Names"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5200 | |
| 5201 | ASSERT_STREQ("TestInfoTest", test_info->test_case_name()); |
| 5202 | ASSERT_STREQ("Names", test_info->name()); |
| 5203 | } |
| 5204 | |
| 5205 | // Tests TestInfo::result(). |
| 5206 | TEST_F(TestInfoTest, result) { |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 5207 | const TestInfo* const test_info = GetTestInfo("result"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5208 | |
| 5209 | // Initially, there is no TestPartResult for this test. |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 5210 | ASSERT_EQ(0, GetTestResult(test_info)->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5211 | |
| 5212 | // After the previous assertion, there is still none. |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 5213 | ASSERT_EQ(0, GetTestResult(test_info)->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | // Tests setting up and tearing down a test case. |
| 5217 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5218 | class SetUpTestCaseTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5219 | protected: |
| 5220 | // This will be called once before the first test in this test case |
| 5221 | // is run. |
| 5222 | static void SetUpTestCase() { |
| 5223 | printf("Setting up the test case . . .\n"); |
| 5224 | |
| 5225 | // Initializes some shared resource. In this simple example, we |
| 5226 | // just create a C string. More complex stuff can be done if |
| 5227 | // desired. |
| 5228 | shared_resource_ = "123"; |
| 5229 | |
| 5230 | // Increments the number of test cases that have been set up. |
| 5231 | counter_++; |
| 5232 | |
| 5233 | // SetUpTestCase() should be called only once. |
| 5234 | EXPECT_EQ(1, counter_); |
| 5235 | } |
| 5236 | |
| 5237 | // This will be called once after the last test in this test case is |
| 5238 | // run. |
| 5239 | static void TearDownTestCase() { |
| 5240 | printf("Tearing down the test case . . .\n"); |
| 5241 | |
| 5242 | // Decrements the number of test cases that have been set up. |
| 5243 | counter_--; |
| 5244 | |
| 5245 | // TearDownTestCase() should be called only once. |
| 5246 | EXPECT_EQ(0, counter_); |
| 5247 | |
| 5248 | // Cleans up the shared resource. |
| 5249 | shared_resource_ = NULL; |
| 5250 | } |
| 5251 | |
| 5252 | // This will be called before each test in this test case. |
| 5253 | virtual void SetUp() { |
| 5254 | // SetUpTestCase() should be called only once, so counter_ should |
| 5255 | // always be 1. |
| 5256 | EXPECT_EQ(1, counter_); |
| 5257 | } |
| 5258 | |
| 5259 | // Number of test cases that have been set up. |
| 5260 | static int counter_; |
| 5261 | |
| 5262 | // Some resource to be shared by all tests in this test case. |
| 5263 | static const char* shared_resource_; |
| 5264 | }; |
| 5265 | |
| 5266 | int SetUpTestCaseTest::counter_ = 0; |
| 5267 | const char* SetUpTestCaseTest::shared_resource_ = NULL; |
| 5268 | |
| 5269 | // A test that uses the shared resource. |
| 5270 | TEST_F(SetUpTestCaseTest, Test1) { |
| 5271 | EXPECT_STRNE(NULL, shared_resource_); |
| 5272 | } |
| 5273 | |
| 5274 | // Another test that uses the shared resource. |
| 5275 | TEST_F(SetUpTestCaseTest, Test2) { |
| 5276 | EXPECT_STREQ("123", shared_resource_); |
| 5277 | } |
| 5278 | |
| 5279 | // The InitGoogleTestTest test case tests testing::InitGoogleTest(). |
| 5280 | |
| 5281 | // The Flags struct stores a copy of all Google Test flags. |
| 5282 | struct Flags { |
| 5283 | // Constructs a Flags struct where each flag has its default value. |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5284 | Flags() : also_run_disabled_tests(false), |
| 5285 | break_on_failure(false), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5286 | catch_exceptions(false), |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5287 | death_test_use_fork(false), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5288 | filter(""), |
| 5289 | list_tests(false), |
| 5290 | output(""), |
zhanyong.wan | 73ad5a3 | 2009-04-14 23:19:22 +0000 | [diff] [blame] | 5291 | print_time(true), |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5292 | random_seed(0), |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5293 | repeat(1), |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5294 | shuffle(false), |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5295 | stack_trace_depth(kMaxStackTraceDepth), |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5296 | throw_on_failure(false) {} |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5297 | |
| 5298 | // Factory methods. |
| 5299 | |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5300 | // Creates a Flags struct where the gtest_also_run_disabled_tests flag has |
| 5301 | // the given value. |
| 5302 | static Flags AlsoRunDisabledTests(bool also_run_disabled_tests) { |
| 5303 | Flags flags; |
| 5304 | flags.also_run_disabled_tests = also_run_disabled_tests; |
| 5305 | return flags; |
| 5306 | } |
| 5307 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5308 | // Creates a Flags struct where the gtest_break_on_failure flag has |
| 5309 | // the given value. |
| 5310 | static Flags BreakOnFailure(bool break_on_failure) { |
| 5311 | Flags flags; |
| 5312 | flags.break_on_failure = break_on_failure; |
| 5313 | return flags; |
| 5314 | } |
| 5315 | |
| 5316 | // Creates a Flags struct where the gtest_catch_exceptions flag has |
| 5317 | // the given value. |
| 5318 | static Flags CatchExceptions(bool catch_exceptions) { |
| 5319 | Flags flags; |
| 5320 | flags.catch_exceptions = catch_exceptions; |
| 5321 | return flags; |
| 5322 | } |
| 5323 | |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5324 | // Creates a Flags struct where the gtest_death_test_use_fork flag has |
| 5325 | // the given value. |
| 5326 | static Flags DeathTestUseFork(bool death_test_use_fork) { |
| 5327 | Flags flags; |
| 5328 | flags.death_test_use_fork = death_test_use_fork; |
| 5329 | return flags; |
| 5330 | } |
| 5331 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5332 | // Creates a Flags struct where the gtest_filter flag has the given |
| 5333 | // value. |
| 5334 | static Flags Filter(const char* filter) { |
| 5335 | Flags flags; |
| 5336 | flags.filter = filter; |
| 5337 | return flags; |
| 5338 | } |
| 5339 | |
| 5340 | // Creates a Flags struct where the gtest_list_tests flag has the |
| 5341 | // given value. |
| 5342 | static Flags ListTests(bool list_tests) { |
| 5343 | Flags flags; |
| 5344 | flags.list_tests = list_tests; |
| 5345 | return flags; |
| 5346 | } |
| 5347 | |
| 5348 | // Creates a Flags struct where the gtest_output flag has the given |
| 5349 | // value. |
| 5350 | static Flags Output(const char* output) { |
| 5351 | Flags flags; |
| 5352 | flags.output = output; |
| 5353 | return flags; |
| 5354 | } |
| 5355 | |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5356 | // Creates a Flags struct where the gtest_print_time flag has the given |
| 5357 | // value. |
| 5358 | static Flags PrintTime(bool print_time) { |
| 5359 | Flags flags; |
| 5360 | flags.print_time = print_time; |
| 5361 | return flags; |
| 5362 | } |
| 5363 | |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5364 | // Creates a Flags struct where the gtest_random_seed flag has |
| 5365 | // the given value. |
| 5366 | static Flags RandomSeed(Int32 random_seed) { |
| 5367 | Flags flags; |
| 5368 | flags.random_seed = random_seed; |
| 5369 | return flags; |
| 5370 | } |
| 5371 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5372 | // Creates a Flags struct where the gtest_repeat flag has the given |
| 5373 | // value. |
| 5374 | static Flags Repeat(Int32 repeat) { |
| 5375 | Flags flags; |
| 5376 | flags.repeat = repeat; |
| 5377 | return flags; |
| 5378 | } |
| 5379 | |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5380 | // Creates a Flags struct where the gtest_shuffle flag has |
| 5381 | // the given value. |
| 5382 | static Flags Shuffle(bool shuffle) { |
| 5383 | Flags flags; |
| 5384 | flags.shuffle = shuffle; |
| 5385 | return flags; |
| 5386 | } |
| 5387 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5388 | // Creates a Flags struct where the GTEST_FLAG(stack_trace_depth) flag has |
| 5389 | // the given value. |
| 5390 | static Flags StackTraceDepth(Int32 stack_trace_depth) { |
| 5391 | Flags flags; |
| 5392 | flags.stack_trace_depth = stack_trace_depth; |
| 5393 | return flags; |
| 5394 | } |
| 5395 | |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5396 | // Creates a Flags struct where the gtest_throw_on_failure flag has |
| 5397 | // the given value. |
| 5398 | static Flags ThrowOnFailure(bool throw_on_failure) { |
| 5399 | Flags flags; |
| 5400 | flags.throw_on_failure = throw_on_failure; |
| 5401 | return flags; |
| 5402 | } |
| 5403 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5404 | // These fields store the flag values. |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5405 | bool also_run_disabled_tests; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5406 | bool break_on_failure; |
| 5407 | bool catch_exceptions; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5408 | bool death_test_use_fork; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5409 | const char* filter; |
| 5410 | bool list_tests; |
| 5411 | const char* output; |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5412 | bool print_time; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5413 | Int32 random_seed; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5414 | Int32 repeat; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5415 | bool shuffle; |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5416 | Int32 stack_trace_depth; |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5417 | bool throw_on_failure; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5418 | }; |
| 5419 | |
| 5420 | // Fixture for testing InitGoogleTest(). |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5421 | class InitGoogleTestTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5422 | protected: |
| 5423 | // Clears the flags before each test. |
| 5424 | virtual void SetUp() { |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5425 | GTEST_FLAG(also_run_disabled_tests) = false; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5426 | GTEST_FLAG(break_on_failure) = false; |
| 5427 | GTEST_FLAG(catch_exceptions) = false; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5428 | GTEST_FLAG(death_test_use_fork) = false; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5429 | GTEST_FLAG(filter) = ""; |
| 5430 | GTEST_FLAG(list_tests) = false; |
| 5431 | GTEST_FLAG(output) = ""; |
zhanyong.wan | 73ad5a3 | 2009-04-14 23:19:22 +0000 | [diff] [blame] | 5432 | GTEST_FLAG(print_time) = true; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5433 | GTEST_FLAG(random_seed) = 0; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5434 | GTEST_FLAG(repeat) = 1; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5435 | GTEST_FLAG(shuffle) = false; |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5436 | GTEST_FLAG(stack_trace_depth) = kMaxStackTraceDepth; |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5437 | GTEST_FLAG(throw_on_failure) = false; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5438 | } |
| 5439 | |
| 5440 | // Asserts that two narrow or wide string arrays are equal. |
| 5441 | template <typename CharType> |
| 5442 | static void AssertStringArrayEq(size_t size1, CharType** array1, |
| 5443 | size_t size2, CharType** array2) { |
| 5444 | ASSERT_EQ(size1, size2) << " Array sizes different."; |
| 5445 | |
| 5446 | for (size_t i = 0; i != size1; i++) { |
| 5447 | ASSERT_STREQ(array1[i], array2[i]) << " where i == " << i; |
| 5448 | } |
| 5449 | } |
| 5450 | |
| 5451 | // Verifies that the flag values match the expected values. |
| 5452 | static void CheckFlags(const Flags& expected) { |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5453 | EXPECT_EQ(expected.also_run_disabled_tests, |
| 5454 | GTEST_FLAG(also_run_disabled_tests)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5455 | EXPECT_EQ(expected.break_on_failure, GTEST_FLAG(break_on_failure)); |
| 5456 | EXPECT_EQ(expected.catch_exceptions, GTEST_FLAG(catch_exceptions)); |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5457 | EXPECT_EQ(expected.death_test_use_fork, GTEST_FLAG(death_test_use_fork)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5458 | EXPECT_STREQ(expected.filter, GTEST_FLAG(filter).c_str()); |
| 5459 | EXPECT_EQ(expected.list_tests, GTEST_FLAG(list_tests)); |
| 5460 | EXPECT_STREQ(expected.output, GTEST_FLAG(output).c_str()); |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5461 | EXPECT_EQ(expected.print_time, GTEST_FLAG(print_time)); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5462 | EXPECT_EQ(expected.random_seed, GTEST_FLAG(random_seed)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5463 | EXPECT_EQ(expected.repeat, GTEST_FLAG(repeat)); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5464 | EXPECT_EQ(expected.shuffle, GTEST_FLAG(shuffle)); |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5465 | EXPECT_EQ(expected.throw_on_failure, GTEST_FLAG(throw_on_failure)); |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5466 | EXPECT_EQ(expected.stack_trace_depth, GTEST_FLAG(stack_trace_depth)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5467 | } |
| 5468 | |
| 5469 | // Parses a command line (specified by argc1 and argv1), then |
| 5470 | // verifies that the flag values are expected and that the |
| 5471 | // recognized flags are removed from the command line. |
| 5472 | template <typename CharType> |
| 5473 | static void TestParsingFlags(int argc1, const CharType** argv1, |
| 5474 | int argc2, const CharType** argv2, |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5475 | const Flags& expected, bool should_print_help) { |
| 5476 | const bool saved_help_flag = ::testing::internal::g_help_flag; |
| 5477 | ::testing::internal::g_help_flag = false; |
| 5478 | |
zhanyong.wan | cf8a584 | 2010-01-28 21:50:29 +0000 | [diff] [blame^] | 5479 | #if GTEST_HAS_STREAM_REDIRECTION_ |
| 5480 | CaptureStdout(); |
| 5481 | #endif // GTEST_HAS_STREAM_REDIRECTION_ |
| 5482 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5483 | // Parses the command line. |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 5484 | internal::ParseGoogleTestFlagsOnly(&argc1, const_cast<CharType**>(argv1)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5485 | |
zhanyong.wan | cf8a584 | 2010-01-28 21:50:29 +0000 | [diff] [blame^] | 5486 | #if GTEST_HAS_STREAM_REDIRECTION_ |
| 5487 | const String captured_stdout = GetCapturedStdout(); |
| 5488 | #endif // GTEST_HAS_STREAM_REDIRECTION_ |
| 5489 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5490 | // Verifies the flag values. |
| 5491 | CheckFlags(expected); |
| 5492 | |
| 5493 | // Verifies that the recognized flags are removed from the command |
| 5494 | // line. |
| 5495 | AssertStringArrayEq(argc1 + 1, argv1, argc2 + 1, argv2); |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5496 | |
| 5497 | // ParseGoogleTestFlagsOnly should neither set g_help_flag nor print the |
| 5498 | // help message for the flags it recognizes. |
| 5499 | EXPECT_EQ(should_print_help, ::testing::internal::g_help_flag); |
| 5500 | |
zhanyong.wan | cf8a584 | 2010-01-28 21:50:29 +0000 | [diff] [blame^] | 5501 | #if GTEST_HAS_STREAM_REDIRECTION_ |
| 5502 | const char* const expected_help_fragment = |
| 5503 | "This program contains tests written using"; |
| 5504 | if (should_print_help) { |
| 5505 | EXPECT_PRED_FORMAT2(IsSubstring, expected_help_fragment, captured_stdout); |
| 5506 | } else { |
| 5507 | EXPECT_PRED_FORMAT2(IsNotSubstring, |
| 5508 | expected_help_fragment, captured_stdout); |
| 5509 | } |
| 5510 | #endif // GTEST_HAS_STREAM_REDIRECTION_ |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5511 | |
| 5512 | ::testing::internal::g_help_flag = saved_help_flag; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5513 | } |
| 5514 | |
| 5515 | // This macro wraps TestParsingFlags s.t. the user doesn't need |
| 5516 | // to specify the array sizes. |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5517 | #define GTEST_TEST_PARSING_FLAGS_(argv1, argv2, expected, should_print_help) \ |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5518 | TestParsingFlags(sizeof(argv1)/sizeof(*argv1) - 1, argv1, \ |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5519 | sizeof(argv2)/sizeof(*argv2) - 1, argv2, \ |
| 5520 | expected, should_print_help) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5521 | }; |
| 5522 | |
| 5523 | // Tests parsing an empty command line. |
| 5524 | TEST_F(InitGoogleTestTest, Empty) { |
| 5525 | const char* argv[] = { |
| 5526 | NULL |
| 5527 | }; |
| 5528 | |
| 5529 | const char* argv2[] = { |
| 5530 | NULL |
| 5531 | }; |
| 5532 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5533 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
| 5536 | // Tests parsing a command line that has no flag. |
| 5537 | TEST_F(InitGoogleTestTest, NoFlag) { |
| 5538 | const char* argv[] = { |
| 5539 | "foo.exe", |
| 5540 | NULL |
| 5541 | }; |
| 5542 | |
| 5543 | const char* argv2[] = { |
| 5544 | "foo.exe", |
| 5545 | NULL |
| 5546 | }; |
| 5547 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5548 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | // Tests parsing a bad --gtest_filter flag. |
| 5552 | TEST_F(InitGoogleTestTest, FilterBad) { |
| 5553 | const char* argv[] = { |
| 5554 | "foo.exe", |
| 5555 | "--gtest_filter", |
| 5556 | NULL |
| 5557 | }; |
| 5558 | |
| 5559 | const char* argv2[] = { |
| 5560 | "foo.exe", |
| 5561 | "--gtest_filter", |
| 5562 | NULL |
| 5563 | }; |
| 5564 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5565 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter(""), true); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5566 | } |
| 5567 | |
| 5568 | // Tests parsing an empty --gtest_filter flag. |
| 5569 | TEST_F(InitGoogleTestTest, FilterEmpty) { |
| 5570 | const char* argv[] = { |
| 5571 | "foo.exe", |
| 5572 | "--gtest_filter=", |
| 5573 | NULL |
| 5574 | }; |
| 5575 | |
| 5576 | const char* argv2[] = { |
| 5577 | "foo.exe", |
| 5578 | NULL |
| 5579 | }; |
| 5580 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5581 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter(""), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | // Tests parsing a non-empty --gtest_filter flag. |
| 5585 | TEST_F(InitGoogleTestTest, FilterNonEmpty) { |
| 5586 | const char* argv[] = { |
| 5587 | "foo.exe", |
| 5588 | "--gtest_filter=abc", |
| 5589 | NULL |
| 5590 | }; |
| 5591 | |
| 5592 | const char* argv2[] = { |
| 5593 | "foo.exe", |
| 5594 | NULL |
| 5595 | }; |
| 5596 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5597 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter("abc"), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5598 | } |
| 5599 | |
| 5600 | // Tests parsing --gtest_break_on_failure. |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5601 | TEST_F(InitGoogleTestTest, BreakOnFailureWithoutValue) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5602 | const char* argv[] = { |
| 5603 | "foo.exe", |
| 5604 | "--gtest_break_on_failure", |
| 5605 | NULL |
| 5606 | }; |
| 5607 | |
| 5608 | const char* argv2[] = { |
| 5609 | "foo.exe", |
| 5610 | NULL |
| 5611 | }; |
| 5612 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5613 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(true), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5614 | } |
| 5615 | |
| 5616 | // Tests parsing --gtest_break_on_failure=0. |
| 5617 | TEST_F(InitGoogleTestTest, BreakOnFailureFalse_0) { |
| 5618 | const char* argv[] = { |
| 5619 | "foo.exe", |
| 5620 | "--gtest_break_on_failure=0", |
| 5621 | NULL |
| 5622 | }; |
| 5623 | |
| 5624 | const char* argv2[] = { |
| 5625 | "foo.exe", |
| 5626 | NULL |
| 5627 | }; |
| 5628 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5629 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(false), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5630 | } |
| 5631 | |
| 5632 | // Tests parsing --gtest_break_on_failure=f. |
| 5633 | TEST_F(InitGoogleTestTest, BreakOnFailureFalse_f) { |
| 5634 | const char* argv[] = { |
| 5635 | "foo.exe", |
| 5636 | "--gtest_break_on_failure=f", |
| 5637 | NULL |
| 5638 | }; |
| 5639 | |
| 5640 | const char* argv2[] = { |
| 5641 | "foo.exe", |
| 5642 | NULL |
| 5643 | }; |
| 5644 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5645 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(false), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | // Tests parsing --gtest_break_on_failure=F. |
| 5649 | TEST_F(InitGoogleTestTest, BreakOnFailureFalse_F) { |
| 5650 | const char* argv[] = { |
| 5651 | "foo.exe", |
| 5652 | "--gtest_break_on_failure=F", |
| 5653 | NULL |
| 5654 | }; |
| 5655 | |
| 5656 | const char* argv2[] = { |
| 5657 | "foo.exe", |
| 5658 | NULL |
| 5659 | }; |
| 5660 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5661 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(false), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5662 | } |
| 5663 | |
| 5664 | // Tests parsing a --gtest_break_on_failure flag that has a "true" |
| 5665 | // definition. |
| 5666 | TEST_F(InitGoogleTestTest, BreakOnFailureTrue) { |
| 5667 | const char* argv[] = { |
| 5668 | "foo.exe", |
| 5669 | "--gtest_break_on_failure=1", |
| 5670 | NULL |
| 5671 | }; |
| 5672 | |
| 5673 | const char* argv2[] = { |
| 5674 | "foo.exe", |
| 5675 | NULL |
| 5676 | }; |
| 5677 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5678 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(true), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5679 | } |
| 5680 | |
| 5681 | // Tests parsing --gtest_catch_exceptions. |
| 5682 | TEST_F(InitGoogleTestTest, CatchExceptions) { |
| 5683 | const char* argv[] = { |
| 5684 | "foo.exe", |
| 5685 | "--gtest_catch_exceptions", |
| 5686 | NULL |
| 5687 | }; |
| 5688 | |
| 5689 | const char* argv2[] = { |
| 5690 | "foo.exe", |
| 5691 | NULL |
| 5692 | }; |
| 5693 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5694 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::CatchExceptions(true), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5695 | } |
| 5696 | |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5697 | // Tests parsing --gtest_death_test_use_fork. |
| 5698 | TEST_F(InitGoogleTestTest, DeathTestUseFork) { |
| 5699 | const char* argv[] = { |
| 5700 | "foo.exe", |
| 5701 | "--gtest_death_test_use_fork", |
| 5702 | NULL |
| 5703 | }; |
| 5704 | |
| 5705 | const char* argv2[] = { |
| 5706 | "foo.exe", |
| 5707 | NULL |
| 5708 | }; |
| 5709 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5710 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::DeathTestUseFork(true), false); |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5713 | // Tests having the same flag twice with different values. The |
| 5714 | // expected behavior is that the one coming last takes precedence. |
| 5715 | TEST_F(InitGoogleTestTest, DuplicatedFlags) { |
| 5716 | const char* argv[] = { |
| 5717 | "foo.exe", |
| 5718 | "--gtest_filter=a", |
| 5719 | "--gtest_filter=b", |
| 5720 | NULL |
| 5721 | }; |
| 5722 | |
| 5723 | const char* argv2[] = { |
| 5724 | "foo.exe", |
| 5725 | NULL |
| 5726 | }; |
| 5727 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5728 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter("b"), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5729 | } |
| 5730 | |
| 5731 | // Tests having an unrecognized flag on the command line. |
| 5732 | TEST_F(InitGoogleTestTest, UnrecognizedFlag) { |
| 5733 | const char* argv[] = { |
| 5734 | "foo.exe", |
| 5735 | "--gtest_break_on_failure", |
| 5736 | "bar", // Unrecognized by Google Test. |
| 5737 | "--gtest_filter=b", |
| 5738 | NULL |
| 5739 | }; |
| 5740 | |
| 5741 | const char* argv2[] = { |
| 5742 | "foo.exe", |
| 5743 | "bar", |
| 5744 | NULL |
| 5745 | }; |
| 5746 | |
| 5747 | Flags flags; |
| 5748 | flags.break_on_failure = true; |
| 5749 | flags.filter = "b"; |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5750 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, flags, false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5751 | } |
| 5752 | |
| 5753 | // Tests having a --gtest_list_tests flag |
| 5754 | TEST_F(InitGoogleTestTest, ListTestsFlag) { |
| 5755 | const char* argv[] = { |
| 5756 | "foo.exe", |
| 5757 | "--gtest_list_tests", |
| 5758 | NULL |
| 5759 | }; |
| 5760 | |
| 5761 | const char* argv2[] = { |
| 5762 | "foo.exe", |
| 5763 | NULL |
| 5764 | }; |
| 5765 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5766 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(true), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5767 | } |
| 5768 | |
| 5769 | // Tests having a --gtest_list_tests flag with a "true" value |
| 5770 | TEST_F(InitGoogleTestTest, ListTestsTrue) { |
| 5771 | const char* argv[] = { |
| 5772 | "foo.exe", |
| 5773 | "--gtest_list_tests=1", |
| 5774 | NULL |
| 5775 | }; |
| 5776 | |
| 5777 | const char* argv2[] = { |
| 5778 | "foo.exe", |
| 5779 | NULL |
| 5780 | }; |
| 5781 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5782 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(true), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
| 5785 | // Tests having a --gtest_list_tests flag with a "false" value |
| 5786 | TEST_F(InitGoogleTestTest, ListTestsFalse) { |
| 5787 | const char* argv[] = { |
| 5788 | "foo.exe", |
| 5789 | "--gtest_list_tests=0", |
| 5790 | NULL |
| 5791 | }; |
| 5792 | |
| 5793 | const char* argv2[] = { |
| 5794 | "foo.exe", |
| 5795 | NULL |
| 5796 | }; |
| 5797 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5798 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(false), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5799 | } |
| 5800 | |
| 5801 | // Tests parsing --gtest_list_tests=f. |
| 5802 | TEST_F(InitGoogleTestTest, ListTestsFalse_f) { |
| 5803 | const char* argv[] = { |
| 5804 | "foo.exe", |
| 5805 | "--gtest_list_tests=f", |
| 5806 | NULL |
| 5807 | }; |
| 5808 | |
| 5809 | const char* argv2[] = { |
| 5810 | "foo.exe", |
| 5811 | NULL |
| 5812 | }; |
| 5813 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5814 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(false), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5815 | } |
| 5816 | |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5817 | // Tests parsing --gtest_list_tests=F. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5818 | TEST_F(InitGoogleTestTest, ListTestsFalse_F) { |
| 5819 | const char* argv[] = { |
| 5820 | "foo.exe", |
| 5821 | "--gtest_list_tests=F", |
| 5822 | NULL |
| 5823 | }; |
| 5824 | |
| 5825 | const char* argv2[] = { |
| 5826 | "foo.exe", |
| 5827 | NULL |
| 5828 | }; |
| 5829 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5830 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(false), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5831 | } |
| 5832 | |
| 5833 | // Tests parsing --gtest_output (invalid). |
| 5834 | TEST_F(InitGoogleTestTest, OutputEmpty) { |
| 5835 | const char* argv[] = { |
| 5836 | "foo.exe", |
| 5837 | "--gtest_output", |
| 5838 | NULL |
| 5839 | }; |
| 5840 | |
| 5841 | const char* argv2[] = { |
| 5842 | "foo.exe", |
| 5843 | "--gtest_output", |
| 5844 | NULL |
| 5845 | }; |
| 5846 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5847 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), true); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5848 | } |
| 5849 | |
| 5850 | // Tests parsing --gtest_output=xml |
| 5851 | TEST_F(InitGoogleTestTest, OutputXml) { |
| 5852 | const char* argv[] = { |
| 5853 | "foo.exe", |
| 5854 | "--gtest_output=xml", |
| 5855 | NULL |
| 5856 | }; |
| 5857 | |
| 5858 | const char* argv2[] = { |
| 5859 | "foo.exe", |
| 5860 | NULL |
| 5861 | }; |
| 5862 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5863 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Output("xml"), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5864 | } |
| 5865 | |
| 5866 | // Tests parsing --gtest_output=xml:file |
| 5867 | TEST_F(InitGoogleTestTest, OutputXmlFile) { |
| 5868 | const char* argv[] = { |
| 5869 | "foo.exe", |
| 5870 | "--gtest_output=xml:file", |
| 5871 | NULL |
| 5872 | }; |
| 5873 | |
| 5874 | const char* argv2[] = { |
| 5875 | "foo.exe", |
| 5876 | NULL |
| 5877 | }; |
| 5878 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5879 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Output("xml:file"), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | // Tests parsing --gtest_output=xml:directory/path/ |
| 5883 | TEST_F(InitGoogleTestTest, OutputXmlDirectory) { |
| 5884 | const char* argv[] = { |
| 5885 | "foo.exe", |
| 5886 | "--gtest_output=xml:directory/path/", |
| 5887 | NULL |
| 5888 | }; |
| 5889 | |
| 5890 | const char* argv2[] = { |
| 5891 | "foo.exe", |
| 5892 | NULL |
| 5893 | }; |
| 5894 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5895 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, |
| 5896 | Flags::Output("xml:directory/path/"), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5897 | } |
| 5898 | |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5899 | // Tests having a --gtest_print_time flag |
| 5900 | TEST_F(InitGoogleTestTest, PrintTimeFlag) { |
| 5901 | const char* argv[] = { |
| 5902 | "foo.exe", |
| 5903 | "--gtest_print_time", |
| 5904 | NULL |
| 5905 | }; |
| 5906 | |
| 5907 | const char* argv2[] = { |
| 5908 | "foo.exe", |
| 5909 | NULL |
| 5910 | }; |
| 5911 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5912 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(true), false); |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5913 | } |
| 5914 | |
| 5915 | // Tests having a --gtest_print_time flag with a "true" value |
| 5916 | TEST_F(InitGoogleTestTest, PrintTimeTrue) { |
| 5917 | const char* argv[] = { |
| 5918 | "foo.exe", |
| 5919 | "--gtest_print_time=1", |
| 5920 | NULL |
| 5921 | }; |
| 5922 | |
| 5923 | const char* argv2[] = { |
| 5924 | "foo.exe", |
| 5925 | NULL |
| 5926 | }; |
| 5927 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5928 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(true), false); |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5929 | } |
| 5930 | |
| 5931 | // Tests having a --gtest_print_time flag with a "false" value |
| 5932 | TEST_F(InitGoogleTestTest, PrintTimeFalse) { |
| 5933 | const char* argv[] = { |
| 5934 | "foo.exe", |
| 5935 | "--gtest_print_time=0", |
| 5936 | NULL |
| 5937 | }; |
| 5938 | |
| 5939 | const char* argv2[] = { |
| 5940 | "foo.exe", |
| 5941 | NULL |
| 5942 | }; |
| 5943 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5944 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(false), false); |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5945 | } |
| 5946 | |
| 5947 | // Tests parsing --gtest_print_time=f. |
| 5948 | TEST_F(InitGoogleTestTest, PrintTimeFalse_f) { |
| 5949 | const char* argv[] = { |
| 5950 | "foo.exe", |
| 5951 | "--gtest_print_time=f", |
| 5952 | NULL |
| 5953 | }; |
| 5954 | |
| 5955 | const char* argv2[] = { |
| 5956 | "foo.exe", |
| 5957 | NULL |
| 5958 | }; |
| 5959 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5960 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(false), false); |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5961 | } |
| 5962 | |
| 5963 | // Tests parsing --gtest_print_time=F. |
| 5964 | TEST_F(InitGoogleTestTest, PrintTimeFalse_F) { |
| 5965 | const char* argv[] = { |
| 5966 | "foo.exe", |
| 5967 | "--gtest_print_time=F", |
| 5968 | NULL |
| 5969 | }; |
| 5970 | |
| 5971 | const char* argv2[] = { |
| 5972 | "foo.exe", |
| 5973 | NULL |
| 5974 | }; |
| 5975 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5976 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(false), false); |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5977 | } |
| 5978 | |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5979 | // Tests parsing --gtest_random_seed=number |
| 5980 | TEST_F(InitGoogleTestTest, RandomSeed) { |
| 5981 | const char* argv[] = { |
| 5982 | "foo.exe", |
| 5983 | "--gtest_random_seed=1000", |
| 5984 | NULL |
| 5985 | }; |
| 5986 | |
| 5987 | const char* argv2[] = { |
| 5988 | "foo.exe", |
| 5989 | NULL |
| 5990 | }; |
| 5991 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5992 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::RandomSeed(1000), false); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5993 | } |
| 5994 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5995 | // Tests parsing --gtest_repeat=number |
| 5996 | TEST_F(InitGoogleTestTest, Repeat) { |
| 5997 | const char* argv[] = { |
| 5998 | "foo.exe", |
| 5999 | "--gtest_repeat=1000", |
| 6000 | NULL |
| 6001 | }; |
| 6002 | |
| 6003 | const char* argv2[] = { |
| 6004 | "foo.exe", |
| 6005 | NULL |
| 6006 | }; |
| 6007 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6008 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Repeat(1000), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6009 | } |
| 6010 | |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 6011 | // Tests having a --gtest_also_run_disabled_tests flag |
| 6012 | TEST_F(InitGoogleTestTest, AlsoRunDisabledTestsFlag) { |
| 6013 | const char* argv[] = { |
| 6014 | "foo.exe", |
| 6015 | "--gtest_also_run_disabled_tests", |
| 6016 | NULL |
| 6017 | }; |
| 6018 | |
| 6019 | const char* argv2[] = { |
| 6020 | "foo.exe", |
| 6021 | NULL |
| 6022 | }; |
| 6023 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6024 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, |
| 6025 | Flags::AlsoRunDisabledTests(true), false); |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 6026 | } |
| 6027 | |
| 6028 | // Tests having a --gtest_also_run_disabled_tests flag with a "true" value |
| 6029 | TEST_F(InitGoogleTestTest, AlsoRunDisabledTestsTrue) { |
| 6030 | const char* argv[] = { |
| 6031 | "foo.exe", |
| 6032 | "--gtest_also_run_disabled_tests=1", |
| 6033 | NULL |
| 6034 | }; |
| 6035 | |
| 6036 | const char* argv2[] = { |
| 6037 | "foo.exe", |
| 6038 | NULL |
| 6039 | }; |
| 6040 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6041 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, |
| 6042 | Flags::AlsoRunDisabledTests(true), false); |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 6043 | } |
| 6044 | |
| 6045 | // Tests having a --gtest_also_run_disabled_tests flag with a "false" value |
| 6046 | TEST_F(InitGoogleTestTest, AlsoRunDisabledTestsFalse) { |
| 6047 | const char* argv[] = { |
| 6048 | "foo.exe", |
| 6049 | "--gtest_also_run_disabled_tests=0", |
| 6050 | NULL |
| 6051 | }; |
| 6052 | |
| 6053 | const char* argv2[] = { |
| 6054 | "foo.exe", |
| 6055 | NULL |
| 6056 | }; |
| 6057 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6058 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, |
| 6059 | Flags::AlsoRunDisabledTests(false), false); |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 6060 | } |
| 6061 | |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 6062 | // Tests parsing --gtest_shuffle. |
| 6063 | TEST_F(InitGoogleTestTest, ShuffleWithoutValue) { |
| 6064 | const char* argv[] = { |
| 6065 | "foo.exe", |
| 6066 | "--gtest_shuffle", |
| 6067 | NULL |
| 6068 | }; |
| 6069 | |
| 6070 | const char* argv2[] = { |
| 6071 | "foo.exe", |
| 6072 | NULL |
| 6073 | }; |
| 6074 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6075 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Shuffle(true), false); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 6076 | } |
| 6077 | |
| 6078 | // Tests parsing --gtest_shuffle=0. |
| 6079 | TEST_F(InitGoogleTestTest, ShuffleFalse_0) { |
| 6080 | const char* argv[] = { |
| 6081 | "foo.exe", |
| 6082 | "--gtest_shuffle=0", |
| 6083 | NULL |
| 6084 | }; |
| 6085 | |
| 6086 | const char* argv2[] = { |
| 6087 | "foo.exe", |
| 6088 | NULL |
| 6089 | }; |
| 6090 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6091 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Shuffle(false), false); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 6092 | } |
| 6093 | |
| 6094 | // Tests parsing a --gtest_shuffle flag that has a "true" |
| 6095 | // definition. |
| 6096 | TEST_F(InitGoogleTestTest, ShuffleTrue) { |
| 6097 | const char* argv[] = { |
| 6098 | "foo.exe", |
| 6099 | "--gtest_shuffle=1", |
| 6100 | NULL |
| 6101 | }; |
| 6102 | |
| 6103 | const char* argv2[] = { |
| 6104 | "foo.exe", |
| 6105 | NULL |
| 6106 | }; |
| 6107 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6108 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Shuffle(true), false); |
| 6109 | } |
| 6110 | |
| 6111 | // Tests parsing --gtest_stack_trace_depth=number. |
| 6112 | TEST_F(InitGoogleTestTest, StackTraceDepth) { |
| 6113 | const char* argv[] = { |
| 6114 | "foo.exe", |
| 6115 | "--gtest_stack_trace_depth=5", |
| 6116 | NULL |
| 6117 | }; |
| 6118 | |
| 6119 | const char* argv2[] = { |
| 6120 | "foo.exe", |
| 6121 | NULL |
| 6122 | }; |
| 6123 | |
| 6124 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::StackTraceDepth(5), false); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 6125 | } |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 6126 | |
| 6127 | // Tests parsing --gtest_throw_on_failure. |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 6128 | TEST_F(InitGoogleTestTest, ThrowOnFailureWithoutValue) { |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 6129 | const char* argv[] = { |
| 6130 | "foo.exe", |
| 6131 | "--gtest_throw_on_failure", |
| 6132 | NULL |
| 6133 | }; |
| 6134 | |
| 6135 | const char* argv2[] = { |
| 6136 | "foo.exe", |
| 6137 | NULL |
| 6138 | }; |
| 6139 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6140 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ThrowOnFailure(true), false); |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 6141 | } |
| 6142 | |
| 6143 | // Tests parsing --gtest_throw_on_failure=0. |
| 6144 | TEST_F(InitGoogleTestTest, ThrowOnFailureFalse_0) { |
| 6145 | const char* argv[] = { |
| 6146 | "foo.exe", |
| 6147 | "--gtest_throw_on_failure=0", |
| 6148 | NULL |
| 6149 | }; |
| 6150 | |
| 6151 | const char* argv2[] = { |
| 6152 | "foo.exe", |
| 6153 | NULL |
| 6154 | }; |
| 6155 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6156 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ThrowOnFailure(false), false); |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 6157 | } |
| 6158 | |
| 6159 | // Tests parsing a --gtest_throw_on_failure flag that has a "true" |
| 6160 | // definition. |
| 6161 | TEST_F(InitGoogleTestTest, ThrowOnFailureTrue) { |
| 6162 | const char* argv[] = { |
| 6163 | "foo.exe", |
| 6164 | "--gtest_throw_on_failure=1", |
| 6165 | NULL |
| 6166 | }; |
| 6167 | |
| 6168 | const char* argv2[] = { |
| 6169 | "foo.exe", |
| 6170 | NULL |
| 6171 | }; |
| 6172 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6173 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ThrowOnFailure(true), false); |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 6176 | #if GTEST_OS_WINDOWS |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6177 | // Tests parsing wide strings. |
| 6178 | TEST_F(InitGoogleTestTest, WideStrings) { |
| 6179 | const wchar_t* argv[] = { |
| 6180 | L"foo.exe", |
| 6181 | L"--gtest_filter=Foo*", |
| 6182 | L"--gtest_list_tests=1", |
| 6183 | L"--gtest_break_on_failure", |
| 6184 | L"--non_gtest_flag", |
| 6185 | NULL |
| 6186 | }; |
| 6187 | |
| 6188 | const wchar_t* argv2[] = { |
| 6189 | L"foo.exe", |
| 6190 | L"--non_gtest_flag", |
| 6191 | NULL |
| 6192 | }; |
| 6193 | |
| 6194 | Flags expected_flags; |
| 6195 | expected_flags.break_on_failure = true; |
| 6196 | expected_flags.filter = "Foo*"; |
| 6197 | expected_flags.list_tests = true; |
| 6198 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6199 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, expected_flags, false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6200 | } |
| 6201 | #endif // GTEST_OS_WINDOWS |
| 6202 | |
| 6203 | // Tests current_test_info() in UnitTest. |
| 6204 | class CurrentTestInfoTest : public Test { |
| 6205 | protected: |
| 6206 | // Tests that current_test_info() returns NULL before the first test in |
| 6207 | // the test case is run. |
| 6208 | static void SetUpTestCase() { |
| 6209 | // There should be no tests running at this point. |
| 6210 | const TestInfo* test_info = |
| 6211 | UnitTest::GetInstance()->current_test_info(); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 6212 | EXPECT_TRUE(test_info == NULL) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6213 | << "There should be no tests running at this point."; |
| 6214 | } |
| 6215 | |
| 6216 | // Tests that current_test_info() returns NULL after the last test in |
| 6217 | // the test case has run. |
| 6218 | static void TearDownTestCase() { |
| 6219 | const TestInfo* test_info = |
| 6220 | UnitTest::GetInstance()->current_test_info(); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 6221 | EXPECT_TRUE(test_info == NULL) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6222 | << "There should be no tests running at this point."; |
| 6223 | } |
| 6224 | }; |
| 6225 | |
| 6226 | // Tests that current_test_info() returns TestInfo for currently running |
| 6227 | // test by checking the expected test name against the actual one. |
| 6228 | TEST_F(CurrentTestInfoTest, WorksForFirstTestInATestCase) { |
| 6229 | const TestInfo* test_info = |
| 6230 | UnitTest::GetInstance()->current_test_info(); |
| 6231 | ASSERT_TRUE(NULL != test_info) |
| 6232 | << "There is a test running so we should have a valid TestInfo."; |
| 6233 | EXPECT_STREQ("CurrentTestInfoTest", test_info->test_case_name()) |
| 6234 | << "Expected the name of the currently running test case."; |
| 6235 | EXPECT_STREQ("WorksForFirstTestInATestCase", test_info->name()) |
| 6236 | << "Expected the name of the currently running test."; |
| 6237 | } |
| 6238 | |
| 6239 | // Tests that current_test_info() returns TestInfo for currently running |
| 6240 | // test by checking the expected test name against the actual one. We |
| 6241 | // use this test to see that the TestInfo object actually changed from |
| 6242 | // the previous invocation. |
| 6243 | TEST_F(CurrentTestInfoTest, WorksForSecondTestInATestCase) { |
| 6244 | const TestInfo* test_info = |
| 6245 | UnitTest::GetInstance()->current_test_info(); |
| 6246 | ASSERT_TRUE(NULL != test_info) |
| 6247 | << "There is a test running so we should have a valid TestInfo."; |
| 6248 | EXPECT_STREQ("CurrentTestInfoTest", test_info->test_case_name()) |
| 6249 | << "Expected the name of the currently running test case."; |
| 6250 | EXPECT_STREQ("WorksForSecondTestInATestCase", test_info->name()) |
| 6251 | << "Expected the name of the currently running test."; |
| 6252 | } |
| 6253 | |
| 6254 | } // namespace testing |
| 6255 | |
| 6256 | // These two lines test that we can define tests in a namespace that |
| 6257 | // has the name "testing" and is nested in another namespace. |
| 6258 | namespace my_namespace { |
| 6259 | namespace testing { |
| 6260 | |
| 6261 | // Makes sure that TEST knows to use ::testing::Test instead of |
| 6262 | // ::my_namespace::testing::Test. |
| 6263 | class Test {}; |
| 6264 | |
| 6265 | // Makes sure that an assertion knows to use ::testing::Message instead of |
| 6266 | // ::my_namespace::testing::Message. |
| 6267 | class Message {}; |
| 6268 | |
| 6269 | // Makes sure that an assertion knows to use |
| 6270 | // ::testing::AssertionResult instead of |
| 6271 | // ::my_namespace::testing::AssertionResult. |
| 6272 | class AssertionResult {}; |
| 6273 | |
| 6274 | // Tests that an assertion that should succeed works as expected. |
| 6275 | TEST(NestedTestingNamespaceTest, Success) { |
| 6276 | EXPECT_EQ(1, 1) << "This shouldn't fail."; |
| 6277 | } |
| 6278 | |
| 6279 | // Tests that an assertion that should fail works as expected. |
| 6280 | TEST(NestedTestingNamespaceTest, Failure) { |
| 6281 | EXPECT_FATAL_FAILURE(FAIL() << "This failure is expected.", |
| 6282 | "This failure is expected."); |
| 6283 | } |
| 6284 | |
| 6285 | } // namespace testing |
| 6286 | } // namespace my_namespace |
| 6287 | |
| 6288 | // Tests that one can call superclass SetUp and TearDown methods-- |
| 6289 | // that is, that they are not private. |
| 6290 | // No tests are based on this fixture; the test "passes" if it compiles |
| 6291 | // successfully. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 6292 | class ProtectedFixtureMethodsTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6293 | protected: |
| 6294 | virtual void SetUp() { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 6295 | Test::SetUp(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6296 | } |
| 6297 | virtual void TearDown() { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 6298 | Test::TearDown(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6299 | } |
| 6300 | }; |
| 6301 | |
| 6302 | // StreamingAssertionsTest tests the streaming versions of a representative |
| 6303 | // sample of assertions. |
| 6304 | TEST(StreamingAssertionsTest, Unconditional) { |
| 6305 | SUCCEED() << "expected success"; |
| 6306 | EXPECT_NONFATAL_FAILURE(ADD_FAILURE() << "expected failure", |
| 6307 | "expected failure"); |
| 6308 | EXPECT_FATAL_FAILURE(FAIL() << "expected failure", |
| 6309 | "expected failure"); |
| 6310 | } |
| 6311 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 6312 | #ifdef __BORLANDC__ |
| 6313 | // Silences warnings: "Condition is always true", "Unreachable code" |
| 6314 | #pragma option push -w-ccc -w-rch |
| 6315 | #endif |
| 6316 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6317 | TEST(StreamingAssertionsTest, Truth) { |
| 6318 | EXPECT_TRUE(true) << "unexpected failure"; |
| 6319 | ASSERT_TRUE(true) << "unexpected failure"; |
| 6320 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(false) << "expected failure", |
| 6321 | "expected failure"); |
| 6322 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(false) << "expected failure", |
| 6323 | "expected failure"); |
| 6324 | } |
| 6325 | |
| 6326 | TEST(StreamingAssertionsTest, Truth2) { |
| 6327 | EXPECT_FALSE(false) << "unexpected failure"; |
| 6328 | ASSERT_FALSE(false) << "unexpected failure"; |
| 6329 | EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(true) << "expected failure", |
| 6330 | "expected failure"); |
| 6331 | EXPECT_FATAL_FAILURE(ASSERT_FALSE(true) << "expected failure", |
| 6332 | "expected failure"); |
| 6333 | } |
| 6334 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 6335 | #ifdef __BORLANDC__ |
| 6336 | // Restores warnings after previous "#pragma option push" supressed them |
| 6337 | #pragma option pop |
| 6338 | #endif |
| 6339 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6340 | TEST(StreamingAssertionsTest, IntegerEquals) { |
| 6341 | EXPECT_EQ(1, 1) << "unexpected failure"; |
| 6342 | ASSERT_EQ(1, 1) << "unexpected failure"; |
| 6343 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(1, 2) << "expected failure", |
| 6344 | "expected failure"); |
| 6345 | EXPECT_FATAL_FAILURE(ASSERT_EQ(1, 2) << "expected failure", |
| 6346 | "expected failure"); |
| 6347 | } |
| 6348 | |
| 6349 | TEST(StreamingAssertionsTest, IntegerLessThan) { |
| 6350 | EXPECT_LT(1, 2) << "unexpected failure"; |
| 6351 | ASSERT_LT(1, 2) << "unexpected failure"; |
| 6352 | EXPECT_NONFATAL_FAILURE(EXPECT_LT(2, 1) << "expected failure", |
| 6353 | "expected failure"); |
| 6354 | EXPECT_FATAL_FAILURE(ASSERT_LT(2, 1) << "expected failure", |
| 6355 | "expected failure"); |
| 6356 | } |
| 6357 | |
| 6358 | TEST(StreamingAssertionsTest, StringsEqual) { |
| 6359 | EXPECT_STREQ("foo", "foo") << "unexpected failure"; |
| 6360 | ASSERT_STREQ("foo", "foo") << "unexpected failure"; |
| 6361 | EXPECT_NONFATAL_FAILURE(EXPECT_STREQ("foo", "bar") << "expected failure", |
| 6362 | "expected failure"); |
| 6363 | EXPECT_FATAL_FAILURE(ASSERT_STREQ("foo", "bar") << "expected failure", |
| 6364 | "expected failure"); |
| 6365 | } |
| 6366 | |
| 6367 | TEST(StreamingAssertionsTest, StringsNotEqual) { |
| 6368 | EXPECT_STRNE("foo", "bar") << "unexpected failure"; |
| 6369 | ASSERT_STRNE("foo", "bar") << "unexpected failure"; |
| 6370 | EXPECT_NONFATAL_FAILURE(EXPECT_STRNE("foo", "foo") << "expected failure", |
| 6371 | "expected failure"); |
| 6372 | EXPECT_FATAL_FAILURE(ASSERT_STRNE("foo", "foo") << "expected failure", |
| 6373 | "expected failure"); |
| 6374 | } |
| 6375 | |
| 6376 | TEST(StreamingAssertionsTest, StringsEqualIgnoringCase) { |
| 6377 | EXPECT_STRCASEEQ("foo", "FOO") << "unexpected failure"; |
| 6378 | ASSERT_STRCASEEQ("foo", "FOO") << "unexpected failure"; |
| 6379 | EXPECT_NONFATAL_FAILURE(EXPECT_STRCASEEQ("foo", "bar") << "expected failure", |
| 6380 | "expected failure"); |
| 6381 | EXPECT_FATAL_FAILURE(ASSERT_STRCASEEQ("foo", "bar") << "expected failure", |
| 6382 | "expected failure"); |
| 6383 | } |
| 6384 | |
| 6385 | TEST(StreamingAssertionsTest, StringNotEqualIgnoringCase) { |
| 6386 | EXPECT_STRCASENE("foo", "bar") << "unexpected failure"; |
| 6387 | ASSERT_STRCASENE("foo", "bar") << "unexpected failure"; |
| 6388 | EXPECT_NONFATAL_FAILURE(EXPECT_STRCASENE("foo", "FOO") << "expected failure", |
| 6389 | "expected failure"); |
| 6390 | EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("bar", "BAR") << "expected failure", |
| 6391 | "expected failure"); |
| 6392 | } |
| 6393 | |
| 6394 | TEST(StreamingAssertionsTest, FloatingPointEquals) { |
| 6395 | EXPECT_FLOAT_EQ(1.0, 1.0) << "unexpected failure"; |
| 6396 | ASSERT_FLOAT_EQ(1.0, 1.0) << "unexpected failure"; |
| 6397 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(0.0, 1.0) << "expected failure", |
| 6398 | "expected failure"); |
| 6399 | EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(0.0, 1.0) << "expected failure", |
| 6400 | "expected failure"); |
| 6401 | } |
| 6402 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 6403 | #if GTEST_HAS_EXCEPTIONS |
| 6404 | |
| 6405 | TEST(StreamingAssertionsTest, Throw) { |
| 6406 | EXPECT_THROW(ThrowAnInteger(), int) << "unexpected failure"; |
| 6407 | ASSERT_THROW(ThrowAnInteger(), int) << "unexpected failure"; |
| 6408 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnInteger(), bool) << |
| 6409 | "expected failure", "expected failure"); |
| 6410 | EXPECT_FATAL_FAILURE(ASSERT_THROW(ThrowAnInteger(), bool) << |
| 6411 | "expected failure", "expected failure"); |
| 6412 | } |
| 6413 | |
| 6414 | TEST(StreamingAssertionsTest, NoThrow) { |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 6415 | EXPECT_NO_THROW(ThrowNothing()) << "unexpected failure"; |
| 6416 | ASSERT_NO_THROW(ThrowNothing()) << "unexpected failure"; |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 6417 | EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()) << |
| 6418 | "expected failure", "expected failure"); |
| 6419 | EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()) << |
| 6420 | "expected failure", "expected failure"); |
| 6421 | } |
| 6422 | |
| 6423 | TEST(StreamingAssertionsTest, AnyThrow) { |
| 6424 | EXPECT_ANY_THROW(ThrowAnInteger()) << "unexpected failure"; |
| 6425 | ASSERT_ANY_THROW(ThrowAnInteger()) << "unexpected failure"; |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 6426 | EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(ThrowNothing()) << |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 6427 | "expected failure", "expected failure"); |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 6428 | EXPECT_FATAL_FAILURE(ASSERT_ANY_THROW(ThrowNothing()) << |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 6429 | "expected failure", "expected failure"); |
| 6430 | } |
| 6431 | |
| 6432 | #endif // GTEST_HAS_EXCEPTIONS |
| 6433 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6434 | // Tests that Google Test correctly decides whether to use colors in the output. |
| 6435 | |
| 6436 | TEST(ColoredOutputTest, UsesColorsWhenGTestColorFlagIsYes) { |
| 6437 | GTEST_FLAG(color) = "yes"; |
| 6438 | |
| 6439 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 6440 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6441 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6442 | |
| 6443 | SetEnv("TERM", "dumb"); // TERM doesn't support colors. |
| 6444 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6445 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6446 | } |
| 6447 | |
| 6448 | TEST(ColoredOutputTest, UsesColorsWhenGTestColorFlagIsAliasOfYes) { |
| 6449 | SetEnv("TERM", "dumb"); // TERM doesn't support colors. |
| 6450 | |
| 6451 | GTEST_FLAG(color) = "True"; |
| 6452 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6453 | |
| 6454 | GTEST_FLAG(color) = "t"; |
| 6455 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6456 | |
| 6457 | GTEST_FLAG(color) = "1"; |
| 6458 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6459 | } |
| 6460 | |
| 6461 | TEST(ColoredOutputTest, UsesNoColorWhenGTestColorFlagIsNo) { |
| 6462 | GTEST_FLAG(color) = "no"; |
| 6463 | |
| 6464 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 6465 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6466 | EXPECT_FALSE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6467 | |
| 6468 | SetEnv("TERM", "dumb"); // TERM doesn't support colors. |
| 6469 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6470 | EXPECT_FALSE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6471 | } |
| 6472 | |
| 6473 | TEST(ColoredOutputTest, UsesNoColorWhenGTestColorFlagIsInvalid) { |
| 6474 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 6475 | |
| 6476 | GTEST_FLAG(color) = "F"; |
| 6477 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6478 | |
| 6479 | GTEST_FLAG(color) = "0"; |
| 6480 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6481 | |
| 6482 | GTEST_FLAG(color) = "unknown"; |
| 6483 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6484 | } |
| 6485 | |
| 6486 | TEST(ColoredOutputTest, UsesColorsWhenStdoutIsTty) { |
| 6487 | GTEST_FLAG(color) = "auto"; |
| 6488 | |
| 6489 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 6490 | EXPECT_FALSE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6491 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6492 | } |
| 6493 | |
| 6494 | TEST(ColoredOutputTest, UsesColorsWhenTermSupportsColors) { |
| 6495 | GTEST_FLAG(color) = "auto"; |
| 6496 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 6497 | #if GTEST_OS_WINDOWS |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6498 | // On Windows, we ignore the TERM variable as it's usually not set. |
| 6499 | |
| 6500 | SetEnv("TERM", "dumb"); |
| 6501 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6502 | |
| 6503 | SetEnv("TERM", ""); |
| 6504 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6505 | |
| 6506 | SetEnv("TERM", "xterm"); |
| 6507 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6508 | #else |
| 6509 | // On non-Windows platforms, we rely on TERM to determine if the |
| 6510 | // terminal supports colors. |
| 6511 | |
| 6512 | SetEnv("TERM", "dumb"); // TERM doesn't support colors. |
| 6513 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6514 | |
| 6515 | SetEnv("TERM", "emacs"); // TERM doesn't support colors. |
| 6516 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6517 | |
| 6518 | SetEnv("TERM", "vt100"); // TERM doesn't support colors. |
| 6519 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6520 | |
| 6521 | SetEnv("TERM", "xterm-mono"); // TERM doesn't support colors. |
| 6522 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6523 | |
| 6524 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 6525 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6526 | |
| 6527 | SetEnv("TERM", "xterm-color"); // TERM supports colors. |
| 6528 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 6529 | |
| 6530 | SetEnv("TERM", "linux"); // TERM supports colors. |
| 6531 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6532 | #endif // GTEST_OS_WINDOWS |
| 6533 | } |
| 6534 | |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 6535 | // Verifies that StaticAssertTypeEq works in a namespace scope. |
| 6536 | |
| 6537 | static bool dummy1 = StaticAssertTypeEq<bool, bool>(); |
| 6538 | static bool dummy2 = StaticAssertTypeEq<const int, const int>(); |
| 6539 | |
| 6540 | // Verifies that StaticAssertTypeEq works in a class. |
| 6541 | |
| 6542 | template <typename T> |
| 6543 | class StaticAssertTypeEqTestHelper { |
| 6544 | public: |
| 6545 | StaticAssertTypeEqTestHelper() { StaticAssertTypeEq<bool, T>(); } |
| 6546 | }; |
| 6547 | |
| 6548 | TEST(StaticAssertTypeEqTest, WorksInClass) { |
| 6549 | StaticAssertTypeEqTestHelper<bool>(); |
| 6550 | } |
| 6551 | |
| 6552 | // Verifies that StaticAssertTypeEq works inside a function. |
| 6553 | |
| 6554 | typedef int IntAlias; |
| 6555 | |
| 6556 | TEST(StaticAssertTypeEqTest, CompilesForEqualTypes) { |
| 6557 | StaticAssertTypeEq<int, IntAlias>(); |
| 6558 | StaticAssertTypeEq<int*, IntAlias*>(); |
| 6559 | } |
| 6560 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 6561 | TEST(ThreadLocalTest, DefaultConstructor) { |
| 6562 | ThreadLocal<int> t1; |
| 6563 | EXPECT_EQ(0, t1.get()); |
| 6564 | |
| 6565 | ThreadLocal<void*> t2; |
| 6566 | EXPECT_TRUE(t2.get() == NULL); |
| 6567 | } |
| 6568 | |
| 6569 | TEST(ThreadLocalTest, Init) { |
| 6570 | ThreadLocal<int> t1(123); |
| 6571 | EXPECT_EQ(123, t1.get()); |
| 6572 | |
| 6573 | int i = 0; |
| 6574 | ThreadLocal<int*> t2(&i); |
| 6575 | EXPECT_EQ(&i, t2.get()); |
| 6576 | } |
| 6577 | |
vladlosev | f904a61 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 6578 | TEST(GetCurrentOsStackTraceExceptTopTest, ReturnsTheStackTrace) { |
| 6579 | testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); |
| 6580 | |
| 6581 | // We don't have a stack walker in Google Test yet. |
| 6582 | EXPECT_STREQ("", GetCurrentOsStackTraceExceptTop(unit_test, 0).c_str()); |
| 6583 | EXPECT_STREQ("", GetCurrentOsStackTraceExceptTop(unit_test, 1).c_str()); |
| 6584 | } |
zhanyong.wan | 1b17110 | 2009-04-07 21:03:22 +0000 | [diff] [blame] | 6585 | |
| 6586 | TEST(HasNonfatalFailureTest, ReturnsFalseWhenThereIsNoFailure) { |
| 6587 | EXPECT_FALSE(HasNonfatalFailure()); |
| 6588 | } |
| 6589 | |
| 6590 | static void FailFatally() { FAIL(); } |
| 6591 | |
| 6592 | TEST(HasNonfatalFailureTest, ReturnsFalseWhenThereIsOnlyFatalFailure) { |
| 6593 | FailFatally(); |
| 6594 | const bool has_nonfatal_failure = HasNonfatalFailure(); |
| 6595 | ClearCurrentTestPartResults(); |
| 6596 | EXPECT_FALSE(has_nonfatal_failure); |
| 6597 | } |
| 6598 | |
| 6599 | TEST(HasNonfatalFailureTest, ReturnsTrueWhenThereIsNonfatalFailure) { |
| 6600 | ADD_FAILURE(); |
| 6601 | const bool has_nonfatal_failure = HasNonfatalFailure(); |
| 6602 | ClearCurrentTestPartResults(); |
| 6603 | EXPECT_TRUE(has_nonfatal_failure); |
| 6604 | } |
| 6605 | |
| 6606 | TEST(HasNonfatalFailureTest, ReturnsTrueWhenThereAreFatalAndNonfatalFailures) { |
| 6607 | FailFatally(); |
| 6608 | ADD_FAILURE(); |
| 6609 | const bool has_nonfatal_failure = HasNonfatalFailure(); |
| 6610 | ClearCurrentTestPartResults(); |
| 6611 | EXPECT_TRUE(has_nonfatal_failure); |
| 6612 | } |
| 6613 | |
| 6614 | // A wrapper for calling HasNonfatalFailure outside of a test body. |
| 6615 | static bool HasNonfatalFailureHelper() { |
| 6616 | return testing::Test::HasNonfatalFailure(); |
| 6617 | } |
| 6618 | |
| 6619 | TEST(HasNonfatalFailureTest, WorksOutsideOfTestBody) { |
| 6620 | EXPECT_FALSE(HasNonfatalFailureHelper()); |
| 6621 | } |
| 6622 | |
| 6623 | TEST(HasNonfatalFailureTest, WorksOutsideOfTestBody2) { |
| 6624 | ADD_FAILURE(); |
| 6625 | const bool has_nonfatal_failure = HasNonfatalFailureHelper(); |
| 6626 | ClearCurrentTestPartResults(); |
| 6627 | EXPECT_TRUE(has_nonfatal_failure); |
| 6628 | } |
| 6629 | |
| 6630 | TEST(HasFailureTest, ReturnsFalseWhenThereIsNoFailure) { |
| 6631 | EXPECT_FALSE(HasFailure()); |
| 6632 | } |
| 6633 | |
| 6634 | TEST(HasFailureTest, ReturnsTrueWhenThereIsFatalFailure) { |
| 6635 | FailFatally(); |
| 6636 | const bool has_failure = HasFailure(); |
| 6637 | ClearCurrentTestPartResults(); |
| 6638 | EXPECT_TRUE(has_failure); |
| 6639 | } |
| 6640 | |
| 6641 | TEST(HasFailureTest, ReturnsTrueWhenThereIsNonfatalFailure) { |
| 6642 | ADD_FAILURE(); |
| 6643 | const bool has_failure = HasFailure(); |
| 6644 | ClearCurrentTestPartResults(); |
| 6645 | EXPECT_TRUE(has_failure); |
| 6646 | } |
| 6647 | |
| 6648 | TEST(HasFailureTest, ReturnsTrueWhenThereAreFatalAndNonfatalFailures) { |
| 6649 | FailFatally(); |
| 6650 | ADD_FAILURE(); |
| 6651 | const bool has_failure = HasFailure(); |
| 6652 | ClearCurrentTestPartResults(); |
| 6653 | EXPECT_TRUE(has_failure); |
| 6654 | } |
| 6655 | |
| 6656 | // A wrapper for calling HasFailure outside of a test body. |
| 6657 | static bool HasFailureHelper() { return testing::Test::HasFailure(); } |
| 6658 | |
| 6659 | TEST(HasFailureTest, WorksOutsideOfTestBody) { |
| 6660 | EXPECT_FALSE(HasFailureHelper()); |
| 6661 | } |
| 6662 | |
| 6663 | TEST(HasFailureTest, WorksOutsideOfTestBody2) { |
| 6664 | ADD_FAILURE(); |
| 6665 | const bool has_failure = HasFailureHelper(); |
| 6666 | ClearCurrentTestPartResults(); |
| 6667 | EXPECT_TRUE(has_failure); |
| 6668 | } |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6669 | |
| 6670 | class TestListener : public EmptyTestEventListener { |
| 6671 | public: |
| 6672 | TestListener() : on_start_counter_(NULL), is_destroyed_(NULL) {} |
| 6673 | TestListener(int* on_start_counter, bool* is_destroyed) |
| 6674 | : on_start_counter_(on_start_counter), |
| 6675 | is_destroyed_(is_destroyed) {} |
| 6676 | |
| 6677 | virtual ~TestListener() { |
| 6678 | if (is_destroyed_) |
| 6679 | *is_destroyed_ = true; |
| 6680 | } |
| 6681 | |
| 6682 | protected: |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6683 | virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6684 | if (on_start_counter_ != NULL) |
| 6685 | (*on_start_counter_)++; |
| 6686 | } |
| 6687 | |
| 6688 | private: |
| 6689 | int* on_start_counter_; |
| 6690 | bool* is_destroyed_; |
| 6691 | }; |
| 6692 | |
| 6693 | // Tests the constructor. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6694 | TEST(TestEventListenersTest, ConstructionWorks) { |
| 6695 | TestEventListeners listeners; |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6696 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6697 | EXPECT_TRUE(TestEventListenersAccessor::GetRepeater(&listeners) != NULL); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6698 | EXPECT_TRUE(listeners.default_result_printer() == NULL); |
| 6699 | EXPECT_TRUE(listeners.default_xml_generator() == NULL); |
| 6700 | } |
| 6701 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6702 | // Tests that the TestEventListeners destructor deletes all the listeners it |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6703 | // owns. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6704 | TEST(TestEventListenersTest, DestructionWorks) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6705 | bool default_result_printer_is_destroyed = false; |
| 6706 | bool default_xml_printer_is_destroyed = false; |
| 6707 | bool extra_listener_is_destroyed = false; |
| 6708 | TestListener* default_result_printer = new TestListener( |
| 6709 | NULL, &default_result_printer_is_destroyed); |
| 6710 | TestListener* default_xml_printer = new TestListener( |
| 6711 | NULL, &default_xml_printer_is_destroyed); |
| 6712 | TestListener* extra_listener = new TestListener( |
| 6713 | NULL, &extra_listener_is_destroyed); |
| 6714 | |
| 6715 | { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6716 | TestEventListeners listeners; |
| 6717 | TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, |
| 6718 | default_result_printer); |
| 6719 | TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, |
| 6720 | default_xml_printer); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6721 | listeners.Append(extra_listener); |
| 6722 | } |
| 6723 | EXPECT_TRUE(default_result_printer_is_destroyed); |
| 6724 | EXPECT_TRUE(default_xml_printer_is_destroyed); |
| 6725 | EXPECT_TRUE(extra_listener_is_destroyed); |
| 6726 | } |
| 6727 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6728 | // Tests that a listener Append'ed to a TestEventListeners list starts |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6729 | // receiving events. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6730 | TEST(TestEventListenersTest, Append) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6731 | int on_start_counter = 0; |
| 6732 | bool is_destroyed = false; |
| 6733 | TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); |
| 6734 | { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6735 | TestEventListeners listeners; |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6736 | listeners.Append(listener); |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6737 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6738 | *UnitTest::GetInstance()); |
| 6739 | EXPECT_EQ(1, on_start_counter); |
| 6740 | } |
| 6741 | EXPECT_TRUE(is_destroyed); |
| 6742 | } |
| 6743 | |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6744 | // Tests that listeners receive events in the order they were appended to |
| 6745 | // the list, except for *End requests, which must be received in the reverse |
| 6746 | // order. |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6747 | class SequenceTestingListener : public EmptyTestEventListener { |
| 6748 | public: |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6749 | SequenceTestingListener(Vector<String>* vector, const char* id) |
| 6750 | : vector_(vector), id_(id) {} |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6751 | |
| 6752 | protected: |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6753 | virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) { |
| 6754 | vector_->PushBack(GetEventDescription("OnTestProgramStart")); |
| 6755 | } |
| 6756 | |
| 6757 | virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) { |
| 6758 | vector_->PushBack(GetEventDescription("OnTestProgramEnd")); |
| 6759 | } |
| 6760 | |
| 6761 | virtual void OnTestIterationStart(const UnitTest& /*unit_test*/, |
| 6762 | int /*iteration*/) { |
| 6763 | vector_->PushBack(GetEventDescription("OnTestIterationStart")); |
| 6764 | } |
| 6765 | |
| 6766 | virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/, |
| 6767 | int /*iteration*/) { |
| 6768 | vector_->PushBack(GetEventDescription("OnTestIterationEnd")); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6769 | } |
| 6770 | |
| 6771 | private: |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6772 | String GetEventDescription(const char* method) { |
| 6773 | Message message; |
| 6774 | message << id_ << "." << method; |
| 6775 | return message.GetString(); |
| 6776 | } |
| 6777 | |
| 6778 | Vector<String>* vector_; |
| 6779 | const char* const id_; |
zhanyong.wan | d586f9f | 2009-09-18 16:35:15 +0000 | [diff] [blame] | 6780 | |
| 6781 | GTEST_DISALLOW_COPY_AND_ASSIGN_(SequenceTestingListener); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6782 | }; |
| 6783 | |
| 6784 | TEST(EventListenerTest, AppendKeepsOrder) { |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6785 | Vector<String> vec; |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6786 | TestEventListeners listeners; |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6787 | listeners.Append(new SequenceTestingListener(&vec, "1st")); |
| 6788 | listeners.Append(new SequenceTestingListener(&vec, "2nd")); |
| 6789 | listeners.Append(new SequenceTestingListener(&vec, "3rd")); |
| 6790 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6791 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6792 | *UnitTest::GetInstance()); |
| 6793 | ASSERT_EQ(3, vec.size()); |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6794 | EXPECT_STREQ("1st.OnTestProgramStart", vec.GetElement(0).c_str()); |
| 6795 | EXPECT_STREQ("2nd.OnTestProgramStart", vec.GetElement(1).c_str()); |
| 6796 | EXPECT_STREQ("3rd.OnTestProgramStart", vec.GetElement(2).c_str()); |
| 6797 | |
| 6798 | vec.Clear(); |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6799 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramEnd( |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6800 | *UnitTest::GetInstance()); |
| 6801 | ASSERT_EQ(3, vec.size()); |
| 6802 | EXPECT_STREQ("3rd.OnTestProgramEnd", vec.GetElement(0).c_str()); |
| 6803 | EXPECT_STREQ("2nd.OnTestProgramEnd", vec.GetElement(1).c_str()); |
| 6804 | EXPECT_STREQ("1st.OnTestProgramEnd", vec.GetElement(2).c_str()); |
| 6805 | |
| 6806 | vec.Clear(); |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6807 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestIterationStart( |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6808 | *UnitTest::GetInstance(), 0); |
| 6809 | ASSERT_EQ(3, vec.size()); |
| 6810 | EXPECT_STREQ("1st.OnTestIterationStart", vec.GetElement(0).c_str()); |
| 6811 | EXPECT_STREQ("2nd.OnTestIterationStart", vec.GetElement(1).c_str()); |
| 6812 | EXPECT_STREQ("3rd.OnTestIterationStart", vec.GetElement(2).c_str()); |
| 6813 | |
| 6814 | vec.Clear(); |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6815 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestIterationEnd( |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6816 | *UnitTest::GetInstance(), 0); |
| 6817 | ASSERT_EQ(3, vec.size()); |
| 6818 | EXPECT_STREQ("3rd.OnTestIterationEnd", vec.GetElement(0).c_str()); |
| 6819 | EXPECT_STREQ("2nd.OnTestIterationEnd", vec.GetElement(1).c_str()); |
| 6820 | EXPECT_STREQ("1st.OnTestIterationEnd", vec.GetElement(2).c_str()); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6821 | } |
| 6822 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6823 | // Tests that a listener removed from a TestEventListeners list stops receiving |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6824 | // events and is not deleted when the list is destroyed. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6825 | TEST(TestEventListenersTest, Release) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6826 | int on_start_counter = 0; |
| 6827 | bool is_destroyed = false; |
| 6828 | // Although Append passes the ownership of this object to the list, |
| 6829 | // the following calls release it, and we need to delete it before the |
| 6830 | // test ends. |
| 6831 | TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); |
| 6832 | { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6833 | TestEventListeners listeners; |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6834 | listeners.Append(listener); |
| 6835 | EXPECT_EQ(listener, listeners.Release(listener)); |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6836 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6837 | *UnitTest::GetInstance()); |
| 6838 | EXPECT_TRUE(listeners.Release(listener) == NULL); |
| 6839 | } |
| 6840 | EXPECT_EQ(0, on_start_counter); |
| 6841 | EXPECT_FALSE(is_destroyed); |
| 6842 | delete listener; |
| 6843 | } |
| 6844 | |
| 6845 | // Tests that no events are forwarded when event forwarding is disabled. |
| 6846 | TEST(EventListenerTest, SuppressEventForwarding) { |
| 6847 | int on_start_counter = 0; |
| 6848 | TestListener* listener = new TestListener(&on_start_counter, NULL); |
| 6849 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6850 | TestEventListeners listeners; |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6851 | listeners.Append(listener); |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6852 | ASSERT_TRUE(TestEventListenersAccessor::EventForwardingEnabled(listeners)); |
| 6853 | TestEventListenersAccessor::SuppressEventForwarding(&listeners); |
| 6854 | ASSERT_FALSE(TestEventListenersAccessor::EventForwardingEnabled(listeners)); |
| 6855 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6856 | *UnitTest::GetInstance()); |
| 6857 | EXPECT_EQ(0, on_start_counter); |
| 6858 | } |
| 6859 | |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6860 | // Tests that events generated by Google Test are not forwarded in |
| 6861 | // death test subprocesses. |
| 6862 | TEST(EventListenerDeathTest, EventsNotForwardedInDeathTestSubprecesses) { |
zhanyong.wan | d541f02 | 2009-09-11 06:59:42 +0000 | [diff] [blame] | 6863 | EXPECT_DEATH_IF_SUPPORTED({ |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6864 | GTEST_CHECK_(TestEventListenersAccessor::EventForwardingEnabled( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6865 | *GetUnitTestImpl()->listeners())) << "expected failure";}, |
| 6866 | "expected failure"); |
| 6867 | } |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6868 | |
| 6869 | // Tests that a listener installed via SetDefaultResultPrinter() starts |
| 6870 | // receiving events and is returned via default_result_printer() and that |
| 6871 | // the previous default_result_printer is removed from the list and deleted. |
| 6872 | TEST(EventListenerTest, default_result_printer) { |
| 6873 | int on_start_counter = 0; |
| 6874 | bool is_destroyed = false; |
| 6875 | TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); |
| 6876 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6877 | TestEventListeners listeners; |
| 6878 | TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, listener); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6879 | |
| 6880 | EXPECT_EQ(listener, listeners.default_result_printer()); |
| 6881 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6882 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6883 | *UnitTest::GetInstance()); |
| 6884 | |
| 6885 | EXPECT_EQ(1, on_start_counter); |
| 6886 | |
| 6887 | // Replacing default_result_printer with something else should remove it |
| 6888 | // from the list and destroy it. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6889 | TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, NULL); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6890 | |
| 6891 | EXPECT_TRUE(listeners.default_result_printer() == NULL); |
| 6892 | EXPECT_TRUE(is_destroyed); |
| 6893 | |
| 6894 | // After broadcasting an event the counter is still the same, indicating |
| 6895 | // the listener is not in the list anymore. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6896 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6897 | *UnitTest::GetInstance()); |
| 6898 | EXPECT_EQ(1, on_start_counter); |
| 6899 | } |
| 6900 | |
| 6901 | // Tests that the default_result_printer listener stops receiving events |
| 6902 | // when removed via Release and that is not owned by the list anymore. |
| 6903 | TEST(EventListenerTest, RemovingDefaultResultPrinterWorks) { |
| 6904 | int on_start_counter = 0; |
| 6905 | bool is_destroyed = false; |
| 6906 | // Although Append passes the ownership of this object to the list, |
| 6907 | // the following calls release it, and we need to delete it before the |
| 6908 | // test ends. |
| 6909 | TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); |
| 6910 | { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6911 | TestEventListeners listeners; |
| 6912 | TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, listener); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6913 | |
| 6914 | EXPECT_EQ(listener, listeners.Release(listener)); |
| 6915 | EXPECT_TRUE(listeners.default_result_printer() == NULL); |
| 6916 | EXPECT_FALSE(is_destroyed); |
| 6917 | |
| 6918 | // Broadcasting events now should not affect default_result_printer. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6919 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6920 | *UnitTest::GetInstance()); |
| 6921 | EXPECT_EQ(0, on_start_counter); |
| 6922 | } |
| 6923 | // Destroying the list should not affect the listener now, too. |
| 6924 | EXPECT_FALSE(is_destroyed); |
| 6925 | delete listener; |
| 6926 | } |
| 6927 | |
| 6928 | // Tests that a listener installed via SetDefaultXmlGenerator() starts |
| 6929 | // receiving events and is returned via default_xml_generator() and that |
| 6930 | // the previous default_xml_generator is removed from the list and deleted. |
| 6931 | TEST(EventListenerTest, default_xml_generator) { |
| 6932 | int on_start_counter = 0; |
| 6933 | bool is_destroyed = false; |
| 6934 | TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); |
| 6935 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6936 | TestEventListeners listeners; |
| 6937 | TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, listener); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6938 | |
| 6939 | EXPECT_EQ(listener, listeners.default_xml_generator()); |
| 6940 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6941 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6942 | *UnitTest::GetInstance()); |
| 6943 | |
| 6944 | EXPECT_EQ(1, on_start_counter); |
| 6945 | |
| 6946 | // Replacing default_xml_generator with something else should remove it |
| 6947 | // from the list and destroy it. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6948 | TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, NULL); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6949 | |
| 6950 | EXPECT_TRUE(listeners.default_xml_generator() == NULL); |
| 6951 | EXPECT_TRUE(is_destroyed); |
| 6952 | |
| 6953 | // After broadcasting an event the counter is still the same, indicating |
| 6954 | // the listener is not in the list anymore. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6955 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6956 | *UnitTest::GetInstance()); |
| 6957 | EXPECT_EQ(1, on_start_counter); |
| 6958 | } |
| 6959 | |
| 6960 | // Tests that the default_xml_generator listener stops receiving events |
| 6961 | // when removed via Release and that is not owned by the list anymore. |
| 6962 | TEST(EventListenerTest, RemovingDefaultXmlGeneratorWorks) { |
| 6963 | int on_start_counter = 0; |
| 6964 | bool is_destroyed = false; |
| 6965 | // Although Append passes the ownership of this object to the list, |
| 6966 | // the following calls release it, and we need to delete it before the |
| 6967 | // test ends. |
| 6968 | TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); |
| 6969 | { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6970 | TestEventListeners listeners; |
| 6971 | TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, listener); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6972 | |
| 6973 | EXPECT_EQ(listener, listeners.Release(listener)); |
| 6974 | EXPECT_TRUE(listeners.default_xml_generator() == NULL); |
| 6975 | EXPECT_FALSE(is_destroyed); |
| 6976 | |
| 6977 | // Broadcasting events now should not affect default_xml_generator. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6978 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6979 | *UnitTest::GetInstance()); |
| 6980 | EXPECT_EQ(0, on_start_counter); |
| 6981 | } |
| 6982 | // Destroying the list should not affect the listener now, too. |
| 6983 | EXPECT_FALSE(is_destroyed); |
| 6984 | delete listener; |
| 6985 | } |