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 | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 36 | #include <vector> |
zhanyong.wan | 0ebc16a | 2009-02-02 06:37:03 +0000 | [diff] [blame] | 37 | |
| 38 | // Verifies that the command line flag variables can be accessed |
| 39 | // in code once <gtest/gtest.h> has been #included. |
| 40 | // Do not move it after other #includes. |
| 41 | TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) { |
| 42 | bool dummy = testing::GTEST_FLAG(also_run_disabled_tests) |
| 43 | || testing::GTEST_FLAG(break_on_failure) |
| 44 | || testing::GTEST_FLAG(catch_exceptions) |
| 45 | || testing::GTEST_FLAG(color) != "unknown" |
| 46 | || testing::GTEST_FLAG(filter) != "unknown" |
| 47 | || testing::GTEST_FLAG(list_tests) |
| 48 | || testing::GTEST_FLAG(output) != "unknown" |
| 49 | || testing::GTEST_FLAG(print_time) |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 50 | || testing::GTEST_FLAG(random_seed) |
zhanyong.wan | 0ebc16a | 2009-02-02 06:37:03 +0000 | [diff] [blame] | 51 | || testing::GTEST_FLAG(repeat) > 0 |
| 52 | || testing::GTEST_FLAG(show_internal_stack_frames) |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 53 | || testing::GTEST_FLAG(shuffle) |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 54 | || testing::GTEST_FLAG(stack_trace_depth) > 0 |
| 55 | || testing::GTEST_FLAG(throw_on_failure); |
zhanyong.wan | 0ebc16a | 2009-02-02 06:37:03 +0000 | [diff] [blame] | 56 | EXPECT_TRUE(dummy || !dummy); // Suppresses warning that dummy is unused. |
| 57 | } |
| 58 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 59 | #include <gtest/gtest-spi.h> |
| 60 | |
| 61 | // Indicates that this translation unit is part of Google Test's |
| 62 | // implementation. It must come before gtest-internal-inl.h is |
| 63 | // included, or there will be a compiler error. This trick is to |
| 64 | // prevent a user from accidentally including gtest-internal-inl.h in |
| 65 | // his code. |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 66 | #define GTEST_IMPLEMENTATION_ 1 |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 67 | #include "src/gtest-internal-inl.h" |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 68 | #undef GTEST_IMPLEMENTATION_ |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 69 | |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 70 | #include <limits.h> // For INT_MAX. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 71 | #include <stdlib.h> |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 72 | #include <time.h> |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 73 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 74 | #include <map> |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 75 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 76 | namespace testing { |
| 77 | namespace internal { |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 78 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 79 | // Provides access to otherwise private parts of the TestEventListeners class |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 80 | // that are needed to test it. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 81 | class TestEventListenersAccessor { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 82 | public: |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 83 | static TestEventListener* GetRepeater(TestEventListeners* listeners) { |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 84 | return listeners->repeater(); |
| 85 | } |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 86 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 87 | static void SetDefaultResultPrinter(TestEventListeners* listeners, |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 88 | TestEventListener* listener) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 89 | listeners->SetDefaultResultPrinter(listener); |
| 90 | } |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 91 | static void SetDefaultXmlGenerator(TestEventListeners* listeners, |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 92 | TestEventListener* listener) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 93 | listeners->SetDefaultXmlGenerator(listener); |
| 94 | } |
| 95 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 96 | static bool EventForwardingEnabled(const TestEventListeners& listeners) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 97 | return listeners.EventForwardingEnabled(); |
| 98 | } |
| 99 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 100 | static void SuppressEventForwarding(TestEventListeners* listeners) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 101 | listeners->SuppressEventForwarding(); |
| 102 | } |
| 103 | }; |
| 104 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 105 | } // namespace internal |
| 106 | } // namespace testing |
| 107 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 108 | using testing::AssertionFailure; |
| 109 | using testing::AssertionResult; |
| 110 | using testing::AssertionSuccess; |
| 111 | using testing::DoubleLE; |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 112 | using testing::EmptyTestEventListener; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 113 | using testing::FloatLE; |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 114 | using testing::GTEST_FLAG(also_run_disabled_tests); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 115 | using testing::GTEST_FLAG(break_on_failure); |
| 116 | using testing::GTEST_FLAG(catch_exceptions); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 117 | using testing::GTEST_FLAG(color); |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 118 | using testing::GTEST_FLAG(death_test_use_fork); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 119 | using testing::GTEST_FLAG(filter); |
| 120 | using testing::GTEST_FLAG(list_tests); |
| 121 | using testing::GTEST_FLAG(output); |
| 122 | using testing::GTEST_FLAG(print_time); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 123 | using testing::GTEST_FLAG(random_seed); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 124 | using testing::GTEST_FLAG(repeat); |
| 125 | using testing::GTEST_FLAG(show_internal_stack_frames); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 126 | using testing::GTEST_FLAG(shuffle); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 127 | using testing::GTEST_FLAG(stack_trace_depth); |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 128 | using testing::GTEST_FLAG(throw_on_failure); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 129 | using testing::IsNotSubstring; |
| 130 | using testing::IsSubstring; |
| 131 | using testing::Message; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 132 | using testing::ScopedFakeTestPartResultReporter; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 133 | using testing::StaticAssertTypeEq; |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 134 | using testing::Test; |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 135 | using testing::TestCase; |
zhanyong.wan | 678f92b | 2010-05-10 17:11:58 +0000 | [diff] [blame] | 136 | using testing::TestEventListeners; |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 137 | using testing::TestPartResult; |
| 138 | using testing::TestPartResultArray; |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 139 | using testing::TestProperty; |
| 140 | using testing::TestResult; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 141 | using testing::UnitTest; |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 142 | using testing::kMaxStackTraceDepth; |
zhanyong.wan | 678f92b | 2010-05-10 17:11:58 +0000 | [diff] [blame] | 143 | using testing::internal::AddReference; |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 144 | using testing::internal::AlwaysFalse; |
| 145 | using testing::internal::AlwaysTrue; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 146 | using testing::internal::AppendUserMessage; |
zhanyong.wan | 678f92b | 2010-05-10 17:11:58 +0000 | [diff] [blame] | 147 | using testing::internal::ArrayAwareFind; |
| 148 | using testing::internal::ArrayEq; |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 149 | using testing::internal::CodePointToUtf8; |
zhanyong.wan | 678f92b | 2010-05-10 17:11:58 +0000 | [diff] [blame] | 150 | using testing::internal::CompileAssertTypesEqual; |
| 151 | using testing::internal::CopyArray; |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 152 | using testing::internal::CountIf; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 153 | using testing::internal::EqFailure; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 154 | using testing::internal::FloatingPoint; |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 155 | using testing::internal::ForEach; |
zhanyong.wan | 678f92b | 2010-05-10 17:11:58 +0000 | [diff] [blame] | 156 | using testing::internal::FormatTimeInMillisAsSeconds; |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 157 | using testing::internal::GTestFlagSaver; |
vladlosev | f904a61 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 158 | using testing::internal::GetCurrentOsStackTraceExceptTop; |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 159 | using testing::internal::GetElementOr; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 160 | using testing::internal::GetNextRandomSeed; |
| 161 | using testing::internal::GetRandomSeedFromFlag; |
shiqian | fe6a9a4 | 2008-11-24 20:13:22 +0000 | [diff] [blame] | 162 | using testing::internal::GetTestTypeId; |
| 163 | using testing::internal::GetTypeId; |
zhanyong.wan | a80f23f | 2009-06-25 20:49:23 +0000 | [diff] [blame] | 164 | using testing::internal::GetUnitTestImpl; |
zhanyong.wan | 678f92b | 2010-05-10 17:11:58 +0000 | [diff] [blame] | 165 | using testing::internal::ImplicitlyConvertible; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 166 | using testing::internal::Int32; |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 167 | using testing::internal::Int32FromEnvOrDie; |
zhanyong.wan | 678f92b | 2010-05-10 17:11:58 +0000 | [diff] [blame] | 168 | using testing::internal::IsAProtocolMessage; |
| 169 | using testing::internal::IsContainer; |
| 170 | using testing::internal::IsContainerTest; |
| 171 | using testing::internal::IsNotContainer; |
| 172 | using testing::internal::NativeArray; |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 173 | using testing::internal::ParseInt32Flag; |
zhanyong.wan | 678f92b | 2010-05-10 17:11:58 +0000 | [diff] [blame] | 174 | using testing::internal::RemoveConst; |
| 175 | using testing::internal::RemoveReference; |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 176 | using testing::internal::ShouldRunTestOnShard; |
| 177 | using testing::internal::ShouldShard; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 178 | using testing::internal::ShouldUseColor; |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 179 | using testing::internal::Shuffle; |
| 180 | using testing::internal::ShuffleRange; |
zhanyong.wan | 9748de0 | 2010-06-08 22:51:46 +0000 | [diff] [blame] | 181 | using testing::internal::SkipPrefix; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 182 | using testing::internal::StreamableToString; |
| 183 | using testing::internal::String; |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 184 | using testing::internal::TestEventListenersAccessor; |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 185 | using testing::internal::TestResultAccessor; |
zhanyong.wan | 85f555a | 2009-09-21 19:42:03 +0000 | [diff] [blame] | 186 | using testing::internal::UInt32; |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 187 | using testing::internal::WideStringToUtf8; |
zhanyong.wan | 678f92b | 2010-05-10 17:11:58 +0000 | [diff] [blame] | 188 | using testing::internal::kCopy; |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 189 | using testing::internal::kMaxRandomSeed; |
zhanyong.wan | 678f92b | 2010-05-10 17:11:58 +0000 | [diff] [blame] | 190 | using testing::internal::kReference; |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 191 | using testing::internal::kTestTypeIdInGoogleTest; |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 192 | using testing::internal::scoped_ptr; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 193 | |
zhanyong.wan | cf8a584 | 2010-01-28 21:50:29 +0000 | [diff] [blame] | 194 | #if GTEST_HAS_STREAM_REDIRECTION_ |
| 195 | using testing::internal::CaptureStdout; |
| 196 | using testing::internal::GetCapturedStdout; |
| 197 | #endif // GTEST_HAS_STREAM_REDIRECTION_ |
| 198 | |
zhanyong.wan | f6fb532 | 2010-03-04 22:15:53 +0000 | [diff] [blame] | 199 | #if GTEST_IS_THREADSAFE |
| 200 | using testing::internal::ThreadWithParam; |
| 201 | #endif |
| 202 | |
zhanyong.wan | 678f92b | 2010-05-10 17:11:58 +0000 | [diff] [blame] | 203 | #if GTEST_HAS_PROTOBUF_ |
| 204 | using ::testing::internal::TestMessage; |
| 205 | #endif // GTEST_HAS_PROTOBUF_ |
| 206 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 207 | class TestingVector : public std::vector<int> { |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | ::std::ostream& operator<<(::std::ostream& os, |
| 211 | const TestingVector& vector) { |
| 212 | os << "{ "; |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 213 | for (size_t i = 0; i < vector.size(); i++) { |
| 214 | os << vector[i] << " "; |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 215 | } |
| 216 | os << "}"; |
| 217 | return os; |
| 218 | } |
| 219 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 220 | // This line tests that we can define tests in an unnamed namespace. |
| 221 | namespace { |
| 222 | |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 223 | TEST(GetRandomSeedFromFlagTest, HandlesZero) { |
| 224 | const int seed = GetRandomSeedFromFlag(0); |
| 225 | EXPECT_LE(1, seed); |
| 226 | EXPECT_LE(seed, static_cast<int>(kMaxRandomSeed)); |
| 227 | } |
| 228 | |
| 229 | TEST(GetRandomSeedFromFlagTest, PreservesValidSeed) { |
| 230 | EXPECT_EQ(1, GetRandomSeedFromFlag(1)); |
| 231 | EXPECT_EQ(2, GetRandomSeedFromFlag(2)); |
| 232 | EXPECT_EQ(kMaxRandomSeed - 1, GetRandomSeedFromFlag(kMaxRandomSeed - 1)); |
| 233 | EXPECT_EQ(static_cast<int>(kMaxRandomSeed), |
| 234 | GetRandomSeedFromFlag(kMaxRandomSeed)); |
| 235 | } |
| 236 | |
| 237 | TEST(GetRandomSeedFromFlagTest, NormalizesInvalidSeed) { |
| 238 | const int seed1 = GetRandomSeedFromFlag(-1); |
| 239 | EXPECT_LE(1, seed1); |
| 240 | EXPECT_LE(seed1, static_cast<int>(kMaxRandomSeed)); |
| 241 | |
| 242 | const int seed2 = GetRandomSeedFromFlag(kMaxRandomSeed + 1); |
| 243 | EXPECT_LE(1, seed2); |
| 244 | EXPECT_LE(seed2, static_cast<int>(kMaxRandomSeed)); |
| 245 | } |
| 246 | |
| 247 | TEST(GetNextRandomSeedTest, WorksForValidInput) { |
| 248 | EXPECT_EQ(2, GetNextRandomSeed(1)); |
| 249 | EXPECT_EQ(3, GetNextRandomSeed(2)); |
| 250 | EXPECT_EQ(static_cast<int>(kMaxRandomSeed), |
| 251 | GetNextRandomSeed(kMaxRandomSeed - 1)); |
| 252 | EXPECT_EQ(1, GetNextRandomSeed(kMaxRandomSeed)); |
| 253 | |
| 254 | // We deliberately don't test GetNextRandomSeed() with invalid |
| 255 | // inputs, as that requires death tests, which are expensive. This |
| 256 | // is fine as GetNextRandomSeed() is internal and has a |
| 257 | // straightforward definition. |
| 258 | } |
| 259 | |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 260 | static void ClearCurrentTestPartResults() { |
| 261 | TestResultAccessor::ClearTestPartResults( |
| 262 | GetUnitTestImpl()->current_test_result()); |
| 263 | } |
| 264 | |
shiqian | fe6a9a4 | 2008-11-24 20:13:22 +0000 | [diff] [blame] | 265 | // Tests GetTypeId. |
| 266 | |
| 267 | TEST(GetTypeIdTest, ReturnsSameValueForSameType) { |
| 268 | EXPECT_EQ(GetTypeId<int>(), GetTypeId<int>()); |
| 269 | EXPECT_EQ(GetTypeId<Test>(), GetTypeId<Test>()); |
| 270 | } |
| 271 | |
| 272 | class SubClassOfTest : public Test {}; |
| 273 | class AnotherSubClassOfTest : public Test {}; |
| 274 | |
| 275 | TEST(GetTypeIdTest, ReturnsDifferentValuesForDifferentTypes) { |
| 276 | EXPECT_NE(GetTypeId<int>(), GetTypeId<const int>()); |
| 277 | EXPECT_NE(GetTypeId<int>(), GetTypeId<char>()); |
| 278 | EXPECT_NE(GetTypeId<int>(), GetTestTypeId()); |
| 279 | EXPECT_NE(GetTypeId<SubClassOfTest>(), GetTestTypeId()); |
| 280 | EXPECT_NE(GetTypeId<AnotherSubClassOfTest>(), GetTestTypeId()); |
| 281 | EXPECT_NE(GetTypeId<AnotherSubClassOfTest>(), GetTypeId<SubClassOfTest>()); |
| 282 | } |
| 283 | |
| 284 | // Verifies that GetTestTypeId() returns the same value, no matter it |
| 285 | // is called from inside Google Test or outside of it. |
| 286 | TEST(GetTestTypeIdTest, ReturnsTheSameValueInsideOrOutsideOfGoogleTest) { |
| 287 | EXPECT_EQ(kTestTypeIdInGoogleTest, GetTestTypeId()); |
| 288 | } |
| 289 | |
shiqian | f0e809a | 2008-09-26 16:08:30 +0000 | [diff] [blame] | 290 | // Tests FormatTimeInMillisAsSeconds(). |
| 291 | |
| 292 | TEST(FormatTimeInMillisAsSecondsTest, FormatsZero) { |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 293 | EXPECT_EQ("0", FormatTimeInMillisAsSeconds(0)); |
shiqian | f0e809a | 2008-09-26 16:08:30 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | TEST(FormatTimeInMillisAsSecondsTest, FormatsPositiveNumber) { |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 297 | EXPECT_EQ("0.003", FormatTimeInMillisAsSeconds(3)); |
| 298 | EXPECT_EQ("0.01", FormatTimeInMillisAsSeconds(10)); |
| 299 | EXPECT_EQ("0.2", FormatTimeInMillisAsSeconds(200)); |
| 300 | EXPECT_EQ("1.2", FormatTimeInMillisAsSeconds(1200)); |
| 301 | EXPECT_EQ("3", FormatTimeInMillisAsSeconds(3000)); |
shiqian | f0e809a | 2008-09-26 16:08:30 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | TEST(FormatTimeInMillisAsSecondsTest, FormatsNegativeNumber) { |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 305 | EXPECT_EQ("-0.003", FormatTimeInMillisAsSeconds(-3)); |
| 306 | EXPECT_EQ("-0.01", FormatTimeInMillisAsSeconds(-10)); |
| 307 | EXPECT_EQ("-0.2", FormatTimeInMillisAsSeconds(-200)); |
| 308 | EXPECT_EQ("-1.2", FormatTimeInMillisAsSeconds(-1200)); |
| 309 | EXPECT_EQ("-3", FormatTimeInMillisAsSeconds(-3000)); |
shiqian | f0e809a | 2008-09-26 16:08:30 +0000 | [diff] [blame] | 310 | } |
| 311 | |
vladlosev | 5147b45 | 2010-03-17 18:22:59 +0000 | [diff] [blame] | 312 | #if GTEST_CAN_COMPARE_NULL |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 313 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 314 | #ifdef __BORLANDC__ |
| 315 | // Silences warnings: "Condition is always true", "Unreachable code" |
| 316 | #pragma option push -w-ccc -w-rch |
| 317 | #endif |
| 318 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 319 | // 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] | 320 | // pointer literal. |
| 321 | TEST(NullLiteralTest, IsTrueForNullLiterals) { |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 322 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(NULL)); |
| 323 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0)); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 324 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0U)); |
| 325 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(0L)); |
| 326 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(false)); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 327 | #ifndef __BORLANDC__ |
| 328 | // Some compilers may fail to detect some null pointer literals; |
| 329 | // as long as users of the framework don't use such literals, this |
| 330 | // is harmless. |
| 331 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(1 - 1)); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 332 | EXPECT_TRUE(GTEST_IS_NULL_LITERAL_(true && false)); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 333 | #endif |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 334 | } |
| 335 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 336 | // 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] | 337 | // pointer literal. |
| 338 | TEST(NullLiteralTest, IsFalseForNonNullLiterals) { |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 339 | EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(1)); |
| 340 | EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(0.0)); |
| 341 | EXPECT_FALSE(GTEST_IS_NULL_LITERAL_('a')); |
| 342 | EXPECT_FALSE(GTEST_IS_NULL_LITERAL_(static_cast<void*>(NULL))); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 343 | } |
| 344 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 345 | #ifdef __BORLANDC__ |
vladlosev | d6b4941 | 2010-04-07 05:32:34 +0000 | [diff] [blame] | 346 | // Restores warnings after previous "#pragma option push" suppressed them. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 347 | #pragma option pop |
| 348 | #endif |
| 349 | |
vladlosev | 5147b45 | 2010-03-17 18:22:59 +0000 | [diff] [blame] | 350 | #endif // GTEST_CAN_COMPARE_NULL |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 351 | // |
| 352 | // Tests CodePointToUtf8(). |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 353 | |
| 354 | // Tests that the NUL character L'\0' is encoded correctly. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 355 | TEST(CodePointToUtf8Test, CanEncodeNul) { |
| 356 | char buffer[32]; |
| 357 | EXPECT_STREQ("", CodePointToUtf8(L'\0', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // Tests that ASCII characters are encoded correctly. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 361 | TEST(CodePointToUtf8Test, CanEncodeAscii) { |
| 362 | char buffer[32]; |
| 363 | EXPECT_STREQ("a", CodePointToUtf8(L'a', buffer)); |
| 364 | EXPECT_STREQ("Z", CodePointToUtf8(L'Z', buffer)); |
| 365 | EXPECT_STREQ("&", CodePointToUtf8(L'&', buffer)); |
| 366 | EXPECT_STREQ("\x7F", CodePointToUtf8(L'\x7F', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | // Tests that Unicode code-points that have 8 to 11 bits are encoded |
| 370 | // as 110xxxxx 10xxxxxx. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 371 | TEST(CodePointToUtf8Test, CanEncode8To11Bits) { |
| 372 | char buffer[32]; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 373 | // 000 1101 0011 => 110-00011 10-010011 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 374 | EXPECT_STREQ("\xC3\x93", CodePointToUtf8(L'\xD3', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 375 | |
| 376 | // 101 0111 0110 => 110-10101 10-110110 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 377 | EXPECT_STREQ("\xD5\xB6", CodePointToUtf8(L'\x576', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | // Tests that Unicode code-points that have 12 to 16 bits are encoded |
| 381 | // as 1110xxxx 10xxxxxx 10xxxxxx. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 382 | TEST(CodePointToUtf8Test, CanEncode12To16Bits) { |
| 383 | char buffer[32]; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 384 | // 0000 1000 1101 0011 => 1110-0000 10-100011 10-010011 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 385 | EXPECT_STREQ("\xE0\xA3\x93", CodePointToUtf8(L'\x8D3', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 386 | |
| 387 | // 1100 0111 0100 1101 => 1110-1100 10-011101 10-001101 |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 388 | EXPECT_STREQ("\xEC\x9D\x8D", CodePointToUtf8(L'\xC74D', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 389 | } |
| 390 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 391 | #if !GTEST_WIDE_STRING_USES_UTF16_ |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 392 | // 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] | 393 | // are skipped on Windows, Cygwin, and Symbian, where a wchar_t is |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 394 | // 16-bit wide. This code may not compile on those systems. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 395 | |
| 396 | // Tests that Unicode code-points that have 17 to 21 bits are encoded |
| 397 | // as 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 398 | TEST(CodePointToUtf8Test, CanEncode17To21Bits) { |
| 399 | char buffer[32]; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 400 | // 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] | 401 | EXPECT_STREQ("\xF0\x90\xA3\x93", CodePointToUtf8(L'\x108D3', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 402 | |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 403 | // 0 0001 0000 0100 0000 0000 => 11110-000 10-010000 10-010000 10-000000 |
| 404 | EXPECT_STREQ("\xF0\x90\x90\x80", CodePointToUtf8(L'\x10400', buffer)); |
| 405 | |
| 406 | // 1 0000 1000 0110 0011 0100 => 11110-100 10-001000 10-011000 10-110100 |
| 407 | EXPECT_STREQ("\xF4\x88\x98\xB4", CodePointToUtf8(L'\x108634', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | // Tests that encoding an invalid code-point generates the expected result. |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 411 | TEST(CodePointToUtf8Test, CanEncodeInvalidCodePoint) { |
| 412 | char buffer[32]; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 413 | EXPECT_STREQ("(Invalid Unicode 0x1234ABCD)", |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 414 | CodePointToUtf8(L'\x1234ABCD', buffer)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 415 | } |
| 416 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 417 | #endif // !GTEST_WIDE_STRING_USES_UTF16_ |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 418 | |
| 419 | // Tests WideStringToUtf8(). |
| 420 | |
| 421 | // Tests that the NUL character L'\0' is encoded correctly. |
| 422 | TEST(WideStringToUtf8Test, CanEncodeNul) { |
| 423 | EXPECT_STREQ("", WideStringToUtf8(L"", 0).c_str()); |
| 424 | EXPECT_STREQ("", WideStringToUtf8(L"", -1).c_str()); |
| 425 | } |
| 426 | |
| 427 | // Tests that ASCII strings are encoded correctly. |
| 428 | TEST(WideStringToUtf8Test, CanEncodeAscii) { |
| 429 | EXPECT_STREQ("a", WideStringToUtf8(L"a", 1).c_str()); |
| 430 | EXPECT_STREQ("ab", WideStringToUtf8(L"ab", 2).c_str()); |
| 431 | EXPECT_STREQ("a", WideStringToUtf8(L"a", -1).c_str()); |
| 432 | EXPECT_STREQ("ab", WideStringToUtf8(L"ab", -1).c_str()); |
| 433 | } |
| 434 | |
| 435 | // Tests that Unicode code-points that have 8 to 11 bits are encoded |
| 436 | // as 110xxxxx 10xxxxxx. |
| 437 | TEST(WideStringToUtf8Test, CanEncode8To11Bits) { |
| 438 | // 000 1101 0011 => 110-00011 10-010011 |
| 439 | EXPECT_STREQ("\xC3\x93", WideStringToUtf8(L"\xD3", 1).c_str()); |
| 440 | EXPECT_STREQ("\xC3\x93", WideStringToUtf8(L"\xD3", -1).c_str()); |
| 441 | |
| 442 | // 101 0111 0110 => 110-10101 10-110110 |
| 443 | EXPECT_STREQ("\xD5\xB6", WideStringToUtf8(L"\x576", 1).c_str()); |
| 444 | EXPECT_STREQ("\xD5\xB6", WideStringToUtf8(L"\x576", -1).c_str()); |
| 445 | } |
| 446 | |
| 447 | // Tests that Unicode code-points that have 12 to 16 bits are encoded |
| 448 | // as 1110xxxx 10xxxxxx 10xxxxxx. |
| 449 | TEST(WideStringToUtf8Test, CanEncode12To16Bits) { |
| 450 | // 0000 1000 1101 0011 => 1110-0000 10-100011 10-010011 |
| 451 | EXPECT_STREQ("\xE0\xA3\x93", WideStringToUtf8(L"\x8D3", 1).c_str()); |
| 452 | EXPECT_STREQ("\xE0\xA3\x93", WideStringToUtf8(L"\x8D3", -1).c_str()); |
| 453 | |
| 454 | // 1100 0111 0100 1101 => 1110-1100 10-011101 10-001101 |
| 455 | EXPECT_STREQ("\xEC\x9D\x8D", WideStringToUtf8(L"\xC74D", 1).c_str()); |
| 456 | EXPECT_STREQ("\xEC\x9D\x8D", WideStringToUtf8(L"\xC74D", -1).c_str()); |
| 457 | } |
| 458 | |
| 459 | // Tests that the conversion stops when the function encounters \0 character. |
| 460 | TEST(WideStringToUtf8Test, StopsOnNulCharacter) { |
| 461 | EXPECT_STREQ("ABC", WideStringToUtf8(L"ABC\0XYZ", 100).c_str()); |
| 462 | } |
| 463 | |
| 464 | // Tests that the conversion stops when the function reaches the limit |
| 465 | // specified by the 'length' parameter. |
| 466 | TEST(WideStringToUtf8Test, StopsWhenLengthLimitReached) { |
| 467 | EXPECT_STREQ("ABC", WideStringToUtf8(L"ABCDEF", 3).c_str()); |
| 468 | } |
| 469 | |
| 470 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 471 | #if !GTEST_WIDE_STRING_USES_UTF16_ |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 472 | // Tests that Unicode code-points that have 17 to 21 bits are encoded |
| 473 | // as 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx. This code may not compile |
| 474 | // on the systems using UTF-16 encoding. |
| 475 | TEST(WideStringToUtf8Test, CanEncode17To21Bits) { |
| 476 | // 0 0001 0000 1000 1101 0011 => 11110-000 10-010000 10-100011 10-010011 |
| 477 | EXPECT_STREQ("\xF0\x90\xA3\x93", WideStringToUtf8(L"\x108D3", 1).c_str()); |
| 478 | EXPECT_STREQ("\xF0\x90\xA3\x93", WideStringToUtf8(L"\x108D3", -1).c_str()); |
| 479 | |
| 480 | // 1 0000 1000 0110 0011 0100 => 11110-100 10-001000 10-011000 10-110100 |
| 481 | EXPECT_STREQ("\xF4\x88\x98\xB4", WideStringToUtf8(L"\x108634", 1).c_str()); |
| 482 | EXPECT_STREQ("\xF4\x88\x98\xB4", WideStringToUtf8(L"\x108634", -1).c_str()); |
| 483 | } |
| 484 | |
| 485 | // Tests that encoding an invalid code-point generates the expected result. |
| 486 | TEST(WideStringToUtf8Test, CanEncodeInvalidCodePoint) { |
| 487 | EXPECT_STREQ("(Invalid Unicode 0xABCDFF)", |
| 488 | WideStringToUtf8(L"\xABCDFF", -1).c_str()); |
| 489 | } |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 490 | #else // !GTEST_WIDE_STRING_USES_UTF16_ |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 491 | // Tests that surrogate pairs are encoded correctly on the systems using |
| 492 | // UTF-16 encoding in the wide strings. |
| 493 | TEST(WideStringToUtf8Test, CanEncodeValidUtf16SUrrogatePairs) { |
| 494 | EXPECT_STREQ("\xF0\x90\x90\x80", |
| 495 | WideStringToUtf8(L"\xD801\xDC00", -1).c_str()); |
| 496 | } |
| 497 | |
| 498 | // Tests that encoding an invalid UTF-16 surrogate pair |
| 499 | // generates the expected result. |
| 500 | TEST(WideStringToUtf8Test, CanEncodeInvalidUtf16SurrogatePair) { |
| 501 | // Leading surrogate is at the end of the string. |
| 502 | EXPECT_STREQ("\xED\xA0\x80", WideStringToUtf8(L"\xD800", -1).c_str()); |
| 503 | // Leading surrogate is not followed by the trailing surrogate. |
| 504 | EXPECT_STREQ("\xED\xA0\x80$", WideStringToUtf8(L"\xD800$", -1).c_str()); |
| 505 | // Trailing surrogate appearas without a leading surrogate. |
| 506 | EXPECT_STREQ("\xED\xB0\x80PQR", WideStringToUtf8(L"\xDC00PQR", -1).c_str()); |
| 507 | } |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 508 | #endif // !GTEST_WIDE_STRING_USES_UTF16_ |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 509 | |
| 510 | // Tests that codepoint concatenation works correctly. |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 511 | #if !GTEST_WIDE_STRING_USES_UTF16_ |
vladlosev | e006e68 | 2008-08-25 23:11:54 +0000 | [diff] [blame] | 512 | TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) { |
| 513 | EXPECT_STREQ( |
| 514 | "\xF4\x88\x98\xB4" |
| 515 | "\xEC\x9D\x8D" |
| 516 | "\n" |
| 517 | "\xD5\xB6" |
| 518 | "\xE0\xA3\x93" |
| 519 | "\xF4\x88\x98\xB4", |
| 520 | WideStringToUtf8(L"\x108634\xC74D\n\x576\x8D3\x108634", -1).c_str()); |
| 521 | } |
| 522 | #else |
| 523 | TEST(WideStringToUtf8Test, ConcatenatesCodepointsCorrectly) { |
| 524 | EXPECT_STREQ( |
| 525 | "\xEC\x9D\x8D" "\n" "\xD5\xB6" "\xE0\xA3\x93", |
| 526 | WideStringToUtf8(L"\xC74D\n\x576\x8D3", -1).c_str()); |
| 527 | } |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 528 | #endif // !GTEST_WIDE_STRING_USES_UTF16_ |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 529 | |
zhanyong.wan | 85f555a | 2009-09-21 19:42:03 +0000 | [diff] [blame] | 530 | // Tests the Random class. |
| 531 | |
| 532 | TEST(RandomDeathTest, GeneratesCrashesOnInvalidRange) { |
| 533 | testing::internal::Random random(42); |
| 534 | EXPECT_DEATH_IF_SUPPORTED( |
| 535 | random.Generate(0), |
| 536 | "Cannot generate a number in the range \\[0, 0\\)"); |
| 537 | EXPECT_DEATH_IF_SUPPORTED( |
| 538 | random.Generate(testing::internal::Random::kMaxRange + 1), |
| 539 | "Generation of a number in \\[0, 2147483649\\) was requested, " |
| 540 | "but this can only generate numbers in \\[0, 2147483648\\)"); |
| 541 | } |
| 542 | |
| 543 | TEST(RandomTest, GeneratesNumbersWithinRange) { |
| 544 | const UInt32 kRange = 10000; |
| 545 | testing::internal::Random random(12345); |
| 546 | for (int i = 0; i < 10; i++) { |
| 547 | EXPECT_LT(random.Generate(kRange), kRange) << " for iteration " << i; |
| 548 | } |
| 549 | |
| 550 | testing::internal::Random random2(testing::internal::Random::kMaxRange); |
| 551 | for (int i = 0; i < 10; i++) { |
| 552 | EXPECT_LT(random2.Generate(kRange), kRange) << " for iteration " << i; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | TEST(RandomTest, RepeatsWhenReseeded) { |
| 557 | const int kSeed = 123; |
| 558 | const int kArraySize = 10; |
| 559 | const UInt32 kRange = 10000; |
| 560 | UInt32 values[kArraySize]; |
| 561 | |
| 562 | testing::internal::Random random(kSeed); |
| 563 | for (int i = 0; i < kArraySize; i++) { |
| 564 | values[i] = random.Generate(kRange); |
| 565 | } |
| 566 | |
| 567 | random.Reseed(kSeed); |
| 568 | for (int i = 0; i < kArraySize; i++) { |
| 569 | EXPECT_EQ(values[i], random.Generate(kRange)) << " for iteration " << i; |
| 570 | } |
| 571 | } |
| 572 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 573 | // Tests STL container utilities. |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 574 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 575 | // Tests CountIf(). |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 576 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 577 | static bool IsPositive(int n) { return n > 0; } |
| 578 | |
| 579 | TEST(ContainerUtilityTest, CountIf) { |
| 580 | std::vector<int> v; |
| 581 | EXPECT_EQ(0, CountIf(v, IsPositive)); // Works for an empty container. |
| 582 | |
| 583 | v.push_back(-1); |
| 584 | v.push_back(0); |
| 585 | EXPECT_EQ(0, CountIf(v, IsPositive)); // Works when no value satisfies. |
| 586 | |
| 587 | v.push_back(2); |
| 588 | v.push_back(-10); |
| 589 | v.push_back(10); |
| 590 | EXPECT_EQ(2, CountIf(v, IsPositive)); |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 591 | } |
| 592 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 593 | // Tests ForEach(). |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 594 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 595 | static int g_sum = 0; |
| 596 | static void Accumulate(int n) { g_sum += n; } |
| 597 | |
| 598 | TEST(ContainerUtilityTest, ForEach) { |
| 599 | std::vector<int> v; |
| 600 | g_sum = 0; |
| 601 | ForEach(v, Accumulate); |
| 602 | EXPECT_EQ(0, g_sum); // Works for an empty container; |
| 603 | |
| 604 | g_sum = 0; |
| 605 | v.push_back(1); |
| 606 | ForEach(v, Accumulate); |
| 607 | EXPECT_EQ(1, g_sum); // Works for a container with one element. |
| 608 | |
| 609 | g_sum = 0; |
| 610 | v.push_back(20); |
| 611 | v.push_back(300); |
| 612 | ForEach(v, Accumulate); |
| 613 | EXPECT_EQ(321, g_sum); |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 614 | } |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 615 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 616 | // Tests GetElementOr(). |
| 617 | TEST(ContainerUtilityTest, GetElementOr) { |
| 618 | std::vector<char> a; |
| 619 | EXPECT_EQ('x', GetElementOr(a, 0, 'x')); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 620 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 621 | a.push_back('a'); |
| 622 | a.push_back('b'); |
| 623 | EXPECT_EQ('a', GetElementOr(a, 0, 'x')); |
| 624 | EXPECT_EQ('b', GetElementOr(a, 1, 'x')); |
| 625 | EXPECT_EQ('x', GetElementOr(a, -2, 'x')); |
| 626 | EXPECT_EQ('x', GetElementOr(a, 2, 'x')); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 627 | } |
| 628 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 629 | TEST(ContainerUtilityDeathTest, ShuffleRange) { |
| 630 | std::vector<int> a; |
| 631 | a.push_back(0); |
| 632 | a.push_back(1); |
| 633 | a.push_back(2); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 634 | testing::internal::Random random(1); |
| 635 | |
| 636 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 637 | ShuffleRange(&random, -1, 1, &a), |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 638 | "Invalid shuffle range start -1: must be in range \\[0, 3\\]"); |
| 639 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 640 | ShuffleRange(&random, 4, 4, &a), |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 641 | "Invalid shuffle range start 4: must be in range \\[0, 3\\]"); |
| 642 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 643 | ShuffleRange(&random, 3, 2, &a), |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 644 | "Invalid shuffle range finish 2: must be in range \\[3, 3\\]"); |
| 645 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 646 | ShuffleRange(&random, 3, 4, &a), |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 647 | "Invalid shuffle range finish 4: must be in range \\[3, 3\\]"); |
| 648 | } |
| 649 | |
| 650 | class VectorShuffleTest : public Test { |
| 651 | protected: |
| 652 | static const int kVectorSize = 20; |
| 653 | |
| 654 | VectorShuffleTest() : random_(1) { |
| 655 | for (int i = 0; i < kVectorSize; i++) { |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 656 | vector_.push_back(i); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | static bool VectorIsCorrupt(const TestingVector& vector) { |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 661 | if (kVectorSize != static_cast<int>(vector.size())) { |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 662 | return true; |
| 663 | } |
| 664 | |
| 665 | bool found_in_vector[kVectorSize] = { false }; |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 666 | for (size_t i = 0; i < vector.size(); i++) { |
| 667 | const int e = vector[i]; |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 668 | if (e < 0 || e >= kVectorSize || found_in_vector[e]) { |
| 669 | return true; |
| 670 | } |
| 671 | found_in_vector[e] = true; |
| 672 | } |
| 673 | |
| 674 | // Vector size is correct, elements' range is correct, no |
| 675 | // duplicate elements. Therefore no corruption has occurred. |
| 676 | return false; |
| 677 | } |
| 678 | |
| 679 | static bool VectorIsNotCorrupt(const TestingVector& vector) { |
| 680 | return !VectorIsCorrupt(vector); |
| 681 | } |
| 682 | |
| 683 | static bool RangeIsShuffled(const TestingVector& vector, int begin, int end) { |
| 684 | for (int i = begin; i < end; i++) { |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 685 | if (i != vector[i]) { |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 686 | return true; |
| 687 | } |
| 688 | } |
| 689 | return false; |
| 690 | } |
| 691 | |
| 692 | static bool RangeIsUnshuffled( |
| 693 | const TestingVector& vector, int begin, int end) { |
| 694 | return !RangeIsShuffled(vector, begin, end); |
| 695 | } |
| 696 | |
| 697 | static bool VectorIsShuffled(const TestingVector& vector) { |
zhanyong.wan | b03ca47 | 2010-02-25 22:15:27 +0000 | [diff] [blame] | 698 | return RangeIsShuffled(vector, 0, static_cast<int>(vector.size())); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static bool VectorIsUnshuffled(const TestingVector& vector) { |
| 702 | return !VectorIsShuffled(vector); |
| 703 | } |
| 704 | |
| 705 | testing::internal::Random random_; |
| 706 | TestingVector vector_; |
| 707 | }; // class VectorShuffleTest |
| 708 | |
| 709 | const int VectorShuffleTest::kVectorSize; |
| 710 | |
| 711 | TEST_F(VectorShuffleTest, HandlesEmptyRange) { |
| 712 | // Tests an empty range at the beginning... |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 713 | ShuffleRange(&random_, 0, 0, &vector_); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 714 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 715 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 716 | |
| 717 | // ...in the middle... |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 718 | ShuffleRange(&random_, kVectorSize/2, kVectorSize/2, &vector_); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 719 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 720 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 721 | |
| 722 | // ...at the end... |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 723 | ShuffleRange(&random_, kVectorSize - 1, kVectorSize - 1, &vector_); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 724 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 725 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 726 | |
| 727 | // ...and past the end. |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 728 | ShuffleRange(&random_, kVectorSize, kVectorSize, &vector_); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 729 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 730 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 731 | } |
| 732 | |
| 733 | TEST_F(VectorShuffleTest, HandlesRangeOfSizeOne) { |
| 734 | // Tests a size one range at the beginning... |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 735 | ShuffleRange(&random_, 0, 1, &vector_); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 736 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 737 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 738 | |
| 739 | // ...in the middle... |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 740 | ShuffleRange(&random_, kVectorSize/2, kVectorSize/2 + 1, &vector_); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 741 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 742 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 743 | |
| 744 | // ...and at the end. |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 745 | ShuffleRange(&random_, kVectorSize - 1, kVectorSize, &vector_); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 746 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 747 | ASSERT_PRED1(VectorIsUnshuffled, vector_); |
| 748 | } |
| 749 | |
| 750 | // Because we use our own random number generator and a fixed seed, |
| 751 | // we can guarantee that the following "random" tests will succeed. |
| 752 | |
| 753 | TEST_F(VectorShuffleTest, ShufflesEntireVector) { |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 754 | Shuffle(&random_, &vector_); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 755 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 756 | EXPECT_FALSE(VectorIsUnshuffled(vector_)) << vector_; |
| 757 | |
| 758 | // Tests the first and last elements in particular to ensure that |
| 759 | // there are no off-by-one problems in our shuffle algorithm. |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 760 | EXPECT_NE(0, vector_[0]); |
| 761 | EXPECT_NE(kVectorSize - 1, vector_[kVectorSize - 1]); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | TEST_F(VectorShuffleTest, ShufflesStartOfVector) { |
| 765 | const int kRangeSize = kVectorSize/2; |
| 766 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 767 | ShuffleRange(&random_, 0, kRangeSize, &vector_); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 768 | |
| 769 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 770 | EXPECT_PRED3(RangeIsShuffled, vector_, 0, kRangeSize); |
| 771 | EXPECT_PRED3(RangeIsUnshuffled, vector_, kRangeSize, kVectorSize); |
| 772 | } |
| 773 | |
| 774 | TEST_F(VectorShuffleTest, ShufflesEndOfVector) { |
| 775 | const int kRangeSize = kVectorSize / 2; |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 776 | ShuffleRange(&random_, kRangeSize, kVectorSize, &vector_); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 777 | |
| 778 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 779 | EXPECT_PRED3(RangeIsUnshuffled, vector_, 0, kRangeSize); |
| 780 | EXPECT_PRED3(RangeIsShuffled, vector_, kRangeSize, kVectorSize); |
| 781 | } |
| 782 | |
| 783 | TEST_F(VectorShuffleTest, ShufflesMiddleOfVector) { |
| 784 | int kRangeSize = kVectorSize/3; |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 785 | ShuffleRange(&random_, kRangeSize, 2*kRangeSize, &vector_); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 786 | |
| 787 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 788 | EXPECT_PRED3(RangeIsUnshuffled, vector_, 0, kRangeSize); |
| 789 | EXPECT_PRED3(RangeIsShuffled, vector_, kRangeSize, 2*kRangeSize); |
| 790 | EXPECT_PRED3(RangeIsUnshuffled, vector_, 2*kRangeSize, kVectorSize); |
| 791 | } |
| 792 | |
| 793 | TEST_F(VectorShuffleTest, ShufflesRepeatably) { |
| 794 | TestingVector vector2; |
| 795 | for (int i = 0; i < kVectorSize; i++) { |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 796 | vector2.push_back(i); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | random_.Reseed(1234); |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 800 | Shuffle(&random_, &vector_); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 801 | random_.Reseed(1234); |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 802 | Shuffle(&random_, &vector2); |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 803 | |
| 804 | ASSERT_PRED1(VectorIsNotCorrupt, vector_); |
| 805 | ASSERT_PRED1(VectorIsNotCorrupt, vector2); |
| 806 | |
| 807 | for (int i = 0; i < kVectorSize; i++) { |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 808 | EXPECT_EQ(vector_[i], vector2[i]) << " where i is " << i; |
zhanyong.wan | f19450f | 2009-09-30 23:46:28 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 812 | // Tests the size of the AssertHelper class. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 813 | |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 814 | TEST(AssertHelperTest, AssertHelperIsSmall) { |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 815 | // To avoid breaking clients that use lots of assertions in one |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 816 | // function, we cannot grow the size of AssertHelper. |
| 817 | EXPECT_LE(sizeof(testing::internal::AssertHelper), sizeof(void*)); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 818 | } |
| 819 | |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 820 | // Tests the String class. |
| 821 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 822 | // Tests String's constructors. |
| 823 | TEST(StringTest, Constructors) { |
| 824 | // Default ctor. |
| 825 | String s1; |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 826 | // We aren't using EXPECT_EQ(NULL, s1.c_str()) because comparing |
| 827 | // pointers with NULL isn't supported on all platforms. |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 828 | EXPECT_EQ(0U, s1.length()); |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 829 | EXPECT_TRUE(NULL == s1.c_str()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 830 | |
| 831 | // Implicitly constructs from a C-string. |
| 832 | String s2 = "Hi"; |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 833 | EXPECT_EQ(2U, s2.length()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 834 | EXPECT_STREQ("Hi", s2.c_str()); |
| 835 | |
| 836 | // Constructs from a C-string and a length. |
| 837 | String s3("hello", 3); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 838 | EXPECT_EQ(3U, s3.length()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 839 | EXPECT_STREQ("hel", s3.c_str()); |
| 840 | |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 841 | // The empty String should be created when String is constructed with |
| 842 | // a NULL pointer and length 0. |
| 843 | EXPECT_EQ(0U, String(NULL, 0).length()); |
| 844 | EXPECT_FALSE(String(NULL, 0).c_str() == NULL); |
| 845 | |
| 846 | // Constructs a String that contains '\0'. |
| 847 | String s4("a\0bcd", 4); |
| 848 | EXPECT_EQ(4U, s4.length()); |
| 849 | EXPECT_EQ('a', s4.c_str()[0]); |
| 850 | EXPECT_EQ('\0', s4.c_str()[1]); |
| 851 | EXPECT_EQ('b', s4.c_str()[2]); |
| 852 | EXPECT_EQ('c', s4.c_str()[3]); |
| 853 | |
| 854 | // Copy ctor where the source is NULL. |
| 855 | const String null_str; |
| 856 | String s5 = null_str; |
| 857 | EXPECT_TRUE(s5.c_str() == NULL); |
| 858 | |
| 859 | // Copy ctor where the source isn't NULL. |
| 860 | String s6 = s3; |
| 861 | EXPECT_EQ(3U, s6.length()); |
| 862 | EXPECT_STREQ("hel", s6.c_str()); |
| 863 | |
| 864 | // Copy ctor where the source contains '\0'. |
| 865 | String s7 = s4; |
| 866 | EXPECT_EQ(4U, s7.length()); |
| 867 | EXPECT_EQ('a', s7.c_str()[0]); |
| 868 | EXPECT_EQ('\0', s7.c_str()[1]); |
| 869 | EXPECT_EQ('b', s7.c_str()[2]); |
| 870 | EXPECT_EQ('c', s7.c_str()[3]); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 871 | } |
| 872 | |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 873 | TEST(StringTest, ConvertsFromStdString) { |
| 874 | // An empty std::string. |
| 875 | const std::string src1(""); |
| 876 | const String dest1 = src1; |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 877 | EXPECT_EQ(0U, dest1.length()); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 878 | EXPECT_STREQ("", dest1.c_str()); |
| 879 | |
| 880 | // A normal std::string. |
| 881 | const std::string src2("Hi"); |
| 882 | const String dest2 = src2; |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 883 | EXPECT_EQ(2U, dest2.length()); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 884 | EXPECT_STREQ("Hi", dest2.c_str()); |
| 885 | |
| 886 | // An std::string with an embedded NUL character. |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 887 | const char src3[] = "a\0b"; |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 888 | const String dest3 = std::string(src3, sizeof(src3)); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 889 | EXPECT_EQ(sizeof(src3), dest3.length()); |
| 890 | EXPECT_EQ('a', dest3.c_str()[0]); |
| 891 | EXPECT_EQ('\0', dest3.c_str()[1]); |
| 892 | EXPECT_EQ('b', dest3.c_str()[2]); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | TEST(StringTest, ConvertsToStdString) { |
| 896 | // An empty String. |
| 897 | const String src1(""); |
| 898 | const std::string dest1 = src1; |
| 899 | EXPECT_EQ("", dest1); |
| 900 | |
| 901 | // A normal String. |
| 902 | const String src2("Hi"); |
| 903 | const std::string dest2 = src2; |
| 904 | EXPECT_EQ("Hi", dest2); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 905 | |
| 906 | // A String containing a '\0'. |
| 907 | const String src3("x\0y", 3); |
| 908 | const std::string dest3 = src3; |
| 909 | EXPECT_EQ(std::string("x\0y", 3), dest3); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 910 | } |
| 911 | |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 912 | #if GTEST_HAS_GLOBAL_STRING |
| 913 | |
| 914 | TEST(StringTest, ConvertsFromGlobalString) { |
| 915 | // An empty ::string. |
| 916 | const ::string src1(""); |
| 917 | const String dest1 = src1; |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 918 | EXPECT_EQ(0U, dest1.length()); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 919 | EXPECT_STREQ("", dest1.c_str()); |
| 920 | |
| 921 | // A normal ::string. |
| 922 | const ::string src2("Hi"); |
| 923 | const String dest2 = src2; |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 924 | EXPECT_EQ(2U, dest2.length()); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 925 | EXPECT_STREQ("Hi", dest2.c_str()); |
| 926 | |
| 927 | // An ::string with an embedded NUL character. |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 928 | const char src3[] = "x\0y"; |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 929 | const String dest3 = ::string(src3, sizeof(src3)); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 930 | EXPECT_EQ(sizeof(src3), dest3.length()); |
| 931 | EXPECT_EQ('x', dest3.c_str()[0]); |
| 932 | EXPECT_EQ('\0', dest3.c_str()[1]); |
| 933 | EXPECT_EQ('y', dest3.c_str()[2]); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | TEST(StringTest, ConvertsToGlobalString) { |
| 937 | // An empty String. |
| 938 | const String src1(""); |
| 939 | const ::string dest1 = src1; |
| 940 | EXPECT_EQ("", dest1); |
| 941 | |
| 942 | // A normal String. |
| 943 | const String src2("Hi"); |
| 944 | const ::string dest2 = src2; |
| 945 | EXPECT_EQ("Hi", dest2); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 946 | |
| 947 | const String src3("x\0y", 3); |
| 948 | const ::string dest3 = src3; |
| 949 | EXPECT_EQ(::string("x\0y", 3), dest3); |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | #endif // GTEST_HAS_GLOBAL_STRING |
| 953 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 954 | // Tests String::ShowCStringQuoted(). |
| 955 | TEST(StringTest, ShowCStringQuoted) { |
| 956 | EXPECT_STREQ("(null)", |
| 957 | String::ShowCStringQuoted(NULL).c_str()); |
| 958 | EXPECT_STREQ("\"\"", |
| 959 | String::ShowCStringQuoted("").c_str()); |
| 960 | EXPECT_STREQ("\"foo\"", |
| 961 | String::ShowCStringQuoted("foo").c_str()); |
| 962 | } |
| 963 | |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 964 | // Tests String::empty(). |
| 965 | TEST(StringTest, Empty) { |
| 966 | EXPECT_TRUE(String("").empty()); |
| 967 | EXPECT_FALSE(String().empty()); |
| 968 | EXPECT_FALSE(String(NULL).empty()); |
| 969 | EXPECT_FALSE(String("a").empty()); |
| 970 | EXPECT_FALSE(String("\0", 1).empty()); |
| 971 | } |
| 972 | |
| 973 | // Tests String::Compare(). |
| 974 | TEST(StringTest, Compare) { |
| 975 | // NULL vs NULL. |
| 976 | EXPECT_EQ(0, String().Compare(String())); |
| 977 | |
| 978 | // NULL vs non-NULL. |
| 979 | EXPECT_EQ(-1, String().Compare(String(""))); |
| 980 | |
| 981 | // Non-NULL vs NULL. |
| 982 | EXPECT_EQ(1, String("").Compare(String())); |
| 983 | |
| 984 | // The following covers non-NULL vs non-NULL. |
| 985 | |
| 986 | // "" vs "". |
| 987 | EXPECT_EQ(0, String("").Compare(String(""))); |
| 988 | |
| 989 | // "" vs non-"". |
| 990 | EXPECT_EQ(-1, String("").Compare(String("\0", 1))); |
| 991 | EXPECT_EQ(-1, String("").Compare(" ")); |
| 992 | |
| 993 | // Non-"" vs "". |
| 994 | EXPECT_EQ(1, String("a").Compare(String(""))); |
| 995 | |
| 996 | // The following covers non-"" vs non-"". |
| 997 | |
| 998 | // Same length and equal. |
| 999 | EXPECT_EQ(0, String("a").Compare(String("a"))); |
| 1000 | |
| 1001 | // Same length and different. |
| 1002 | EXPECT_EQ(-1, String("a\0b", 3).Compare(String("a\0c", 3))); |
| 1003 | EXPECT_EQ(1, String("b").Compare(String("a"))); |
| 1004 | |
| 1005 | // Different lengths. |
| 1006 | EXPECT_EQ(-1, String("a").Compare(String("ab"))); |
| 1007 | EXPECT_EQ(-1, String("a").Compare(String("a\0", 2))); |
| 1008 | EXPECT_EQ(1, String("abc").Compare(String("aacd"))); |
| 1009 | } |
| 1010 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1011 | // Tests String::operator==(). |
| 1012 | TEST(StringTest, Equals) { |
| 1013 | const String null(NULL); |
| 1014 | EXPECT_TRUE(null == NULL); // NOLINT |
| 1015 | EXPECT_FALSE(null == ""); // NOLINT |
| 1016 | EXPECT_FALSE(null == "bar"); // NOLINT |
| 1017 | |
| 1018 | const String empty(""); |
| 1019 | EXPECT_FALSE(empty == NULL); // NOLINT |
| 1020 | EXPECT_TRUE(empty == ""); // NOLINT |
| 1021 | EXPECT_FALSE(empty == "bar"); // NOLINT |
| 1022 | |
| 1023 | const String foo("foo"); |
| 1024 | EXPECT_FALSE(foo == NULL); // NOLINT |
| 1025 | EXPECT_FALSE(foo == ""); // NOLINT |
| 1026 | EXPECT_FALSE(foo == "bar"); // NOLINT |
| 1027 | EXPECT_TRUE(foo == "foo"); // NOLINT |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1028 | |
| 1029 | const String bar("x\0y", 3); |
| 1030 | EXPECT_FALSE(bar == "x"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | // Tests String::operator!=(). |
| 1034 | TEST(StringTest, NotEquals) { |
| 1035 | const String null(NULL); |
| 1036 | EXPECT_FALSE(null != NULL); // NOLINT |
| 1037 | EXPECT_TRUE(null != ""); // NOLINT |
| 1038 | EXPECT_TRUE(null != "bar"); // NOLINT |
| 1039 | |
| 1040 | const String empty(""); |
| 1041 | EXPECT_TRUE(empty != NULL); // NOLINT |
| 1042 | EXPECT_FALSE(empty != ""); // NOLINT |
| 1043 | EXPECT_TRUE(empty != "bar"); // NOLINT |
| 1044 | |
| 1045 | const String foo("foo"); |
| 1046 | EXPECT_TRUE(foo != NULL); // NOLINT |
| 1047 | EXPECT_TRUE(foo != ""); // NOLINT |
| 1048 | EXPECT_TRUE(foo != "bar"); // NOLINT |
| 1049 | EXPECT_FALSE(foo != "foo"); // NOLINT |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1050 | |
| 1051 | const String bar("x\0y", 3); |
| 1052 | EXPECT_TRUE(bar != "x"); |
| 1053 | } |
| 1054 | |
| 1055 | // Tests String::length(). |
| 1056 | TEST(StringTest, Length) { |
| 1057 | EXPECT_EQ(0U, String().length()); |
| 1058 | EXPECT_EQ(0U, String("").length()); |
| 1059 | EXPECT_EQ(2U, String("ab").length()); |
| 1060 | EXPECT_EQ(3U, String("a\0b", 3).length()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | // Tests String::EndsWith(). |
| 1064 | TEST(StringTest, EndsWith) { |
| 1065 | EXPECT_TRUE(String("foobar").EndsWith("bar")); |
| 1066 | EXPECT_TRUE(String("foobar").EndsWith("")); |
| 1067 | EXPECT_TRUE(String("").EndsWith("")); |
| 1068 | |
| 1069 | EXPECT_FALSE(String("foobar").EndsWith("foo")); |
| 1070 | EXPECT_FALSE(String("").EndsWith("foo")); |
| 1071 | } |
| 1072 | |
| 1073 | // Tests String::EndsWithCaseInsensitive(). |
| 1074 | TEST(StringTest, EndsWithCaseInsensitive) { |
| 1075 | EXPECT_TRUE(String("foobar").EndsWithCaseInsensitive("BAR")); |
| 1076 | EXPECT_TRUE(String("foobaR").EndsWithCaseInsensitive("bar")); |
| 1077 | EXPECT_TRUE(String("foobar").EndsWithCaseInsensitive("")); |
| 1078 | EXPECT_TRUE(String("").EndsWithCaseInsensitive("")); |
| 1079 | |
| 1080 | EXPECT_FALSE(String("Foobar").EndsWithCaseInsensitive("foo")); |
| 1081 | EXPECT_FALSE(String("foobar").EndsWithCaseInsensitive("Foo")); |
| 1082 | EXPECT_FALSE(String("").EndsWithCaseInsensitive("foo")); |
| 1083 | } |
| 1084 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1085 | // C++Builder's preprocessor is buggy; it fails to expand macros that |
| 1086 | // appear in macro parameters after wide char literals. Provide an alias |
| 1087 | // for NULL as a workaround. |
| 1088 | static const wchar_t* const kNull = NULL; |
| 1089 | |
shiqian | e8ff148 | 2008-09-08 17:55:52 +0000 | [diff] [blame] | 1090 | // Tests String::CaseInsensitiveWideCStringEquals |
| 1091 | TEST(StringTest, CaseInsensitiveWideCStringEquals) { |
| 1092 | EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(NULL, NULL)); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1093 | EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(kNull, L"")); |
| 1094 | EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(L"", kNull)); |
| 1095 | EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(kNull, L"foobar")); |
| 1096 | EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(L"foobar", kNull)); |
shiqian | e8ff148 | 2008-09-08 17:55:52 +0000 | [diff] [blame] | 1097 | EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"foobar", L"foobar")); |
| 1098 | EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"foobar", L"FOOBAR")); |
| 1099 | EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"FOOBAR", L"foobar")); |
| 1100 | } |
| 1101 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1102 | // Tests that NULL can be assigned to a String. |
| 1103 | TEST(StringTest, CanBeAssignedNULL) { |
| 1104 | const String src(NULL); |
| 1105 | String dest; |
| 1106 | |
| 1107 | dest = src; |
| 1108 | EXPECT_STREQ(NULL, dest.c_str()); |
| 1109 | } |
| 1110 | |
| 1111 | // Tests that the empty string "" can be assigned to a String. |
| 1112 | TEST(StringTest, CanBeAssignedEmpty) { |
| 1113 | const String src(""); |
| 1114 | String dest; |
| 1115 | |
| 1116 | dest = src; |
| 1117 | EXPECT_STREQ("", dest.c_str()); |
| 1118 | } |
| 1119 | |
| 1120 | // Tests that a non-empty string can be assigned to a String. |
| 1121 | TEST(StringTest, CanBeAssignedNonEmpty) { |
| 1122 | const String src("hello"); |
| 1123 | String dest; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1124 | dest = src; |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1125 | EXPECT_EQ(5U, dest.length()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1126 | EXPECT_STREQ("hello", dest.c_str()); |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1127 | |
| 1128 | const String src2("x\0y", 3); |
| 1129 | String dest2; |
| 1130 | dest2 = src2; |
| 1131 | EXPECT_EQ(3U, dest2.length()); |
| 1132 | EXPECT_EQ('x', dest2.c_str()[0]); |
| 1133 | EXPECT_EQ('\0', dest2.c_str()[1]); |
| 1134 | EXPECT_EQ('y', dest2.c_str()[2]); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | // Tests that a String can be assigned to itself. |
| 1138 | TEST(StringTest, CanBeAssignedSelf) { |
| 1139 | String dest("hello"); |
| 1140 | |
| 1141 | dest = dest; |
| 1142 | EXPECT_STREQ("hello", dest.c_str()); |
| 1143 | } |
| 1144 | |
vladlosev | 673a0cb | 2010-02-03 02:27:02 +0000 | [diff] [blame] | 1145 | // Sun Studio < 12 incorrectly rejects this code due to an overloading |
| 1146 | // ambiguity. |
| 1147 | #if !(defined(__SUNPRO_CC) && __SUNPRO_CC < 0x590) |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1148 | // Tests streaming a String. |
| 1149 | TEST(StringTest, Streams) { |
| 1150 | EXPECT_EQ(StreamableToString(String()), "(null)"); |
| 1151 | EXPECT_EQ(StreamableToString(String("")), ""); |
| 1152 | EXPECT_EQ(StreamableToString(String("a\0b", 3)), "a\\0b"); |
| 1153 | } |
vladlosev | 673a0cb | 2010-02-03 02:27:02 +0000 | [diff] [blame] | 1154 | #endif |
zhanyong.wan | 89be576 | 2009-09-01 18:53:56 +0000 | [diff] [blame] | 1155 | |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 1156 | // Tests that String::Format() works. |
| 1157 | TEST(StringTest, FormatWorks) { |
| 1158 | // Normal case: the format spec is valid, the arguments match the |
| 1159 | // spec, and the result is < 4095 characters. |
| 1160 | EXPECT_STREQ("Hello, 42", String::Format("%s, %d", "Hello", 42).c_str()); |
| 1161 | |
| 1162 | // Edge case: the result is 4095 characters. |
| 1163 | char buffer[4096]; |
| 1164 | const size_t kSize = sizeof(buffer); |
| 1165 | memset(buffer, 'a', kSize - 1); |
| 1166 | buffer[kSize - 1] = '\0'; |
| 1167 | EXPECT_STREQ(buffer, String::Format("%s", buffer).c_str()); |
| 1168 | |
| 1169 | // The result needs to be 4096 characters, exceeding Format()'s limit. |
| 1170 | EXPECT_STREQ("<formatting error or buffer exceeded>", |
| 1171 | String::Format("x%s", buffer).c_str()); |
| 1172 | |
| 1173 | #if GTEST_OS_LINUX |
| 1174 | // On Linux, invalid format spec should lead to an error message. |
| 1175 | // In other environment (e.g. MSVC on Windows), String::Format() may |
| 1176 | // simply ignore a bad format spec, so this assertion is run on |
| 1177 | // Linux only. |
| 1178 | EXPECT_STREQ("<formatting error or buffer exceeded>", |
| 1179 | String::Format("%").c_str()); |
| 1180 | #endif |
| 1181 | } |
| 1182 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1183 | #if GTEST_OS_WINDOWS |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1184 | |
| 1185 | // Tests String::ShowWideCString(). |
| 1186 | TEST(StringTest, ShowWideCString) { |
| 1187 | EXPECT_STREQ("(null)", |
| 1188 | String::ShowWideCString(NULL).c_str()); |
| 1189 | EXPECT_STREQ("", String::ShowWideCString(L"").c_str()); |
| 1190 | EXPECT_STREQ("foo", String::ShowWideCString(L"foo").c_str()); |
| 1191 | } |
| 1192 | |
| 1193 | // Tests String::ShowWideCStringQuoted(). |
| 1194 | TEST(StringTest, ShowWideCStringQuoted) { |
| 1195 | EXPECT_STREQ("(null)", |
| 1196 | String::ShowWideCStringQuoted(NULL).c_str()); |
| 1197 | EXPECT_STREQ("L\"\"", |
| 1198 | String::ShowWideCStringQuoted(L"").c_str()); |
| 1199 | EXPECT_STREQ("L\"foo\"", |
| 1200 | String::ShowWideCStringQuoted(L"foo").c_str()); |
| 1201 | } |
| 1202 | |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 1203 | #if GTEST_OS_WINDOWS_MOBILE |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 1204 | TEST(StringTest, AnsiAndUtf16Null) { |
| 1205 | EXPECT_EQ(NULL, String::AnsiToUtf16(NULL)); |
| 1206 | EXPECT_EQ(NULL, String::Utf16ToAnsi(NULL)); |
| 1207 | } |
| 1208 | |
| 1209 | TEST(StringTest, AnsiAndUtf16ConvertBasic) { |
| 1210 | const char* ansi = String::Utf16ToAnsi(L"str"); |
| 1211 | EXPECT_STREQ("str", ansi); |
| 1212 | delete [] ansi; |
| 1213 | const WCHAR* utf16 = String::AnsiToUtf16("str"); |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 1214 | EXPECT_EQ(0, wcsncmp(L"str", utf16, 3)); |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 1215 | delete [] utf16; |
| 1216 | } |
| 1217 | |
| 1218 | TEST(StringTest, AnsiAndUtf16ConvertPathChars) { |
| 1219 | const char* ansi = String::Utf16ToAnsi(L".:\\ \"*?"); |
| 1220 | EXPECT_STREQ(".:\\ \"*?", ansi); |
| 1221 | delete [] ansi; |
| 1222 | const WCHAR* utf16 = String::AnsiToUtf16(".:\\ \"*?"); |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 1223 | EXPECT_EQ(0, wcsncmp(L".:\\ \"*?", utf16, 3)); |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 1224 | delete [] utf16; |
| 1225 | } |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 1226 | #endif // GTEST_OS_WINDOWS_MOBILE |
shiqian | dd4a17b | 2008-07-31 18:34:08 +0000 | [diff] [blame] | 1227 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1228 | #endif // GTEST_OS_WINDOWS |
| 1229 | |
| 1230 | // Tests TestProperty construction. |
| 1231 | TEST(TestPropertyTest, StringValue) { |
| 1232 | TestProperty property("key", "1"); |
| 1233 | EXPECT_STREQ("key", property.key()); |
| 1234 | EXPECT_STREQ("1", property.value()); |
| 1235 | } |
| 1236 | |
| 1237 | // Tests TestProperty replacing a value. |
| 1238 | TEST(TestPropertyTest, ReplaceStringValue) { |
| 1239 | TestProperty property("key", "1"); |
| 1240 | EXPECT_STREQ("1", property.value()); |
| 1241 | property.SetValue("2"); |
| 1242 | EXPECT_STREQ("2", property.value()); |
| 1243 | } |
| 1244 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1245 | // AddFatalFailure() and AddNonfatalFailure() must be stand-alone |
| 1246 | // functions (i.e. their definitions cannot be inlined at the call |
| 1247 | // sites), or C++Builder won't compile the code. |
| 1248 | static void AddFatalFailure() { |
| 1249 | FAIL() << "Expected fatal failure."; |
| 1250 | } |
| 1251 | |
| 1252 | static void AddNonfatalFailure() { |
| 1253 | ADD_FAILURE() << "Expected non-fatal failure."; |
| 1254 | } |
| 1255 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1256 | class ScopedFakeTestPartResultReporterTest : public Test { |
tsunanet | acd0f32 | 2009-05-18 20:53:57 +0000 | [diff] [blame] | 1257 | 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] | 1258 | enum FailureMode { |
| 1259 | FATAL_FAILURE, |
| 1260 | NONFATAL_FAILURE |
| 1261 | }; |
| 1262 | static void AddFailure(FailureMode failure) { |
| 1263 | if (failure == FATAL_FAILURE) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1264 | AddFatalFailure(); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1265 | } else { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1266 | AddNonfatalFailure(); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1269 | }; |
| 1270 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1271 | // Tests that ScopedFakeTestPartResultReporter intercepts test |
| 1272 | // failures. |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1273 | TEST_F(ScopedFakeTestPartResultReporterTest, InterceptsTestFailures) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1274 | TestPartResultArray results; |
| 1275 | { |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1276 | ScopedFakeTestPartResultReporter reporter( |
| 1277 | ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD, |
| 1278 | &results); |
| 1279 | AddFailure(NONFATAL_FAILURE); |
| 1280 | AddFailure(FATAL_FAILURE); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | EXPECT_EQ(2, results.size()); |
| 1284 | EXPECT_TRUE(results.GetTestPartResult(0).nonfatally_failed()); |
| 1285 | EXPECT_TRUE(results.GetTestPartResult(1).fatally_failed()); |
| 1286 | } |
| 1287 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1288 | TEST_F(ScopedFakeTestPartResultReporterTest, DeprecatedConstructor) { |
| 1289 | TestPartResultArray results; |
| 1290 | { |
| 1291 | // Tests, that the deprecated constructor still works. |
| 1292 | ScopedFakeTestPartResultReporter reporter(&results); |
| 1293 | AddFailure(NONFATAL_FAILURE); |
| 1294 | } |
| 1295 | EXPECT_EQ(1, results.size()); |
| 1296 | } |
| 1297 | |
zhanyong.wan | f6fb532 | 2010-03-04 22:15:53 +0000 | [diff] [blame] | 1298 | #if GTEST_IS_THREADSAFE |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1299 | |
| 1300 | class ScopedFakeTestPartResultReporterWithThreadsTest |
| 1301 | : public ScopedFakeTestPartResultReporterTest { |
| 1302 | protected: |
| 1303 | static void AddFailureInOtherThread(FailureMode failure) { |
zhanyong.wan | f6fb532 | 2010-03-04 22:15:53 +0000 | [diff] [blame] | 1304 | ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL); |
| 1305 | thread.Join(); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1306 | } |
| 1307 | }; |
| 1308 | |
| 1309 | TEST_F(ScopedFakeTestPartResultReporterWithThreadsTest, |
| 1310 | InterceptsTestFailuresInAllThreads) { |
| 1311 | TestPartResultArray results; |
| 1312 | { |
| 1313 | ScopedFakeTestPartResultReporter reporter( |
| 1314 | ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, &results); |
| 1315 | AddFailure(NONFATAL_FAILURE); |
| 1316 | AddFailure(FATAL_FAILURE); |
| 1317 | AddFailureInOtherThread(NONFATAL_FAILURE); |
| 1318 | AddFailureInOtherThread(FATAL_FAILURE); |
| 1319 | } |
| 1320 | |
| 1321 | EXPECT_EQ(4, results.size()); |
| 1322 | EXPECT_TRUE(results.GetTestPartResult(0).nonfatally_failed()); |
| 1323 | EXPECT_TRUE(results.GetTestPartResult(1).fatally_failed()); |
| 1324 | EXPECT_TRUE(results.GetTestPartResult(2).nonfatally_failed()); |
| 1325 | EXPECT_TRUE(results.GetTestPartResult(3).fatally_failed()); |
| 1326 | } |
| 1327 | |
zhanyong.wan | f6fb532 | 2010-03-04 22:15:53 +0000 | [diff] [blame] | 1328 | #endif // GTEST_IS_THREADSAFE |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1329 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1330 | // Tests EXPECT_FATAL_FAILURE{,ON_ALL_THREADS}. Makes sure that they |
| 1331 | // work even if the failure is generated in a called function rather than |
| 1332 | // the current context. |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1333 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1334 | typedef ScopedFakeTestPartResultReporterTest ExpectFatalFailureTest; |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1335 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1336 | TEST_F(ExpectFatalFailureTest, CatchesFatalFaliure) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1337 | EXPECT_FATAL_FAILURE(AddFatalFailure(), "Expected fatal failure."); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1340 | TEST_F(ExpectFatalFailureTest, CatchesFatalFailureOnAllThreads) { |
| 1341 | // We have another test below to verify that the macro catches fatal |
| 1342 | // failures generated on another thread. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1343 | EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFatalFailure(), |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1344 | "Expected fatal failure."); |
| 1345 | } |
| 1346 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1347 | #ifdef __BORLANDC__ |
| 1348 | // Silences warnings: "Condition is always true" |
| 1349 | #pragma option push -w-ccc |
| 1350 | #endif |
| 1351 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1352 | // Tests that EXPECT_FATAL_FAILURE() can be used in a non-void |
| 1353 | // function even when the statement in it contains ASSERT_*. |
| 1354 | |
| 1355 | int NonVoidFunction() { |
| 1356 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(false), ""); |
| 1357 | EXPECT_FATAL_FAILURE_ON_ALL_THREADS(FAIL(), ""); |
| 1358 | return 0; |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1361 | TEST_F(ExpectFatalFailureTest, CanBeUsedInNonVoidFunction) { |
| 1362 | NonVoidFunction(); |
| 1363 | } |
| 1364 | |
| 1365 | // Tests that EXPECT_FATAL_FAILURE(statement, ...) doesn't abort the |
| 1366 | // current function even though 'statement' generates a fatal failure. |
| 1367 | |
| 1368 | void DoesNotAbortHelper(bool* aborted) { |
| 1369 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(false), ""); |
| 1370 | EXPECT_FATAL_FAILURE_ON_ALL_THREADS(FAIL(), ""); |
| 1371 | |
| 1372 | *aborted = false; |
| 1373 | } |
| 1374 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1375 | #ifdef __BORLANDC__ |
vladlosev | d6b4941 | 2010-04-07 05:32:34 +0000 | [diff] [blame] | 1376 | // Restores warnings after previous "#pragma option push" suppressed them. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1377 | #pragma option pop |
| 1378 | #endif |
| 1379 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1380 | TEST_F(ExpectFatalFailureTest, DoesNotAbort) { |
| 1381 | bool aborted = true; |
| 1382 | DoesNotAbortHelper(&aborted); |
| 1383 | EXPECT_FALSE(aborted); |
| 1384 | } |
| 1385 | |
| 1386 | // Tests that the EXPECT_FATAL_FAILURE{,_ON_ALL_THREADS} accepts a |
| 1387 | // statement that contains a macro which expands to code containing an |
| 1388 | // unprotected comma. |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1389 | |
| 1390 | static int global_var = 0; |
| 1391 | #define GTEST_USE_UNPROTECTED_COMMA_ global_var++, global_var++ |
| 1392 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1393 | TEST_F(ExpectFatalFailureTest, AcceptsMacroThatExpandsToUnprotectedComma) { |
vladlosev | d6b4941 | 2010-04-07 05:32:34 +0000 | [diff] [blame] | 1394 | #if !defined(__BORLANDC__) || __BORLANDC__ >= 0x600 |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1395 | // ICE's in C++Builder 2007. |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1396 | EXPECT_FATAL_FAILURE({ |
| 1397 | GTEST_USE_UNPROTECTED_COMMA_; |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1398 | AddFatalFailure(); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1399 | }, ""); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1400 | #endif |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1401 | |
| 1402 | EXPECT_FATAL_FAILURE_ON_ALL_THREADS({ |
| 1403 | GTEST_USE_UNPROTECTED_COMMA_; |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1404 | AddFatalFailure(); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1405 | }, ""); |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1406 | } |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1407 | |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1408 | // Tests EXPECT_NONFATAL_FAILURE{,ON_ALL_THREADS}. |
| 1409 | |
| 1410 | typedef ScopedFakeTestPartResultReporterTest ExpectNonfatalFailureTest; |
| 1411 | |
| 1412 | TEST_F(ExpectNonfatalFailureTest, CatchesNonfatalFailure) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1413 | EXPECT_NONFATAL_FAILURE(AddNonfatalFailure(), |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1414 | "Expected non-fatal failure."); |
| 1415 | } |
| 1416 | |
| 1417 | TEST_F(ExpectNonfatalFailureTest, CatchesNonfatalFailureOnAllThreads) { |
| 1418 | // We have another test below to verify that the macro catches |
| 1419 | // non-fatal failures generated on another thread. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1420 | EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddNonfatalFailure(), |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 1421 | "Expected non-fatal failure."); |
| 1422 | } |
| 1423 | |
| 1424 | // Tests that the EXPECT_NONFATAL_FAILURE{,_ON_ALL_THREADS} accepts a |
| 1425 | // statement that contains a macro which expands to code containing an |
| 1426 | // unprotected comma. |
| 1427 | TEST_F(ExpectNonfatalFailureTest, AcceptsMacroThatExpandsToUnprotectedComma) { |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1428 | EXPECT_NONFATAL_FAILURE({ |
| 1429 | GTEST_USE_UNPROTECTED_COMMA_; |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1430 | AddNonfatalFailure(); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1431 | }, ""); |
| 1432 | |
| 1433 | EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS({ |
| 1434 | GTEST_USE_UNPROTECTED_COMMA_; |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1435 | AddNonfatalFailure(); |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1436 | }, ""); |
| 1437 | } |
| 1438 | |
zhanyong.wan | f6fb532 | 2010-03-04 22:15:53 +0000 | [diff] [blame] | 1439 | #if GTEST_IS_THREADSAFE |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1440 | |
| 1441 | typedef ScopedFakeTestPartResultReporterWithThreadsTest |
| 1442 | ExpectFailureWithThreadsTest; |
| 1443 | |
| 1444 | TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailureOnAllThreads) { |
| 1445 | EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailureInOtherThread(FATAL_FAILURE), |
| 1446 | "Expected fatal failure."); |
| 1447 | } |
| 1448 | |
| 1449 | TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailureOnAllThreads) { |
| 1450 | EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS( |
| 1451 | AddFailureInOtherThread(NONFATAL_FAILURE), "Expected non-fatal failure."); |
| 1452 | } |
| 1453 | |
zhanyong.wan | f6fb532 | 2010-03-04 22:15:53 +0000 | [diff] [blame] | 1454 | #endif // GTEST_IS_THREADSAFE |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 1455 | |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 1456 | // Tests the TestProperty class. |
| 1457 | |
| 1458 | TEST(TestPropertyTest, ConstructorWorks) { |
| 1459 | const TestProperty property("key", "value"); |
| 1460 | EXPECT_STREQ("key", property.key()); |
| 1461 | EXPECT_STREQ("value", property.value()); |
| 1462 | } |
| 1463 | |
| 1464 | TEST(TestPropertyTest, SetValue) { |
| 1465 | TestProperty property("key", "value_1"); |
| 1466 | EXPECT_STREQ("key", property.key()); |
| 1467 | property.SetValue("value_2"); |
| 1468 | EXPECT_STREQ("key", property.key()); |
| 1469 | EXPECT_STREQ("value_2", property.value()); |
| 1470 | } |
| 1471 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1472 | // Tests the TestResult class |
| 1473 | |
| 1474 | // The test fixture for testing TestResult. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1475 | class TestResultTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1476 | protected: |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 1477 | typedef std::vector<TestPartResult> TPRVector; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1478 | |
| 1479 | // We make use of 2 TestPartResult objects, |
| 1480 | TestPartResult * pr1, * pr2; |
| 1481 | |
| 1482 | // ... and 3 TestResult objects. |
| 1483 | TestResult * r0, * r1, * r2; |
| 1484 | |
| 1485 | virtual void SetUp() { |
| 1486 | // pr1 is for success. |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 1487 | pr1 = new TestPartResult(TestPartResult::kSuccess, |
| 1488 | "foo/bar.cc", |
| 1489 | 10, |
| 1490 | "Success!"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1491 | |
| 1492 | // pr2 is for fatal failure. |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 1493 | pr2 = new TestPartResult(TestPartResult::kFatalFailure, |
| 1494 | "foo/bar.cc", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1495 | -1, // This line number means "unknown" |
| 1496 | "Failure!"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1497 | |
| 1498 | // Creates the TestResult objects. |
| 1499 | r0 = new TestResult(); |
| 1500 | r1 = new TestResult(); |
| 1501 | r2 = new TestResult(); |
| 1502 | |
| 1503 | // In order to test TestResult, we need to modify its internal |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 1504 | // state, in particular the TestPartResult vector it holds. |
| 1505 | // test_part_results() returns a const reference to this vector. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1506 | // We cast it to a non-const object s.t. it can be modified (yes, |
| 1507 | // this is a hack). |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 1508 | TPRVector* results1 = const_cast<TPRVector*>( |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1509 | &TestResultAccessor::test_part_results(*r1)); |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 1510 | TPRVector* results2 = const_cast<TPRVector*>( |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1511 | &TestResultAccessor::test_part_results(*r2)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1512 | |
| 1513 | // r0 is an empty TestResult. |
| 1514 | |
| 1515 | // r1 contains a single SUCCESS TestPartResult. |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 1516 | results1->push_back(*pr1); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1517 | |
| 1518 | // r2 contains a SUCCESS, and a FAILURE. |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 1519 | results2->push_back(*pr1); |
| 1520 | results2->push_back(*pr2); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | virtual void TearDown() { |
| 1524 | delete pr1; |
| 1525 | delete pr2; |
| 1526 | |
| 1527 | delete r0; |
| 1528 | delete r1; |
| 1529 | delete r2; |
| 1530 | } |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1531 | |
| 1532 | // Helper that compares two two TestPartResults. |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1533 | static void CompareTestPartResult(const TestPartResult& expected, |
| 1534 | const TestPartResult& actual) { |
| 1535 | EXPECT_EQ(expected.type(), actual.type()); |
| 1536 | EXPECT_STREQ(expected.file_name(), actual.file_name()); |
| 1537 | EXPECT_EQ(expected.line_number(), actual.line_number()); |
| 1538 | EXPECT_STREQ(expected.summary(), actual.summary()); |
| 1539 | EXPECT_STREQ(expected.message(), actual.message()); |
| 1540 | EXPECT_EQ(expected.passed(), actual.passed()); |
| 1541 | EXPECT_EQ(expected.failed(), actual.failed()); |
| 1542 | EXPECT_EQ(expected.nonfatally_failed(), actual.nonfatally_failed()); |
| 1543 | EXPECT_EQ(expected.fatally_failed(), actual.fatally_failed()); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1544 | } |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1545 | }; |
| 1546 | |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1547 | // Tests TestResult::total_part_count(). |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1548 | TEST_F(TestResultTest, total_part_count) { |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1549 | ASSERT_EQ(0, r0->total_part_count()); |
| 1550 | ASSERT_EQ(1, r1->total_part_count()); |
| 1551 | ASSERT_EQ(2, r2->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1554 | // Tests TestResult::Passed(). |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1555 | TEST_F(TestResultTest, Passed) { |
| 1556 | ASSERT_TRUE(r0->Passed()); |
| 1557 | ASSERT_TRUE(r1->Passed()); |
| 1558 | ASSERT_FALSE(r2->Passed()); |
| 1559 | } |
| 1560 | |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1561 | // Tests TestResult::Failed(). |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1562 | TEST_F(TestResultTest, Failed) { |
| 1563 | ASSERT_FALSE(r0->Failed()); |
| 1564 | ASSERT_FALSE(r1->Failed()); |
| 1565 | ASSERT_TRUE(r2->Failed()); |
| 1566 | } |
| 1567 | |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1568 | // Tests TestResult::GetTestPartResult(). |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1569 | |
| 1570 | typedef TestResultTest TestResultDeathTest; |
| 1571 | |
| 1572 | TEST_F(TestResultDeathTest, GetTestPartResult) { |
| 1573 | CompareTestPartResult(*pr1, r2->GetTestPartResult(0)); |
| 1574 | CompareTestPartResult(*pr2, r2->GetTestPartResult(1)); |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 1575 | EXPECT_DEATH_IF_SUPPORTED(r2->GetTestPartResult(2), ""); |
| 1576 | EXPECT_DEATH_IF_SUPPORTED(r2->GetTestPartResult(-1), ""); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1579 | // Tests TestResult has no properties when none are added. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1580 | TEST(TestResultPropertyTest, NoPropertiesFoundWhenNoneAreAdded) { |
| 1581 | TestResult test_result; |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1582 | ASSERT_EQ(0, test_result.test_property_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1585 | // Tests TestResult has the expected property when added. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1586 | TEST(TestResultPropertyTest, OnePropertyFoundWhenAdded) { |
| 1587 | TestResult test_result; |
| 1588 | TestProperty property("key_1", "1"); |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1589 | TestResultAccessor::RecordProperty(&test_result, property); |
| 1590 | ASSERT_EQ(1, test_result.test_property_count()); |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1591 | const TestProperty& actual_property = test_result.GetTestProperty(0); |
| 1592 | EXPECT_STREQ("key_1", actual_property.key()); |
| 1593 | EXPECT_STREQ("1", actual_property.value()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1596 | // Tests TestResult has multiple properties when added. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1597 | TEST(TestResultPropertyTest, MultiplePropertiesFoundWhenAdded) { |
| 1598 | TestResult test_result; |
| 1599 | TestProperty property_1("key_1", "1"); |
| 1600 | TestProperty property_2("key_2", "2"); |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1601 | TestResultAccessor::RecordProperty(&test_result, property_1); |
| 1602 | TestResultAccessor::RecordProperty(&test_result, property_2); |
| 1603 | ASSERT_EQ(2, test_result.test_property_count()); |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1604 | const TestProperty& actual_property_1 = test_result.GetTestProperty(0); |
| 1605 | EXPECT_STREQ("key_1", actual_property_1.key()); |
| 1606 | EXPECT_STREQ("1", actual_property_1.value()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1607 | |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1608 | const TestProperty& actual_property_2 = test_result.GetTestProperty(1); |
| 1609 | EXPECT_STREQ("key_2", actual_property_2.key()); |
| 1610 | EXPECT_STREQ("2", actual_property_2.value()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1613 | // Tests TestResult::RecordProperty() overrides values for duplicate keys. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1614 | TEST(TestResultPropertyTest, OverridesValuesForDuplicateKeys) { |
| 1615 | TestResult test_result; |
| 1616 | TestProperty property_1_1("key_1", "1"); |
| 1617 | TestProperty property_2_1("key_2", "2"); |
| 1618 | TestProperty property_1_2("key_1", "12"); |
| 1619 | TestProperty property_2_2("key_2", "22"); |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1620 | TestResultAccessor::RecordProperty(&test_result, property_1_1); |
| 1621 | TestResultAccessor::RecordProperty(&test_result, property_2_1); |
| 1622 | TestResultAccessor::RecordProperty(&test_result, property_1_2); |
| 1623 | TestResultAccessor::RecordProperty(&test_result, property_2_2); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1624 | |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1625 | ASSERT_EQ(2, test_result.test_property_count()); |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1626 | const TestProperty& actual_property_1 = test_result.GetTestProperty(0); |
| 1627 | EXPECT_STREQ("key_1", actual_property_1.key()); |
| 1628 | EXPECT_STREQ("12", actual_property_1.value()); |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1629 | |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1630 | const TestProperty& actual_property_2 = test_result.GetTestProperty(1); |
| 1631 | EXPECT_STREQ("key_2", actual_property_2.key()); |
| 1632 | EXPECT_STREQ("22", actual_property_2.value()); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | // Tests TestResult::GetTestProperty(). |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1636 | TEST(TestResultPropertyDeathTest, GetTestProperty) { |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1637 | TestResult test_result; |
| 1638 | TestProperty property_1("key_1", "1"); |
| 1639 | TestProperty property_2("key_2", "2"); |
| 1640 | TestProperty property_3("key_3", "3"); |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1641 | TestResultAccessor::RecordProperty(&test_result, property_1); |
| 1642 | TestResultAccessor::RecordProperty(&test_result, property_2); |
| 1643 | TestResultAccessor::RecordProperty(&test_result, property_3); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1644 | |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1645 | const TestProperty& fetched_property_1 = test_result.GetTestProperty(0); |
| 1646 | const TestProperty& fetched_property_2 = test_result.GetTestProperty(1); |
| 1647 | const TestProperty& fetched_property_3 = test_result.GetTestProperty(2); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1648 | |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1649 | EXPECT_STREQ("key_1", fetched_property_1.key()); |
| 1650 | EXPECT_STREQ("1", fetched_property_1.value()); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1651 | |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1652 | EXPECT_STREQ("key_2", fetched_property_2.key()); |
| 1653 | EXPECT_STREQ("2", fetched_property_2.value()); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1654 | |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1655 | EXPECT_STREQ("key_3", fetched_property_3.key()); |
| 1656 | EXPECT_STREQ("3", fetched_property_3.value()); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1657 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 1658 | EXPECT_DEATH_IF_SUPPORTED(test_result.GetTestProperty(3), ""); |
| 1659 | EXPECT_DEATH_IF_SUPPORTED(test_result.GetTestProperty(-1), ""); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1662 | // When a property using a reserved key is supplied to this function, it tests |
| 1663 | // that a non-fatal failure is added, a fatal failure is not added, and that the |
| 1664 | // property is not recorded. |
| 1665 | void ExpectNonFatalFailureRecordingPropertyWithReservedKey(const char* key) { |
| 1666 | TestResult test_result; |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame] | 1667 | TestProperty property(key, "1"); |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 1668 | EXPECT_NONFATAL_FAILURE( |
| 1669 | TestResultAccessor::RecordProperty(&test_result, property), |
| 1670 | "Reserved key"); |
| 1671 | ASSERT_EQ(0, test_result.test_property_count()) << "Not recorded"; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | // Attempting to recording a property with the Reserved literal "name" |
| 1675 | // should add a non-fatal failure and the property should not be recorded. |
| 1676 | TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledName) { |
| 1677 | ExpectNonFatalFailureRecordingPropertyWithReservedKey("name"); |
| 1678 | } |
| 1679 | |
| 1680 | // Attempting to recording a property with the Reserved literal "status" |
| 1681 | // should add a non-fatal failure and the property should not be recorded. |
| 1682 | TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledStatus) { |
| 1683 | ExpectNonFatalFailureRecordingPropertyWithReservedKey("status"); |
| 1684 | } |
| 1685 | |
| 1686 | // Attempting to recording a property with the Reserved literal "time" |
| 1687 | // should add a non-fatal failure and the property should not be recorded. |
| 1688 | TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledTime) { |
| 1689 | ExpectNonFatalFailureRecordingPropertyWithReservedKey("time"); |
| 1690 | } |
| 1691 | |
| 1692 | // Attempting to recording a property with the Reserved literal "classname" |
| 1693 | // should add a non-fatal failure and the property should not be recorded. |
| 1694 | TEST(TestResultPropertyTest, AddFailureWhenUsingReservedKeyCalledClassname) { |
| 1695 | ExpectNonFatalFailureRecordingPropertyWithReservedKey("classname"); |
| 1696 | } |
| 1697 | |
| 1698 | // Tests that GTestFlagSaver works on Windows and Mac. |
| 1699 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1700 | class GTestFlagSaverTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1701 | protected: |
| 1702 | // Saves the Google Test flags such that we can restore them later, and |
| 1703 | // then sets them to their default values. This will be called |
| 1704 | // before the first test in this test case is run. |
| 1705 | static void SetUpTestCase() { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1706 | saver_ = new GTestFlagSaver; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1707 | |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 1708 | GTEST_FLAG(also_run_disabled_tests) = false; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1709 | GTEST_FLAG(break_on_failure) = false; |
| 1710 | GTEST_FLAG(catch_exceptions) = false; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 1711 | GTEST_FLAG(death_test_use_fork) = false; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1712 | GTEST_FLAG(color) = "auto"; |
| 1713 | GTEST_FLAG(filter) = ""; |
| 1714 | GTEST_FLAG(list_tests) = false; |
| 1715 | GTEST_FLAG(output) = ""; |
zhanyong.wan | 73ad5a3 | 2009-04-14 23:19:22 +0000 | [diff] [blame] | 1716 | GTEST_FLAG(print_time) = true; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 1717 | GTEST_FLAG(random_seed) = 0; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1718 | GTEST_FLAG(repeat) = 1; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 1719 | GTEST_FLAG(shuffle) = false; |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 1720 | GTEST_FLAG(stack_trace_depth) = kMaxStackTraceDepth; |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 1721 | GTEST_FLAG(throw_on_failure) = false; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | // Restores the Google Test flags that the tests have modified. This will |
| 1725 | // be called after the last test in this test case is run. |
| 1726 | static void TearDownTestCase() { |
| 1727 | delete saver_; |
| 1728 | saver_ = NULL; |
| 1729 | } |
| 1730 | |
| 1731 | // Verifies that the Google Test flags have their default values, and then |
| 1732 | // modifies each of them. |
| 1733 | void VerifyAndModifyFlags() { |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 1734 | EXPECT_FALSE(GTEST_FLAG(also_run_disabled_tests)); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1735 | EXPECT_FALSE(GTEST_FLAG(break_on_failure)); |
| 1736 | EXPECT_FALSE(GTEST_FLAG(catch_exceptions)); |
| 1737 | EXPECT_STREQ("auto", GTEST_FLAG(color).c_str()); |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 1738 | EXPECT_FALSE(GTEST_FLAG(death_test_use_fork)); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1739 | EXPECT_STREQ("", GTEST_FLAG(filter).c_str()); |
| 1740 | EXPECT_FALSE(GTEST_FLAG(list_tests)); |
| 1741 | EXPECT_STREQ("", GTEST_FLAG(output).c_str()); |
zhanyong.wan | 73ad5a3 | 2009-04-14 23:19:22 +0000 | [diff] [blame] | 1742 | EXPECT_TRUE(GTEST_FLAG(print_time)); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 1743 | EXPECT_EQ(0, GTEST_FLAG(random_seed)); |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1744 | EXPECT_EQ(1, GTEST_FLAG(repeat)); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 1745 | EXPECT_FALSE(GTEST_FLAG(shuffle)); |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 1746 | EXPECT_EQ(kMaxStackTraceDepth, GTEST_FLAG(stack_trace_depth)); |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 1747 | EXPECT_FALSE(GTEST_FLAG(throw_on_failure)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1748 | |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 1749 | GTEST_FLAG(also_run_disabled_tests) = true; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1750 | GTEST_FLAG(break_on_failure) = true; |
| 1751 | GTEST_FLAG(catch_exceptions) = true; |
| 1752 | GTEST_FLAG(color) = "no"; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 1753 | GTEST_FLAG(death_test_use_fork) = true; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1754 | GTEST_FLAG(filter) = "abc"; |
| 1755 | GTEST_FLAG(list_tests) = true; |
| 1756 | GTEST_FLAG(output) = "xml:foo.xml"; |
zhanyong.wan | 73ad5a3 | 2009-04-14 23:19:22 +0000 | [diff] [blame] | 1757 | GTEST_FLAG(print_time) = false; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 1758 | GTEST_FLAG(random_seed) = 1; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1759 | GTEST_FLAG(repeat) = 100; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 1760 | GTEST_FLAG(shuffle) = true; |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 1761 | GTEST_FLAG(stack_trace_depth) = 1; |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 1762 | GTEST_FLAG(throw_on_failure) = true; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1763 | } |
| 1764 | private: |
| 1765 | // For saving Google Test flags during this test case. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1766 | static GTestFlagSaver* saver_; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1767 | }; |
| 1768 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1769 | GTestFlagSaver* GTestFlagSaverTest::saver_ = NULL; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1770 | |
| 1771 | // Google Test doesn't guarantee the order of tests. The following two |
| 1772 | // tests are designed to work regardless of their order. |
| 1773 | |
| 1774 | // Modifies the Google Test flags in the test body. |
| 1775 | TEST_F(GTestFlagSaverTest, ModifyGTestFlags) { |
| 1776 | VerifyAndModifyFlags(); |
| 1777 | } |
| 1778 | |
| 1779 | // Verifies that the Google Test flags in the body of the previous test were |
| 1780 | // restored to their original values. |
| 1781 | TEST_F(GTestFlagSaverTest, VerifyGTestFlags) { |
| 1782 | VerifyAndModifyFlags(); |
| 1783 | } |
| 1784 | |
| 1785 | // Sets an environment variable with the given name to the given |
| 1786 | // value. If the value argument is "", unsets the environment |
| 1787 | // variable. The caller must ensure that both arguments are not NULL. |
| 1788 | static void SetEnv(const char* name, const char* value) { |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 1789 | #if GTEST_OS_WINDOWS_MOBILE |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1790 | // Environment variables are not supported on Windows CE. |
| 1791 | return; |
vladlosev | 673a0cb | 2010-02-03 02:27:02 +0000 | [diff] [blame] | 1792 | #elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9) |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1793 | // C++Builder's putenv only stores a pointer to its parameter; we have to |
| 1794 | // ensure that the string remains valid as long as it might be needed. |
| 1795 | // We use an std::map to do so. |
| 1796 | static std::map<String, String*> added_env; |
| 1797 | |
| 1798 | // Because putenv stores a pointer to the string buffer, we can't delete the |
| 1799 | // previous string (if present) until after it's replaced. |
| 1800 | String *prev_env = NULL; |
| 1801 | if (added_env.find(name) != added_env.end()) { |
| 1802 | prev_env = added_env[name]; |
| 1803 | } |
| 1804 | added_env[name] = new String((Message() << name << "=" << value).GetString()); |
vladlosev | 673a0cb | 2010-02-03 02:27:02 +0000 | [diff] [blame] | 1805 | |
| 1806 | // The standard signature of putenv accepts a 'char*' argument. Other |
| 1807 | // implementations, like C++Builder's, accept a 'const char*'. |
| 1808 | // We cast away the 'const' since that would work for both variants. |
| 1809 | putenv(const_cast<char*>(added_env[name]->c_str())); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1810 | delete prev_env; |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1811 | #elif GTEST_OS_WINDOWS // If we are on Windows proper. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1812 | _putenv((Message() << name << "=" << value).GetString().c_str()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1813 | #else |
| 1814 | if (*value == '\0') { |
| 1815 | unsetenv(name); |
| 1816 | } else { |
| 1817 | setenv(name, value, 1); |
| 1818 | } |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 1819 | #endif // GTEST_OS_WINDOWS_MOBILE |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 1822 | #if !GTEST_OS_WINDOWS_MOBILE |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1823 | // Environment variables are not supported on Windows CE. |
| 1824 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 1825 | using testing::internal::Int32FromGTestEnv; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1826 | |
| 1827 | // Tests Int32FromGTestEnv(). |
| 1828 | |
| 1829 | // Tests that Int32FromGTestEnv() returns the default value when the |
| 1830 | // environment variable is not set. |
| 1831 | TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenVariableIsNotSet) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1832 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", ""); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1833 | EXPECT_EQ(10, Int32FromGTestEnv("temp", 10)); |
| 1834 | } |
| 1835 | |
| 1836 | // Tests that Int32FromGTestEnv() returns the default value when the |
| 1837 | // environment variable overflows as an Int32. |
| 1838 | TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenValueOverflows) { |
| 1839 | printf("(expecting 2 warnings)\n"); |
| 1840 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1841 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "12345678987654321"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1842 | EXPECT_EQ(20, Int32FromGTestEnv("temp", 20)); |
| 1843 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1844 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "-12345678987654321"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1845 | EXPECT_EQ(30, Int32FromGTestEnv("temp", 30)); |
| 1846 | } |
| 1847 | |
| 1848 | // Tests that Int32FromGTestEnv() returns the default value when the |
| 1849 | // environment variable does not represent a valid decimal integer. |
| 1850 | TEST(Int32FromGTestEnvTest, ReturnsDefaultWhenValueIsInvalid) { |
| 1851 | printf("(expecting 2 warnings)\n"); |
| 1852 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1853 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "A1"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1854 | EXPECT_EQ(40, Int32FromGTestEnv("temp", 40)); |
| 1855 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1856 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "12X"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1857 | EXPECT_EQ(50, Int32FromGTestEnv("temp", 50)); |
| 1858 | } |
| 1859 | |
| 1860 | // Tests that Int32FromGTestEnv() parses and returns the value of the |
| 1861 | // environment variable when it represents a valid decimal integer in |
| 1862 | // the range of an Int32. |
| 1863 | TEST(Int32FromGTestEnvTest, ParsesAndReturnsValidValue) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1864 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "123"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1865 | EXPECT_EQ(123, Int32FromGTestEnv("temp", 0)); |
| 1866 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1867 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "-321"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1868 | EXPECT_EQ(-321, Int32FromGTestEnv("temp", 0)); |
| 1869 | } |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 1870 | #endif // !GTEST_OS_WINDOWS_MOBILE |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1871 | |
| 1872 | // Tests ParseInt32Flag(). |
| 1873 | |
| 1874 | // Tests that ParseInt32Flag() returns false and doesn't change the |
| 1875 | // output value when the flag has wrong format |
| 1876 | TEST(ParseInt32FlagTest, ReturnsFalseForInvalidFlag) { |
| 1877 | Int32 value = 123; |
| 1878 | EXPECT_FALSE(ParseInt32Flag("--a=100", "b", &value)); |
| 1879 | EXPECT_EQ(123, value); |
| 1880 | |
| 1881 | EXPECT_FALSE(ParseInt32Flag("a=100", "a", &value)); |
| 1882 | EXPECT_EQ(123, value); |
| 1883 | } |
| 1884 | |
| 1885 | // Tests that ParseInt32Flag() returns false and doesn't change the |
| 1886 | // output value when the flag overflows as an Int32. |
| 1887 | TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueOverflows) { |
| 1888 | printf("(expecting 2 warnings)\n"); |
| 1889 | |
| 1890 | Int32 value = 123; |
| 1891 | EXPECT_FALSE(ParseInt32Flag("--abc=12345678987654321", "abc", &value)); |
| 1892 | EXPECT_EQ(123, value); |
| 1893 | |
| 1894 | EXPECT_FALSE(ParseInt32Flag("--abc=-12345678987654321", "abc", &value)); |
| 1895 | EXPECT_EQ(123, value); |
| 1896 | } |
| 1897 | |
| 1898 | // Tests that ParseInt32Flag() returns false and doesn't change the |
| 1899 | // output value when the flag does not represent a valid decimal |
| 1900 | // integer. |
| 1901 | TEST(ParseInt32FlagTest, ReturnsDefaultWhenValueIsInvalid) { |
| 1902 | printf("(expecting 2 warnings)\n"); |
| 1903 | |
| 1904 | Int32 value = 123; |
| 1905 | EXPECT_FALSE(ParseInt32Flag("--abc=A1", "abc", &value)); |
| 1906 | EXPECT_EQ(123, value); |
| 1907 | |
| 1908 | EXPECT_FALSE(ParseInt32Flag("--abc=12X", "abc", &value)); |
| 1909 | EXPECT_EQ(123, value); |
| 1910 | } |
| 1911 | |
| 1912 | // Tests that ParseInt32Flag() parses the value of the flag and |
| 1913 | // returns true when the flag represents a valid decimal integer in |
| 1914 | // the range of an Int32. |
| 1915 | TEST(ParseInt32FlagTest, ParsesAndReturnsValidValue) { |
| 1916 | Int32 value = 123; |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1917 | EXPECT_TRUE(ParseInt32Flag("--" GTEST_FLAG_PREFIX_ "abc=456", "abc", &value)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1918 | EXPECT_EQ(456, value); |
| 1919 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 1920 | EXPECT_TRUE(ParseInt32Flag("--" GTEST_FLAG_PREFIX_ "abc=-789", |
| 1921 | "abc", &value)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 1922 | EXPECT_EQ(-789, value); |
| 1923 | } |
| 1924 | |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 1925 | // Tests that Int32FromEnvOrDie() parses the value of the var or |
| 1926 | // returns the correct default. |
zhanyong.wan | c427f5e | 2009-06-19 17:23:54 +0000 | [diff] [blame] | 1927 | // Environment variables are not supported on Windows CE. |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 1928 | #if !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 1929 | TEST(Int32FromEnvOrDieTest, ParsesAndReturnsValidValue) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1930 | EXPECT_EQ(333, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333)); |
| 1931 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "123"); |
| 1932 | EXPECT_EQ(123, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333)); |
| 1933 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "-123"); |
| 1934 | EXPECT_EQ(-123, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333)); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 1935 | } |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 1936 | #endif // !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 1937 | |
| 1938 | // Tests that Int32FromEnvOrDie() aborts with an error message |
| 1939 | // if the variable is not an Int32. |
| 1940 | TEST(Int32FromEnvOrDieDeathTest, AbortsOnFailure) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1941 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "xxx"); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 1942 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1943 | Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123), |
| 1944 | ".*"); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | // Tests that Int32FromEnvOrDie() aborts with an error message |
| 1948 | // if the variable cannot be represnted by an Int32. |
| 1949 | TEST(Int32FromEnvOrDieDeathTest, AbortsOnInt32Overflow) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1950 | SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "1234567891234567891234"); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 1951 | EXPECT_DEATH_IF_SUPPORTED( |
zhanyong.wan | 449f84d | 2009-07-01 22:55:05 +0000 | [diff] [blame] | 1952 | Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123), |
| 1953 | ".*"); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 1956 | // Tests that ShouldRunTestOnShard() selects all tests |
| 1957 | // where there is 1 shard. |
| 1958 | TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereIsOneShard) { |
| 1959 | EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 0)); |
| 1960 | EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 1)); |
| 1961 | EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 2)); |
| 1962 | EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 3)); |
| 1963 | EXPECT_TRUE(ShouldRunTestOnShard(1, 0, 4)); |
| 1964 | } |
| 1965 | |
| 1966 | class ShouldShardTest : public testing::Test { |
| 1967 | protected: |
| 1968 | virtual void SetUp() { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 1969 | index_var_ = GTEST_FLAG_PREFIX_UPPER_ "INDEX"; |
| 1970 | total_var_ = GTEST_FLAG_PREFIX_UPPER_ "TOTAL"; |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | virtual void TearDown() { |
| 1974 | SetEnv(index_var_, ""); |
| 1975 | SetEnv(total_var_, ""); |
| 1976 | } |
| 1977 | |
| 1978 | const char* index_var_; |
| 1979 | const char* total_var_; |
| 1980 | }; |
| 1981 | |
| 1982 | // Tests that sharding is disabled if neither of the environment variables |
| 1983 | // are set. |
| 1984 | TEST_F(ShouldShardTest, ReturnsFalseWhenNeitherEnvVarIsSet) { |
| 1985 | SetEnv(index_var_, ""); |
| 1986 | SetEnv(total_var_, ""); |
| 1987 | |
| 1988 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, false)); |
| 1989 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); |
| 1990 | } |
| 1991 | |
| 1992 | // Tests that sharding is not enabled if total_shards == 1. |
| 1993 | TEST_F(ShouldShardTest, ReturnsFalseWhenTotalShardIsOne) { |
| 1994 | SetEnv(index_var_, "0"); |
| 1995 | SetEnv(total_var_, "1"); |
| 1996 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, false)); |
| 1997 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); |
| 1998 | } |
| 1999 | |
| 2000 | // Tests that sharding is enabled if total_shards > 1 and |
| 2001 | // we are not in a death test subprocess. |
zhanyong.wan | c427f5e | 2009-06-19 17:23:54 +0000 | [diff] [blame] | 2002 | // Environment variables are not supported on Windows CE. |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 2003 | #if !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2004 | TEST_F(ShouldShardTest, WorksWhenShardEnvVarsAreValid) { |
| 2005 | SetEnv(index_var_, "4"); |
| 2006 | SetEnv(total_var_, "22"); |
| 2007 | EXPECT_TRUE(ShouldShard(total_var_, index_var_, false)); |
| 2008 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); |
| 2009 | |
| 2010 | SetEnv(index_var_, "8"); |
| 2011 | SetEnv(total_var_, "9"); |
| 2012 | EXPECT_TRUE(ShouldShard(total_var_, index_var_, false)); |
| 2013 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); |
| 2014 | |
| 2015 | SetEnv(index_var_, "0"); |
| 2016 | SetEnv(total_var_, "9"); |
| 2017 | EXPECT_TRUE(ShouldShard(total_var_, index_var_, false)); |
| 2018 | EXPECT_FALSE(ShouldShard(total_var_, index_var_, true)); |
| 2019 | } |
zhanyong.wan | fff0334 | 2009-09-24 21:15:59 +0000 | [diff] [blame] | 2020 | #endif // !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2021 | |
| 2022 | // 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] | 2023 | |
| 2024 | typedef ShouldShardTest ShouldShardDeathTest; |
| 2025 | |
| 2026 | TEST_F(ShouldShardDeathTest, AbortsWhenShardingEnvVarsAreInvalid) { |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2027 | SetEnv(index_var_, "4"); |
| 2028 | SetEnv(total_var_, "4"); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 2029 | EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2030 | |
| 2031 | SetEnv(index_var_, "4"); |
| 2032 | SetEnv(total_var_, "-2"); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 2033 | EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2034 | |
| 2035 | SetEnv(index_var_, "5"); |
| 2036 | SetEnv(total_var_, ""); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 2037 | EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2038 | |
| 2039 | SetEnv(index_var_, ""); |
| 2040 | SetEnv(total_var_, "5"); |
zhanyong.wan | 535de53 | 2009-08-07 06:47:47 +0000 | [diff] [blame] | 2041 | EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
zhanyong.wan | 905074c | 2009-02-09 18:05:21 +0000 | [diff] [blame] | 2044 | // Tests that ShouldRunTestOnShard is a partition when 5 |
| 2045 | // shards are used. |
| 2046 | TEST(ShouldRunTestOnShardTest, IsPartitionWhenThereAreFiveShards) { |
| 2047 | // Choose an arbitrary number of tests and shards. |
| 2048 | const int num_tests = 17; |
| 2049 | const int num_shards = 5; |
| 2050 | |
| 2051 | // Check partitioning: each test should be on exactly 1 shard. |
| 2052 | for (int test_id = 0; test_id < num_tests; test_id++) { |
| 2053 | int prev_selected_shard_index = -1; |
| 2054 | for (int shard_index = 0; shard_index < num_shards; shard_index++) { |
| 2055 | if (ShouldRunTestOnShard(num_shards, shard_index, test_id)) { |
| 2056 | if (prev_selected_shard_index < 0) { |
| 2057 | prev_selected_shard_index = shard_index; |
| 2058 | } else { |
| 2059 | ADD_FAILURE() << "Shard " << prev_selected_shard_index << " and " |
| 2060 | << shard_index << " are both selected to run test " << test_id; |
| 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | // Check balance: This is not required by the sharding protocol, but is a |
| 2067 | // desirable property for performance. |
| 2068 | for (int shard_index = 0; shard_index < num_shards; shard_index++) { |
| 2069 | int num_tests_on_shard = 0; |
| 2070 | for (int test_id = 0; test_id < num_tests; test_id++) { |
| 2071 | num_tests_on_shard += |
| 2072 | ShouldRunTestOnShard(num_shards, shard_index, test_id); |
| 2073 | } |
| 2074 | EXPECT_GE(num_tests_on_shard, num_tests / num_shards); |
| 2075 | } |
| 2076 | } |
| 2077 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2078 | // For the same reason we are not explicitly testing everything in the |
shiqian | c3b4de3 | 2008-09-12 04:01:37 +0000 | [diff] [blame] | 2079 | // Test class, there are no separate tests for the following classes |
| 2080 | // (except for some trivial cases): |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2081 | // |
| 2082 | // TestCase, UnitTest, UnitTestResultPrinter. |
| 2083 | // |
| 2084 | // Similarly, there are no separate tests for the following macros: |
| 2085 | // |
| 2086 | // TEST, TEST_F, RUN_ALL_TESTS |
| 2087 | |
shiqian | c3b4de3 | 2008-09-12 04:01:37 +0000 | [diff] [blame] | 2088 | TEST(UnitTestTest, CanGetOriginalWorkingDir) { |
| 2089 | ASSERT_TRUE(UnitTest::GetInstance()->original_working_dir() != NULL); |
| 2090 | EXPECT_STRNE(UnitTest::GetInstance()->original_working_dir(), ""); |
| 2091 | } |
| 2092 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2093 | // This group of tests is for predicate assertions (ASSERT_PRED*, etc) |
| 2094 | // of various arities. They do not attempt to be exhaustive. Rather, |
| 2095 | // view them as smoke tests that can be easily reviewed and verified. |
| 2096 | // A more complete set of tests for predicate assertions can be found |
| 2097 | // in gtest_pred_impl_unittest.cc. |
| 2098 | |
| 2099 | // First, some predicates and predicate-formatters needed by the tests. |
| 2100 | |
| 2101 | // Returns true iff the argument is an even number. |
| 2102 | bool IsEven(int n) { |
| 2103 | return (n % 2) == 0; |
| 2104 | } |
| 2105 | |
| 2106 | // A functor that returns true iff the argument is an even number. |
| 2107 | struct IsEvenFunctor { |
| 2108 | bool operator()(int n) { return IsEven(n); } |
| 2109 | }; |
| 2110 | |
| 2111 | // A predicate-formatter function that asserts the argument is an even |
| 2112 | // number. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2113 | AssertionResult AssertIsEven(const char* expr, int n) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2114 | if (IsEven(n)) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2115 | return AssertionSuccess(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2118 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2119 | msg << expr << " evaluates to " << n << ", which is not even."; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2120 | return AssertionFailure(msg); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 2123 | // A predicate function that returns AssertionResult for use in |
| 2124 | // EXPECT/ASSERT_TRUE/FALSE. |
| 2125 | AssertionResult ResultIsEven(int n) { |
| 2126 | if (IsEven(n)) |
| 2127 | return AssertionSuccess() << n << " is even"; |
| 2128 | else |
| 2129 | return AssertionFailure() << n << " is odd"; |
| 2130 | } |
| 2131 | |
| 2132 | // A predicate function that returns AssertionResult but gives no |
| 2133 | // explanation why it succeeds. Needed for testing that |
| 2134 | // EXPECT/ASSERT_FALSE handles such functions correctly. |
| 2135 | AssertionResult ResultIsEvenNoExplanation(int n) { |
| 2136 | if (IsEven(n)) |
| 2137 | return AssertionSuccess(); |
| 2138 | else |
| 2139 | return AssertionFailure() << n << " is odd"; |
| 2140 | } |
| 2141 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2142 | // A predicate-formatter functor that asserts the argument is an even |
| 2143 | // number. |
| 2144 | struct AssertIsEvenFunctor { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2145 | AssertionResult operator()(const char* expr, int n) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2146 | return AssertIsEven(expr, n); |
| 2147 | } |
| 2148 | }; |
| 2149 | |
| 2150 | // Returns true iff the sum of the arguments is an even number. |
| 2151 | bool SumIsEven2(int n1, int n2) { |
| 2152 | return IsEven(n1 + n2); |
| 2153 | } |
| 2154 | |
| 2155 | // A functor that returns true iff the sum of the arguments is an even |
| 2156 | // number. |
| 2157 | struct SumIsEven3Functor { |
| 2158 | bool operator()(int n1, int n2, int n3) { |
| 2159 | return IsEven(n1 + n2 + n3); |
| 2160 | } |
| 2161 | }; |
| 2162 | |
| 2163 | // A predicate-formatter function that asserts the sum of the |
| 2164 | // arguments is an even number. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2165 | AssertionResult AssertSumIsEven4( |
| 2166 | const char* e1, const char* e2, const char* e3, const char* e4, |
| 2167 | int n1, int n2, int n3, int n4) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2168 | const int sum = n1 + n2 + n3 + n4; |
| 2169 | if (IsEven(sum)) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2170 | return AssertionSuccess(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2173 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2174 | msg << e1 << " + " << e2 << " + " << e3 << " + " << e4 |
| 2175 | << " (" << n1 << " + " << n2 << " + " << n3 << " + " << n4 |
| 2176 | << ") evaluates to " << sum << ", which is not even."; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2177 | return AssertionFailure(msg); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | // A predicate-formatter functor that asserts the sum of the arguments |
| 2181 | // is an even number. |
| 2182 | struct AssertSumIsEven5Functor { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2183 | AssertionResult operator()( |
| 2184 | const char* e1, const char* e2, const char* e3, const char* e4, |
| 2185 | const char* e5, int n1, int n2, int n3, int n4, int n5) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2186 | const int sum = n1 + n2 + n3 + n4 + n5; |
| 2187 | if (IsEven(sum)) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2188 | return AssertionSuccess(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2191 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2192 | msg << e1 << " + " << e2 << " + " << e3 << " + " << e4 << " + " << e5 |
| 2193 | << " (" |
| 2194 | << n1 << " + " << n2 << " + " << n3 << " + " << n4 << " + " << n5 |
| 2195 | << ") evaluates to " << sum << ", which is not even."; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2196 | return AssertionFailure(msg); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2197 | } |
| 2198 | }; |
| 2199 | |
| 2200 | |
| 2201 | // Tests unary predicate assertions. |
| 2202 | |
| 2203 | // Tests unary predicate assertions that don't use a custom formatter. |
| 2204 | TEST(Pred1Test, WithoutFormat) { |
| 2205 | // Success cases. |
| 2206 | EXPECT_PRED1(IsEvenFunctor(), 2) << "This failure is UNEXPECTED!"; |
| 2207 | ASSERT_PRED1(IsEven, 4); |
| 2208 | |
| 2209 | // Failure cases. |
| 2210 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 2211 | EXPECT_PRED1(IsEven, 5) << "This failure is expected."; |
| 2212 | }, "This failure is expected."); |
| 2213 | EXPECT_FATAL_FAILURE(ASSERT_PRED1(IsEvenFunctor(), 5), |
| 2214 | "evaluates to false"); |
| 2215 | } |
| 2216 | |
| 2217 | // Tests unary predicate assertions that use a custom formatter. |
| 2218 | TEST(Pred1Test, WithFormat) { |
| 2219 | // Success cases. |
| 2220 | EXPECT_PRED_FORMAT1(AssertIsEven, 2); |
| 2221 | ASSERT_PRED_FORMAT1(AssertIsEvenFunctor(), 4) |
| 2222 | << "This failure is UNEXPECTED!"; |
| 2223 | |
| 2224 | // Failure cases. |
| 2225 | const int n = 5; |
| 2226 | EXPECT_NONFATAL_FAILURE(EXPECT_PRED_FORMAT1(AssertIsEvenFunctor(), n), |
| 2227 | "n evaluates to 5, which is not even."); |
| 2228 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 2229 | ASSERT_PRED_FORMAT1(AssertIsEven, 5) << "This failure is expected."; |
| 2230 | }, "This failure is expected."); |
| 2231 | } |
| 2232 | |
| 2233 | // Tests that unary predicate assertions evaluates their arguments |
| 2234 | // exactly once. |
| 2235 | TEST(Pred1Test, SingleEvaluationOnFailure) { |
| 2236 | // A success case. |
| 2237 | static int n = 0; |
| 2238 | EXPECT_PRED1(IsEven, n++); |
| 2239 | EXPECT_EQ(1, n) << "The argument is not evaluated exactly once."; |
| 2240 | |
| 2241 | // A failure case. |
| 2242 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 2243 | ASSERT_PRED_FORMAT1(AssertIsEvenFunctor(), n++) |
| 2244 | << "This failure is expected."; |
| 2245 | }, "This failure is expected."); |
| 2246 | EXPECT_EQ(2, n) << "The argument is not evaluated exactly once."; |
| 2247 | } |
| 2248 | |
| 2249 | |
| 2250 | // Tests predicate assertions whose arity is >= 2. |
| 2251 | |
| 2252 | // Tests predicate assertions that don't use a custom formatter. |
| 2253 | TEST(PredTest, WithoutFormat) { |
| 2254 | // Success cases. |
| 2255 | ASSERT_PRED2(SumIsEven2, 2, 4) << "This failure is UNEXPECTED!"; |
| 2256 | EXPECT_PRED3(SumIsEven3Functor(), 4, 6, 8); |
| 2257 | |
| 2258 | // Failure cases. |
| 2259 | const int n1 = 1; |
| 2260 | const int n2 = 2; |
| 2261 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 2262 | EXPECT_PRED2(SumIsEven2, n1, n2) << "This failure is expected."; |
| 2263 | }, "This failure is expected."); |
| 2264 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 2265 | ASSERT_PRED3(SumIsEven3Functor(), 1, 2, 4); |
| 2266 | }, "evaluates to false"); |
| 2267 | } |
| 2268 | |
| 2269 | // Tests predicate assertions that use a custom formatter. |
| 2270 | TEST(PredTest, WithFormat) { |
| 2271 | // Success cases. |
| 2272 | ASSERT_PRED_FORMAT4(AssertSumIsEven4, 4, 6, 8, 10) << |
| 2273 | "This failure is UNEXPECTED!"; |
| 2274 | EXPECT_PRED_FORMAT5(AssertSumIsEven5Functor(), 2, 4, 6, 8, 10); |
| 2275 | |
| 2276 | // Failure cases. |
| 2277 | const int n1 = 1; |
| 2278 | const int n2 = 2; |
| 2279 | const int n3 = 4; |
| 2280 | const int n4 = 6; |
| 2281 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 2282 | EXPECT_PRED_FORMAT4(AssertSumIsEven4, n1, n2, n3, n4); |
| 2283 | }, "evaluates to 13, which is not even."); |
| 2284 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 2285 | ASSERT_PRED_FORMAT5(AssertSumIsEven5Functor(), 1, 2, 4, 6, 8) |
| 2286 | << "This failure is expected."; |
| 2287 | }, "This failure is expected."); |
| 2288 | } |
| 2289 | |
| 2290 | // Tests that predicate assertions evaluates their arguments |
| 2291 | // exactly once. |
| 2292 | TEST(PredTest, SingleEvaluationOnFailure) { |
| 2293 | // A success case. |
| 2294 | int n1 = 0; |
| 2295 | int n2 = 0; |
| 2296 | EXPECT_PRED2(SumIsEven2, n1++, n2++); |
| 2297 | EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; |
| 2298 | EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; |
| 2299 | |
| 2300 | // Another success case. |
| 2301 | n1 = n2 = 0; |
| 2302 | int n3 = 0; |
| 2303 | int n4 = 0; |
| 2304 | int n5 = 0; |
| 2305 | ASSERT_PRED_FORMAT5(AssertSumIsEven5Functor(), |
| 2306 | n1++, n2++, n3++, n4++, n5++) |
| 2307 | << "This failure is UNEXPECTED!"; |
| 2308 | EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; |
| 2309 | EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; |
| 2310 | EXPECT_EQ(1, n3) << "Argument 3 is not evaluated exactly once."; |
| 2311 | EXPECT_EQ(1, n4) << "Argument 4 is not evaluated exactly once."; |
| 2312 | EXPECT_EQ(1, n5) << "Argument 5 is not evaluated exactly once."; |
| 2313 | |
| 2314 | // A failure case. |
| 2315 | n1 = n2 = n3 = 0; |
| 2316 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 2317 | EXPECT_PRED3(SumIsEven3Functor(), ++n1, n2++, n3++) |
| 2318 | << "This failure is expected."; |
| 2319 | }, "This failure is expected."); |
| 2320 | EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; |
| 2321 | EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; |
| 2322 | EXPECT_EQ(1, n3) << "Argument 3 is not evaluated exactly once."; |
| 2323 | |
| 2324 | // Another failure case. |
| 2325 | n1 = n2 = n3 = n4 = 0; |
| 2326 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 2327 | EXPECT_PRED_FORMAT4(AssertSumIsEven4, ++n1, n2++, n3++, n4++); |
| 2328 | }, "evaluates to 1, which is not even."); |
| 2329 | EXPECT_EQ(1, n1) << "Argument 1 is not evaluated exactly once."; |
| 2330 | EXPECT_EQ(1, n2) << "Argument 2 is not evaluated exactly once."; |
| 2331 | EXPECT_EQ(1, n3) << "Argument 3 is not evaluated exactly once."; |
| 2332 | EXPECT_EQ(1, n4) << "Argument 4 is not evaluated exactly once."; |
| 2333 | } |
| 2334 | |
| 2335 | |
| 2336 | // Some helper functions for testing using overloaded/template |
| 2337 | // functions with ASSERT_PREDn and EXPECT_PREDn. |
| 2338 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2339 | bool IsPositive(double x) { |
| 2340 | return x > 0; |
| 2341 | } |
| 2342 | |
| 2343 | template <typename T> |
| 2344 | bool IsNegative(T x) { |
| 2345 | return x < 0; |
| 2346 | } |
| 2347 | |
| 2348 | template <typename T1, typename T2> |
| 2349 | bool GreaterThan(T1 x1, T2 x2) { |
| 2350 | return x1 > x2; |
| 2351 | } |
| 2352 | |
| 2353 | // Tests that overloaded functions can be used in *_PRED* as long as |
| 2354 | // their types are explicitly specified. |
| 2355 | TEST(PredicateAssertionTest, AcceptsOverloadedFunction) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2356 | // C++Builder requires C-style casts rather than static_cast. |
| 2357 | EXPECT_PRED1((bool (*)(int))(IsPositive), 5); // NOLINT |
| 2358 | ASSERT_PRED1((bool (*)(double))(IsPositive), 6.0); // NOLINT |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | // Tests that template functions can be used in *_PRED* as long as |
| 2362 | // their types are explicitly specified. |
| 2363 | TEST(PredicateAssertionTest, AcceptsTemplateFunction) { |
| 2364 | EXPECT_PRED1(IsNegative<int>, -5); |
| 2365 | // Makes sure that we can handle templates with more than one |
| 2366 | // parameter. |
| 2367 | ASSERT_PRED2((GreaterThan<int, int>), 5, 0); |
| 2368 | } |
| 2369 | |
| 2370 | |
| 2371 | // Some helper functions for testing using overloaded/template |
| 2372 | // functions with ASSERT_PRED_FORMATn and EXPECT_PRED_FORMATn. |
| 2373 | |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame] | 2374 | AssertionResult IsPositiveFormat(const char* /* expr */, int n) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2375 | return n > 0 ? AssertionSuccess() : |
| 2376 | AssertionFailure(Message() << "Failure"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame] | 2379 | AssertionResult IsPositiveFormat(const char* /* expr */, double x) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2380 | return x > 0 ? AssertionSuccess() : |
| 2381 | AssertionFailure(Message() << "Failure"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | template <typename T> |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame] | 2385 | AssertionResult IsNegativeFormat(const char* /* expr */, T x) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2386 | return x < 0 ? AssertionSuccess() : |
| 2387 | AssertionFailure(Message() << "Failure"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | template <typename T1, typename T2> |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame] | 2391 | AssertionResult EqualsFormat(const char* /* expr1 */, const char* /* expr2 */, |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2392 | const T1& x1, const T2& x2) { |
| 2393 | return x1 == x2 ? AssertionSuccess() : |
| 2394 | AssertionFailure(Message() << "Failure"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | // Tests that overloaded functions can be used in *_PRED_FORMAT* |
zhanyong.wan | b0a12f7 | 2009-01-29 06:49:00 +0000 | [diff] [blame] | 2398 | // without explicitly specifying their types. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2399 | TEST(PredicateFormatAssertionTest, AcceptsOverloadedFunction) { |
| 2400 | EXPECT_PRED_FORMAT1(IsPositiveFormat, 5); |
| 2401 | ASSERT_PRED_FORMAT1(IsPositiveFormat, 6.0); |
| 2402 | } |
| 2403 | |
| 2404 | // Tests that template functions can be used in *_PRED_FORMAT* without |
| 2405 | // explicitly specifying their types. |
| 2406 | TEST(PredicateFormatAssertionTest, AcceptsTemplateFunction) { |
| 2407 | EXPECT_PRED_FORMAT1(IsNegativeFormat, -5); |
| 2408 | ASSERT_PRED_FORMAT2(EqualsFormat, 3, 3); |
| 2409 | } |
| 2410 | |
| 2411 | |
| 2412 | // Tests string assertions. |
| 2413 | |
| 2414 | // Tests ASSERT_STREQ with non-NULL arguments. |
| 2415 | TEST(StringAssertionTest, ASSERT_STREQ) { |
| 2416 | const char * const p1 = "good"; |
| 2417 | ASSERT_STREQ(p1, p1); |
| 2418 | |
| 2419 | // Let p2 have the same content as p1, but be at a different address. |
| 2420 | const char p2[] = "good"; |
| 2421 | ASSERT_STREQ(p1, p2); |
| 2422 | |
| 2423 | EXPECT_FATAL_FAILURE(ASSERT_STREQ("bad", "good"), |
| 2424 | "Expected: \"bad\""); |
| 2425 | } |
| 2426 | |
| 2427 | // Tests ASSERT_STREQ with NULL arguments. |
| 2428 | TEST(StringAssertionTest, ASSERT_STREQ_Null) { |
| 2429 | ASSERT_STREQ(static_cast<const char *>(NULL), NULL); |
| 2430 | EXPECT_FATAL_FAILURE(ASSERT_STREQ(NULL, "non-null"), |
| 2431 | "non-null"); |
| 2432 | } |
| 2433 | |
| 2434 | // Tests ASSERT_STREQ with NULL arguments. |
| 2435 | TEST(StringAssertionTest, ASSERT_STREQ_Null2) { |
| 2436 | EXPECT_FATAL_FAILURE(ASSERT_STREQ("non-null", NULL), |
| 2437 | "non-null"); |
| 2438 | } |
| 2439 | |
| 2440 | // Tests ASSERT_STRNE. |
| 2441 | TEST(StringAssertionTest, ASSERT_STRNE) { |
| 2442 | ASSERT_STRNE("hi", "Hi"); |
| 2443 | ASSERT_STRNE("Hi", NULL); |
| 2444 | ASSERT_STRNE(NULL, "Hi"); |
| 2445 | ASSERT_STRNE("", NULL); |
| 2446 | ASSERT_STRNE(NULL, ""); |
| 2447 | ASSERT_STRNE("", "Hi"); |
| 2448 | ASSERT_STRNE("Hi", ""); |
| 2449 | EXPECT_FATAL_FAILURE(ASSERT_STRNE("Hi", "Hi"), |
| 2450 | "\"Hi\" vs \"Hi\""); |
| 2451 | } |
| 2452 | |
| 2453 | // Tests ASSERT_STRCASEEQ. |
| 2454 | TEST(StringAssertionTest, ASSERT_STRCASEEQ) { |
| 2455 | ASSERT_STRCASEEQ("hi", "Hi"); |
| 2456 | ASSERT_STRCASEEQ(static_cast<const char *>(NULL), NULL); |
| 2457 | |
| 2458 | ASSERT_STRCASEEQ("", ""); |
| 2459 | EXPECT_FATAL_FAILURE(ASSERT_STRCASEEQ("Hi", "hi2"), |
| 2460 | "(ignoring case)"); |
| 2461 | } |
| 2462 | |
| 2463 | // Tests ASSERT_STRCASENE. |
| 2464 | TEST(StringAssertionTest, ASSERT_STRCASENE) { |
| 2465 | ASSERT_STRCASENE("hi1", "Hi2"); |
| 2466 | ASSERT_STRCASENE("Hi", NULL); |
| 2467 | ASSERT_STRCASENE(NULL, "Hi"); |
| 2468 | ASSERT_STRCASENE("", NULL); |
| 2469 | ASSERT_STRCASENE(NULL, ""); |
| 2470 | ASSERT_STRCASENE("", "Hi"); |
| 2471 | ASSERT_STRCASENE("Hi", ""); |
| 2472 | EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("Hi", "hi"), |
| 2473 | "(ignoring case)"); |
| 2474 | } |
| 2475 | |
| 2476 | // Tests *_STREQ on wide strings. |
| 2477 | TEST(StringAssertionTest, STREQ_Wide) { |
| 2478 | // NULL strings. |
| 2479 | ASSERT_STREQ(static_cast<const wchar_t *>(NULL), NULL); |
| 2480 | |
| 2481 | // Empty strings. |
| 2482 | ASSERT_STREQ(L"", L""); |
| 2483 | |
| 2484 | // Non-null vs NULL. |
| 2485 | EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"non-null", NULL), |
| 2486 | "non-null"); |
| 2487 | |
| 2488 | // Equal strings. |
| 2489 | EXPECT_STREQ(L"Hi", L"Hi"); |
| 2490 | |
| 2491 | // Unequal strings. |
| 2492 | EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"abc", L"Abc"), |
| 2493 | "Abc"); |
| 2494 | |
| 2495 | // Strings containing wide characters. |
| 2496 | EXPECT_NONFATAL_FAILURE(EXPECT_STREQ(L"abc\x8119", L"abc\x8120"), |
| 2497 | "abc"); |
| 2498 | } |
| 2499 | |
| 2500 | // Tests *_STRNE on wide strings. |
| 2501 | TEST(StringAssertionTest, STRNE_Wide) { |
| 2502 | // NULL strings. |
| 2503 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 2504 | EXPECT_STRNE(static_cast<const wchar_t *>(NULL), NULL); |
| 2505 | }, ""); |
| 2506 | |
| 2507 | // Empty strings. |
| 2508 | EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"", L""), |
| 2509 | "L\"\""); |
| 2510 | |
| 2511 | // Non-null vs NULL. |
| 2512 | ASSERT_STRNE(L"non-null", NULL); |
| 2513 | |
| 2514 | // Equal strings. |
| 2515 | EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"Hi", L"Hi"), |
| 2516 | "L\"Hi\""); |
| 2517 | |
| 2518 | // Unequal strings. |
| 2519 | EXPECT_STRNE(L"abc", L"Abc"); |
| 2520 | |
| 2521 | // Strings containing wide characters. |
| 2522 | EXPECT_NONFATAL_FAILURE(EXPECT_STRNE(L"abc\x8119", L"abc\x8119"), |
| 2523 | "abc"); |
| 2524 | } |
| 2525 | |
| 2526 | // Tests for ::testing::IsSubstring(). |
| 2527 | |
| 2528 | // Tests that IsSubstring() returns the correct result when the input |
| 2529 | // argument type is const char*. |
| 2530 | TEST(IsSubstringTest, ReturnsCorrectResultForCString) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2531 | EXPECT_FALSE(IsSubstring("", "", NULL, "a")); |
| 2532 | EXPECT_FALSE(IsSubstring("", "", "b", NULL)); |
| 2533 | EXPECT_FALSE(IsSubstring("", "", "needle", "haystack")); |
| 2534 | |
| 2535 | EXPECT_TRUE(IsSubstring("", "", static_cast<const char*>(NULL), NULL)); |
| 2536 | EXPECT_TRUE(IsSubstring("", "", "needle", "two needles")); |
| 2537 | } |
| 2538 | |
| 2539 | // Tests that IsSubstring() returns the correct result when the input |
| 2540 | // argument type is const wchar_t*. |
| 2541 | TEST(IsSubstringTest, ReturnsCorrectResultForWideCString) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2542 | EXPECT_FALSE(IsSubstring("", "", kNull, L"a")); |
| 2543 | EXPECT_FALSE(IsSubstring("", "", L"b", kNull)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2544 | EXPECT_FALSE(IsSubstring("", "", L"needle", L"haystack")); |
| 2545 | |
| 2546 | EXPECT_TRUE(IsSubstring("", "", static_cast<const wchar_t*>(NULL), NULL)); |
| 2547 | EXPECT_TRUE(IsSubstring("", "", L"needle", L"two needles")); |
| 2548 | } |
| 2549 | |
| 2550 | // Tests that IsSubstring() generates the correct message when the input |
| 2551 | // argument type is const char*. |
| 2552 | TEST(IsSubstringTest, GeneratesCorrectMessageForCString) { |
| 2553 | EXPECT_STREQ("Value of: needle_expr\n" |
| 2554 | " Actual: \"needle\"\n" |
| 2555 | "Expected: a substring of haystack_expr\n" |
| 2556 | "Which is: \"haystack\"", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2557 | IsSubstring("needle_expr", "haystack_expr", |
| 2558 | "needle", "haystack").failure_message()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2561 | // Tests that IsSubstring returns the correct result when the input |
| 2562 | // argument type is ::std::string. |
| 2563 | TEST(IsSubstringTest, ReturnsCorrectResultsForStdString) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2564 | EXPECT_TRUE(IsSubstring("", "", std::string("hello"), "ahellob")); |
| 2565 | EXPECT_FALSE(IsSubstring("", "", "hello", std::string("world"))); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2568 | #if GTEST_HAS_STD_WSTRING |
| 2569 | // Tests that IsSubstring returns the correct result when the input |
| 2570 | // argument type is ::std::wstring. |
| 2571 | TEST(IsSubstringTest, ReturnsCorrectResultForStdWstring) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2572 | EXPECT_TRUE(IsSubstring("", "", ::std::wstring(L"needle"), L"two needles")); |
| 2573 | EXPECT_FALSE(IsSubstring("", "", L"needle", ::std::wstring(L"haystack"))); |
| 2574 | } |
| 2575 | |
| 2576 | // Tests that IsSubstring() generates the correct message when the input |
| 2577 | // argument type is ::std::wstring. |
| 2578 | TEST(IsSubstringTest, GeneratesCorrectMessageForWstring) { |
| 2579 | EXPECT_STREQ("Value of: needle_expr\n" |
| 2580 | " Actual: L\"needle\"\n" |
| 2581 | "Expected: a substring of haystack_expr\n" |
| 2582 | "Which is: L\"haystack\"", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2583 | IsSubstring( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2584 | "needle_expr", "haystack_expr", |
| 2585 | ::std::wstring(L"needle"), L"haystack").failure_message()); |
| 2586 | } |
| 2587 | |
| 2588 | #endif // GTEST_HAS_STD_WSTRING |
| 2589 | |
| 2590 | // Tests for ::testing::IsNotSubstring(). |
| 2591 | |
| 2592 | // Tests that IsNotSubstring() returns the correct result when the input |
| 2593 | // argument type is const char*. |
| 2594 | TEST(IsNotSubstringTest, ReturnsCorrectResultForCString) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2595 | EXPECT_TRUE(IsNotSubstring("", "", "needle", "haystack")); |
| 2596 | EXPECT_FALSE(IsNotSubstring("", "", "needle", "two needles")); |
| 2597 | } |
| 2598 | |
| 2599 | // Tests that IsNotSubstring() returns the correct result when the input |
| 2600 | // argument type is const wchar_t*. |
| 2601 | TEST(IsNotSubstringTest, ReturnsCorrectResultForWideCString) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2602 | EXPECT_TRUE(IsNotSubstring("", "", L"needle", L"haystack")); |
| 2603 | EXPECT_FALSE(IsNotSubstring("", "", L"needle", L"two needles")); |
| 2604 | } |
| 2605 | |
| 2606 | // Tests that IsNotSubstring() generates the correct message when the input |
| 2607 | // argument type is const wchar_t*. |
| 2608 | TEST(IsNotSubstringTest, GeneratesCorrectMessageForWideCString) { |
| 2609 | EXPECT_STREQ("Value of: needle_expr\n" |
| 2610 | " Actual: L\"needle\"\n" |
| 2611 | "Expected: not a substring of haystack_expr\n" |
| 2612 | "Which is: L\"two needles\"", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2613 | IsNotSubstring( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2614 | "needle_expr", "haystack_expr", |
| 2615 | L"needle", L"two needles").failure_message()); |
| 2616 | } |
| 2617 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2618 | // Tests that IsNotSubstring returns the correct result when the input |
| 2619 | // argument type is ::std::string. |
| 2620 | TEST(IsNotSubstringTest, ReturnsCorrectResultsForStdString) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2621 | EXPECT_FALSE(IsNotSubstring("", "", std::string("hello"), "ahellob")); |
| 2622 | EXPECT_TRUE(IsNotSubstring("", "", "hello", std::string("world"))); |
| 2623 | } |
| 2624 | |
| 2625 | // Tests that IsNotSubstring() generates the correct message when the input |
| 2626 | // argument type is ::std::string. |
| 2627 | TEST(IsNotSubstringTest, GeneratesCorrectMessageForStdString) { |
| 2628 | EXPECT_STREQ("Value of: needle_expr\n" |
| 2629 | " Actual: \"needle\"\n" |
| 2630 | "Expected: not a substring of haystack_expr\n" |
| 2631 | "Which is: \"two needles\"", |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2632 | IsNotSubstring( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2633 | "needle_expr", "haystack_expr", |
| 2634 | ::std::string("needle"), "two needles").failure_message()); |
| 2635 | } |
| 2636 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2637 | #if GTEST_HAS_STD_WSTRING |
| 2638 | |
| 2639 | // Tests that IsNotSubstring returns the correct result when the input |
| 2640 | // argument type is ::std::wstring. |
| 2641 | TEST(IsNotSubstringTest, ReturnsCorrectResultForStdWstring) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2642 | EXPECT_FALSE( |
| 2643 | IsNotSubstring("", "", ::std::wstring(L"needle"), L"two needles")); |
| 2644 | EXPECT_TRUE(IsNotSubstring("", "", L"needle", ::std::wstring(L"haystack"))); |
| 2645 | } |
| 2646 | |
| 2647 | #endif // GTEST_HAS_STD_WSTRING |
| 2648 | |
| 2649 | // Tests floating-point assertions. |
| 2650 | |
| 2651 | template <typename RawType> |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2652 | class FloatingPointTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2653 | protected: |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2654 | |
| 2655 | // Pre-calculated numbers to be used by the tests. |
| 2656 | struct TestValues { |
| 2657 | RawType close_to_positive_zero; |
| 2658 | RawType close_to_negative_zero; |
| 2659 | RawType further_from_negative_zero; |
| 2660 | |
| 2661 | RawType close_to_one; |
| 2662 | RawType further_from_one; |
| 2663 | |
| 2664 | RawType infinity; |
| 2665 | RawType close_to_infinity; |
| 2666 | RawType further_from_infinity; |
| 2667 | |
| 2668 | RawType nan1; |
| 2669 | RawType nan2; |
| 2670 | }; |
| 2671 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2672 | typedef typename testing::internal::FloatingPoint<RawType> Floating; |
| 2673 | typedef typename Floating::Bits Bits; |
| 2674 | |
| 2675 | virtual void SetUp() { |
| 2676 | const size_t max_ulps = Floating::kMaxUlps; |
| 2677 | |
| 2678 | // The bits that represent 0.0. |
| 2679 | const Bits zero_bits = Floating(0).bits(); |
| 2680 | |
| 2681 | // Makes some numbers close to 0.0. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2682 | values_.close_to_positive_zero = Floating::ReinterpretBits( |
| 2683 | zero_bits + max_ulps/2); |
| 2684 | values_.close_to_negative_zero = -Floating::ReinterpretBits( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2685 | zero_bits + max_ulps - max_ulps/2); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2686 | values_.further_from_negative_zero = -Floating::ReinterpretBits( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2687 | zero_bits + max_ulps + 1 - max_ulps/2); |
| 2688 | |
| 2689 | // The bits that represent 1.0. |
| 2690 | const Bits one_bits = Floating(1).bits(); |
| 2691 | |
| 2692 | // Makes some numbers close to 1.0. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2693 | values_.close_to_one = Floating::ReinterpretBits(one_bits + max_ulps); |
| 2694 | values_.further_from_one = Floating::ReinterpretBits( |
| 2695 | one_bits + max_ulps + 1); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2696 | |
| 2697 | // +infinity. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2698 | values_.infinity = Floating::Infinity(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2699 | |
| 2700 | // The bits that represent +infinity. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2701 | const Bits infinity_bits = Floating(values_.infinity).bits(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2702 | |
| 2703 | // Makes some numbers close to infinity. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2704 | values_.close_to_infinity = Floating::ReinterpretBits( |
| 2705 | infinity_bits - max_ulps); |
| 2706 | values_.further_from_infinity = Floating::ReinterpretBits( |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2707 | infinity_bits - max_ulps - 1); |
| 2708 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2709 | // Makes some NAN's. Sets the most significant bit of the fraction so that |
| 2710 | // our NaN's are quiet; trying to process a signaling NaN would raise an |
| 2711 | // exception if our environment enables floating point exceptions. |
| 2712 | values_.nan1 = Floating::ReinterpretBits(Floating::kExponentBitMask |
| 2713 | | (static_cast<Bits>(1) << (Floating::kFractionBitCount - 1)) | 1); |
| 2714 | values_.nan2 = Floating::ReinterpretBits(Floating::kExponentBitMask |
| 2715 | | (static_cast<Bits>(1) << (Floating::kFractionBitCount - 1)) | 200); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | void TestSize() { |
| 2719 | EXPECT_EQ(sizeof(RawType), sizeof(Bits)); |
| 2720 | } |
| 2721 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2722 | static TestValues values_; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2723 | }; |
| 2724 | |
| 2725 | template <typename RawType> |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2726 | typename FloatingPointTest<RawType>::TestValues |
| 2727 | FloatingPointTest<RawType>::values_; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2728 | |
| 2729 | // Instantiates FloatingPointTest for testing *_FLOAT_EQ. |
| 2730 | typedef FloatingPointTest<float> FloatTest; |
| 2731 | |
| 2732 | // Tests that the size of Float::Bits matches the size of float. |
| 2733 | TEST_F(FloatTest, Size) { |
| 2734 | TestSize(); |
| 2735 | } |
| 2736 | |
| 2737 | // Tests comparing with +0 and -0. |
| 2738 | TEST_F(FloatTest, Zeros) { |
| 2739 | EXPECT_FLOAT_EQ(0.0, -0.0); |
| 2740 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(-0.0, 1.0), |
| 2741 | "1.0"); |
| 2742 | EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(0.0, 1.5), |
| 2743 | "1.5"); |
| 2744 | } |
| 2745 | |
| 2746 | // Tests comparing numbers close to 0. |
| 2747 | // |
| 2748 | // This ensures that *_FLOAT_EQ handles the sign correctly and no |
| 2749 | // overflow occurs when comparing numbers whose absolute value is very |
| 2750 | // small. |
| 2751 | TEST_F(FloatTest, AlmostZeros) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2752 | // In C++Builder, names within local classes (such as used by |
| 2753 | // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the |
| 2754 | // scoping class. Use a static local alias as a workaround. |
vladlosev | 673a0cb | 2010-02-03 02:27:02 +0000 | [diff] [blame] | 2755 | // We use the assignment syntax since some compilers, like Sun Studio, |
| 2756 | // don't allow initializing references using construction syntax |
| 2757 | // (parentheses). |
| 2758 | static const FloatTest::TestValues& v = this->values_; |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2759 | |
| 2760 | EXPECT_FLOAT_EQ(0.0, v.close_to_positive_zero); |
| 2761 | EXPECT_FLOAT_EQ(-0.0, v.close_to_negative_zero); |
| 2762 | EXPECT_FLOAT_EQ(v.close_to_positive_zero, v.close_to_negative_zero); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2763 | |
| 2764 | EXPECT_FATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2765 | ASSERT_FLOAT_EQ(v.close_to_positive_zero, |
| 2766 | v.further_from_negative_zero); |
| 2767 | }, "v.further_from_negative_zero"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | // Tests comparing numbers close to each other. |
| 2771 | TEST_F(FloatTest, SmallDiff) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2772 | EXPECT_FLOAT_EQ(1.0, values_.close_to_one); |
| 2773 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(1.0, values_.further_from_one), |
| 2774 | "values_.further_from_one"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
| 2777 | // Tests comparing numbers far apart. |
| 2778 | TEST_F(FloatTest, LargeDiff) { |
| 2779 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(2.5, 3.0), |
| 2780 | "3.0"); |
| 2781 | } |
| 2782 | |
| 2783 | // Tests comparing with infinity. |
| 2784 | // |
| 2785 | // This ensures that no overflow occurs when comparing numbers whose |
| 2786 | // absolute value is very large. |
| 2787 | TEST_F(FloatTest, Infinity) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2788 | EXPECT_FLOAT_EQ(values_.infinity, values_.close_to_infinity); |
| 2789 | EXPECT_FLOAT_EQ(-values_.infinity, -values_.close_to_infinity); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2790 | #if !GTEST_OS_SYMBIAN |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2791 | // Nokia's STLport crashes if we try to output infinity or NaN. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2792 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.infinity, -values_.infinity), |
| 2793 | "-values_.infinity"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2794 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2795 | // This is interesting as the representations of infinity and nan1 |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2796 | // are only 1 DLP apart. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2797 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.infinity, values_.nan1), |
| 2798 | "values_.nan1"); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2799 | #endif // !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | // Tests that comparing with NAN always returns false. |
| 2803 | TEST_F(FloatTest, NaN) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2804 | #if !GTEST_OS_SYMBIAN |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2805 | // Nokia's STLport crashes if we try to output infinity or NaN. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2806 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2807 | // In C++Builder, names within local classes (such as used by |
| 2808 | // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the |
| 2809 | // scoping class. Use a static local alias as a workaround. |
vladlosev | 673a0cb | 2010-02-03 02:27:02 +0000 | [diff] [blame] | 2810 | // We use the assignment syntax since some compilers, like Sun Studio, |
| 2811 | // don't allow initializing references using construction syntax |
| 2812 | // (parentheses). |
| 2813 | static const FloatTest::TestValues& v = this->values_; |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2814 | |
| 2815 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(v.nan1, v.nan1), |
| 2816 | "v.nan1"); |
| 2817 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(v.nan1, v.nan2), |
| 2818 | "v.nan2"); |
| 2819 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(1.0, v.nan1), |
| 2820 | "v.nan1"); |
| 2821 | |
| 2822 | EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(v.nan1, v.infinity), |
| 2823 | "v.infinity"); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2824 | #endif // !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | // Tests that *_FLOAT_EQ are reflexive. |
| 2828 | TEST_F(FloatTest, Reflexive) { |
| 2829 | EXPECT_FLOAT_EQ(0.0, 0.0); |
| 2830 | EXPECT_FLOAT_EQ(1.0, 1.0); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2831 | ASSERT_FLOAT_EQ(values_.infinity, values_.infinity); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | // Tests that *_FLOAT_EQ are commutative. |
| 2835 | TEST_F(FloatTest, Commutative) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2836 | // We already tested EXPECT_FLOAT_EQ(1.0, values_.close_to_one). |
| 2837 | EXPECT_FLOAT_EQ(values_.close_to_one, 1.0); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2838 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2839 | // We already tested EXPECT_FLOAT_EQ(1.0, values_.further_from_one). |
| 2840 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.further_from_one, 1.0), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2841 | "1.0"); |
| 2842 | } |
| 2843 | |
| 2844 | // Tests EXPECT_NEAR. |
| 2845 | TEST_F(FloatTest, EXPECT_NEAR) { |
| 2846 | EXPECT_NEAR(-1.0f, -1.1f, 0.2f); |
| 2847 | EXPECT_NEAR(2.0f, 3.0f, 1.0f); |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 2848 | EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0f,1.5f, 0.25f), // NOLINT |
| 2849 | "The difference between 1.0f and 1.5f is 0.5, " |
| 2850 | "which exceeds 0.25f"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2851 | // To work around a bug in gcc 2.95.0, there is intentionally no |
| 2852 | // space after the first comma in the previous line. |
| 2853 | } |
| 2854 | |
| 2855 | // Tests ASSERT_NEAR. |
| 2856 | TEST_F(FloatTest, ASSERT_NEAR) { |
| 2857 | ASSERT_NEAR(-1.0f, -1.1f, 0.2f); |
| 2858 | ASSERT_NEAR(2.0f, 3.0f, 1.0f); |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 2859 | EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0f,1.5f, 0.25f), // NOLINT |
| 2860 | "The difference between 1.0f and 1.5f is 0.5, " |
| 2861 | "which exceeds 0.25f"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2862 | // To work around a bug in gcc 2.95.0, there is intentionally no |
| 2863 | // space after the first comma in the previous line. |
| 2864 | } |
| 2865 | |
| 2866 | // Tests the cases where FloatLE() should succeed. |
| 2867 | TEST_F(FloatTest, FloatLESucceeds) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2868 | EXPECT_PRED_FORMAT2(FloatLE, 1.0f, 2.0f); // When val1 < val2, |
| 2869 | ASSERT_PRED_FORMAT2(FloatLE, 1.0f, 1.0f); // val1 == val2, |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2870 | |
| 2871 | // or when val1 is greater than, but almost equals to, val2. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2872 | EXPECT_PRED_FORMAT2(FloatLE, values_.close_to_positive_zero, 0.0f); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | // Tests the cases where FloatLE() should fail. |
| 2876 | TEST_F(FloatTest, FloatLEFails) { |
| 2877 | // When val1 is greater than val2 by a large margin, |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 2878 | EXPECT_NONFATAL_FAILURE(EXPECT_PRED_FORMAT2(FloatLE, 2.0f, 1.0f), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2879 | "(2.0f) <= (1.0f)"); |
| 2880 | |
| 2881 | // or by a small yet non-negligible margin, |
| 2882 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2883 | EXPECT_PRED_FORMAT2(FloatLE, values_.further_from_one, 1.0f); |
| 2884 | }, "(values_.further_from_one) <= (1.0f)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2885 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2886 | #if !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2887 | // Nokia's STLport crashes if we try to output infinity or NaN. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2888 | // C++Builder gives bad results for ordered comparisons involving NaNs |
| 2889 | // due to compiler bugs. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2890 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2891 | EXPECT_PRED_FORMAT2(FloatLE, values_.nan1, values_.infinity); |
| 2892 | }, "(values_.nan1) <= (values_.infinity)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2893 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2894 | EXPECT_PRED_FORMAT2(FloatLE, -values_.infinity, values_.nan1); |
| 2895 | }, "(-values_.infinity) <= (values_.nan1)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2896 | EXPECT_FATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2897 | ASSERT_PRED_FORMAT2(FloatLE, values_.nan1, values_.nan1); |
| 2898 | }, "(values_.nan1) <= (values_.nan1)"); |
| 2899 | #endif // !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | // Instantiates FloatingPointTest for testing *_DOUBLE_EQ. |
| 2903 | typedef FloatingPointTest<double> DoubleTest; |
| 2904 | |
| 2905 | // Tests that the size of Double::Bits matches the size of double. |
| 2906 | TEST_F(DoubleTest, Size) { |
| 2907 | TestSize(); |
| 2908 | } |
| 2909 | |
| 2910 | // Tests comparing with +0 and -0. |
| 2911 | TEST_F(DoubleTest, Zeros) { |
| 2912 | EXPECT_DOUBLE_EQ(0.0, -0.0); |
| 2913 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(-0.0, 1.0), |
| 2914 | "1.0"); |
| 2915 | EXPECT_FATAL_FAILURE(ASSERT_DOUBLE_EQ(0.0, 1.0), |
| 2916 | "1.0"); |
| 2917 | } |
| 2918 | |
| 2919 | // Tests comparing numbers close to 0. |
| 2920 | // |
| 2921 | // This ensures that *_DOUBLE_EQ handles the sign correctly and no |
| 2922 | // overflow occurs when comparing numbers whose absolute value is very |
| 2923 | // small. |
| 2924 | TEST_F(DoubleTest, AlmostZeros) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2925 | // In C++Builder, names within local classes (such as used by |
| 2926 | // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the |
| 2927 | // scoping class. Use a static local alias as a workaround. |
vladlosev | 673a0cb | 2010-02-03 02:27:02 +0000 | [diff] [blame] | 2928 | // We use the assignment syntax since some compilers, like Sun Studio, |
| 2929 | // don't allow initializing references using construction syntax |
| 2930 | // (parentheses). |
| 2931 | static const DoubleTest::TestValues& v = this->values_; |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2932 | |
| 2933 | EXPECT_DOUBLE_EQ(0.0, v.close_to_positive_zero); |
| 2934 | EXPECT_DOUBLE_EQ(-0.0, v.close_to_negative_zero); |
| 2935 | EXPECT_DOUBLE_EQ(v.close_to_positive_zero, v.close_to_negative_zero); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2936 | |
| 2937 | EXPECT_FATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2938 | ASSERT_DOUBLE_EQ(v.close_to_positive_zero, |
| 2939 | v.further_from_negative_zero); |
| 2940 | }, "v.further_from_negative_zero"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | // Tests comparing numbers close to each other. |
| 2944 | TEST_F(DoubleTest, SmallDiff) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2945 | EXPECT_DOUBLE_EQ(1.0, values_.close_to_one); |
| 2946 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(1.0, values_.further_from_one), |
| 2947 | "values_.further_from_one"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2948 | } |
| 2949 | |
| 2950 | // Tests comparing numbers far apart. |
| 2951 | TEST_F(DoubleTest, LargeDiff) { |
| 2952 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(2.0, 3.0), |
| 2953 | "3.0"); |
| 2954 | } |
| 2955 | |
| 2956 | // Tests comparing with infinity. |
| 2957 | // |
| 2958 | // This ensures that no overflow occurs when comparing numbers whose |
| 2959 | // absolute value is very large. |
| 2960 | TEST_F(DoubleTest, Infinity) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2961 | EXPECT_DOUBLE_EQ(values_.infinity, values_.close_to_infinity); |
| 2962 | EXPECT_DOUBLE_EQ(-values_.infinity, -values_.close_to_infinity); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2963 | #if !GTEST_OS_SYMBIAN |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2964 | // Nokia's STLport crashes if we try to output infinity or NaN. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2965 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(values_.infinity, -values_.infinity), |
| 2966 | "-values_.infinity"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2967 | |
| 2968 | // This is interesting as the representations of infinity_ and nan1_ |
| 2969 | // are only 1 DLP apart. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2970 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(values_.infinity, values_.nan1), |
| 2971 | "values_.nan1"); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2972 | #endif // !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
| 2975 | // Tests that comparing with NAN always returns false. |
| 2976 | TEST_F(DoubleTest, NaN) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2977 | #if !GTEST_OS_SYMBIAN |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2978 | // In C++Builder, names within local classes (such as used by |
| 2979 | // EXPECT_FATAL_FAILURE) cannot be resolved against static members of the |
| 2980 | // scoping class. Use a static local alias as a workaround. |
vladlosev | 673a0cb | 2010-02-03 02:27:02 +0000 | [diff] [blame] | 2981 | // We use the assignment syntax since some compilers, like Sun Studio, |
| 2982 | // don't allow initializing references using construction syntax |
| 2983 | // (parentheses). |
| 2984 | static const DoubleTest::TestValues& v = this->values_; |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2985 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 2986 | // Nokia's STLport crashes if we try to output infinity or NaN. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 2987 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(v.nan1, v.nan1), |
| 2988 | "v.nan1"); |
| 2989 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(v.nan1, v.nan2), "v.nan2"); |
| 2990 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(1.0, v.nan1), "v.nan1"); |
| 2991 | EXPECT_FATAL_FAILURE(ASSERT_DOUBLE_EQ(v.nan1, v.infinity), |
| 2992 | "v.infinity"); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 2993 | #endif // !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
| 2996 | // Tests that *_DOUBLE_EQ are reflexive. |
| 2997 | TEST_F(DoubleTest, Reflexive) { |
| 2998 | EXPECT_DOUBLE_EQ(0.0, 0.0); |
| 2999 | EXPECT_DOUBLE_EQ(1.0, 1.0); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3000 | #if !GTEST_OS_SYMBIAN |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3001 | // Nokia's STLport crashes if we try to output infinity or NaN. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3002 | ASSERT_DOUBLE_EQ(values_.infinity, values_.infinity); |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3003 | #endif // !GTEST_OS_SYMBIAN |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | // Tests that *_DOUBLE_EQ are commutative. |
| 3007 | TEST_F(DoubleTest, Commutative) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3008 | // We already tested EXPECT_DOUBLE_EQ(1.0, values_.close_to_one). |
| 3009 | EXPECT_DOUBLE_EQ(values_.close_to_one, 1.0); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3010 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3011 | // We already tested EXPECT_DOUBLE_EQ(1.0, values_.further_from_one). |
| 3012 | EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(values_.further_from_one, 1.0), |
| 3013 | "1.0"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | // Tests EXPECT_NEAR. |
| 3017 | TEST_F(DoubleTest, EXPECT_NEAR) { |
| 3018 | EXPECT_NEAR(-1.0, -1.1, 0.2); |
| 3019 | EXPECT_NEAR(2.0, 3.0, 1.0); |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 3020 | EXPECT_NONFATAL_FAILURE(EXPECT_NEAR(1.0, 1.5, 0.25), // NOLINT |
| 3021 | "The difference between 1.0 and 1.5 is 0.5, " |
| 3022 | "which exceeds 0.25"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3023 | // To work around a bug in gcc 2.95.0, there is intentionally no |
| 3024 | // space after the first comma in the previous statement. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | // Tests ASSERT_NEAR. |
| 3028 | TEST_F(DoubleTest, ASSERT_NEAR) { |
| 3029 | ASSERT_NEAR(-1.0, -1.1, 0.2); |
| 3030 | ASSERT_NEAR(2.0, 3.0, 1.0); |
zhanyong.wan | 65de7e0 | 2010-01-08 00:23:45 +0000 | [diff] [blame] | 3031 | EXPECT_FATAL_FAILURE(ASSERT_NEAR(1.0, 1.5, 0.25), // NOLINT |
| 3032 | "The difference between 1.0 and 1.5 is 0.5, " |
| 3033 | "which exceeds 0.25"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3034 | // To work around a bug in gcc 2.95.0, there is intentionally no |
| 3035 | // space after the first comma in the previous statement. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | // Tests the cases where DoubleLE() should succeed. |
| 3039 | TEST_F(DoubleTest, DoubleLESucceeds) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3040 | EXPECT_PRED_FORMAT2(DoubleLE, 1.0, 2.0); // When val1 < val2, |
| 3041 | ASSERT_PRED_FORMAT2(DoubleLE, 1.0, 1.0); // val1 == val2, |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3042 | |
| 3043 | // or when val1 is greater than, but almost equals to, val2. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3044 | EXPECT_PRED_FORMAT2(DoubleLE, values_.close_to_positive_zero, 0.0); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | // Tests the cases where DoubleLE() should fail. |
| 3048 | TEST_F(DoubleTest, DoubleLEFails) { |
| 3049 | // When val1 is greater than val2 by a large margin, |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3050 | EXPECT_NONFATAL_FAILURE(EXPECT_PRED_FORMAT2(DoubleLE, 2.0, 1.0), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3051 | "(2.0) <= (1.0)"); |
| 3052 | |
| 3053 | // or by a small yet non-negligible margin, |
| 3054 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3055 | EXPECT_PRED_FORMAT2(DoubleLE, values_.further_from_one, 1.0); |
| 3056 | }, "(values_.further_from_one) <= (1.0)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3057 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3058 | #if !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3059 | // Nokia's STLport crashes if we try to output infinity or NaN. |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3060 | // C++Builder gives bad results for ordered comparisons involving NaNs |
| 3061 | // due to compiler bugs. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3062 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3063 | EXPECT_PRED_FORMAT2(DoubleLE, values_.nan1, values_.infinity); |
| 3064 | }, "(values_.nan1) <= (values_.infinity)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3065 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3066 | EXPECT_PRED_FORMAT2(DoubleLE, -values_.infinity, values_.nan1); |
| 3067 | }, " (-values_.infinity) <= (values_.nan1)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3068 | EXPECT_FATAL_FAILURE({ // NOLINT |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3069 | ASSERT_PRED_FORMAT2(DoubleLE, values_.nan1, values_.nan1); |
| 3070 | }, "(values_.nan1) <= (values_.nan1)"); |
| 3071 | #endif // !GTEST_OS_SYMBIAN && !defined(__BORLANDC__) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | |
| 3075 | // Verifies that a test or test case whose name starts with DISABLED_ is |
| 3076 | // not run. |
| 3077 | |
| 3078 | // A test whose name starts with DISABLED_. |
| 3079 | // Should not run. |
| 3080 | TEST(DisabledTest, DISABLED_TestShouldNotRun) { |
| 3081 | FAIL() << "Unexpected failure: Disabled test should not be run."; |
| 3082 | } |
| 3083 | |
| 3084 | // A test whose name does not start with DISABLED_. |
| 3085 | // Should run. |
| 3086 | TEST(DisabledTest, NotDISABLED_TestShouldRun) { |
| 3087 | EXPECT_EQ(1, 1); |
| 3088 | } |
| 3089 | |
| 3090 | // A test case whose name starts with DISABLED_. |
| 3091 | // Should not run. |
| 3092 | TEST(DISABLED_TestCase, TestShouldNotRun) { |
| 3093 | FAIL() << "Unexpected failure: Test in disabled test case should not be run."; |
| 3094 | } |
| 3095 | |
| 3096 | // A test case and test whose names start with DISABLED_. |
| 3097 | // Should not run. |
| 3098 | TEST(DISABLED_TestCase, DISABLED_TestShouldNotRun) { |
| 3099 | FAIL() << "Unexpected failure: Test in disabled test case should not be run."; |
| 3100 | } |
| 3101 | |
| 3102 | // Check that when all tests in a test case are disabled, SetupTestCase() and |
| 3103 | // TearDownTestCase() are not called. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3104 | class DisabledTestsTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3105 | protected: |
| 3106 | static void SetUpTestCase() { |
| 3107 | FAIL() << "Unexpected failure: All tests disabled in test case. " |
| 3108 | "SetupTestCase() should not be called."; |
| 3109 | } |
| 3110 | |
| 3111 | static void TearDownTestCase() { |
| 3112 | FAIL() << "Unexpected failure: All tests disabled in test case. " |
| 3113 | "TearDownTestCase() should not be called."; |
| 3114 | } |
| 3115 | }; |
| 3116 | |
| 3117 | TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_1) { |
| 3118 | FAIL() << "Unexpected failure: Disabled test should not be run."; |
| 3119 | } |
| 3120 | |
| 3121 | TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_2) { |
| 3122 | FAIL() << "Unexpected failure: Disabled test should not be run."; |
| 3123 | } |
| 3124 | |
shiqian | e8ff148 | 2008-09-08 17:55:52 +0000 | [diff] [blame] | 3125 | // Tests that disabled typed tests aren't run. |
| 3126 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3127 | #if GTEST_HAS_TYPED_TEST |
shiqian | e8ff148 | 2008-09-08 17:55:52 +0000 | [diff] [blame] | 3128 | |
| 3129 | template <typename T> |
| 3130 | class TypedTest : public Test { |
| 3131 | }; |
| 3132 | |
| 3133 | typedef testing::Types<int, double> NumericTypes; |
| 3134 | TYPED_TEST_CASE(TypedTest, NumericTypes); |
| 3135 | |
| 3136 | TYPED_TEST(TypedTest, DISABLED_ShouldNotRun) { |
| 3137 | FAIL() << "Unexpected failure: Disabled typed test should not run."; |
| 3138 | } |
| 3139 | |
| 3140 | template <typename T> |
| 3141 | class DISABLED_TypedTest : public Test { |
| 3142 | }; |
| 3143 | |
| 3144 | TYPED_TEST_CASE(DISABLED_TypedTest, NumericTypes); |
| 3145 | |
| 3146 | TYPED_TEST(DISABLED_TypedTest, ShouldNotRun) { |
| 3147 | FAIL() << "Unexpected failure: Disabled typed test should not run."; |
| 3148 | } |
| 3149 | |
| 3150 | #endif // GTEST_HAS_TYPED_TEST |
| 3151 | |
| 3152 | // Tests that disabled type-parameterized tests aren't run. |
| 3153 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3154 | #if GTEST_HAS_TYPED_TEST_P |
shiqian | e8ff148 | 2008-09-08 17:55:52 +0000 | [diff] [blame] | 3155 | |
| 3156 | template <typename T> |
| 3157 | class TypedTestP : public Test { |
| 3158 | }; |
| 3159 | |
| 3160 | TYPED_TEST_CASE_P(TypedTestP); |
| 3161 | |
| 3162 | TYPED_TEST_P(TypedTestP, DISABLED_ShouldNotRun) { |
| 3163 | FAIL() << "Unexpected failure: " |
| 3164 | << "Disabled type-parameterized test should not run."; |
| 3165 | } |
| 3166 | |
| 3167 | REGISTER_TYPED_TEST_CASE_P(TypedTestP, DISABLED_ShouldNotRun); |
| 3168 | |
| 3169 | INSTANTIATE_TYPED_TEST_CASE_P(My, TypedTestP, NumericTypes); |
| 3170 | |
| 3171 | template <typename T> |
| 3172 | class DISABLED_TypedTestP : public Test { |
| 3173 | }; |
| 3174 | |
| 3175 | TYPED_TEST_CASE_P(DISABLED_TypedTestP); |
| 3176 | |
| 3177 | TYPED_TEST_P(DISABLED_TypedTestP, ShouldNotRun) { |
| 3178 | FAIL() << "Unexpected failure: " |
| 3179 | << "Disabled type-parameterized test should not run."; |
| 3180 | } |
| 3181 | |
| 3182 | REGISTER_TYPED_TEST_CASE_P(DISABLED_TypedTestP, ShouldNotRun); |
| 3183 | |
| 3184 | INSTANTIATE_TYPED_TEST_CASE_P(My, DISABLED_TypedTestP, NumericTypes); |
| 3185 | |
| 3186 | #endif // GTEST_HAS_TYPED_TEST_P |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3187 | |
| 3188 | // Tests that assertion macros evaluate their arguments exactly once. |
| 3189 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3190 | class SingleEvaluationTest : public Test { |
tsunanet | acd0f32 | 2009-05-18 20:53:57 +0000 | [diff] [blame] | 3191 | 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] | 3192 | // This helper function is needed by the FailedASSERT_STREQ test |
| 3193 | // below. It's public to work around C++Builder's bug with scoping local |
| 3194 | // classes. |
| 3195 | static void CompareAndIncrementCharPtrs() { |
| 3196 | ASSERT_STREQ(p1_++, p2_++); |
| 3197 | } |
| 3198 | |
| 3199 | // This helper function is needed by the FailedASSERT_NE test below. It's |
| 3200 | // public to work around C++Builder's bug with scoping local classes. |
| 3201 | static void CompareAndIncrementInts() { |
| 3202 | ASSERT_NE(a_++, b_++); |
| 3203 | } |
| 3204 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3205 | protected: |
| 3206 | SingleEvaluationTest() { |
| 3207 | p1_ = s1_; |
| 3208 | p2_ = s2_; |
| 3209 | a_ = 0; |
| 3210 | b_ = 0; |
| 3211 | } |
| 3212 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3213 | static const char* const s1_; |
| 3214 | static const char* const s2_; |
| 3215 | static const char* p1_; |
| 3216 | static const char* p2_; |
| 3217 | |
| 3218 | static int a_; |
| 3219 | static int b_; |
| 3220 | }; |
| 3221 | |
| 3222 | const char* const SingleEvaluationTest::s1_ = "01234"; |
| 3223 | const char* const SingleEvaluationTest::s2_ = "abcde"; |
| 3224 | const char* SingleEvaluationTest::p1_; |
| 3225 | const char* SingleEvaluationTest::p2_; |
| 3226 | int SingleEvaluationTest::a_; |
| 3227 | int SingleEvaluationTest::b_; |
| 3228 | |
| 3229 | // Tests that when ASSERT_STREQ fails, it evaluates its arguments |
| 3230 | // exactly once. |
| 3231 | TEST_F(SingleEvaluationTest, FailedASSERT_STREQ) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3232 | EXPECT_FATAL_FAILURE(SingleEvaluationTest::CompareAndIncrementCharPtrs(), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3233 | "p2_++"); |
| 3234 | EXPECT_EQ(s1_ + 1, p1_); |
| 3235 | EXPECT_EQ(s2_ + 1, p2_); |
| 3236 | } |
| 3237 | |
| 3238 | // Tests that string assertion arguments are evaluated exactly once. |
| 3239 | TEST_F(SingleEvaluationTest, ASSERT_STR) { |
| 3240 | // successful EXPECT_STRNE |
| 3241 | EXPECT_STRNE(p1_++, p2_++); |
| 3242 | EXPECT_EQ(s1_ + 1, p1_); |
| 3243 | EXPECT_EQ(s2_ + 1, p2_); |
| 3244 | |
| 3245 | // failed EXPECT_STRCASEEQ |
| 3246 | EXPECT_NONFATAL_FAILURE(EXPECT_STRCASEEQ(p1_++, p2_++), |
| 3247 | "ignoring case"); |
| 3248 | EXPECT_EQ(s1_ + 2, p1_); |
| 3249 | EXPECT_EQ(s2_ + 2, p2_); |
| 3250 | } |
| 3251 | |
| 3252 | // Tests that when ASSERT_NE fails, it evaluates its arguments exactly |
| 3253 | // once. |
| 3254 | TEST_F(SingleEvaluationTest, FailedASSERT_NE) { |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3255 | EXPECT_FATAL_FAILURE(SingleEvaluationTest::CompareAndIncrementInts(), |
| 3256 | "(a_++) != (b_++)"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3257 | EXPECT_EQ(1, a_); |
| 3258 | EXPECT_EQ(1, b_); |
| 3259 | } |
| 3260 | |
| 3261 | // Tests that assertion arguments are evaluated exactly once. |
| 3262 | TEST_F(SingleEvaluationTest, OtherCases) { |
| 3263 | // successful EXPECT_TRUE |
| 3264 | EXPECT_TRUE(0 == a_++); // NOLINT |
| 3265 | EXPECT_EQ(1, a_); |
| 3266 | |
| 3267 | // failed EXPECT_TRUE |
| 3268 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(-1 == a_++), "-1 == a_++"); |
| 3269 | EXPECT_EQ(2, a_); |
| 3270 | |
| 3271 | // successful EXPECT_GT |
| 3272 | EXPECT_GT(a_++, b_++); |
| 3273 | EXPECT_EQ(3, a_); |
| 3274 | EXPECT_EQ(1, b_); |
| 3275 | |
| 3276 | // failed EXPECT_LT |
| 3277 | EXPECT_NONFATAL_FAILURE(EXPECT_LT(a_++, b_++), "(a_++) < (b_++)"); |
| 3278 | EXPECT_EQ(4, a_); |
| 3279 | EXPECT_EQ(2, b_); |
| 3280 | |
| 3281 | // successful ASSERT_TRUE |
| 3282 | ASSERT_TRUE(0 < a_++); // NOLINT |
| 3283 | EXPECT_EQ(5, a_); |
| 3284 | |
| 3285 | // successful ASSERT_GT |
| 3286 | ASSERT_GT(a_++, b_++); |
| 3287 | EXPECT_EQ(6, a_); |
| 3288 | EXPECT_EQ(3, b_); |
| 3289 | } |
| 3290 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3291 | #if GTEST_HAS_EXCEPTIONS |
| 3292 | |
| 3293 | void ThrowAnInteger() { |
| 3294 | throw 1; |
| 3295 | } |
| 3296 | |
| 3297 | // Tests that assertion arguments are evaluated exactly once. |
| 3298 | TEST_F(SingleEvaluationTest, ExceptionTests) { |
| 3299 | // successful EXPECT_THROW |
| 3300 | EXPECT_THROW({ // NOLINT |
| 3301 | a_++; |
| 3302 | ThrowAnInteger(); |
| 3303 | }, int); |
| 3304 | EXPECT_EQ(1, a_); |
| 3305 | |
| 3306 | // failed EXPECT_THROW, throws different |
| 3307 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW({ // NOLINT |
| 3308 | a_++; |
| 3309 | ThrowAnInteger(); |
| 3310 | }, bool), "throws a different type"); |
| 3311 | EXPECT_EQ(2, a_); |
| 3312 | |
| 3313 | // failed EXPECT_THROW, throws nothing |
| 3314 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(a_++, bool), "throws nothing"); |
| 3315 | EXPECT_EQ(3, a_); |
| 3316 | |
| 3317 | // successful EXPECT_NO_THROW |
| 3318 | EXPECT_NO_THROW(a_++); |
| 3319 | EXPECT_EQ(4, a_); |
| 3320 | |
| 3321 | // failed EXPECT_NO_THROW |
| 3322 | EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW({ // NOLINT |
| 3323 | a_++; |
| 3324 | ThrowAnInteger(); |
| 3325 | }), "it throws"); |
| 3326 | EXPECT_EQ(5, a_); |
| 3327 | |
| 3328 | // successful EXPECT_ANY_THROW |
| 3329 | EXPECT_ANY_THROW({ // NOLINT |
| 3330 | a_++; |
| 3331 | ThrowAnInteger(); |
| 3332 | }); |
| 3333 | EXPECT_EQ(6, a_); |
| 3334 | |
| 3335 | // failed EXPECT_ANY_THROW |
| 3336 | EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(a_++), "it doesn't"); |
| 3337 | EXPECT_EQ(7, a_); |
| 3338 | } |
| 3339 | |
| 3340 | #endif // GTEST_HAS_EXCEPTIONS |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3341 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3342 | // Tests {ASSERT|EXPECT}_NO_FATAL_FAILURE. |
| 3343 | class NoFatalFailureTest : public Test { |
| 3344 | protected: |
| 3345 | void Succeeds() {} |
| 3346 | void FailsNonFatal() { |
| 3347 | ADD_FAILURE() << "some non-fatal failure"; |
| 3348 | } |
| 3349 | void Fails() { |
| 3350 | FAIL() << "some fatal failure"; |
| 3351 | } |
| 3352 | |
| 3353 | void DoAssertNoFatalFailureOnFails() { |
| 3354 | ASSERT_NO_FATAL_FAILURE(Fails()); |
| 3355 | ADD_FAILURE() << "shold not reach here."; |
| 3356 | } |
| 3357 | |
| 3358 | void DoExpectNoFatalFailureOnFails() { |
| 3359 | EXPECT_NO_FATAL_FAILURE(Fails()); |
| 3360 | ADD_FAILURE() << "other failure"; |
| 3361 | } |
| 3362 | }; |
| 3363 | |
| 3364 | TEST_F(NoFatalFailureTest, NoFailure) { |
| 3365 | EXPECT_NO_FATAL_FAILURE(Succeeds()); |
| 3366 | ASSERT_NO_FATAL_FAILURE(Succeeds()); |
| 3367 | } |
| 3368 | |
| 3369 | TEST_F(NoFatalFailureTest, NonFatalIsNoFailure) { |
| 3370 | EXPECT_NONFATAL_FAILURE( |
| 3371 | EXPECT_NO_FATAL_FAILURE(FailsNonFatal()), |
| 3372 | "some non-fatal failure"); |
| 3373 | EXPECT_NONFATAL_FAILURE( |
| 3374 | ASSERT_NO_FATAL_FAILURE(FailsNonFatal()), |
| 3375 | "some non-fatal failure"); |
| 3376 | } |
| 3377 | |
| 3378 | TEST_F(NoFatalFailureTest, AssertNoFatalFailureOnFatalFailure) { |
| 3379 | TestPartResultArray gtest_failures; |
| 3380 | { |
| 3381 | ScopedFakeTestPartResultReporter gtest_reporter(>est_failures); |
| 3382 | DoAssertNoFatalFailureOnFails(); |
| 3383 | } |
| 3384 | ASSERT_EQ(2, gtest_failures.size()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3385 | EXPECT_EQ(TestPartResult::kFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3386 | gtest_failures.GetTestPartResult(0).type()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3387 | EXPECT_EQ(TestPartResult::kFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3388 | gtest_failures.GetTestPartResult(1).type()); |
| 3389 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "some fatal failure", |
| 3390 | gtest_failures.GetTestPartResult(0).message()); |
| 3391 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "it does", |
| 3392 | gtest_failures.GetTestPartResult(1).message()); |
| 3393 | } |
| 3394 | |
| 3395 | TEST_F(NoFatalFailureTest, ExpectNoFatalFailureOnFatalFailure) { |
| 3396 | TestPartResultArray gtest_failures; |
| 3397 | { |
| 3398 | ScopedFakeTestPartResultReporter gtest_reporter(>est_failures); |
| 3399 | DoExpectNoFatalFailureOnFails(); |
| 3400 | } |
| 3401 | ASSERT_EQ(3, gtest_failures.size()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3402 | EXPECT_EQ(TestPartResult::kFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3403 | gtest_failures.GetTestPartResult(0).type()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3404 | EXPECT_EQ(TestPartResult::kNonFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3405 | gtest_failures.GetTestPartResult(1).type()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3406 | EXPECT_EQ(TestPartResult::kNonFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3407 | gtest_failures.GetTestPartResult(2).type()); |
| 3408 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "some fatal failure", |
| 3409 | gtest_failures.GetTestPartResult(0).message()); |
| 3410 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "it does", |
| 3411 | gtest_failures.GetTestPartResult(1).message()); |
| 3412 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "other failure", |
| 3413 | gtest_failures.GetTestPartResult(2).message()); |
| 3414 | } |
| 3415 | |
| 3416 | TEST_F(NoFatalFailureTest, MessageIsStreamable) { |
| 3417 | TestPartResultArray gtest_failures; |
| 3418 | { |
| 3419 | ScopedFakeTestPartResultReporter gtest_reporter(>est_failures); |
| 3420 | EXPECT_NO_FATAL_FAILURE(FAIL() << "foo") << "my message"; |
| 3421 | } |
| 3422 | ASSERT_EQ(2, gtest_failures.size()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3423 | EXPECT_EQ(TestPartResult::kNonFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3424 | gtest_failures.GetTestPartResult(0).type()); |
zhanyong.wan | 334aaea | 2009-09-18 18:16:20 +0000 | [diff] [blame] | 3425 | EXPECT_EQ(TestPartResult::kNonFatalFailure, |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3426 | gtest_failures.GetTestPartResult(1).type()); |
| 3427 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "foo", |
| 3428 | gtest_failures.GetTestPartResult(0).message()); |
| 3429 | EXPECT_PRED_FORMAT2(testing::IsSubstring, "my message", |
| 3430 | gtest_failures.GetTestPartResult(1).message()); |
| 3431 | } |
| 3432 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3433 | // Tests non-string assertions. |
| 3434 | |
| 3435 | // Tests EqFailure(), used for implementing *EQ* assertions. |
| 3436 | TEST(AssertionTest, EqFailure) { |
| 3437 | const String foo_val("5"), bar_val("6"); |
| 3438 | const String msg1( |
| 3439 | EqFailure("foo", "bar", foo_val, bar_val, false) |
| 3440 | .failure_message()); |
| 3441 | EXPECT_STREQ( |
| 3442 | "Value of: bar\n" |
| 3443 | " Actual: 6\n" |
| 3444 | "Expected: foo\n" |
| 3445 | "Which is: 5", |
| 3446 | msg1.c_str()); |
| 3447 | |
| 3448 | const String msg2( |
| 3449 | EqFailure("foo", "6", foo_val, bar_val, false) |
| 3450 | .failure_message()); |
| 3451 | EXPECT_STREQ( |
| 3452 | "Value of: 6\n" |
| 3453 | "Expected: foo\n" |
| 3454 | "Which is: 5", |
| 3455 | msg2.c_str()); |
| 3456 | |
| 3457 | const String msg3( |
| 3458 | EqFailure("5", "bar", foo_val, bar_val, false) |
| 3459 | .failure_message()); |
| 3460 | EXPECT_STREQ( |
| 3461 | "Value of: bar\n" |
| 3462 | " Actual: 6\n" |
| 3463 | "Expected: 5", |
| 3464 | msg3.c_str()); |
| 3465 | |
| 3466 | const String msg4( |
| 3467 | EqFailure("5", "6", foo_val, bar_val, false).failure_message()); |
| 3468 | EXPECT_STREQ( |
| 3469 | "Value of: 6\n" |
| 3470 | "Expected: 5", |
| 3471 | msg4.c_str()); |
| 3472 | |
| 3473 | const String msg5( |
| 3474 | EqFailure("foo", "bar", |
| 3475 | String("\"x\""), String("\"y\""), |
| 3476 | true).failure_message()); |
| 3477 | EXPECT_STREQ( |
| 3478 | "Value of: bar\n" |
| 3479 | " Actual: \"y\"\n" |
| 3480 | "Expected: foo (ignoring case)\n" |
| 3481 | "Which is: \"x\"", |
| 3482 | msg5.c_str()); |
| 3483 | } |
| 3484 | |
| 3485 | // Tests AppendUserMessage(), used for implementing the *EQ* macros. |
| 3486 | TEST(AssertionTest, AppendUserMessage) { |
| 3487 | const String foo("foo"); |
| 3488 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 3489 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3490 | EXPECT_STREQ("foo", |
| 3491 | AppendUserMessage(foo, msg).c_str()); |
| 3492 | |
| 3493 | msg << "bar"; |
| 3494 | EXPECT_STREQ("foo\nbar", |
| 3495 | AppendUserMessage(foo, msg).c_str()); |
| 3496 | } |
| 3497 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3498 | #ifdef __BORLANDC__ |
| 3499 | // Silences warnings: "Condition is always true", "Unreachable code" |
| 3500 | #pragma option push -w-ccc -w-rch |
| 3501 | #endif |
| 3502 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3503 | // Tests ASSERT_TRUE. |
| 3504 | TEST(AssertionTest, ASSERT_TRUE) { |
| 3505 | ASSERT_TRUE(2 > 1); // NOLINT |
| 3506 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(2 < 1), |
| 3507 | "2 < 1"); |
| 3508 | } |
| 3509 | |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 3510 | // Tests ASSERT_TRUE(predicate) for predicates returning AssertionResult. |
| 3511 | TEST(AssertionTest, AssertTrueWithAssertionResult) { |
| 3512 | ASSERT_TRUE(ResultIsEven(2)); |
vladlosev | d6b4941 | 2010-04-07 05:32:34 +0000 | [diff] [blame] | 3513 | #if !defined(__BORLANDC__) || __BORLANDC__ >= 0x600 |
| 3514 | // ICE's in C++Builder 2007. |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 3515 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(ResultIsEven(3)), |
| 3516 | "Value of: ResultIsEven(3)\n" |
| 3517 | " Actual: false (3 is odd)\n" |
| 3518 | "Expected: true"); |
vladlosev | d6b4941 | 2010-04-07 05:32:34 +0000 | [diff] [blame] | 3519 | #endif |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 3520 | ASSERT_TRUE(ResultIsEvenNoExplanation(2)); |
| 3521 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(ResultIsEvenNoExplanation(3)), |
| 3522 | "Value of: ResultIsEvenNoExplanation(3)\n" |
| 3523 | " Actual: false (3 is odd)\n" |
| 3524 | "Expected: true"); |
| 3525 | } |
| 3526 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3527 | // Tests ASSERT_FALSE. |
| 3528 | TEST(AssertionTest, ASSERT_FALSE) { |
| 3529 | ASSERT_FALSE(2 < 1); // NOLINT |
| 3530 | EXPECT_FATAL_FAILURE(ASSERT_FALSE(2 > 1), |
| 3531 | "Value of: 2 > 1\n" |
| 3532 | " Actual: true\n" |
| 3533 | "Expected: false"); |
| 3534 | } |
| 3535 | |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 3536 | // Tests ASSERT_FALSE(predicate) for predicates returning AssertionResult. |
| 3537 | TEST(AssertionTest, AssertFalseWithAssertionResult) { |
| 3538 | ASSERT_FALSE(ResultIsEven(3)); |
vladlosev | d6b4941 | 2010-04-07 05:32:34 +0000 | [diff] [blame] | 3539 | #if !defined(__BORLANDC__) || __BORLANDC__ >= 0x600 |
| 3540 | // ICE's in C++Builder 2007. |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 3541 | EXPECT_FATAL_FAILURE(ASSERT_FALSE(ResultIsEven(2)), |
| 3542 | "Value of: ResultIsEven(2)\n" |
| 3543 | " Actual: true (2 is even)\n" |
| 3544 | "Expected: false"); |
vladlosev | d6b4941 | 2010-04-07 05:32:34 +0000 | [diff] [blame] | 3545 | #endif |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 3546 | ASSERT_FALSE(ResultIsEvenNoExplanation(3)); |
| 3547 | EXPECT_FATAL_FAILURE(ASSERT_FALSE(ResultIsEvenNoExplanation(2)), |
| 3548 | "Value of: ResultIsEvenNoExplanation(2)\n" |
| 3549 | " Actual: true\n" |
| 3550 | "Expected: false"); |
| 3551 | } |
| 3552 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3553 | #ifdef __BORLANDC__ |
| 3554 | // Restores warnings after previous "#pragma option push" supressed them |
| 3555 | #pragma option pop |
| 3556 | #endif |
| 3557 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3558 | // Tests using ASSERT_EQ on double values. The purpose is to make |
| 3559 | // sure that the specialization we did for integer and anonymous enums |
| 3560 | // isn't used for double arguments. |
| 3561 | TEST(ExpectTest, ASSERT_EQ_Double) { |
| 3562 | // A success. |
| 3563 | ASSERT_EQ(5.6, 5.6); |
| 3564 | |
| 3565 | // A failure. |
| 3566 | EXPECT_FATAL_FAILURE(ASSERT_EQ(5.1, 5.2), |
| 3567 | "5.1"); |
| 3568 | } |
| 3569 | |
| 3570 | // Tests ASSERT_EQ. |
| 3571 | TEST(AssertionTest, ASSERT_EQ) { |
| 3572 | ASSERT_EQ(5, 2 + 3); |
| 3573 | EXPECT_FATAL_FAILURE(ASSERT_EQ(5, 2*3), |
| 3574 | "Value of: 2*3\n" |
| 3575 | " Actual: 6\n" |
| 3576 | "Expected: 5"); |
| 3577 | } |
| 3578 | |
| 3579 | // Tests ASSERT_EQ(NULL, pointer). |
vladlosev | 5147b45 | 2010-03-17 18:22:59 +0000 | [diff] [blame] | 3580 | #if GTEST_CAN_COMPARE_NULL |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3581 | TEST(AssertionTest, ASSERT_EQ_NULL) { |
| 3582 | // A success. |
| 3583 | const char* p = NULL; |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 3584 | // Some older GCC versions may issue a spurious waring in this or the next |
| 3585 | // assertion statement. This warning should not be suppressed with |
| 3586 | // static_cast since the test verifies the ability to use bare NULL as the |
| 3587 | // expected parameter to the macro. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3588 | ASSERT_EQ(NULL, p); |
| 3589 | |
| 3590 | // A failure. |
| 3591 | static int n = 0; |
| 3592 | EXPECT_FATAL_FAILURE(ASSERT_EQ(NULL, &n), |
| 3593 | "Value of: &n\n"); |
| 3594 | } |
vladlosev | 5147b45 | 2010-03-17 18:22:59 +0000 | [diff] [blame] | 3595 | #endif // GTEST_CAN_COMPARE_NULL |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3596 | |
| 3597 | // Tests ASSERT_EQ(0, non_pointer). Since the literal 0 can be |
| 3598 | // treated as a null pointer by the compiler, we need to make sure |
| 3599 | // that ASSERT_EQ(0, non_pointer) isn't interpreted by Google Test as |
| 3600 | // ASSERT_EQ(static_cast<void*>(NULL), non_pointer). |
| 3601 | TEST(ExpectTest, ASSERT_EQ_0) { |
| 3602 | int n = 0; |
| 3603 | |
| 3604 | // A success. |
| 3605 | ASSERT_EQ(0, n); |
| 3606 | |
| 3607 | // A failure. |
| 3608 | EXPECT_FATAL_FAILURE(ASSERT_EQ(0, 5.6), |
| 3609 | "Expected: 0"); |
| 3610 | } |
| 3611 | |
| 3612 | // Tests ASSERT_NE. |
| 3613 | TEST(AssertionTest, ASSERT_NE) { |
| 3614 | ASSERT_NE(6, 7); |
| 3615 | EXPECT_FATAL_FAILURE(ASSERT_NE('a', 'a'), |
| 3616 | "Expected: ('a') != ('a'), " |
| 3617 | "actual: 'a' (97, 0x61) vs 'a' (97, 0x61)"); |
| 3618 | } |
| 3619 | |
| 3620 | // Tests ASSERT_LE. |
| 3621 | TEST(AssertionTest, ASSERT_LE) { |
| 3622 | ASSERT_LE(2, 3); |
| 3623 | ASSERT_LE(2, 2); |
| 3624 | EXPECT_FATAL_FAILURE(ASSERT_LE(2, 0), |
| 3625 | "Expected: (2) <= (0), actual: 2 vs 0"); |
| 3626 | } |
| 3627 | |
| 3628 | // Tests ASSERT_LT. |
| 3629 | TEST(AssertionTest, ASSERT_LT) { |
| 3630 | ASSERT_LT(2, 3); |
| 3631 | EXPECT_FATAL_FAILURE(ASSERT_LT(2, 2), |
| 3632 | "Expected: (2) < (2), actual: 2 vs 2"); |
| 3633 | } |
| 3634 | |
| 3635 | // Tests ASSERT_GE. |
| 3636 | TEST(AssertionTest, ASSERT_GE) { |
| 3637 | ASSERT_GE(2, 1); |
| 3638 | ASSERT_GE(2, 2); |
| 3639 | EXPECT_FATAL_FAILURE(ASSERT_GE(2, 3), |
| 3640 | "Expected: (2) >= (3), actual: 2 vs 3"); |
| 3641 | } |
| 3642 | |
| 3643 | // Tests ASSERT_GT. |
| 3644 | TEST(AssertionTest, ASSERT_GT) { |
| 3645 | ASSERT_GT(2, 1); |
| 3646 | EXPECT_FATAL_FAILURE(ASSERT_GT(2, 2), |
| 3647 | "Expected: (2) > (2), actual: 2 vs 2"); |
| 3648 | } |
| 3649 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3650 | #if GTEST_HAS_EXCEPTIONS |
| 3651 | |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3652 | void ThrowNothing() {} |
| 3653 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3654 | // Tests ASSERT_THROW. |
| 3655 | TEST(AssertionTest, ASSERT_THROW) { |
| 3656 | ASSERT_THROW(ThrowAnInteger(), int); |
vladlosev | d6b4941 | 2010-04-07 05:32:34 +0000 | [diff] [blame] | 3657 | |
| 3658 | #ifndef __BORLANDC__ |
| 3659 | // ICE's in C++Builder 2007 and 2009. |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3660 | EXPECT_FATAL_FAILURE( |
| 3661 | ASSERT_THROW(ThrowAnInteger(), bool), |
| 3662 | "Expected: ThrowAnInteger() throws an exception of type bool.\n" |
| 3663 | " Actual: it throws a different type."); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3664 | #endif |
vladlosev | d6b4941 | 2010-04-07 05:32:34 +0000 | [diff] [blame] | 3665 | |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3666 | EXPECT_FATAL_FAILURE( |
| 3667 | ASSERT_THROW(ThrowNothing(), bool), |
| 3668 | "Expected: ThrowNothing() throws an exception of type bool.\n" |
| 3669 | " Actual: it throws nothing."); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | // Tests ASSERT_NO_THROW. |
| 3673 | TEST(AssertionTest, ASSERT_NO_THROW) { |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3674 | ASSERT_NO_THROW(ThrowNothing()); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3675 | EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()), |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3676 | "Expected: ThrowAnInteger() doesn't throw an exception." |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3677 | "\n Actual: it throws."); |
| 3678 | } |
| 3679 | |
| 3680 | // Tests ASSERT_ANY_THROW. |
| 3681 | TEST(AssertionTest, ASSERT_ANY_THROW) { |
| 3682 | ASSERT_ANY_THROW(ThrowAnInteger()); |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3683 | EXPECT_FATAL_FAILURE( |
| 3684 | ASSERT_ANY_THROW(ThrowNothing()), |
| 3685 | "Expected: ThrowNothing() throws an exception.\n" |
| 3686 | " Actual: it doesn't."); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | #endif // GTEST_HAS_EXCEPTIONS |
| 3690 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3691 | // Makes sure we deal with the precedence of <<. This test should |
| 3692 | // compile. |
| 3693 | TEST(AssertionTest, AssertPrecedence) { |
| 3694 | ASSERT_EQ(1 < 2, true); |
| 3695 | ASSERT_EQ(true && false, false); |
| 3696 | } |
| 3697 | |
| 3698 | // A subroutine used by the following test. |
| 3699 | void TestEq1(int x) { |
| 3700 | ASSERT_EQ(1, x); |
| 3701 | } |
| 3702 | |
| 3703 | // Tests calling a test subroutine that's not part of a fixture. |
| 3704 | TEST(AssertionTest, NonFixtureSubroutine) { |
| 3705 | EXPECT_FATAL_FAILURE(TestEq1(2), |
| 3706 | "Value of: x"); |
| 3707 | } |
| 3708 | |
| 3709 | // An uncopyable class. |
| 3710 | class Uncopyable { |
| 3711 | public: |
zhanyong.wan | 7de3401 | 2009-12-16 19:54:05 +0000 | [diff] [blame] | 3712 | explicit Uncopyable(int a_value) : value_(a_value) {} |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3713 | |
| 3714 | int value() const { return value_; } |
| 3715 | bool operator==(const Uncopyable& rhs) const { |
| 3716 | return value() == rhs.value(); |
| 3717 | } |
| 3718 | private: |
| 3719 | // This constructor deliberately has no implementation, as we don't |
| 3720 | // want this class to be copyable. |
| 3721 | Uncopyable(const Uncopyable&); // NOLINT |
| 3722 | |
| 3723 | int value_; |
| 3724 | }; |
| 3725 | |
| 3726 | ::std::ostream& operator<<(::std::ostream& os, const Uncopyable& value) { |
| 3727 | return os << value.value(); |
| 3728 | } |
| 3729 | |
| 3730 | |
| 3731 | bool IsPositiveUncopyable(const Uncopyable& x) { |
| 3732 | return x.value() > 0; |
| 3733 | } |
| 3734 | |
| 3735 | // A subroutine used by the following test. |
| 3736 | void TestAssertNonPositive() { |
| 3737 | Uncopyable y(-1); |
| 3738 | ASSERT_PRED1(IsPositiveUncopyable, y); |
| 3739 | } |
| 3740 | // A subroutine used by the following test. |
| 3741 | void TestAssertEqualsUncopyable() { |
| 3742 | Uncopyable x(5); |
| 3743 | Uncopyable y(-1); |
| 3744 | ASSERT_EQ(x, y); |
| 3745 | } |
| 3746 | |
| 3747 | // Tests that uncopyable objects can be used in assertions. |
| 3748 | TEST(AssertionTest, AssertWorksWithUncopyableObject) { |
| 3749 | Uncopyable x(5); |
| 3750 | ASSERT_PRED1(IsPositiveUncopyable, x); |
| 3751 | ASSERT_EQ(x, x); |
| 3752 | EXPECT_FATAL_FAILURE(TestAssertNonPositive(), |
| 3753 | "IsPositiveUncopyable(y) evaluates to false, where\ny evaluates to -1"); |
| 3754 | EXPECT_FATAL_FAILURE(TestAssertEqualsUncopyable(), |
| 3755 | "Value of: y\n Actual: -1\nExpected: x\nWhich is: 5"); |
| 3756 | } |
| 3757 | |
| 3758 | // Tests that uncopyable objects can be used in expects. |
| 3759 | TEST(AssertionTest, ExpectWorksWithUncopyableObject) { |
| 3760 | Uncopyable x(5); |
| 3761 | EXPECT_PRED1(IsPositiveUncopyable, x); |
| 3762 | Uncopyable y(-1); |
| 3763 | EXPECT_NONFATAL_FAILURE(EXPECT_PRED1(IsPositiveUncopyable, y), |
| 3764 | "IsPositiveUncopyable(y) evaluates to false, where\ny evaluates to -1"); |
| 3765 | EXPECT_EQ(x, x); |
| 3766 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(x, y), |
| 3767 | "Value of: y\n Actual: -1\nExpected: x\nWhich is: 5"); |
| 3768 | } |
| 3769 | |
zhanyong.wan | 739d304 | 2010-08-09 18:19:15 +0000 | [diff] [blame^] | 3770 | enum NamedEnum { |
| 3771 | kE1 = 0, |
| 3772 | kE2 = 1, |
| 3773 | }; |
| 3774 | |
| 3775 | TEST(AssertionTest, NamedEnum) { |
| 3776 | EXPECT_EQ(kE1, kE1); |
| 3777 | EXPECT_LT(kE1, kE2); |
| 3778 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(kE1, kE2), "Which is: 0"); |
| 3779 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(kE1, kE2), "Actual: 1"); |
| 3780 | } |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3781 | |
| 3782 | // 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] | 3783 | // anonymous enums in assertions. Therefore the following test is not |
| 3784 | // done on Mac. |
vladlosev | 673a0cb | 2010-02-03 02:27:02 +0000 | [diff] [blame] | 3785 | // Sun Studio also rejects this code. |
| 3786 | #if !GTEST_OS_MAC && !defined(__SUNPRO_CC) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3787 | |
| 3788 | // Tests using assertions with anonymous enums. |
| 3789 | enum { |
zhanyong.wan | 739d304 | 2010-08-09 18:19:15 +0000 | [diff] [blame^] | 3790 | kCaseA = -1, |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3791 | #if GTEST_OS_LINUX |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3792 | // We want to test the case where the size of the anonymous enum is |
| 3793 | // larger than sizeof(int), to make sure our implementation of the |
| 3794 | // assertions doesn't truncate the enums. However, MSVC |
| 3795 | // (incorrectly) doesn't allow an enum value to exceed the range of |
| 3796 | // an int, so this has to be conditionally compiled. |
| 3797 | // |
zhanyong.wan | 739d304 | 2010-08-09 18:19:15 +0000 | [diff] [blame^] | 3798 | // On Linux, kCaseB and kCaseA have the same value when truncated to |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3799 | // int size. We want to test whether this will confuse the |
| 3800 | // assertions. |
zhanyong.wan | 739d304 | 2010-08-09 18:19:15 +0000 | [diff] [blame^] | 3801 | kCaseB = testing::internal::kMaxBiggestInt, |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3802 | #else |
zhanyong.wan | 739d304 | 2010-08-09 18:19:15 +0000 | [diff] [blame^] | 3803 | kCaseB = INT_MAX, |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3804 | #endif // GTEST_OS_LINUX |
zhanyong.wan | 739d304 | 2010-08-09 18:19:15 +0000 | [diff] [blame^] | 3805 | kCaseC = 42, |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3806 | }; |
| 3807 | |
| 3808 | TEST(AssertionTest, AnonymousEnum) { |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3809 | #if GTEST_OS_LINUX |
zhanyong.wan | 739d304 | 2010-08-09 18:19:15 +0000 | [diff] [blame^] | 3810 | EXPECT_EQ(static_cast<int>(kCaseA), static_cast<int>(kCaseB)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3811 | #endif // GTEST_OS_LINUX |
| 3812 | |
zhanyong.wan | 739d304 | 2010-08-09 18:19:15 +0000 | [diff] [blame^] | 3813 | EXPECT_EQ(kCaseA, kCaseA); |
| 3814 | EXPECT_NE(kCaseA, kCaseB); |
| 3815 | EXPECT_LT(kCaseA, kCaseB); |
| 3816 | EXPECT_LE(kCaseA, kCaseB); |
| 3817 | EXPECT_GT(kCaseB, kCaseA); |
| 3818 | EXPECT_GE(kCaseA, kCaseA); |
| 3819 | EXPECT_NONFATAL_FAILURE(EXPECT_GE(kCaseA, kCaseB), |
| 3820 | "(kCaseA) >= (kCaseB)"); |
| 3821 | EXPECT_NONFATAL_FAILURE(EXPECT_GE(kCaseA, kCaseC), |
| 3822 | "-1 vs 42"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3823 | |
zhanyong.wan | 739d304 | 2010-08-09 18:19:15 +0000 | [diff] [blame^] | 3824 | ASSERT_EQ(kCaseA, kCaseA); |
| 3825 | ASSERT_NE(kCaseA, kCaseB); |
| 3826 | ASSERT_LT(kCaseA, kCaseB); |
| 3827 | ASSERT_LE(kCaseA, kCaseB); |
| 3828 | ASSERT_GT(kCaseB, kCaseA); |
| 3829 | ASSERT_GE(kCaseA, kCaseA); |
| 3830 | EXPECT_FATAL_FAILURE(ASSERT_EQ(kCaseA, kCaseB), |
| 3831 | "Value of: kCaseB"); |
| 3832 | EXPECT_FATAL_FAILURE(ASSERT_EQ(kCaseA, kCaseC), |
| 3833 | "Actual: 42"); |
| 3834 | EXPECT_FATAL_FAILURE(ASSERT_EQ(kCaseA, kCaseC), |
| 3835 | "Which is: -1"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3836 | } |
| 3837 | |
vladlosev | 673a0cb | 2010-02-03 02:27:02 +0000 | [diff] [blame] | 3838 | #endif // !GTEST_OS_MAC && !defined(__SUNPRO_CC) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3839 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3840 | #if GTEST_OS_WINDOWS |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3841 | |
| 3842 | static HRESULT UnexpectedHRESULTFailure() { |
| 3843 | return E_UNEXPECTED; |
| 3844 | } |
| 3845 | |
| 3846 | static HRESULT OkHRESULTSuccess() { |
| 3847 | return S_OK; |
| 3848 | } |
| 3849 | |
| 3850 | static HRESULT FalseHRESULTSuccess() { |
| 3851 | return S_FALSE; |
| 3852 | } |
| 3853 | |
| 3854 | // HRESULT assertion tests test both zero and non-zero |
| 3855 | // success codes as well as failure message for each. |
| 3856 | // |
| 3857 | // Windows CE doesn't support message texts. |
| 3858 | TEST(HRESULTAssertionTest, EXPECT_HRESULT_SUCCEEDED) { |
| 3859 | EXPECT_HRESULT_SUCCEEDED(S_OK); |
| 3860 | EXPECT_HRESULT_SUCCEEDED(S_FALSE); |
| 3861 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3862 | EXPECT_NONFATAL_FAILURE(EXPECT_HRESULT_SUCCEEDED(UnexpectedHRESULTFailure()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 3863 | "Expected: (UnexpectedHRESULTFailure()) succeeds.\n" |
| 3864 | " Actual: 0x8000FFFF"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | TEST(HRESULTAssertionTest, ASSERT_HRESULT_SUCCEEDED) { |
| 3868 | ASSERT_HRESULT_SUCCEEDED(S_OK); |
| 3869 | ASSERT_HRESULT_SUCCEEDED(S_FALSE); |
| 3870 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3871 | EXPECT_FATAL_FAILURE(ASSERT_HRESULT_SUCCEEDED(UnexpectedHRESULTFailure()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 3872 | "Expected: (UnexpectedHRESULTFailure()) succeeds.\n" |
| 3873 | " Actual: 0x8000FFFF"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
| 3876 | TEST(HRESULTAssertionTest, EXPECT_HRESULT_FAILED) { |
| 3877 | EXPECT_HRESULT_FAILED(E_UNEXPECTED); |
| 3878 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3879 | EXPECT_NONFATAL_FAILURE(EXPECT_HRESULT_FAILED(OkHRESULTSuccess()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 3880 | "Expected: (OkHRESULTSuccess()) fails.\n" |
| 3881 | " Actual: 0x00000000"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3882 | EXPECT_NONFATAL_FAILURE(EXPECT_HRESULT_FAILED(FalseHRESULTSuccess()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 3883 | "Expected: (FalseHRESULTSuccess()) fails.\n" |
| 3884 | " Actual: 0x00000001"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | TEST(HRESULTAssertionTest, ASSERT_HRESULT_FAILED) { |
| 3888 | ASSERT_HRESULT_FAILED(E_UNEXPECTED); |
| 3889 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3890 | #ifndef __BORLANDC__ |
| 3891 | // ICE's in C++Builder 2007 and 2009. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3892 | EXPECT_FATAL_FAILURE(ASSERT_HRESULT_FAILED(OkHRESULTSuccess()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 3893 | "Expected: (OkHRESULTSuccess()) fails.\n" |
| 3894 | " Actual: 0x00000000"); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3895 | #endif |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3896 | EXPECT_FATAL_FAILURE(ASSERT_HRESULT_FAILED(FalseHRESULTSuccess()), |
shiqian | afebcbd | 2008-09-13 00:49:59 +0000 | [diff] [blame] | 3897 | "Expected: (FalseHRESULTSuccess()) fails.\n" |
| 3898 | " Actual: 0x00000001"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | // Tests that streaming to the HRESULT macros works. |
| 3902 | TEST(HRESULTAssertionTest, Streaming) { |
| 3903 | EXPECT_HRESULT_SUCCEEDED(S_OK) << "unexpected failure"; |
| 3904 | ASSERT_HRESULT_SUCCEEDED(S_OK) << "unexpected failure"; |
| 3905 | EXPECT_HRESULT_FAILED(E_UNEXPECTED) << "unexpected failure"; |
| 3906 | ASSERT_HRESULT_FAILED(E_UNEXPECTED) << "unexpected failure"; |
| 3907 | |
| 3908 | EXPECT_NONFATAL_FAILURE( |
| 3909 | EXPECT_HRESULT_SUCCEEDED(E_UNEXPECTED) << "expected failure", |
| 3910 | "expected failure"); |
| 3911 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3912 | #ifndef __BORLANDC__ |
| 3913 | // ICE's in C++Builder 2007 and 2009. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3914 | EXPECT_FATAL_FAILURE( |
| 3915 | ASSERT_HRESULT_SUCCEEDED(E_UNEXPECTED) << "expected failure", |
| 3916 | "expected failure"); |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3917 | #endif |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3918 | |
| 3919 | EXPECT_NONFATAL_FAILURE( |
| 3920 | EXPECT_HRESULT_FAILED(S_OK) << "expected failure", |
| 3921 | "expected failure"); |
| 3922 | |
| 3923 | EXPECT_FATAL_FAILURE( |
| 3924 | ASSERT_HRESULT_FAILED(S_OK) << "expected failure", |
| 3925 | "expected failure"); |
| 3926 | } |
| 3927 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 3928 | #endif // GTEST_OS_WINDOWS |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3929 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 3930 | #ifdef __BORLANDC__ |
| 3931 | // Silences warnings: "Condition is always true", "Unreachable code" |
| 3932 | #pragma option push -w-ccc -w-rch |
| 3933 | #endif |
| 3934 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3935 | // Tests that the assertion macros behave like single statements. |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3936 | TEST(AssertionSyntaxTest, BasicAssertionsBehavesLikeSingleStatement) { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 3937 | if (AlwaysFalse()) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3938 | ASSERT_TRUE(false) << "This should never be executed; " |
| 3939 | "It's a compilation test only."; |
| 3940 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 3941 | if (AlwaysTrue()) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3942 | EXPECT_FALSE(false); |
| 3943 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 3944 | ; // NOLINT |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3945 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 3946 | if (AlwaysFalse()) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3947 | ASSERT_LT(1, 3); |
| 3948 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 3949 | if (AlwaysFalse()) |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 3950 | ; // NOLINT |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 3951 | else |
| 3952 | EXPECT_GT(3, 2) << ""; |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3953 | } |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3954 | |
| 3955 | #if GTEST_HAS_EXCEPTIONS |
zhanyong.wan | e0ca02f | 2009-02-06 00:47:20 +0000 | [diff] [blame] | 3956 | // Tests that the compiler will not complain about unreachable code in the |
| 3957 | // EXPECT_THROW/EXPECT_ANY_THROW/EXPECT_NO_THROW macros. |
| 3958 | TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) { |
| 3959 | int n = 0; |
| 3960 | |
| 3961 | EXPECT_THROW(throw 1, int); |
| 3962 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(n++, int), ""); |
| 3963 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(throw 1, const char*), ""); |
| 3964 | EXPECT_NO_THROW(n++); |
| 3965 | EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(throw 1), ""); |
| 3966 | EXPECT_ANY_THROW(throw 1); |
| 3967 | EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(n++), ""); |
| 3968 | } |
| 3969 | |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3970 | TEST(AssertionSyntaxTest, ExceptionAssertionsBehavesLikeSingleStatement) { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 3971 | if (AlwaysFalse()) |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3972 | EXPECT_THROW(ThrowNothing(), bool); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3973 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 3974 | if (AlwaysTrue()) |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3975 | EXPECT_THROW(ThrowAnInteger(), int); |
| 3976 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 3977 | ; // NOLINT |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3978 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 3979 | if (AlwaysFalse()) |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3980 | EXPECT_NO_THROW(ThrowAnInteger()); |
| 3981 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 3982 | if (AlwaysTrue()) |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3983 | EXPECT_NO_THROW(ThrowNothing()); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3984 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 3985 | ; // NOLINT |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3986 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 3987 | if (AlwaysFalse()) |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 3988 | EXPECT_ANY_THROW(ThrowNothing()); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3989 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 3990 | if (AlwaysTrue()) |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3991 | EXPECT_ANY_THROW(ThrowAnInteger()); |
| 3992 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 3993 | ; // NOLINT |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3994 | } |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 3995 | #endif // GTEST_HAS_EXCEPTIONS |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3996 | |
| 3997 | TEST(AssertionSyntaxTest, NoFatalFailureAssertionsBehavesLikeSingleStatement) { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 3998 | if (AlwaysFalse()) |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 3999 | EXPECT_NO_FATAL_FAILURE(FAIL()) << "This should never be executed. " |
| 4000 | << "It's a compilation test only."; |
| 4001 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 4002 | ; // NOLINT |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4003 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4004 | if (AlwaysFalse()) |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4005 | ASSERT_NO_FATAL_FAILURE(FAIL()) << ""; |
| 4006 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 4007 | ; // NOLINT |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4008 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4009 | if (AlwaysTrue()) |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4010 | EXPECT_NO_FATAL_FAILURE(SUCCEED()); |
| 4011 | else |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 4012 | ; // NOLINT |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4013 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 4014 | if (AlwaysFalse()) |
zhanyong.wan | 4e7e2fc | 2009-06-19 00:24:28 +0000 | [diff] [blame] | 4015 | ; // NOLINT |
shiqian | e44602e | 2008-10-11 07:20:02 +0000 | [diff] [blame] | 4016 | else |
| 4017 | ASSERT_NO_FATAL_FAILURE(SUCCEED()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | // Tests that the assertion macros work well with switch statements. |
| 4021 | TEST(AssertionSyntaxTest, WorksWithSwitch) { |
| 4022 | switch (0) { |
| 4023 | case 1: |
| 4024 | break; |
| 4025 | default: |
| 4026 | ASSERT_TRUE(true); |
| 4027 | } |
| 4028 | |
| 4029 | switch (0) |
| 4030 | case 0: |
| 4031 | EXPECT_FALSE(false) << "EXPECT_FALSE failed in switch case"; |
| 4032 | |
| 4033 | // Binary assertions are implemented using a different code path |
| 4034 | // than the Boolean assertions. Hence we test them separately. |
| 4035 | switch (0) { |
| 4036 | case 1: |
| 4037 | default: |
| 4038 | ASSERT_EQ(1, 1) << "ASSERT_EQ failed in default switch handler"; |
| 4039 | } |
| 4040 | |
| 4041 | switch (0) |
| 4042 | case 0: |
| 4043 | EXPECT_NE(1, 2); |
| 4044 | } |
| 4045 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4046 | #if GTEST_HAS_EXCEPTIONS |
| 4047 | |
| 4048 | void ThrowAString() { |
| 4049 | throw "String"; |
| 4050 | } |
| 4051 | |
| 4052 | // Test that the exception assertion macros compile and work with const |
| 4053 | // type qualifier. |
| 4054 | TEST(AssertionSyntaxTest, WorksWithConst) { |
| 4055 | ASSERT_THROW(ThrowAString(), const char*); |
| 4056 | |
| 4057 | EXPECT_THROW(ThrowAString(), const char*); |
| 4058 | } |
| 4059 | |
| 4060 | #endif // GTEST_HAS_EXCEPTIONS |
| 4061 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4062 | } // namespace |
| 4063 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4064 | namespace testing { |
| 4065 | |
| 4066 | // Tests that Google Test tracks SUCCEED*. |
| 4067 | TEST(SuccessfulAssertionTest, SUCCEED) { |
| 4068 | SUCCEED(); |
| 4069 | SUCCEED() << "OK"; |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 4070 | EXPECT_EQ(2, GetUnitTestImpl()->current_test_result()->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
| 4073 | // Tests that Google Test doesn't track successful EXPECT_*. |
| 4074 | TEST(SuccessfulAssertionTest, EXPECT) { |
| 4075 | EXPECT_TRUE(true); |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 4076 | EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | // Tests that Google Test doesn't track successful EXPECT_STR*. |
| 4080 | TEST(SuccessfulAssertionTest, EXPECT_STR) { |
| 4081 | EXPECT_STREQ("", ""); |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 4082 | EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | // Tests that Google Test doesn't track successful ASSERT_*. |
| 4086 | TEST(SuccessfulAssertionTest, ASSERT) { |
| 4087 | ASSERT_TRUE(true); |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 4088 | EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | // Tests that Google Test doesn't track successful ASSERT_STR*. |
| 4092 | TEST(SuccessfulAssertionTest, ASSERT_STR) { |
| 4093 | ASSERT_STREQ("", ""); |
zhanyong.wan | 1cdc763 | 2009-07-16 00:36:55 +0000 | [diff] [blame] | 4094 | EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | } // namespace testing |
| 4098 | |
| 4099 | namespace { |
| 4100 | |
| 4101 | // Tests EXPECT_TRUE. |
| 4102 | TEST(ExpectTest, EXPECT_TRUE) { |
| 4103 | EXPECT_TRUE(2 > 1); // NOLINT |
| 4104 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(2 < 1), |
| 4105 | "Value of: 2 < 1\n" |
| 4106 | " Actual: false\n" |
| 4107 | "Expected: true"); |
| 4108 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(2 > 3), |
| 4109 | "2 > 3"); |
| 4110 | } |
| 4111 | |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 4112 | // Tests EXPECT_TRUE(predicate) for predicates returning AssertionResult. |
| 4113 | TEST(ExpectTest, ExpectTrueWithAssertionResult) { |
| 4114 | EXPECT_TRUE(ResultIsEven(2)); |
| 4115 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(ResultIsEven(3)), |
| 4116 | "Value of: ResultIsEven(3)\n" |
| 4117 | " Actual: false (3 is odd)\n" |
| 4118 | "Expected: true"); |
| 4119 | EXPECT_TRUE(ResultIsEvenNoExplanation(2)); |
| 4120 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(ResultIsEvenNoExplanation(3)), |
| 4121 | "Value of: ResultIsEvenNoExplanation(3)\n" |
| 4122 | " Actual: false (3 is odd)\n" |
| 4123 | "Expected: true"); |
| 4124 | } |
| 4125 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4126 | // Tests EXPECT_FALSE. |
| 4127 | TEST(ExpectTest, EXPECT_FALSE) { |
| 4128 | EXPECT_FALSE(2 < 1); // NOLINT |
| 4129 | EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(2 > 1), |
| 4130 | "Value of: 2 > 1\n" |
| 4131 | " Actual: true\n" |
| 4132 | "Expected: false"); |
| 4133 | EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(2 < 3), |
| 4134 | "2 < 3"); |
| 4135 | } |
| 4136 | |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 4137 | // Tests EXPECT_FALSE(predicate) for predicates returning AssertionResult. |
| 4138 | TEST(ExpectTest, ExpectFalseWithAssertionResult) { |
| 4139 | EXPECT_FALSE(ResultIsEven(3)); |
| 4140 | EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(ResultIsEven(2)), |
| 4141 | "Value of: ResultIsEven(2)\n" |
| 4142 | " Actual: true (2 is even)\n" |
| 4143 | "Expected: false"); |
| 4144 | EXPECT_FALSE(ResultIsEvenNoExplanation(3)); |
| 4145 | EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(ResultIsEvenNoExplanation(2)), |
| 4146 | "Value of: ResultIsEvenNoExplanation(2)\n" |
| 4147 | " Actual: true\n" |
| 4148 | "Expected: false"); |
| 4149 | } |
| 4150 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 4151 | #ifdef __BORLANDC__ |
| 4152 | // Restores warnings after previous "#pragma option push" supressed them |
| 4153 | #pragma option pop |
| 4154 | #endif |
| 4155 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4156 | // Tests EXPECT_EQ. |
| 4157 | TEST(ExpectTest, EXPECT_EQ) { |
| 4158 | EXPECT_EQ(5, 2 + 3); |
| 4159 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5, 2*3), |
| 4160 | "Value of: 2*3\n" |
| 4161 | " Actual: 6\n" |
| 4162 | "Expected: 5"); |
| 4163 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5, 2 - 3), |
| 4164 | "2 - 3"); |
| 4165 | } |
| 4166 | |
| 4167 | // Tests using EXPECT_EQ on double values. The purpose is to make |
| 4168 | // sure that the specialization we did for integer and anonymous enums |
| 4169 | // isn't used for double arguments. |
| 4170 | TEST(ExpectTest, EXPECT_EQ_Double) { |
| 4171 | // A success. |
| 4172 | EXPECT_EQ(5.6, 5.6); |
| 4173 | |
| 4174 | // A failure. |
| 4175 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(5.1, 5.2), |
| 4176 | "5.1"); |
| 4177 | } |
| 4178 | |
vladlosev | 5147b45 | 2010-03-17 18:22:59 +0000 | [diff] [blame] | 4179 | #if GTEST_CAN_COMPARE_NULL |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4180 | // Tests EXPECT_EQ(NULL, pointer). |
| 4181 | TEST(ExpectTest, EXPECT_EQ_NULL) { |
| 4182 | // A success. |
| 4183 | const char* p = NULL; |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 4184 | // Some older GCC versions may issue a spurious waring in this or the next |
| 4185 | // assertion statement. This warning should not be suppressed with |
| 4186 | // static_cast since the test verifies the ability to use bare NULL as the |
| 4187 | // expected parameter to the macro. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4188 | EXPECT_EQ(NULL, p); |
| 4189 | |
| 4190 | // A failure. |
| 4191 | int n = 0; |
| 4192 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(NULL, &n), |
| 4193 | "Value of: &n\n"); |
| 4194 | } |
vladlosev | 5147b45 | 2010-03-17 18:22:59 +0000 | [diff] [blame] | 4195 | #endif // GTEST_CAN_COMPARE_NULL |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4196 | |
| 4197 | // Tests EXPECT_EQ(0, non_pointer). Since the literal 0 can be |
| 4198 | // treated as a null pointer by the compiler, we need to make sure |
| 4199 | // that EXPECT_EQ(0, non_pointer) isn't interpreted by Google Test as |
| 4200 | // EXPECT_EQ(static_cast<void*>(NULL), non_pointer). |
| 4201 | TEST(ExpectTest, EXPECT_EQ_0) { |
| 4202 | int n = 0; |
| 4203 | |
| 4204 | // A success. |
| 4205 | EXPECT_EQ(0, n); |
| 4206 | |
| 4207 | // A failure. |
| 4208 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(0, 5.6), |
| 4209 | "Expected: 0"); |
| 4210 | } |
| 4211 | |
| 4212 | // Tests EXPECT_NE. |
| 4213 | TEST(ExpectTest, EXPECT_NE) { |
| 4214 | EXPECT_NE(6, 7); |
| 4215 | |
| 4216 | EXPECT_NONFATAL_FAILURE(EXPECT_NE('a', 'a'), |
| 4217 | "Expected: ('a') != ('a'), " |
| 4218 | "actual: 'a' (97, 0x61) vs 'a' (97, 0x61)"); |
| 4219 | EXPECT_NONFATAL_FAILURE(EXPECT_NE(2, 2), |
| 4220 | "2"); |
| 4221 | char* const p0 = NULL; |
| 4222 | EXPECT_NONFATAL_FAILURE(EXPECT_NE(p0, p0), |
| 4223 | "p0"); |
| 4224 | // Only way to get the Nokia compiler to compile the cast |
| 4225 | // is to have a separate void* variable first. Putting |
| 4226 | // the two casts on the same line doesn't work, neither does |
| 4227 | // a direct C-style to char*. |
| 4228 | void* pv1 = (void*)0x1234; // NOLINT |
| 4229 | char* const p1 = reinterpret_cast<char*>(pv1); |
| 4230 | EXPECT_NONFATAL_FAILURE(EXPECT_NE(p1, p1), |
| 4231 | "p1"); |
| 4232 | } |
| 4233 | |
| 4234 | // Tests EXPECT_LE. |
| 4235 | TEST(ExpectTest, EXPECT_LE) { |
| 4236 | EXPECT_LE(2, 3); |
| 4237 | EXPECT_LE(2, 2); |
| 4238 | EXPECT_NONFATAL_FAILURE(EXPECT_LE(2, 0), |
| 4239 | "Expected: (2) <= (0), actual: 2 vs 0"); |
| 4240 | EXPECT_NONFATAL_FAILURE(EXPECT_LE(1.1, 0.9), |
| 4241 | "(1.1) <= (0.9)"); |
| 4242 | } |
| 4243 | |
| 4244 | // Tests EXPECT_LT. |
| 4245 | TEST(ExpectTest, EXPECT_LT) { |
| 4246 | EXPECT_LT(2, 3); |
| 4247 | EXPECT_NONFATAL_FAILURE(EXPECT_LT(2, 2), |
| 4248 | "Expected: (2) < (2), actual: 2 vs 2"); |
| 4249 | EXPECT_NONFATAL_FAILURE(EXPECT_LT(2, 1), |
| 4250 | "(2) < (1)"); |
| 4251 | } |
| 4252 | |
| 4253 | // Tests EXPECT_GE. |
| 4254 | TEST(ExpectTest, EXPECT_GE) { |
| 4255 | EXPECT_GE(2, 1); |
| 4256 | EXPECT_GE(2, 2); |
| 4257 | EXPECT_NONFATAL_FAILURE(EXPECT_GE(2, 3), |
| 4258 | "Expected: (2) >= (3), actual: 2 vs 3"); |
| 4259 | EXPECT_NONFATAL_FAILURE(EXPECT_GE(0.9, 1.1), |
| 4260 | "(0.9) >= (1.1)"); |
| 4261 | } |
| 4262 | |
| 4263 | // Tests EXPECT_GT. |
| 4264 | TEST(ExpectTest, EXPECT_GT) { |
| 4265 | EXPECT_GT(2, 1); |
| 4266 | EXPECT_NONFATAL_FAILURE(EXPECT_GT(2, 2), |
| 4267 | "Expected: (2) > (2), actual: 2 vs 2"); |
| 4268 | EXPECT_NONFATAL_FAILURE(EXPECT_GT(2, 3), |
| 4269 | "(2) > (3)"); |
| 4270 | } |
| 4271 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4272 | #if GTEST_HAS_EXCEPTIONS |
| 4273 | |
| 4274 | // Tests EXPECT_THROW. |
| 4275 | TEST(ExpectTest, EXPECT_THROW) { |
| 4276 | EXPECT_THROW(ThrowAnInteger(), int); |
| 4277 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnInteger(), bool), |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 4278 | "Expected: ThrowAnInteger() throws an exception of " |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4279 | "type bool.\n Actual: it throws a different type."); |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 4280 | EXPECT_NONFATAL_FAILURE( |
| 4281 | EXPECT_THROW(ThrowNothing(), bool), |
| 4282 | "Expected: ThrowNothing() throws an exception of type bool.\n" |
| 4283 | " Actual: it throws nothing."); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4284 | } |
| 4285 | |
| 4286 | // Tests EXPECT_NO_THROW. |
| 4287 | TEST(ExpectTest, EXPECT_NO_THROW) { |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 4288 | EXPECT_NO_THROW(ThrowNothing()); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4289 | EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()), |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 4290 | "Expected: ThrowAnInteger() doesn't throw an " |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4291 | "exception.\n Actual: it throws."); |
| 4292 | } |
| 4293 | |
| 4294 | // Tests EXPECT_ANY_THROW. |
| 4295 | TEST(ExpectTest, EXPECT_ANY_THROW) { |
| 4296 | EXPECT_ANY_THROW(ThrowAnInteger()); |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 4297 | EXPECT_NONFATAL_FAILURE( |
| 4298 | EXPECT_ANY_THROW(ThrowNothing()), |
| 4299 | "Expected: ThrowNothing() throws an exception.\n" |
| 4300 | " Actual: it doesn't."); |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | #endif // GTEST_HAS_EXCEPTIONS |
| 4304 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4305 | // Make sure we deal with the precedence of <<. |
| 4306 | TEST(ExpectTest, ExpectPrecedence) { |
| 4307 | EXPECT_EQ(1 < 2, true); |
| 4308 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(true, true && false), |
| 4309 | "Value of: true && false"); |
| 4310 | } |
| 4311 | |
| 4312 | |
| 4313 | // Tests the StreamableToString() function. |
| 4314 | |
| 4315 | // Tests using StreamableToString() on a scalar. |
| 4316 | TEST(StreamableToStringTest, Scalar) { |
| 4317 | EXPECT_STREQ("5", StreamableToString(5).c_str()); |
| 4318 | } |
| 4319 | |
| 4320 | // Tests using StreamableToString() on a non-char pointer. |
| 4321 | TEST(StreamableToStringTest, Pointer) { |
| 4322 | int n = 0; |
| 4323 | int* p = &n; |
| 4324 | EXPECT_STRNE("(null)", StreamableToString(p).c_str()); |
| 4325 | } |
| 4326 | |
| 4327 | // Tests using StreamableToString() on a NULL non-char pointer. |
| 4328 | TEST(StreamableToStringTest, NullPointer) { |
| 4329 | int* p = NULL; |
| 4330 | EXPECT_STREQ("(null)", StreamableToString(p).c_str()); |
| 4331 | } |
| 4332 | |
| 4333 | // Tests using StreamableToString() on a C string. |
| 4334 | TEST(StreamableToStringTest, CString) { |
| 4335 | EXPECT_STREQ("Foo", StreamableToString("Foo").c_str()); |
| 4336 | } |
| 4337 | |
| 4338 | // Tests using StreamableToString() on a NULL C string. |
| 4339 | TEST(StreamableToStringTest, NullCString) { |
| 4340 | char* p = NULL; |
| 4341 | EXPECT_STREQ("(null)", StreamableToString(p).c_str()); |
| 4342 | } |
| 4343 | |
| 4344 | // Tests using streamable values as assertion messages. |
| 4345 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4346 | // Tests using std::string as an assertion message. |
| 4347 | TEST(StreamableTest, string) { |
| 4348 | static const std::string str( |
| 4349 | "This failure message is a std::string, and is expected."); |
| 4350 | EXPECT_FATAL_FAILURE(FAIL() << str, |
| 4351 | str.c_str()); |
| 4352 | } |
| 4353 | |
| 4354 | // Tests that we can output strings containing embedded NULs. |
| 4355 | // Limited to Linux because we can only do this with std::string's. |
| 4356 | TEST(StreamableTest, stringWithEmbeddedNUL) { |
| 4357 | static const char char_array_with_nul[] = |
| 4358 | "Here's a NUL\0 and some more string"; |
| 4359 | static const std::string string_with_nul(char_array_with_nul, |
| 4360 | sizeof(char_array_with_nul) |
| 4361 | - 1); // drops the trailing NUL |
| 4362 | EXPECT_FATAL_FAILURE(FAIL() << string_with_nul, |
| 4363 | "Here's a NUL\\0 and some more string"); |
| 4364 | } |
| 4365 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4366 | // Tests that we can output a NUL char. |
| 4367 | TEST(StreamableTest, NULChar) { |
| 4368 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 4369 | FAIL() << "A NUL" << '\0' << " and some more string"; |
| 4370 | }, "A NUL\\0 and some more string"); |
| 4371 | } |
| 4372 | |
| 4373 | // Tests using int as an assertion message. |
| 4374 | TEST(StreamableTest, int) { |
| 4375 | EXPECT_FATAL_FAILURE(FAIL() << 900913, |
| 4376 | "900913"); |
| 4377 | } |
| 4378 | |
| 4379 | // Tests using NULL char pointer as an assertion message. |
| 4380 | // |
| 4381 | // In MSVC, streaming a NULL char * causes access violation. Google Test |
| 4382 | // implemented a workaround (substituting "(null)" for NULL). This |
| 4383 | // tests whether the workaround works. |
| 4384 | TEST(StreamableTest, NullCharPtr) { |
| 4385 | EXPECT_FATAL_FAILURE(FAIL() << static_cast<const char*>(NULL), |
| 4386 | "(null)"); |
| 4387 | } |
| 4388 | |
| 4389 | // Tests that basic IO manipulators (endl, ends, and flush) can be |
| 4390 | // streamed to testing::Message. |
| 4391 | TEST(StreamableTest, BasicIoManip) { |
| 4392 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 4393 | FAIL() << "Line 1." << std::endl |
| 4394 | << "A NUL char " << std::ends << std::flush << " in line 2."; |
| 4395 | }, "Line 1.\nA NUL char \\0 in line 2."); |
| 4396 | } |
| 4397 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4398 | // Tests the macros that haven't been covered so far. |
| 4399 | |
| 4400 | void AddFailureHelper(bool* aborted) { |
| 4401 | *aborted = true; |
| 4402 | ADD_FAILURE() << "Failure"; |
| 4403 | *aborted = false; |
| 4404 | } |
| 4405 | |
| 4406 | // Tests ADD_FAILURE. |
| 4407 | TEST(MacroTest, ADD_FAILURE) { |
| 4408 | bool aborted = true; |
| 4409 | EXPECT_NONFATAL_FAILURE(AddFailureHelper(&aborted), |
| 4410 | "Failure"); |
| 4411 | EXPECT_FALSE(aborted); |
| 4412 | } |
| 4413 | |
zhanyong.wan | d5ad2ca | 2010-07-26 21:59:50 +0000 | [diff] [blame] | 4414 | // Tests ADD_FAILURE_AT. |
| 4415 | TEST(MacroTest, ADD_FAILURE_AT) { |
| 4416 | // Verifies that ADD_FAILURE_AT does generate a nonfatal failure and |
| 4417 | // the failure message contains the user-streamed part. |
| 4418 | EXPECT_NONFATAL_FAILURE(ADD_FAILURE_AT("foo.cc", 42) << "Wrong!", "Wrong!"); |
| 4419 | |
| 4420 | // Verifies that the user-streamed part is optional. |
| 4421 | EXPECT_NONFATAL_FAILURE(ADD_FAILURE_AT("foo.cc", 42), "Failed"); |
| 4422 | |
| 4423 | // Unfortunately, we cannot verify that the failure message contains |
| 4424 | // the right file path and line number the same way, as |
| 4425 | // EXPECT_NONFATAL_FAILURE() doesn't get to see the file path and |
| 4426 | // line number. Instead, we do that in gtest_output_test_.cc. |
| 4427 | } |
| 4428 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4429 | // Tests FAIL. |
| 4430 | TEST(MacroTest, FAIL) { |
| 4431 | EXPECT_FATAL_FAILURE(FAIL(), |
| 4432 | "Failed"); |
| 4433 | EXPECT_FATAL_FAILURE(FAIL() << "Intentional failure.", |
| 4434 | "Intentional failure."); |
| 4435 | } |
| 4436 | |
| 4437 | // Tests SUCCEED |
| 4438 | TEST(MacroTest, SUCCEED) { |
| 4439 | SUCCEED(); |
| 4440 | SUCCEED() << "Explicit success."; |
| 4441 | } |
| 4442 | |
| 4443 | |
| 4444 | // Tests for EXPECT_EQ() and ASSERT_EQ(). |
| 4445 | // |
| 4446 | // These tests fail *intentionally*, s.t. the failure messages can be |
| 4447 | // generated and tested. |
| 4448 | // |
| 4449 | // We have different tests for different argument types. |
| 4450 | |
| 4451 | // Tests using bool values in {EXPECT|ASSERT}_EQ. |
| 4452 | TEST(EqAssertionTest, Bool) { |
| 4453 | EXPECT_EQ(true, true); |
| 4454 | EXPECT_FATAL_FAILURE(ASSERT_EQ(false, true), |
| 4455 | "Value of: true"); |
| 4456 | } |
| 4457 | |
| 4458 | // Tests using int values in {EXPECT|ASSERT}_EQ. |
| 4459 | TEST(EqAssertionTest, Int) { |
| 4460 | ASSERT_EQ(32, 32); |
| 4461 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(32, 33), |
| 4462 | "33"); |
| 4463 | } |
| 4464 | |
| 4465 | // Tests using time_t values in {EXPECT|ASSERT}_EQ. |
| 4466 | TEST(EqAssertionTest, Time_T) { |
| 4467 | EXPECT_EQ(static_cast<time_t>(0), |
| 4468 | static_cast<time_t>(0)); |
| 4469 | EXPECT_FATAL_FAILURE(ASSERT_EQ(static_cast<time_t>(0), |
| 4470 | static_cast<time_t>(1234)), |
| 4471 | "1234"); |
| 4472 | } |
| 4473 | |
| 4474 | // Tests using char values in {EXPECT|ASSERT}_EQ. |
| 4475 | TEST(EqAssertionTest, Char) { |
| 4476 | ASSERT_EQ('z', 'z'); |
| 4477 | const char ch = 'b'; |
| 4478 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ('\0', ch), |
| 4479 | "ch"); |
| 4480 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ('a', ch), |
| 4481 | "ch"); |
| 4482 | } |
| 4483 | |
| 4484 | // Tests using wchar_t values in {EXPECT|ASSERT}_EQ. |
| 4485 | TEST(EqAssertionTest, WideChar) { |
| 4486 | EXPECT_EQ(L'b', L'b'); |
| 4487 | |
| 4488 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(L'\0', L'x'), |
| 4489 | "Value of: L'x'\n" |
| 4490 | " Actual: L'x' (120, 0x78)\n" |
| 4491 | "Expected: L'\0'\n" |
| 4492 | "Which is: L'\0' (0, 0x0)"); |
| 4493 | |
| 4494 | static wchar_t wchar; |
| 4495 | wchar = L'b'; |
| 4496 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(L'a', wchar), |
| 4497 | "wchar"); |
| 4498 | wchar = L'\x8119'; |
| 4499 | EXPECT_FATAL_FAILURE(ASSERT_EQ(L'\x8120', wchar), |
| 4500 | "Value of: wchar"); |
| 4501 | } |
| 4502 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4503 | // Tests using ::std::string values in {EXPECT|ASSERT}_EQ. |
| 4504 | TEST(EqAssertionTest, StdString) { |
| 4505 | // Compares a const char* to an std::string that has identical |
| 4506 | // content. |
| 4507 | ASSERT_EQ("Test", ::std::string("Test")); |
| 4508 | |
| 4509 | // Compares two identical std::strings. |
| 4510 | static const ::std::string str1("A * in the middle"); |
| 4511 | static const ::std::string str2(str1); |
| 4512 | EXPECT_EQ(str1, str2); |
| 4513 | |
| 4514 | // Compares a const char* to an std::string that has different |
| 4515 | // content |
| 4516 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ("Test", ::std::string("test")), |
| 4517 | "::std::string(\"test\")"); |
| 4518 | |
| 4519 | // Compares an std::string to a char* that has different content. |
| 4520 | char* const p1 = const_cast<char*>("foo"); |
| 4521 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(::std::string("bar"), p1), |
| 4522 | "p1"); |
| 4523 | |
| 4524 | // Compares two std::strings that have different contents, one of |
| 4525 | // which having a NUL character in the middle. This should fail. |
| 4526 | static ::std::string str3(str1); |
| 4527 | str3.at(2) = '\0'; |
| 4528 | EXPECT_FATAL_FAILURE(ASSERT_EQ(str1, str3), |
| 4529 | "Value of: str3\n" |
| 4530 | " Actual: \"A \\0 in the middle\""); |
| 4531 | } |
| 4532 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4533 | #if GTEST_HAS_STD_WSTRING |
| 4534 | |
| 4535 | // Tests using ::std::wstring values in {EXPECT|ASSERT}_EQ. |
| 4536 | TEST(EqAssertionTest, StdWideString) { |
| 4537 | // Compares an std::wstring to a const wchar_t* that has identical |
| 4538 | // content. |
| 4539 | EXPECT_EQ(::std::wstring(L"Test\x8119"), L"Test\x8119"); |
| 4540 | |
| 4541 | // Compares two identical std::wstrings. |
| 4542 | const ::std::wstring wstr1(L"A * in the middle"); |
| 4543 | const ::std::wstring wstr2(wstr1); |
| 4544 | ASSERT_EQ(wstr1, wstr2); |
| 4545 | |
| 4546 | // Compares an std::wstring to a const wchar_t* that has different |
| 4547 | // content. |
| 4548 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 4549 | EXPECT_EQ(::std::wstring(L"Test\x8119"), L"Test\x8120"); |
| 4550 | }, "L\"Test\\x8120\""); |
| 4551 | |
| 4552 | // Compares two std::wstrings that have different contents, one of |
| 4553 | // which having a NUL character in the middle. |
| 4554 | ::std::wstring wstr3(wstr1); |
| 4555 | wstr3.at(2) = L'\0'; |
| 4556 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(wstr1, wstr3), |
| 4557 | "wstr3"); |
| 4558 | |
| 4559 | // Compares a wchar_t* to an std::wstring that has different |
| 4560 | // content. |
| 4561 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 4562 | ASSERT_EQ(const_cast<wchar_t*>(L"foo"), ::std::wstring(L"bar")); |
| 4563 | }, ""); |
| 4564 | } |
| 4565 | |
| 4566 | #endif // GTEST_HAS_STD_WSTRING |
| 4567 | |
| 4568 | #if GTEST_HAS_GLOBAL_STRING |
| 4569 | // Tests using ::string values in {EXPECT|ASSERT}_EQ. |
| 4570 | TEST(EqAssertionTest, GlobalString) { |
| 4571 | // Compares a const char* to a ::string that has identical content. |
| 4572 | EXPECT_EQ("Test", ::string("Test")); |
| 4573 | |
| 4574 | // Compares two identical ::strings. |
| 4575 | const ::string str1("A * in the middle"); |
| 4576 | const ::string str2(str1); |
| 4577 | ASSERT_EQ(str1, str2); |
| 4578 | |
| 4579 | // Compares a ::string to a const char* that has different content. |
| 4580 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(::string("Test"), "test"), |
| 4581 | "test"); |
| 4582 | |
| 4583 | // Compares two ::strings that have different contents, one of which |
| 4584 | // having a NUL character in the middle. |
| 4585 | ::string str3(str1); |
| 4586 | str3.at(2) = '\0'; |
| 4587 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(str1, str3), |
| 4588 | "str3"); |
| 4589 | |
| 4590 | // Compares a ::string to a char* that has different content. |
| 4591 | EXPECT_FATAL_FAILURE({ // NOLINT |
| 4592 | ASSERT_EQ(::string("bar"), const_cast<char*>("foo")); |
| 4593 | }, ""); |
| 4594 | } |
| 4595 | |
| 4596 | #endif // GTEST_HAS_GLOBAL_STRING |
| 4597 | |
| 4598 | #if GTEST_HAS_GLOBAL_WSTRING |
| 4599 | |
| 4600 | // Tests using ::wstring values in {EXPECT|ASSERT}_EQ. |
| 4601 | TEST(EqAssertionTest, GlobalWideString) { |
| 4602 | // Compares a const wchar_t* to a ::wstring that has identical content. |
| 4603 | ASSERT_EQ(L"Test\x8119", ::wstring(L"Test\x8119")); |
| 4604 | |
| 4605 | // Compares two identical ::wstrings. |
| 4606 | static const ::wstring wstr1(L"A * in the middle"); |
| 4607 | static const ::wstring wstr2(wstr1); |
| 4608 | EXPECT_EQ(wstr1, wstr2); |
| 4609 | |
| 4610 | // Compares a const wchar_t* to a ::wstring that has different |
| 4611 | // content. |
| 4612 | EXPECT_NONFATAL_FAILURE({ // NOLINT |
| 4613 | EXPECT_EQ(L"Test\x8120", ::wstring(L"Test\x8119")); |
| 4614 | }, "Test\\x8119"); |
| 4615 | |
| 4616 | // Compares a wchar_t* to a ::wstring that has different content. |
| 4617 | wchar_t* const p1 = const_cast<wchar_t*>(L"foo"); |
| 4618 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p1, ::wstring(L"bar")), |
| 4619 | "bar"); |
| 4620 | |
| 4621 | // Compares two ::wstrings that have different contents, one of which |
| 4622 | // having a NUL character in the middle. |
| 4623 | static ::wstring wstr3; |
| 4624 | wstr3 = wstr1; |
| 4625 | wstr3.at(2) = L'\0'; |
| 4626 | EXPECT_FATAL_FAILURE(ASSERT_EQ(wstr1, wstr3), |
| 4627 | "wstr3"); |
| 4628 | } |
| 4629 | |
| 4630 | #endif // GTEST_HAS_GLOBAL_WSTRING |
| 4631 | |
| 4632 | // Tests using char pointers in {EXPECT|ASSERT}_EQ. |
| 4633 | TEST(EqAssertionTest, CharPointer) { |
| 4634 | char* const p0 = NULL; |
| 4635 | // Only way to get the Nokia compiler to compile the cast |
| 4636 | // is to have a separate void* variable first. Putting |
| 4637 | // the two casts on the same line doesn't work, neither does |
| 4638 | // a direct C-style to char*. |
| 4639 | void* pv1 = (void*)0x1234; // NOLINT |
| 4640 | void* pv2 = (void*)0xABC0; // NOLINT |
| 4641 | char* const p1 = reinterpret_cast<char*>(pv1); |
| 4642 | char* const p2 = reinterpret_cast<char*>(pv2); |
| 4643 | ASSERT_EQ(p1, p1); |
| 4644 | |
| 4645 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p0, p2), |
| 4646 | "Value of: p2"); |
| 4647 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p1, p2), |
| 4648 | "p2"); |
| 4649 | EXPECT_FATAL_FAILURE(ASSERT_EQ(reinterpret_cast<char*>(0x1234), |
| 4650 | reinterpret_cast<char*>(0xABC0)), |
| 4651 | "ABC0"); |
| 4652 | } |
| 4653 | |
| 4654 | // Tests using wchar_t pointers in {EXPECT|ASSERT}_EQ. |
| 4655 | TEST(EqAssertionTest, WideCharPointer) { |
| 4656 | wchar_t* const p0 = NULL; |
| 4657 | // Only way to get the Nokia compiler to compile the cast |
| 4658 | // is to have a separate void* variable first. Putting |
| 4659 | // the two casts on the same line doesn't work, neither does |
| 4660 | // a direct C-style to char*. |
| 4661 | void* pv1 = (void*)0x1234; // NOLINT |
| 4662 | void* pv2 = (void*)0xABC0; // NOLINT |
| 4663 | wchar_t* const p1 = reinterpret_cast<wchar_t*>(pv1); |
| 4664 | wchar_t* const p2 = reinterpret_cast<wchar_t*>(pv2); |
| 4665 | EXPECT_EQ(p0, p0); |
| 4666 | |
| 4667 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p0, p2), |
| 4668 | "Value of: p2"); |
| 4669 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p1, p2), |
| 4670 | "p2"); |
| 4671 | void* pv3 = (void*)0x1234; // NOLINT |
| 4672 | void* pv4 = (void*)0xABC0; // NOLINT |
| 4673 | const wchar_t* p3 = reinterpret_cast<const wchar_t*>(pv3); |
| 4674 | const wchar_t* p4 = reinterpret_cast<const wchar_t*>(pv4); |
| 4675 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(p3, p4), |
| 4676 | "p4"); |
| 4677 | } |
| 4678 | |
| 4679 | // Tests using other types of pointers in {EXPECT|ASSERT}_EQ. |
| 4680 | TEST(EqAssertionTest, OtherPointer) { |
| 4681 | ASSERT_EQ(static_cast<const int*>(NULL), |
| 4682 | static_cast<const int*>(NULL)); |
| 4683 | EXPECT_FATAL_FAILURE(ASSERT_EQ(static_cast<const int*>(NULL), |
| 4684 | reinterpret_cast<const int*>(0x1234)), |
| 4685 | "0x1234"); |
| 4686 | } |
| 4687 | |
zhanyong.wan | 39dc95e | 2010-07-21 22:15:17 +0000 | [diff] [blame] | 4688 | // A class that supports binary comparison operators but not streaming. |
| 4689 | class UnprintableChar { |
| 4690 | public: |
| 4691 | explicit UnprintableChar(char ch) : char_(ch) {} |
| 4692 | |
| 4693 | bool operator==(const UnprintableChar& rhs) const { |
| 4694 | return char_ == rhs.char_; |
| 4695 | } |
| 4696 | bool operator!=(const UnprintableChar& rhs) const { |
| 4697 | return char_ != rhs.char_; |
| 4698 | } |
| 4699 | bool operator<(const UnprintableChar& rhs) const { |
| 4700 | return char_ < rhs.char_; |
| 4701 | } |
| 4702 | bool operator<=(const UnprintableChar& rhs) const { |
| 4703 | return char_ <= rhs.char_; |
| 4704 | } |
| 4705 | bool operator>(const UnprintableChar& rhs) const { |
| 4706 | return char_ > rhs.char_; |
| 4707 | } |
| 4708 | bool operator>=(const UnprintableChar& rhs) const { |
| 4709 | return char_ >= rhs.char_; |
| 4710 | } |
| 4711 | |
| 4712 | private: |
| 4713 | char char_; |
| 4714 | }; |
| 4715 | |
| 4716 | // Tests that ASSERT_EQ() and friends don't require the arguments to |
| 4717 | // be printable. |
| 4718 | TEST(ComparisonAssertionTest, AcceptsUnprintableArgs) { |
| 4719 | const UnprintableChar x('x'), y('y'); |
| 4720 | ASSERT_EQ(x, x); |
| 4721 | EXPECT_NE(x, y); |
| 4722 | ASSERT_LT(x, y); |
| 4723 | EXPECT_LE(x, y); |
| 4724 | ASSERT_GT(y, x); |
| 4725 | EXPECT_GE(x, x); |
| 4726 | |
| 4727 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(x, y), "1-byte object <78>"); |
| 4728 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(x, y), "1-byte object <79>"); |
| 4729 | EXPECT_NONFATAL_FAILURE(EXPECT_LT(y, y), "1-byte object <79>"); |
| 4730 | EXPECT_NONFATAL_FAILURE(EXPECT_GT(x, y), "1-byte object <78>"); |
| 4731 | EXPECT_NONFATAL_FAILURE(EXPECT_GT(x, y), "1-byte object <79>"); |
| 4732 | |
| 4733 | // Code tested by EXPECT_FATAL_FAILURE cannot reference local |
| 4734 | // variables, so we have to write UnprintableChar('x') instead of x. |
| 4735 | EXPECT_FATAL_FAILURE(ASSERT_NE(UnprintableChar('x'), UnprintableChar('x')), |
| 4736 | "1-byte object <78>"); |
| 4737 | EXPECT_FATAL_FAILURE(ASSERT_LE(UnprintableChar('y'), UnprintableChar('x')), |
| 4738 | "1-byte object <78>"); |
| 4739 | EXPECT_FATAL_FAILURE(ASSERT_LE(UnprintableChar('y'), UnprintableChar('x')), |
| 4740 | "1-byte object <79>"); |
| 4741 | EXPECT_FATAL_FAILURE(ASSERT_GE(UnprintableChar('x'), UnprintableChar('y')), |
| 4742 | "1-byte object <78>"); |
| 4743 | EXPECT_FATAL_FAILURE(ASSERT_GE(UnprintableChar('x'), UnprintableChar('y')), |
| 4744 | "1-byte object <79>"); |
| 4745 | } |
| 4746 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4747 | // Tests the FRIEND_TEST macro. |
| 4748 | |
| 4749 | // This class has a private member we want to test. We will test it |
| 4750 | // both in a TEST and in a TEST_F. |
| 4751 | class Foo { |
| 4752 | public: |
| 4753 | Foo() {} |
| 4754 | |
| 4755 | private: |
| 4756 | int Bar() const { return 1; } |
| 4757 | |
| 4758 | // Declares the friend tests that can access the private member |
| 4759 | // Bar(). |
| 4760 | FRIEND_TEST(FRIEND_TEST_Test, TEST); |
| 4761 | FRIEND_TEST(FRIEND_TEST_Test2, TEST_F); |
| 4762 | }; |
| 4763 | |
| 4764 | // Tests that the FRIEND_TEST declaration allows a TEST to access a |
| 4765 | // class's private members. This should compile. |
| 4766 | TEST(FRIEND_TEST_Test, TEST) { |
| 4767 | ASSERT_EQ(1, Foo().Bar()); |
| 4768 | } |
| 4769 | |
| 4770 | // The fixture needed to test using FRIEND_TEST with TEST_F. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4771 | class FRIEND_TEST_Test2 : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4772 | protected: |
| 4773 | Foo foo; |
| 4774 | }; |
| 4775 | |
| 4776 | // Tests that the FRIEND_TEST declaration allows a TEST_F to access a |
| 4777 | // class's private members. This should compile. |
| 4778 | TEST_F(FRIEND_TEST_Test2, TEST_F) { |
| 4779 | ASSERT_EQ(1, foo.Bar()); |
| 4780 | } |
| 4781 | |
| 4782 | // Tests the life cycle of Test objects. |
| 4783 | |
| 4784 | // The test fixture for testing the life cycle of Test objects. |
| 4785 | // |
| 4786 | // This class counts the number of live test objects that uses this |
| 4787 | // fixture. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4788 | class TestLifeCycleTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4789 | protected: |
| 4790 | // Constructor. Increments the number of test objects that uses |
| 4791 | // this fixture. |
| 4792 | TestLifeCycleTest() { count_++; } |
| 4793 | |
| 4794 | // Destructor. Decrements the number of test objects that uses this |
| 4795 | // fixture. |
| 4796 | ~TestLifeCycleTest() { count_--; } |
| 4797 | |
| 4798 | // Returns the number of live test objects that uses this fixture. |
| 4799 | int count() const { return count_; } |
| 4800 | |
| 4801 | private: |
| 4802 | static int count_; |
| 4803 | }; |
| 4804 | |
| 4805 | int TestLifeCycleTest::count_ = 0; |
| 4806 | |
| 4807 | // Tests the life cycle of test objects. |
| 4808 | TEST_F(TestLifeCycleTest, Test1) { |
| 4809 | // There should be only one test object in this test case that's |
| 4810 | // currently alive. |
| 4811 | ASSERT_EQ(1, count()); |
| 4812 | } |
| 4813 | |
| 4814 | // Tests the life cycle of test objects. |
| 4815 | TEST_F(TestLifeCycleTest, Test2) { |
| 4816 | // After Test1 is done and Test2 is started, there should still be |
| 4817 | // only one live test object, as the object for Test1 should've been |
| 4818 | // deleted. |
| 4819 | ASSERT_EQ(1, count()); |
| 4820 | } |
| 4821 | |
| 4822 | } // namespace |
| 4823 | |
vladlosev | fbd53a5 | 2009-10-20 21:03:10 +0000 | [diff] [blame] | 4824 | // Tests that the copy constructor works when it is NOT optimized away by |
| 4825 | // the compiler. |
| 4826 | TEST(AssertionResultTest, CopyConstructorWorksWhenNotOptimied) { |
| 4827 | // Checks that the copy constructor doesn't try to dereference NULL pointers |
| 4828 | // in the source object. |
| 4829 | AssertionResult r1 = AssertionSuccess(); |
| 4830 | AssertionResult r2 = r1; |
| 4831 | // The following line is added to prevent the compiler from optimizing |
| 4832 | // away the constructor call. |
| 4833 | r1 << "abc"; |
| 4834 | |
| 4835 | AssertionResult r3 = r1; |
| 4836 | EXPECT_EQ(static_cast<bool>(r3), static_cast<bool>(r1)); |
| 4837 | EXPECT_STREQ("abc", r1.message()); |
| 4838 | } |
| 4839 | |
| 4840 | // Tests that AssertionSuccess and AssertionFailure construct |
| 4841 | // AssertionResult objects as expected. |
| 4842 | TEST(AssertionResultTest, ConstructionWorks) { |
| 4843 | AssertionResult r1 = AssertionSuccess(); |
| 4844 | EXPECT_TRUE(r1); |
| 4845 | EXPECT_STREQ("", r1.message()); |
| 4846 | |
| 4847 | AssertionResult r2 = AssertionSuccess() << "abc"; |
| 4848 | EXPECT_TRUE(r2); |
| 4849 | EXPECT_STREQ("abc", r2.message()); |
| 4850 | |
| 4851 | AssertionResult r3 = AssertionFailure(); |
| 4852 | EXPECT_FALSE(r3); |
| 4853 | EXPECT_STREQ("", r3.message()); |
| 4854 | |
| 4855 | AssertionResult r4 = AssertionFailure() << "def"; |
| 4856 | EXPECT_FALSE(r4); |
| 4857 | EXPECT_STREQ("def", r4.message()); |
| 4858 | |
| 4859 | AssertionResult r5 = AssertionFailure(Message() << "ghi"); |
| 4860 | EXPECT_FALSE(r5); |
| 4861 | EXPECT_STREQ("ghi", r5.message()); |
| 4862 | } |
| 4863 | |
| 4864 | // Tests that the negation fips the predicate result but keeps the message. |
| 4865 | TEST(AssertionResultTest, NegationWorks) { |
| 4866 | AssertionResult r1 = AssertionSuccess() << "abc"; |
| 4867 | EXPECT_FALSE(!r1); |
| 4868 | EXPECT_STREQ("abc", (!r1).message()); |
| 4869 | |
| 4870 | AssertionResult r2 = AssertionFailure() << "def"; |
| 4871 | EXPECT_TRUE(!r2); |
| 4872 | EXPECT_STREQ("def", (!r2).message()); |
| 4873 | } |
| 4874 | |
| 4875 | TEST(AssertionResultTest, StreamingWorks) { |
| 4876 | AssertionResult r = AssertionSuccess(); |
| 4877 | r << "abc" << 'd' << 0 << true; |
| 4878 | EXPECT_STREQ("abcd0true", r.message()); |
| 4879 | } |
| 4880 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4881 | // Tests streaming a user type whose definition and operator << are |
| 4882 | // both in the global namespace. |
| 4883 | class Base { |
| 4884 | public: |
zhanyong.wan | 7de3401 | 2009-12-16 19:54:05 +0000 | [diff] [blame] | 4885 | explicit Base(int an_x) : x_(an_x) {} |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4886 | int x() const { return x_; } |
| 4887 | private: |
| 4888 | int x_; |
| 4889 | }; |
| 4890 | std::ostream& operator<<(std::ostream& os, |
| 4891 | const Base& val) { |
| 4892 | return os << val.x(); |
| 4893 | } |
| 4894 | std::ostream& operator<<(std::ostream& os, |
| 4895 | const Base* pointer) { |
| 4896 | return os << "(" << pointer->x() << ")"; |
| 4897 | } |
| 4898 | |
| 4899 | TEST(MessageTest, CanStreamUserTypeInGlobalNameSpace) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4900 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4901 | Base a(1); |
| 4902 | |
| 4903 | msg << a << &a; // Uses ::operator<<. |
| 4904 | EXPECT_STREQ("1(1)", msg.GetString().c_str()); |
| 4905 | } |
| 4906 | |
| 4907 | // Tests streaming a user type whose definition and operator<< are |
| 4908 | // both in an unnamed namespace. |
| 4909 | namespace { |
| 4910 | class MyTypeInUnnamedNameSpace : public Base { |
| 4911 | public: |
zhanyong.wan | 7de3401 | 2009-12-16 19:54:05 +0000 | [diff] [blame] | 4912 | explicit MyTypeInUnnamedNameSpace(int an_x): Base(an_x) {} |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4913 | }; |
| 4914 | std::ostream& operator<<(std::ostream& os, |
| 4915 | const MyTypeInUnnamedNameSpace& val) { |
| 4916 | return os << val.x(); |
| 4917 | } |
| 4918 | std::ostream& operator<<(std::ostream& os, |
| 4919 | const MyTypeInUnnamedNameSpace* pointer) { |
| 4920 | return os << "(" << pointer->x() << ")"; |
| 4921 | } |
| 4922 | } // namespace |
| 4923 | |
| 4924 | TEST(MessageTest, CanStreamUserTypeInUnnamedNameSpace) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4925 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4926 | MyTypeInUnnamedNameSpace a(1); |
| 4927 | |
| 4928 | msg << a << &a; // Uses <unnamed_namespace>::operator<<. |
| 4929 | EXPECT_STREQ("1(1)", msg.GetString().c_str()); |
| 4930 | } |
| 4931 | |
| 4932 | // Tests streaming a user type whose definition and operator<< are |
| 4933 | // both in a user namespace. |
| 4934 | namespace namespace1 { |
| 4935 | class MyTypeInNameSpace1 : public Base { |
| 4936 | public: |
zhanyong.wan | 7de3401 | 2009-12-16 19:54:05 +0000 | [diff] [blame] | 4937 | explicit MyTypeInNameSpace1(int an_x): Base(an_x) {} |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4938 | }; |
| 4939 | std::ostream& operator<<(std::ostream& os, |
| 4940 | const MyTypeInNameSpace1& val) { |
| 4941 | return os << val.x(); |
| 4942 | } |
| 4943 | std::ostream& operator<<(std::ostream& os, |
| 4944 | const MyTypeInNameSpace1* pointer) { |
| 4945 | return os << "(" << pointer->x() << ")"; |
| 4946 | } |
| 4947 | } // namespace namespace1 |
| 4948 | |
| 4949 | TEST(MessageTest, CanStreamUserTypeInUserNameSpace) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4950 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4951 | namespace1::MyTypeInNameSpace1 a(1); |
| 4952 | |
| 4953 | msg << a << &a; // Uses namespace1::operator<<. |
| 4954 | EXPECT_STREQ("1(1)", msg.GetString().c_str()); |
| 4955 | } |
| 4956 | |
| 4957 | // Tests streaming a user type whose definition is in a user namespace |
| 4958 | // but whose operator<< is in the global namespace. |
| 4959 | namespace namespace2 { |
| 4960 | class MyTypeInNameSpace2 : public ::Base { |
| 4961 | public: |
zhanyong.wan | 7de3401 | 2009-12-16 19:54:05 +0000 | [diff] [blame] | 4962 | explicit MyTypeInNameSpace2(int an_x): Base(an_x) {} |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4963 | }; |
| 4964 | } // namespace namespace2 |
| 4965 | std::ostream& operator<<(std::ostream& os, |
| 4966 | const namespace2::MyTypeInNameSpace2& val) { |
| 4967 | return os << val.x(); |
| 4968 | } |
| 4969 | std::ostream& operator<<(std::ostream& os, |
| 4970 | const namespace2::MyTypeInNameSpace2* pointer) { |
| 4971 | return os << "(" << pointer->x() << ")"; |
| 4972 | } |
| 4973 | |
| 4974 | TEST(MessageTest, CanStreamUserTypeInUserNameSpaceWithStreamOperatorInGlobal) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4975 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4976 | namespace2::MyTypeInNameSpace2 a(1); |
| 4977 | |
| 4978 | msg << a << &a; // Uses ::operator<<. |
| 4979 | EXPECT_STREQ("1(1)", msg.GetString().c_str()); |
| 4980 | } |
| 4981 | |
| 4982 | // Tests streaming NULL pointers to testing::Message. |
| 4983 | TEST(MessageTest, NullPointers) { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4984 | Message msg; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4985 | char* const p1 = NULL; |
| 4986 | unsigned char* const p2 = NULL; |
| 4987 | int* p3 = NULL; |
| 4988 | double* p4 = NULL; |
| 4989 | bool* p5 = NULL; |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 4990 | Message* p6 = NULL; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4991 | |
| 4992 | msg << p1 << p2 << p3 << p4 << p5 << p6; |
| 4993 | ASSERT_STREQ("(null)(null)(null)(null)(null)(null)", |
| 4994 | msg.GetString().c_str()); |
| 4995 | } |
| 4996 | |
| 4997 | // Tests streaming wide strings to testing::Message. |
| 4998 | TEST(MessageTest, WideStrings) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 4999 | // Streams a NULL of type const wchar_t*. |
| 5000 | const wchar_t* const_wstr = NULL; |
| 5001 | EXPECT_STREQ("(null)", |
| 5002 | (Message() << const_wstr).GetString().c_str()); |
| 5003 | |
| 5004 | // Streams a NULL of type wchar_t*. |
| 5005 | wchar_t* wstr = NULL; |
| 5006 | EXPECT_STREQ("(null)", |
| 5007 | (Message() << wstr).GetString().c_str()); |
| 5008 | |
| 5009 | // Streams a non-NULL of type const wchar_t*. |
| 5010 | const_wstr = L"abc\x8119"; |
| 5011 | EXPECT_STREQ("abc\xe8\x84\x99", |
| 5012 | (Message() << const_wstr).GetString().c_str()); |
| 5013 | |
| 5014 | // Streams a non-NULL of type wchar_t*. |
| 5015 | wstr = const_cast<wchar_t*>(const_wstr); |
| 5016 | EXPECT_STREQ("abc\xe8\x84\x99", |
| 5017 | (Message() << wstr).GetString().c_str()); |
| 5018 | } |
| 5019 | |
| 5020 | |
| 5021 | // This line tests that we can define tests in the testing namespace. |
| 5022 | namespace testing { |
| 5023 | |
| 5024 | // Tests the TestInfo class. |
| 5025 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5026 | class TestInfoTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5027 | protected: |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 5028 | static const TestInfo* GetTestInfo(const char* test_name) { |
| 5029 | const TestCase* const test_case = GetUnitTestImpl()-> |
| 5030 | GetTestCase("TestInfoTest", "", NULL, NULL); |
| 5031 | |
| 5032 | for (int i = 0; i < test_case->total_test_count(); ++i) { |
| 5033 | const TestInfo* const test_info = test_case->GetTestInfo(i); |
| 5034 | if (strcmp(test_name, test_info->name()) == 0) |
| 5035 | return test_info; |
| 5036 | } |
| 5037 | return NULL; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | static const TestResult* GetTestResult( |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5041 | const TestInfo* test_info) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5042 | return test_info->result(); |
| 5043 | } |
| 5044 | }; |
| 5045 | |
| 5046 | // Tests TestInfo::test_case_name() and TestInfo::name(). |
| 5047 | TEST_F(TestInfoTest, Names) { |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 5048 | const TestInfo* const test_info = GetTestInfo("Names"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5049 | |
| 5050 | ASSERT_STREQ("TestInfoTest", test_info->test_case_name()); |
| 5051 | ASSERT_STREQ("Names", test_info->name()); |
| 5052 | } |
| 5053 | |
| 5054 | // Tests TestInfo::result(). |
| 5055 | TEST_F(TestInfoTest, result) { |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 5056 | const TestInfo* const test_info = GetTestInfo("result"); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5057 | |
| 5058 | // Initially, there is no TestPartResult for this test. |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 5059 | ASSERT_EQ(0, GetTestResult(test_info)->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5060 | |
| 5061 | // After the previous assertion, there is still none. |
zhanyong.wan | b7ec0f7 | 2009-07-01 04:58:05 +0000 | [diff] [blame] | 5062 | ASSERT_EQ(0, GetTestResult(test_info)->total_part_count()); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5063 | } |
| 5064 | |
| 5065 | // Tests setting up and tearing down a test case. |
| 5066 | |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5067 | class SetUpTestCaseTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5068 | protected: |
| 5069 | // This will be called once before the first test in this test case |
| 5070 | // is run. |
| 5071 | static void SetUpTestCase() { |
| 5072 | printf("Setting up the test case . . .\n"); |
| 5073 | |
| 5074 | // Initializes some shared resource. In this simple example, we |
| 5075 | // just create a C string. More complex stuff can be done if |
| 5076 | // desired. |
| 5077 | shared_resource_ = "123"; |
| 5078 | |
| 5079 | // Increments the number of test cases that have been set up. |
| 5080 | counter_++; |
| 5081 | |
| 5082 | // SetUpTestCase() should be called only once. |
| 5083 | EXPECT_EQ(1, counter_); |
| 5084 | } |
| 5085 | |
| 5086 | // This will be called once after the last test in this test case is |
| 5087 | // run. |
| 5088 | static void TearDownTestCase() { |
| 5089 | printf("Tearing down the test case . . .\n"); |
| 5090 | |
| 5091 | // Decrements the number of test cases that have been set up. |
| 5092 | counter_--; |
| 5093 | |
| 5094 | // TearDownTestCase() should be called only once. |
| 5095 | EXPECT_EQ(0, counter_); |
| 5096 | |
| 5097 | // Cleans up the shared resource. |
| 5098 | shared_resource_ = NULL; |
| 5099 | } |
| 5100 | |
| 5101 | // This will be called before each test in this test case. |
| 5102 | virtual void SetUp() { |
| 5103 | // SetUpTestCase() should be called only once, so counter_ should |
| 5104 | // always be 1. |
| 5105 | EXPECT_EQ(1, counter_); |
| 5106 | } |
| 5107 | |
| 5108 | // Number of test cases that have been set up. |
| 5109 | static int counter_; |
| 5110 | |
| 5111 | // Some resource to be shared by all tests in this test case. |
| 5112 | static const char* shared_resource_; |
| 5113 | }; |
| 5114 | |
| 5115 | int SetUpTestCaseTest::counter_ = 0; |
| 5116 | const char* SetUpTestCaseTest::shared_resource_ = NULL; |
| 5117 | |
| 5118 | // A test that uses the shared resource. |
| 5119 | TEST_F(SetUpTestCaseTest, Test1) { |
| 5120 | EXPECT_STRNE(NULL, shared_resource_); |
| 5121 | } |
| 5122 | |
| 5123 | // Another test that uses the shared resource. |
| 5124 | TEST_F(SetUpTestCaseTest, Test2) { |
| 5125 | EXPECT_STREQ("123", shared_resource_); |
| 5126 | } |
| 5127 | |
| 5128 | // The InitGoogleTestTest test case tests testing::InitGoogleTest(). |
| 5129 | |
| 5130 | // The Flags struct stores a copy of all Google Test flags. |
| 5131 | struct Flags { |
| 5132 | // Constructs a Flags struct where each flag has its default value. |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5133 | Flags() : also_run_disabled_tests(false), |
| 5134 | break_on_failure(false), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5135 | catch_exceptions(false), |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5136 | death_test_use_fork(false), |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5137 | filter(""), |
| 5138 | list_tests(false), |
| 5139 | output(""), |
zhanyong.wan | 73ad5a3 | 2009-04-14 23:19:22 +0000 | [diff] [blame] | 5140 | print_time(true), |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5141 | random_seed(0), |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5142 | repeat(1), |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5143 | shuffle(false), |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5144 | stack_trace_depth(kMaxStackTraceDepth), |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5145 | throw_on_failure(false) {} |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5146 | |
| 5147 | // Factory methods. |
| 5148 | |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5149 | // Creates a Flags struct where the gtest_also_run_disabled_tests flag has |
| 5150 | // the given value. |
| 5151 | static Flags AlsoRunDisabledTests(bool also_run_disabled_tests) { |
| 5152 | Flags flags; |
| 5153 | flags.also_run_disabled_tests = also_run_disabled_tests; |
| 5154 | return flags; |
| 5155 | } |
| 5156 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5157 | // Creates a Flags struct where the gtest_break_on_failure flag has |
| 5158 | // the given value. |
| 5159 | static Flags BreakOnFailure(bool break_on_failure) { |
| 5160 | Flags flags; |
| 5161 | flags.break_on_failure = break_on_failure; |
| 5162 | return flags; |
| 5163 | } |
| 5164 | |
| 5165 | // Creates a Flags struct where the gtest_catch_exceptions flag has |
| 5166 | // the given value. |
| 5167 | static Flags CatchExceptions(bool catch_exceptions) { |
| 5168 | Flags flags; |
| 5169 | flags.catch_exceptions = catch_exceptions; |
| 5170 | return flags; |
| 5171 | } |
| 5172 | |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5173 | // Creates a Flags struct where the gtest_death_test_use_fork flag has |
| 5174 | // the given value. |
| 5175 | static Flags DeathTestUseFork(bool death_test_use_fork) { |
| 5176 | Flags flags; |
| 5177 | flags.death_test_use_fork = death_test_use_fork; |
| 5178 | return flags; |
| 5179 | } |
| 5180 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5181 | // Creates a Flags struct where the gtest_filter flag has the given |
| 5182 | // value. |
| 5183 | static Flags Filter(const char* filter) { |
| 5184 | Flags flags; |
| 5185 | flags.filter = filter; |
| 5186 | return flags; |
| 5187 | } |
| 5188 | |
| 5189 | // Creates a Flags struct where the gtest_list_tests flag has the |
| 5190 | // given value. |
| 5191 | static Flags ListTests(bool list_tests) { |
| 5192 | Flags flags; |
| 5193 | flags.list_tests = list_tests; |
| 5194 | return flags; |
| 5195 | } |
| 5196 | |
| 5197 | // Creates a Flags struct where the gtest_output flag has the given |
| 5198 | // value. |
| 5199 | static Flags Output(const char* output) { |
| 5200 | Flags flags; |
| 5201 | flags.output = output; |
| 5202 | return flags; |
| 5203 | } |
| 5204 | |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5205 | // Creates a Flags struct where the gtest_print_time flag has the given |
| 5206 | // value. |
| 5207 | static Flags PrintTime(bool print_time) { |
| 5208 | Flags flags; |
| 5209 | flags.print_time = print_time; |
| 5210 | return flags; |
| 5211 | } |
| 5212 | |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5213 | // Creates a Flags struct where the gtest_random_seed flag has |
| 5214 | // the given value. |
| 5215 | static Flags RandomSeed(Int32 random_seed) { |
| 5216 | Flags flags; |
| 5217 | flags.random_seed = random_seed; |
| 5218 | return flags; |
| 5219 | } |
| 5220 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5221 | // Creates a Flags struct where the gtest_repeat flag has the given |
| 5222 | // value. |
| 5223 | static Flags Repeat(Int32 repeat) { |
| 5224 | Flags flags; |
| 5225 | flags.repeat = repeat; |
| 5226 | return flags; |
| 5227 | } |
| 5228 | |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5229 | // Creates a Flags struct where the gtest_shuffle flag has |
| 5230 | // the given value. |
| 5231 | static Flags Shuffle(bool shuffle) { |
| 5232 | Flags flags; |
| 5233 | flags.shuffle = shuffle; |
| 5234 | return flags; |
| 5235 | } |
| 5236 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5237 | // Creates a Flags struct where the GTEST_FLAG(stack_trace_depth) flag has |
| 5238 | // the given value. |
| 5239 | static Flags StackTraceDepth(Int32 stack_trace_depth) { |
| 5240 | Flags flags; |
| 5241 | flags.stack_trace_depth = stack_trace_depth; |
| 5242 | return flags; |
| 5243 | } |
| 5244 | |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5245 | // Creates a Flags struct where the gtest_throw_on_failure flag has |
| 5246 | // the given value. |
| 5247 | static Flags ThrowOnFailure(bool throw_on_failure) { |
| 5248 | Flags flags; |
| 5249 | flags.throw_on_failure = throw_on_failure; |
| 5250 | return flags; |
| 5251 | } |
| 5252 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5253 | // These fields store the flag values. |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5254 | bool also_run_disabled_tests; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5255 | bool break_on_failure; |
| 5256 | bool catch_exceptions; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5257 | bool death_test_use_fork; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5258 | const char* filter; |
| 5259 | bool list_tests; |
| 5260 | const char* output; |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5261 | bool print_time; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5262 | Int32 random_seed; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5263 | Int32 repeat; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5264 | bool shuffle; |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5265 | Int32 stack_trace_depth; |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5266 | bool throw_on_failure; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5267 | }; |
| 5268 | |
| 5269 | // Fixture for testing InitGoogleTest(). |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 5270 | class InitGoogleTestTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5271 | protected: |
| 5272 | // Clears the flags before each test. |
| 5273 | virtual void SetUp() { |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5274 | GTEST_FLAG(also_run_disabled_tests) = false; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5275 | GTEST_FLAG(break_on_failure) = false; |
| 5276 | GTEST_FLAG(catch_exceptions) = false; |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5277 | GTEST_FLAG(death_test_use_fork) = false; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5278 | GTEST_FLAG(filter) = ""; |
| 5279 | GTEST_FLAG(list_tests) = false; |
| 5280 | GTEST_FLAG(output) = ""; |
zhanyong.wan | 73ad5a3 | 2009-04-14 23:19:22 +0000 | [diff] [blame] | 5281 | GTEST_FLAG(print_time) = true; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5282 | GTEST_FLAG(random_seed) = 0; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5283 | GTEST_FLAG(repeat) = 1; |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5284 | GTEST_FLAG(shuffle) = false; |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5285 | GTEST_FLAG(stack_trace_depth) = kMaxStackTraceDepth; |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5286 | GTEST_FLAG(throw_on_failure) = false; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5287 | } |
| 5288 | |
| 5289 | // Asserts that two narrow or wide string arrays are equal. |
| 5290 | template <typename CharType> |
| 5291 | static void AssertStringArrayEq(size_t size1, CharType** array1, |
| 5292 | size_t size2, CharType** array2) { |
| 5293 | ASSERT_EQ(size1, size2) << " Array sizes different."; |
| 5294 | |
| 5295 | for (size_t i = 0; i != size1; i++) { |
| 5296 | ASSERT_STREQ(array1[i], array2[i]) << " where i == " << i; |
| 5297 | } |
| 5298 | } |
| 5299 | |
| 5300 | // Verifies that the flag values match the expected values. |
| 5301 | static void CheckFlags(const Flags& expected) { |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5302 | EXPECT_EQ(expected.also_run_disabled_tests, |
| 5303 | GTEST_FLAG(also_run_disabled_tests)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5304 | EXPECT_EQ(expected.break_on_failure, GTEST_FLAG(break_on_failure)); |
| 5305 | EXPECT_EQ(expected.catch_exceptions, GTEST_FLAG(catch_exceptions)); |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5306 | EXPECT_EQ(expected.death_test_use_fork, GTEST_FLAG(death_test_use_fork)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5307 | EXPECT_STREQ(expected.filter, GTEST_FLAG(filter).c_str()); |
| 5308 | EXPECT_EQ(expected.list_tests, GTEST_FLAG(list_tests)); |
| 5309 | EXPECT_STREQ(expected.output, GTEST_FLAG(output).c_str()); |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5310 | EXPECT_EQ(expected.print_time, GTEST_FLAG(print_time)); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5311 | EXPECT_EQ(expected.random_seed, GTEST_FLAG(random_seed)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5312 | EXPECT_EQ(expected.repeat, GTEST_FLAG(repeat)); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5313 | EXPECT_EQ(expected.shuffle, GTEST_FLAG(shuffle)); |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5314 | EXPECT_EQ(expected.throw_on_failure, GTEST_FLAG(throw_on_failure)); |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5315 | EXPECT_EQ(expected.stack_trace_depth, GTEST_FLAG(stack_trace_depth)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5316 | } |
| 5317 | |
| 5318 | // Parses a command line (specified by argc1 and argv1), then |
| 5319 | // verifies that the flag values are expected and that the |
| 5320 | // recognized flags are removed from the command line. |
| 5321 | template <typename CharType> |
| 5322 | static void TestParsingFlags(int argc1, const CharType** argv1, |
| 5323 | int argc2, const CharType** argv2, |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5324 | const Flags& expected, bool should_print_help) { |
| 5325 | const bool saved_help_flag = ::testing::internal::g_help_flag; |
| 5326 | ::testing::internal::g_help_flag = false; |
| 5327 | |
zhanyong.wan | cf8a584 | 2010-01-28 21:50:29 +0000 | [diff] [blame] | 5328 | #if GTEST_HAS_STREAM_REDIRECTION_ |
| 5329 | CaptureStdout(); |
| 5330 | #endif // GTEST_HAS_STREAM_REDIRECTION_ |
| 5331 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5332 | // Parses the command line. |
vladlosev | f179f4e | 2008-11-26 20:48:45 +0000 | [diff] [blame] | 5333 | internal::ParseGoogleTestFlagsOnly(&argc1, const_cast<CharType**>(argv1)); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5334 | |
zhanyong.wan | cf8a584 | 2010-01-28 21:50:29 +0000 | [diff] [blame] | 5335 | #if GTEST_HAS_STREAM_REDIRECTION_ |
| 5336 | const String captured_stdout = GetCapturedStdout(); |
| 5337 | #endif // GTEST_HAS_STREAM_REDIRECTION_ |
| 5338 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5339 | // Verifies the flag values. |
| 5340 | CheckFlags(expected); |
| 5341 | |
| 5342 | // Verifies that the recognized flags are removed from the command |
| 5343 | // line. |
| 5344 | AssertStringArrayEq(argc1 + 1, argv1, argc2 + 1, argv2); |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5345 | |
| 5346 | // ParseGoogleTestFlagsOnly should neither set g_help_flag nor print the |
| 5347 | // help message for the flags it recognizes. |
| 5348 | EXPECT_EQ(should_print_help, ::testing::internal::g_help_flag); |
| 5349 | |
zhanyong.wan | cf8a584 | 2010-01-28 21:50:29 +0000 | [diff] [blame] | 5350 | #if GTEST_HAS_STREAM_REDIRECTION_ |
| 5351 | const char* const expected_help_fragment = |
| 5352 | "This program contains tests written using"; |
| 5353 | if (should_print_help) { |
| 5354 | EXPECT_PRED_FORMAT2(IsSubstring, expected_help_fragment, captured_stdout); |
| 5355 | } else { |
| 5356 | EXPECT_PRED_FORMAT2(IsNotSubstring, |
| 5357 | expected_help_fragment, captured_stdout); |
| 5358 | } |
| 5359 | #endif // GTEST_HAS_STREAM_REDIRECTION_ |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5360 | |
| 5361 | ::testing::internal::g_help_flag = saved_help_flag; |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5362 | } |
| 5363 | |
| 5364 | // This macro wraps TestParsingFlags s.t. the user doesn't need |
| 5365 | // to specify the array sizes. |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5366 | #define GTEST_TEST_PARSING_FLAGS_(argv1, argv2, expected, should_print_help) \ |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5367 | TestParsingFlags(sizeof(argv1)/sizeof(*argv1) - 1, argv1, \ |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5368 | sizeof(argv2)/sizeof(*argv2) - 1, argv2, \ |
| 5369 | expected, should_print_help) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5370 | }; |
| 5371 | |
| 5372 | // Tests parsing an empty command line. |
| 5373 | TEST_F(InitGoogleTestTest, Empty) { |
| 5374 | const char* argv[] = { |
| 5375 | NULL |
| 5376 | }; |
| 5377 | |
| 5378 | const char* argv2[] = { |
| 5379 | NULL |
| 5380 | }; |
| 5381 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5382 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5383 | } |
| 5384 | |
| 5385 | // Tests parsing a command line that has no flag. |
| 5386 | TEST_F(InitGoogleTestTest, NoFlag) { |
| 5387 | const char* argv[] = { |
| 5388 | "foo.exe", |
| 5389 | NULL |
| 5390 | }; |
| 5391 | |
| 5392 | const char* argv2[] = { |
| 5393 | "foo.exe", |
| 5394 | NULL |
| 5395 | }; |
| 5396 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5397 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
| 5400 | // Tests parsing a bad --gtest_filter flag. |
| 5401 | TEST_F(InitGoogleTestTest, FilterBad) { |
| 5402 | const char* argv[] = { |
| 5403 | "foo.exe", |
| 5404 | "--gtest_filter", |
| 5405 | NULL |
| 5406 | }; |
| 5407 | |
| 5408 | const char* argv2[] = { |
| 5409 | "foo.exe", |
| 5410 | "--gtest_filter", |
| 5411 | NULL |
| 5412 | }; |
| 5413 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5414 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter(""), true); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5415 | } |
| 5416 | |
| 5417 | // Tests parsing an empty --gtest_filter flag. |
| 5418 | TEST_F(InitGoogleTestTest, FilterEmpty) { |
| 5419 | const char* argv[] = { |
| 5420 | "foo.exe", |
| 5421 | "--gtest_filter=", |
| 5422 | NULL |
| 5423 | }; |
| 5424 | |
| 5425 | const char* argv2[] = { |
| 5426 | "foo.exe", |
| 5427 | NULL |
| 5428 | }; |
| 5429 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5430 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter(""), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
| 5433 | // Tests parsing a non-empty --gtest_filter flag. |
| 5434 | TEST_F(InitGoogleTestTest, FilterNonEmpty) { |
| 5435 | const char* argv[] = { |
| 5436 | "foo.exe", |
| 5437 | "--gtest_filter=abc", |
| 5438 | NULL |
| 5439 | }; |
| 5440 | |
| 5441 | const char* argv2[] = { |
| 5442 | "foo.exe", |
| 5443 | NULL |
| 5444 | }; |
| 5445 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5446 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter("abc"), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5447 | } |
| 5448 | |
| 5449 | // Tests parsing --gtest_break_on_failure. |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5450 | TEST_F(InitGoogleTestTest, BreakOnFailureWithoutValue) { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5451 | const char* argv[] = { |
| 5452 | "foo.exe", |
| 5453 | "--gtest_break_on_failure", |
| 5454 | NULL |
| 5455 | }; |
| 5456 | |
| 5457 | const char* argv2[] = { |
| 5458 | "foo.exe", |
| 5459 | NULL |
| 5460 | }; |
| 5461 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5462 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(true), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5463 | } |
| 5464 | |
| 5465 | // Tests parsing --gtest_break_on_failure=0. |
| 5466 | TEST_F(InitGoogleTestTest, BreakOnFailureFalse_0) { |
| 5467 | const char* argv[] = { |
| 5468 | "foo.exe", |
| 5469 | "--gtest_break_on_failure=0", |
| 5470 | NULL |
| 5471 | }; |
| 5472 | |
| 5473 | const char* argv2[] = { |
| 5474 | "foo.exe", |
| 5475 | NULL |
| 5476 | }; |
| 5477 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5478 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(false), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5479 | } |
| 5480 | |
| 5481 | // Tests parsing --gtest_break_on_failure=f. |
| 5482 | TEST_F(InitGoogleTestTest, BreakOnFailureFalse_f) { |
| 5483 | const char* argv[] = { |
| 5484 | "foo.exe", |
| 5485 | "--gtest_break_on_failure=f", |
| 5486 | NULL |
| 5487 | }; |
| 5488 | |
| 5489 | const char* argv2[] = { |
| 5490 | "foo.exe", |
| 5491 | NULL |
| 5492 | }; |
| 5493 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5494 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(false), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5495 | } |
| 5496 | |
| 5497 | // Tests parsing --gtest_break_on_failure=F. |
| 5498 | TEST_F(InitGoogleTestTest, BreakOnFailureFalse_F) { |
| 5499 | const char* argv[] = { |
| 5500 | "foo.exe", |
| 5501 | "--gtest_break_on_failure=F", |
| 5502 | NULL |
| 5503 | }; |
| 5504 | |
| 5505 | const char* argv2[] = { |
| 5506 | "foo.exe", |
| 5507 | NULL |
| 5508 | }; |
| 5509 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5510 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(false), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | // Tests parsing a --gtest_break_on_failure flag that has a "true" |
| 5514 | // definition. |
| 5515 | TEST_F(InitGoogleTestTest, BreakOnFailureTrue) { |
| 5516 | const char* argv[] = { |
| 5517 | "foo.exe", |
| 5518 | "--gtest_break_on_failure=1", |
| 5519 | NULL |
| 5520 | }; |
| 5521 | |
| 5522 | const char* argv2[] = { |
| 5523 | "foo.exe", |
| 5524 | NULL |
| 5525 | }; |
| 5526 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5527 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::BreakOnFailure(true), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5528 | } |
| 5529 | |
| 5530 | // Tests parsing --gtest_catch_exceptions. |
| 5531 | TEST_F(InitGoogleTestTest, CatchExceptions) { |
| 5532 | const char* argv[] = { |
| 5533 | "foo.exe", |
| 5534 | "--gtest_catch_exceptions", |
| 5535 | NULL |
| 5536 | }; |
| 5537 | |
| 5538 | const char* argv2[] = { |
| 5539 | "foo.exe", |
| 5540 | NULL |
| 5541 | }; |
| 5542 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5543 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::CatchExceptions(true), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5544 | } |
| 5545 | |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5546 | // Tests parsing --gtest_death_test_use_fork. |
| 5547 | TEST_F(InitGoogleTestTest, DeathTestUseFork) { |
| 5548 | const char* argv[] = { |
| 5549 | "foo.exe", |
| 5550 | "--gtest_death_test_use_fork", |
| 5551 | NULL |
| 5552 | }; |
| 5553 | |
| 5554 | const char* argv2[] = { |
| 5555 | "foo.exe", |
| 5556 | NULL |
| 5557 | }; |
| 5558 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5559 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::DeathTestUseFork(true), false); |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 5560 | } |
| 5561 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5562 | // Tests having the same flag twice with different values. The |
| 5563 | // expected behavior is that the one coming last takes precedence. |
| 5564 | TEST_F(InitGoogleTestTest, DuplicatedFlags) { |
| 5565 | const char* argv[] = { |
| 5566 | "foo.exe", |
| 5567 | "--gtest_filter=a", |
| 5568 | "--gtest_filter=b", |
| 5569 | NULL |
| 5570 | }; |
| 5571 | |
| 5572 | const char* argv2[] = { |
| 5573 | "foo.exe", |
| 5574 | NULL |
| 5575 | }; |
| 5576 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5577 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Filter("b"), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | // Tests having an unrecognized flag on the command line. |
| 5581 | TEST_F(InitGoogleTestTest, UnrecognizedFlag) { |
| 5582 | const char* argv[] = { |
| 5583 | "foo.exe", |
| 5584 | "--gtest_break_on_failure", |
| 5585 | "bar", // Unrecognized by Google Test. |
| 5586 | "--gtest_filter=b", |
| 5587 | NULL |
| 5588 | }; |
| 5589 | |
| 5590 | const char* argv2[] = { |
| 5591 | "foo.exe", |
| 5592 | "bar", |
| 5593 | NULL |
| 5594 | }; |
| 5595 | |
| 5596 | Flags flags; |
| 5597 | flags.break_on_failure = true; |
| 5598 | flags.filter = "b"; |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5599 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, flags, false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5600 | } |
| 5601 | |
| 5602 | // Tests having a --gtest_list_tests flag |
| 5603 | TEST_F(InitGoogleTestTest, ListTestsFlag) { |
| 5604 | const char* argv[] = { |
| 5605 | "foo.exe", |
| 5606 | "--gtest_list_tests", |
| 5607 | NULL |
| 5608 | }; |
| 5609 | |
| 5610 | const char* argv2[] = { |
| 5611 | "foo.exe", |
| 5612 | NULL |
| 5613 | }; |
| 5614 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5615 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(true), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5616 | } |
| 5617 | |
| 5618 | // Tests having a --gtest_list_tests flag with a "true" value |
| 5619 | TEST_F(InitGoogleTestTest, ListTestsTrue) { |
| 5620 | const char* argv[] = { |
| 5621 | "foo.exe", |
| 5622 | "--gtest_list_tests=1", |
| 5623 | NULL |
| 5624 | }; |
| 5625 | |
| 5626 | const char* argv2[] = { |
| 5627 | "foo.exe", |
| 5628 | NULL |
| 5629 | }; |
| 5630 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5631 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(true), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5632 | } |
| 5633 | |
| 5634 | // Tests having a --gtest_list_tests flag with a "false" value |
| 5635 | TEST_F(InitGoogleTestTest, ListTestsFalse) { |
| 5636 | const char* argv[] = { |
| 5637 | "foo.exe", |
| 5638 | "--gtest_list_tests=0", |
| 5639 | NULL |
| 5640 | }; |
| 5641 | |
| 5642 | const char* argv2[] = { |
| 5643 | "foo.exe", |
| 5644 | NULL |
| 5645 | }; |
| 5646 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5647 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(false), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5648 | } |
| 5649 | |
| 5650 | // Tests parsing --gtest_list_tests=f. |
| 5651 | TEST_F(InitGoogleTestTest, ListTestsFalse_f) { |
| 5652 | const char* argv[] = { |
| 5653 | "foo.exe", |
| 5654 | "--gtest_list_tests=f", |
| 5655 | NULL |
| 5656 | }; |
| 5657 | |
| 5658 | const char* argv2[] = { |
| 5659 | "foo.exe", |
| 5660 | NULL |
| 5661 | }; |
| 5662 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5663 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(false), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5664 | } |
| 5665 | |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5666 | // Tests parsing --gtest_list_tests=F. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5667 | TEST_F(InitGoogleTestTest, ListTestsFalse_F) { |
| 5668 | const char* argv[] = { |
| 5669 | "foo.exe", |
| 5670 | "--gtest_list_tests=F", |
| 5671 | NULL |
| 5672 | }; |
| 5673 | |
| 5674 | const char* argv2[] = { |
| 5675 | "foo.exe", |
| 5676 | NULL |
| 5677 | }; |
| 5678 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5679 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ListTests(false), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5680 | } |
| 5681 | |
| 5682 | // Tests parsing --gtest_output (invalid). |
| 5683 | TEST_F(InitGoogleTestTest, OutputEmpty) { |
| 5684 | const char* argv[] = { |
| 5685 | "foo.exe", |
| 5686 | "--gtest_output", |
| 5687 | NULL |
| 5688 | }; |
| 5689 | |
| 5690 | const char* argv2[] = { |
| 5691 | "foo.exe", |
| 5692 | "--gtest_output", |
| 5693 | NULL |
| 5694 | }; |
| 5695 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5696 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags(), true); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5697 | } |
| 5698 | |
| 5699 | // Tests parsing --gtest_output=xml |
| 5700 | TEST_F(InitGoogleTestTest, OutputXml) { |
| 5701 | const char* argv[] = { |
| 5702 | "foo.exe", |
| 5703 | "--gtest_output=xml", |
| 5704 | NULL |
| 5705 | }; |
| 5706 | |
| 5707 | const char* argv2[] = { |
| 5708 | "foo.exe", |
| 5709 | NULL |
| 5710 | }; |
| 5711 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5712 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Output("xml"), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5713 | } |
| 5714 | |
| 5715 | // Tests parsing --gtest_output=xml:file |
| 5716 | TEST_F(InitGoogleTestTest, OutputXmlFile) { |
| 5717 | const char* argv[] = { |
| 5718 | "foo.exe", |
| 5719 | "--gtest_output=xml:file", |
| 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::Output("xml:file"), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5729 | } |
| 5730 | |
| 5731 | // Tests parsing --gtest_output=xml:directory/path/ |
| 5732 | TEST_F(InitGoogleTestTest, OutputXmlDirectory) { |
| 5733 | const char* argv[] = { |
| 5734 | "foo.exe", |
| 5735 | "--gtest_output=xml:directory/path/", |
| 5736 | NULL |
| 5737 | }; |
| 5738 | |
| 5739 | const char* argv2[] = { |
| 5740 | "foo.exe", |
| 5741 | NULL |
| 5742 | }; |
| 5743 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5744 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, |
| 5745 | Flags::Output("xml:directory/path/"), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5746 | } |
| 5747 | |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5748 | // Tests having a --gtest_print_time flag |
| 5749 | TEST_F(InitGoogleTestTest, PrintTimeFlag) { |
| 5750 | const char* argv[] = { |
| 5751 | "foo.exe", |
| 5752 | "--gtest_print_time", |
| 5753 | NULL |
| 5754 | }; |
| 5755 | |
| 5756 | const char* argv2[] = { |
| 5757 | "foo.exe", |
| 5758 | NULL |
| 5759 | }; |
| 5760 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5761 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(true), false); |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | // Tests having a --gtest_print_time flag with a "true" value |
| 5765 | TEST_F(InitGoogleTestTest, PrintTimeTrue) { |
| 5766 | const char* argv[] = { |
| 5767 | "foo.exe", |
| 5768 | "--gtest_print_time=1", |
| 5769 | NULL |
| 5770 | }; |
| 5771 | |
| 5772 | const char* argv2[] = { |
| 5773 | "foo.exe", |
| 5774 | NULL |
| 5775 | }; |
| 5776 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5777 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(true), false); |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5778 | } |
| 5779 | |
| 5780 | // Tests having a --gtest_print_time flag with a "false" value |
| 5781 | TEST_F(InitGoogleTestTest, PrintTimeFalse) { |
| 5782 | const char* argv[] = { |
| 5783 | "foo.exe", |
| 5784 | "--gtest_print_time=0", |
| 5785 | NULL |
| 5786 | }; |
| 5787 | |
| 5788 | const char* argv2[] = { |
| 5789 | "foo.exe", |
| 5790 | NULL |
| 5791 | }; |
| 5792 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5793 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(false), false); |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5794 | } |
| 5795 | |
| 5796 | // Tests parsing --gtest_print_time=f. |
| 5797 | TEST_F(InitGoogleTestTest, PrintTimeFalse_f) { |
| 5798 | const char* argv[] = { |
| 5799 | "foo.exe", |
| 5800 | "--gtest_print_time=f", |
| 5801 | NULL |
| 5802 | }; |
| 5803 | |
| 5804 | const char* argv2[] = { |
| 5805 | "foo.exe", |
| 5806 | NULL |
| 5807 | }; |
| 5808 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5809 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(false), false); |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5810 | } |
| 5811 | |
| 5812 | // Tests parsing --gtest_print_time=F. |
| 5813 | TEST_F(InitGoogleTestTest, PrintTimeFalse_F) { |
| 5814 | const char* argv[] = { |
| 5815 | "foo.exe", |
| 5816 | "--gtest_print_time=F", |
| 5817 | NULL |
| 5818 | }; |
| 5819 | |
| 5820 | const char* argv2[] = { |
| 5821 | "foo.exe", |
| 5822 | NULL |
| 5823 | }; |
| 5824 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5825 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::PrintTime(false), false); |
shiqian | d981cee | 2008-07-25 04:06:16 +0000 | [diff] [blame] | 5826 | } |
| 5827 | |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5828 | // Tests parsing --gtest_random_seed=number |
| 5829 | TEST_F(InitGoogleTestTest, RandomSeed) { |
| 5830 | const char* argv[] = { |
| 5831 | "foo.exe", |
| 5832 | "--gtest_random_seed=1000", |
| 5833 | NULL |
| 5834 | }; |
| 5835 | |
| 5836 | const char* argv2[] = { |
| 5837 | "foo.exe", |
| 5838 | NULL |
| 5839 | }; |
| 5840 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5841 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::RandomSeed(1000), false); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5842 | } |
| 5843 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5844 | // Tests parsing --gtest_repeat=number |
| 5845 | TEST_F(InitGoogleTestTest, Repeat) { |
| 5846 | const char* argv[] = { |
| 5847 | "foo.exe", |
| 5848 | "--gtest_repeat=1000", |
| 5849 | NULL |
| 5850 | }; |
| 5851 | |
| 5852 | const char* argv2[] = { |
| 5853 | "foo.exe", |
| 5854 | NULL |
| 5855 | }; |
| 5856 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5857 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Repeat(1000), false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 5858 | } |
| 5859 | |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5860 | // Tests having a --gtest_also_run_disabled_tests flag |
| 5861 | TEST_F(InitGoogleTestTest, AlsoRunDisabledTestsFlag) { |
| 5862 | const char* argv[] = { |
| 5863 | "foo.exe", |
| 5864 | "--gtest_also_run_disabled_tests", |
| 5865 | NULL |
| 5866 | }; |
| 5867 | |
| 5868 | const char* argv2[] = { |
| 5869 | "foo.exe", |
| 5870 | NULL |
| 5871 | }; |
| 5872 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5873 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, |
| 5874 | Flags::AlsoRunDisabledTests(true), false); |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5875 | } |
| 5876 | |
| 5877 | // Tests having a --gtest_also_run_disabled_tests flag with a "true" value |
| 5878 | TEST_F(InitGoogleTestTest, AlsoRunDisabledTestsTrue) { |
| 5879 | const char* argv[] = { |
| 5880 | "foo.exe", |
| 5881 | "--gtest_also_run_disabled_tests=1", |
| 5882 | NULL |
| 5883 | }; |
| 5884 | |
| 5885 | const char* argv2[] = { |
| 5886 | "foo.exe", |
| 5887 | NULL |
| 5888 | }; |
| 5889 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5890 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, |
| 5891 | Flags::AlsoRunDisabledTests(true), false); |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5892 | } |
| 5893 | |
| 5894 | // Tests having a --gtest_also_run_disabled_tests flag with a "false" value |
| 5895 | TEST_F(InitGoogleTestTest, AlsoRunDisabledTestsFalse) { |
| 5896 | const char* argv[] = { |
| 5897 | "foo.exe", |
| 5898 | "--gtest_also_run_disabled_tests=0", |
| 5899 | NULL |
| 5900 | }; |
| 5901 | |
| 5902 | const char* argv2[] = { |
| 5903 | "foo.exe", |
| 5904 | NULL |
| 5905 | }; |
| 5906 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5907 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, |
| 5908 | Flags::AlsoRunDisabledTests(false), false); |
shiqian | ca6949f | 2009-01-10 01:16:33 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5911 | // Tests parsing --gtest_shuffle. |
| 5912 | TEST_F(InitGoogleTestTest, ShuffleWithoutValue) { |
| 5913 | const char* argv[] = { |
| 5914 | "foo.exe", |
| 5915 | "--gtest_shuffle", |
| 5916 | NULL |
| 5917 | }; |
| 5918 | |
| 5919 | const char* argv2[] = { |
| 5920 | "foo.exe", |
| 5921 | NULL |
| 5922 | }; |
| 5923 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5924 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Shuffle(true), false); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5925 | } |
| 5926 | |
| 5927 | // Tests parsing --gtest_shuffle=0. |
| 5928 | TEST_F(InitGoogleTestTest, ShuffleFalse_0) { |
| 5929 | const char* argv[] = { |
| 5930 | "foo.exe", |
| 5931 | "--gtest_shuffle=0", |
| 5932 | NULL |
| 5933 | }; |
| 5934 | |
| 5935 | const char* argv2[] = { |
| 5936 | "foo.exe", |
| 5937 | NULL |
| 5938 | }; |
| 5939 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5940 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Shuffle(false), false); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5941 | } |
| 5942 | |
| 5943 | // Tests parsing a --gtest_shuffle flag that has a "true" |
| 5944 | // definition. |
| 5945 | TEST_F(InitGoogleTestTest, ShuffleTrue) { |
| 5946 | const char* argv[] = { |
| 5947 | "foo.exe", |
| 5948 | "--gtest_shuffle=1", |
| 5949 | NULL |
| 5950 | }; |
| 5951 | |
| 5952 | const char* argv2[] = { |
| 5953 | "foo.exe", |
| 5954 | NULL |
| 5955 | }; |
| 5956 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5957 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::Shuffle(true), false); |
| 5958 | } |
| 5959 | |
| 5960 | // Tests parsing --gtest_stack_trace_depth=number. |
| 5961 | TEST_F(InitGoogleTestTest, StackTraceDepth) { |
| 5962 | const char* argv[] = { |
| 5963 | "foo.exe", |
| 5964 | "--gtest_stack_trace_depth=5", |
| 5965 | NULL |
| 5966 | }; |
| 5967 | |
| 5968 | const char* argv2[] = { |
| 5969 | "foo.exe", |
| 5970 | NULL |
| 5971 | }; |
| 5972 | |
| 5973 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::StackTraceDepth(5), false); |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5974 | } |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5975 | |
| 5976 | // Tests parsing --gtest_throw_on_failure. |
zhanyong.wan | 9b9794f | 2009-07-14 22:56:46 +0000 | [diff] [blame] | 5977 | TEST_F(InitGoogleTestTest, ThrowOnFailureWithoutValue) { |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5978 | const char* argv[] = { |
| 5979 | "foo.exe", |
| 5980 | "--gtest_throw_on_failure", |
| 5981 | NULL |
| 5982 | }; |
| 5983 | |
| 5984 | const char* argv2[] = { |
| 5985 | "foo.exe", |
| 5986 | NULL |
| 5987 | }; |
| 5988 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 5989 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ThrowOnFailure(true), false); |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 5990 | } |
| 5991 | |
| 5992 | // Tests parsing --gtest_throw_on_failure=0. |
| 5993 | TEST_F(InitGoogleTestTest, ThrowOnFailureFalse_0) { |
| 5994 | const char* argv[] = { |
| 5995 | "foo.exe", |
| 5996 | "--gtest_throw_on_failure=0", |
| 5997 | NULL |
| 5998 | }; |
| 5999 | |
| 6000 | const char* argv2[] = { |
| 6001 | "foo.exe", |
| 6002 | NULL |
| 6003 | }; |
| 6004 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6005 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ThrowOnFailure(false), false); |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 6006 | } |
| 6007 | |
| 6008 | // Tests parsing a --gtest_throw_on_failure flag that has a "true" |
| 6009 | // definition. |
| 6010 | TEST_F(InitGoogleTestTest, ThrowOnFailureTrue) { |
| 6011 | const char* argv[] = { |
| 6012 | "foo.exe", |
| 6013 | "--gtest_throw_on_failure=1", |
| 6014 | NULL |
| 6015 | }; |
| 6016 | |
| 6017 | const char* argv2[] = { |
| 6018 | "foo.exe", |
| 6019 | NULL |
| 6020 | }; |
| 6021 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6022 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, Flags::ThrowOnFailure(true), false); |
zhanyong.wan | b0fe69f | 2009-03-06 20:05:23 +0000 | [diff] [blame] | 6023 | } |
| 6024 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 6025 | #if GTEST_OS_WINDOWS |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6026 | // Tests parsing wide strings. |
| 6027 | TEST_F(InitGoogleTestTest, WideStrings) { |
| 6028 | const wchar_t* argv[] = { |
| 6029 | L"foo.exe", |
| 6030 | L"--gtest_filter=Foo*", |
| 6031 | L"--gtest_list_tests=1", |
| 6032 | L"--gtest_break_on_failure", |
| 6033 | L"--non_gtest_flag", |
| 6034 | NULL |
| 6035 | }; |
| 6036 | |
| 6037 | const wchar_t* argv2[] = { |
| 6038 | L"foo.exe", |
| 6039 | L"--non_gtest_flag", |
| 6040 | NULL |
| 6041 | }; |
| 6042 | |
| 6043 | Flags expected_flags; |
| 6044 | expected_flags.break_on_failure = true; |
| 6045 | expected_flags.filter = "Foo*"; |
| 6046 | expected_flags.list_tests = true; |
| 6047 | |
vladlosev | ba015a9 | 2009-11-17 22:43:15 +0000 | [diff] [blame] | 6048 | GTEST_TEST_PARSING_FLAGS_(argv, argv2, expected_flags, false); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6049 | } |
| 6050 | #endif // GTEST_OS_WINDOWS |
| 6051 | |
| 6052 | // Tests current_test_info() in UnitTest. |
| 6053 | class CurrentTestInfoTest : public Test { |
| 6054 | protected: |
| 6055 | // Tests that current_test_info() returns NULL before the first test in |
| 6056 | // the test case is run. |
| 6057 | static void SetUpTestCase() { |
| 6058 | // There should be no tests running at this point. |
| 6059 | const TestInfo* test_info = |
| 6060 | UnitTest::GetInstance()->current_test_info(); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 6061 | EXPECT_TRUE(test_info == NULL) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6062 | << "There should be no tests running at this point."; |
| 6063 | } |
| 6064 | |
| 6065 | // Tests that current_test_info() returns NULL after the last test in |
| 6066 | // the test case has run. |
| 6067 | static void TearDownTestCase() { |
| 6068 | const TestInfo* test_info = |
| 6069 | UnitTest::GetInstance()->current_test_info(); |
zhanyong.wan | 9644db8 | 2009-06-24 23:02:50 +0000 | [diff] [blame] | 6070 | EXPECT_TRUE(test_info == NULL) |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6071 | << "There should be no tests running at this point."; |
| 6072 | } |
| 6073 | }; |
| 6074 | |
| 6075 | // Tests that current_test_info() returns TestInfo for currently running |
| 6076 | // test by checking the expected test name against the actual one. |
| 6077 | TEST_F(CurrentTestInfoTest, WorksForFirstTestInATestCase) { |
| 6078 | const TestInfo* test_info = |
| 6079 | UnitTest::GetInstance()->current_test_info(); |
| 6080 | ASSERT_TRUE(NULL != test_info) |
| 6081 | << "There is a test running so we should have a valid TestInfo."; |
| 6082 | EXPECT_STREQ("CurrentTestInfoTest", test_info->test_case_name()) |
| 6083 | << "Expected the name of the currently running test case."; |
| 6084 | EXPECT_STREQ("WorksForFirstTestInATestCase", test_info->name()) |
| 6085 | << "Expected the name of the currently running test."; |
| 6086 | } |
| 6087 | |
| 6088 | // Tests that current_test_info() returns TestInfo for currently running |
| 6089 | // test by checking the expected test name against the actual one. We |
| 6090 | // use this test to see that the TestInfo object actually changed from |
| 6091 | // the previous invocation. |
| 6092 | TEST_F(CurrentTestInfoTest, WorksForSecondTestInATestCase) { |
| 6093 | const TestInfo* test_info = |
| 6094 | UnitTest::GetInstance()->current_test_info(); |
| 6095 | ASSERT_TRUE(NULL != test_info) |
| 6096 | << "There is a test running so we should have a valid TestInfo."; |
| 6097 | EXPECT_STREQ("CurrentTestInfoTest", test_info->test_case_name()) |
| 6098 | << "Expected the name of the currently running test case."; |
| 6099 | EXPECT_STREQ("WorksForSecondTestInATestCase", test_info->name()) |
| 6100 | << "Expected the name of the currently running test."; |
| 6101 | } |
| 6102 | |
| 6103 | } // namespace testing |
| 6104 | |
| 6105 | // These two lines test that we can define tests in a namespace that |
| 6106 | // has the name "testing" and is nested in another namespace. |
| 6107 | namespace my_namespace { |
| 6108 | namespace testing { |
| 6109 | |
| 6110 | // Makes sure that TEST knows to use ::testing::Test instead of |
| 6111 | // ::my_namespace::testing::Test. |
| 6112 | class Test {}; |
| 6113 | |
| 6114 | // Makes sure that an assertion knows to use ::testing::Message instead of |
| 6115 | // ::my_namespace::testing::Message. |
| 6116 | class Message {}; |
| 6117 | |
| 6118 | // Makes sure that an assertion knows to use |
| 6119 | // ::testing::AssertionResult instead of |
| 6120 | // ::my_namespace::testing::AssertionResult. |
| 6121 | class AssertionResult {}; |
| 6122 | |
| 6123 | // Tests that an assertion that should succeed works as expected. |
| 6124 | TEST(NestedTestingNamespaceTest, Success) { |
| 6125 | EXPECT_EQ(1, 1) << "This shouldn't fail."; |
| 6126 | } |
| 6127 | |
| 6128 | // Tests that an assertion that should fail works as expected. |
| 6129 | TEST(NestedTestingNamespaceTest, Failure) { |
| 6130 | EXPECT_FATAL_FAILURE(FAIL() << "This failure is expected.", |
| 6131 | "This failure is expected."); |
| 6132 | } |
| 6133 | |
| 6134 | } // namespace testing |
| 6135 | } // namespace my_namespace |
| 6136 | |
| 6137 | // Tests that one can call superclass SetUp and TearDown methods-- |
| 6138 | // that is, that they are not private. |
| 6139 | // No tests are based on this fixture; the test "passes" if it compiles |
| 6140 | // successfully. |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 6141 | class ProtectedFixtureMethodsTest : public Test { |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6142 | protected: |
| 6143 | virtual void SetUp() { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 6144 | Test::SetUp(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6145 | } |
| 6146 | virtual void TearDown() { |
shiqian | 760af5c | 2008-08-06 21:43:15 +0000 | [diff] [blame] | 6147 | Test::TearDown(); |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6148 | } |
| 6149 | }; |
| 6150 | |
| 6151 | // StreamingAssertionsTest tests the streaming versions of a representative |
| 6152 | // sample of assertions. |
| 6153 | TEST(StreamingAssertionsTest, Unconditional) { |
| 6154 | SUCCEED() << "expected success"; |
| 6155 | EXPECT_NONFATAL_FAILURE(ADD_FAILURE() << "expected failure", |
| 6156 | "expected failure"); |
| 6157 | EXPECT_FATAL_FAILURE(FAIL() << "expected failure", |
| 6158 | "expected failure"); |
| 6159 | } |
| 6160 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 6161 | #ifdef __BORLANDC__ |
| 6162 | // Silences warnings: "Condition is always true", "Unreachable code" |
| 6163 | #pragma option push -w-ccc -w-rch |
| 6164 | #endif |
| 6165 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6166 | TEST(StreamingAssertionsTest, Truth) { |
| 6167 | EXPECT_TRUE(true) << "unexpected failure"; |
| 6168 | ASSERT_TRUE(true) << "unexpected failure"; |
| 6169 | EXPECT_NONFATAL_FAILURE(EXPECT_TRUE(false) << "expected failure", |
| 6170 | "expected failure"); |
| 6171 | EXPECT_FATAL_FAILURE(ASSERT_TRUE(false) << "expected failure", |
| 6172 | "expected failure"); |
| 6173 | } |
| 6174 | |
| 6175 | TEST(StreamingAssertionsTest, Truth2) { |
| 6176 | EXPECT_FALSE(false) << "unexpected failure"; |
| 6177 | ASSERT_FALSE(false) << "unexpected failure"; |
| 6178 | EXPECT_NONFATAL_FAILURE(EXPECT_FALSE(true) << "expected failure", |
| 6179 | "expected failure"); |
| 6180 | EXPECT_FATAL_FAILURE(ASSERT_FALSE(true) << "expected failure", |
| 6181 | "expected failure"); |
| 6182 | } |
| 6183 | |
zhanyong.wan | 98efcc4 | 2009-04-28 00:28:09 +0000 | [diff] [blame] | 6184 | #ifdef __BORLANDC__ |
| 6185 | // Restores warnings after previous "#pragma option push" supressed them |
| 6186 | #pragma option pop |
| 6187 | #endif |
| 6188 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6189 | TEST(StreamingAssertionsTest, IntegerEquals) { |
| 6190 | EXPECT_EQ(1, 1) << "unexpected failure"; |
| 6191 | ASSERT_EQ(1, 1) << "unexpected failure"; |
| 6192 | EXPECT_NONFATAL_FAILURE(EXPECT_EQ(1, 2) << "expected failure", |
| 6193 | "expected failure"); |
| 6194 | EXPECT_FATAL_FAILURE(ASSERT_EQ(1, 2) << "expected failure", |
| 6195 | "expected failure"); |
| 6196 | } |
| 6197 | |
| 6198 | TEST(StreamingAssertionsTest, IntegerLessThan) { |
| 6199 | EXPECT_LT(1, 2) << "unexpected failure"; |
| 6200 | ASSERT_LT(1, 2) << "unexpected failure"; |
| 6201 | EXPECT_NONFATAL_FAILURE(EXPECT_LT(2, 1) << "expected failure", |
| 6202 | "expected failure"); |
| 6203 | EXPECT_FATAL_FAILURE(ASSERT_LT(2, 1) << "expected failure", |
| 6204 | "expected failure"); |
| 6205 | } |
| 6206 | |
| 6207 | TEST(StreamingAssertionsTest, StringsEqual) { |
| 6208 | EXPECT_STREQ("foo", "foo") << "unexpected failure"; |
| 6209 | ASSERT_STREQ("foo", "foo") << "unexpected failure"; |
| 6210 | EXPECT_NONFATAL_FAILURE(EXPECT_STREQ("foo", "bar") << "expected failure", |
| 6211 | "expected failure"); |
| 6212 | EXPECT_FATAL_FAILURE(ASSERT_STREQ("foo", "bar") << "expected failure", |
| 6213 | "expected failure"); |
| 6214 | } |
| 6215 | |
| 6216 | TEST(StreamingAssertionsTest, StringsNotEqual) { |
| 6217 | EXPECT_STRNE("foo", "bar") << "unexpected failure"; |
| 6218 | ASSERT_STRNE("foo", "bar") << "unexpected failure"; |
| 6219 | EXPECT_NONFATAL_FAILURE(EXPECT_STRNE("foo", "foo") << "expected failure", |
| 6220 | "expected failure"); |
| 6221 | EXPECT_FATAL_FAILURE(ASSERT_STRNE("foo", "foo") << "expected failure", |
| 6222 | "expected failure"); |
| 6223 | } |
| 6224 | |
| 6225 | TEST(StreamingAssertionsTest, StringsEqualIgnoringCase) { |
| 6226 | EXPECT_STRCASEEQ("foo", "FOO") << "unexpected failure"; |
| 6227 | ASSERT_STRCASEEQ("foo", "FOO") << "unexpected failure"; |
| 6228 | EXPECT_NONFATAL_FAILURE(EXPECT_STRCASEEQ("foo", "bar") << "expected failure", |
| 6229 | "expected failure"); |
| 6230 | EXPECT_FATAL_FAILURE(ASSERT_STRCASEEQ("foo", "bar") << "expected failure", |
| 6231 | "expected failure"); |
| 6232 | } |
| 6233 | |
| 6234 | TEST(StreamingAssertionsTest, StringNotEqualIgnoringCase) { |
| 6235 | EXPECT_STRCASENE("foo", "bar") << "unexpected failure"; |
| 6236 | ASSERT_STRCASENE("foo", "bar") << "unexpected failure"; |
| 6237 | EXPECT_NONFATAL_FAILURE(EXPECT_STRCASENE("foo", "FOO") << "expected failure", |
| 6238 | "expected failure"); |
| 6239 | EXPECT_FATAL_FAILURE(ASSERT_STRCASENE("bar", "BAR") << "expected failure", |
| 6240 | "expected failure"); |
| 6241 | } |
| 6242 | |
| 6243 | TEST(StreamingAssertionsTest, FloatingPointEquals) { |
| 6244 | EXPECT_FLOAT_EQ(1.0, 1.0) << "unexpected failure"; |
| 6245 | ASSERT_FLOAT_EQ(1.0, 1.0) << "unexpected failure"; |
| 6246 | EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(0.0, 1.0) << "expected failure", |
| 6247 | "expected failure"); |
| 6248 | EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(0.0, 1.0) << "expected failure", |
| 6249 | "expected failure"); |
| 6250 | } |
| 6251 | |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 6252 | #if GTEST_HAS_EXCEPTIONS |
| 6253 | |
| 6254 | TEST(StreamingAssertionsTest, Throw) { |
| 6255 | EXPECT_THROW(ThrowAnInteger(), int) << "unexpected failure"; |
| 6256 | ASSERT_THROW(ThrowAnInteger(), int) << "unexpected failure"; |
| 6257 | EXPECT_NONFATAL_FAILURE(EXPECT_THROW(ThrowAnInteger(), bool) << |
| 6258 | "expected failure", "expected failure"); |
| 6259 | EXPECT_FATAL_FAILURE(ASSERT_THROW(ThrowAnInteger(), bool) << |
| 6260 | "expected failure", "expected failure"); |
| 6261 | } |
| 6262 | |
| 6263 | TEST(StreamingAssertionsTest, NoThrow) { |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 6264 | EXPECT_NO_THROW(ThrowNothing()) << "unexpected failure"; |
| 6265 | ASSERT_NO_THROW(ThrowNothing()) << "unexpected failure"; |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 6266 | EXPECT_NONFATAL_FAILURE(EXPECT_NO_THROW(ThrowAnInteger()) << |
| 6267 | "expected failure", "expected failure"); |
| 6268 | EXPECT_FATAL_FAILURE(ASSERT_NO_THROW(ThrowAnInteger()) << |
| 6269 | "expected failure", "expected failure"); |
| 6270 | } |
| 6271 | |
| 6272 | TEST(StreamingAssertionsTest, AnyThrow) { |
| 6273 | EXPECT_ANY_THROW(ThrowAnInteger()) << "unexpected failure"; |
| 6274 | ASSERT_ANY_THROW(ThrowAnInteger()) << "unexpected failure"; |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 6275 | EXPECT_NONFATAL_FAILURE(EXPECT_ANY_THROW(ThrowNothing()) << |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 6276 | "expected failure", "expected failure"); |
zhanyong.wan | ac60cef | 2009-02-08 04:53:35 +0000 | [diff] [blame] | 6277 | EXPECT_FATAL_FAILURE(ASSERT_ANY_THROW(ThrowNothing()) << |
shiqian | 9204c8e | 2008-09-12 20:57:22 +0000 | [diff] [blame] | 6278 | "expected failure", "expected failure"); |
| 6279 | } |
| 6280 | |
| 6281 | #endif // GTEST_HAS_EXCEPTIONS |
| 6282 | |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6283 | // Tests that Google Test correctly decides whether to use colors in the output. |
| 6284 | |
| 6285 | TEST(ColoredOutputTest, UsesColorsWhenGTestColorFlagIsYes) { |
| 6286 | GTEST_FLAG(color) = "yes"; |
| 6287 | |
| 6288 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 6289 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6290 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6291 | |
| 6292 | SetEnv("TERM", "dumb"); // TERM doesn't support colors. |
| 6293 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6294 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6295 | } |
| 6296 | |
| 6297 | TEST(ColoredOutputTest, UsesColorsWhenGTestColorFlagIsAliasOfYes) { |
| 6298 | SetEnv("TERM", "dumb"); // TERM doesn't support colors. |
| 6299 | |
| 6300 | GTEST_FLAG(color) = "True"; |
| 6301 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6302 | |
| 6303 | GTEST_FLAG(color) = "t"; |
| 6304 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6305 | |
| 6306 | GTEST_FLAG(color) = "1"; |
| 6307 | EXPECT_TRUE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6308 | } |
| 6309 | |
| 6310 | TEST(ColoredOutputTest, UsesNoColorWhenGTestColorFlagIsNo) { |
| 6311 | GTEST_FLAG(color) = "no"; |
| 6312 | |
| 6313 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 6314 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6315 | EXPECT_FALSE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6316 | |
| 6317 | SetEnv("TERM", "dumb"); // TERM doesn't support colors. |
| 6318 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6319 | EXPECT_FALSE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6320 | } |
| 6321 | |
| 6322 | TEST(ColoredOutputTest, UsesNoColorWhenGTestColorFlagIsInvalid) { |
| 6323 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 6324 | |
| 6325 | GTEST_FLAG(color) = "F"; |
| 6326 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6327 | |
| 6328 | GTEST_FLAG(color) = "0"; |
| 6329 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6330 | |
| 6331 | GTEST_FLAG(color) = "unknown"; |
| 6332 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6333 | } |
| 6334 | |
| 6335 | TEST(ColoredOutputTest, UsesColorsWhenStdoutIsTty) { |
| 6336 | GTEST_FLAG(color) = "auto"; |
| 6337 | |
| 6338 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 6339 | EXPECT_FALSE(ShouldUseColor(false)); // Stdout is not a TTY. |
| 6340 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6341 | } |
| 6342 | |
| 6343 | TEST(ColoredOutputTest, UsesColorsWhenTermSupportsColors) { |
| 6344 | GTEST_FLAG(color) = "auto"; |
| 6345 | |
zhanyong.wan | 4cd6260 | 2009-02-23 23:21:55 +0000 | [diff] [blame] | 6346 | #if GTEST_OS_WINDOWS |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6347 | // On Windows, we ignore the TERM variable as it's usually not set. |
| 6348 | |
| 6349 | SetEnv("TERM", "dumb"); |
| 6350 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6351 | |
| 6352 | SetEnv("TERM", ""); |
| 6353 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6354 | |
| 6355 | SetEnv("TERM", "xterm"); |
| 6356 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6357 | #else |
| 6358 | // On non-Windows platforms, we rely on TERM to determine if the |
| 6359 | // terminal supports colors. |
| 6360 | |
| 6361 | SetEnv("TERM", "dumb"); // TERM doesn't support colors. |
| 6362 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6363 | |
| 6364 | SetEnv("TERM", "emacs"); // TERM doesn't support colors. |
| 6365 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6366 | |
| 6367 | SetEnv("TERM", "vt100"); // TERM doesn't support colors. |
| 6368 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6369 | |
| 6370 | SetEnv("TERM", "xterm-mono"); // TERM doesn't support colors. |
| 6371 | EXPECT_FALSE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6372 | |
| 6373 | SetEnv("TERM", "xterm"); // TERM supports colors. |
| 6374 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6375 | |
| 6376 | SetEnv("TERM", "xterm-color"); // TERM supports colors. |
| 6377 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 6378 | |
vladlosev | 9f25491 | 2010-04-22 11:44:59 +0000 | [diff] [blame] | 6379 | SetEnv("TERM", "xterm-256color"); // TERM supports colors. |
| 6380 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6381 | |
| 6382 | SetEnv("TERM", "screen"); // TERM supports colors. |
| 6383 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
| 6384 | |
zhanyong.wan | a8a582f | 2009-07-13 19:25:02 +0000 | [diff] [blame] | 6385 | SetEnv("TERM", "linux"); // TERM supports colors. |
| 6386 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
vladlosev | 9f25491 | 2010-04-22 11:44:59 +0000 | [diff] [blame] | 6387 | |
| 6388 | SetEnv("TERM", "cygwin"); // TERM supports colors. |
| 6389 | EXPECT_TRUE(ShouldUseColor(true)); // Stdout is a TTY. |
shiqian | 4b6829f | 2008-07-03 22:38:12 +0000 | [diff] [blame] | 6390 | #endif // GTEST_OS_WINDOWS |
| 6391 | } |
| 6392 | |
shiqian | 21d43d1 | 2009-01-08 01:10:31 +0000 | [diff] [blame] | 6393 | // Verifies that StaticAssertTypeEq works in a namespace scope. |
| 6394 | |
| 6395 | static bool dummy1 = StaticAssertTypeEq<bool, bool>(); |
| 6396 | static bool dummy2 = StaticAssertTypeEq<const int, const int>(); |
| 6397 | |
| 6398 | // Verifies that StaticAssertTypeEq works in a class. |
| 6399 | |
| 6400 | template <typename T> |
| 6401 | class StaticAssertTypeEqTestHelper { |
| 6402 | public: |
| 6403 | StaticAssertTypeEqTestHelper() { StaticAssertTypeEq<bool, T>(); } |
| 6404 | }; |
| 6405 | |
| 6406 | TEST(StaticAssertTypeEqTest, WorksInClass) { |
| 6407 | StaticAssertTypeEqTestHelper<bool>(); |
| 6408 | } |
| 6409 | |
| 6410 | // Verifies that StaticAssertTypeEq works inside a function. |
| 6411 | |
| 6412 | typedef int IntAlias; |
| 6413 | |
| 6414 | TEST(StaticAssertTypeEqTest, CompilesForEqualTypes) { |
| 6415 | StaticAssertTypeEq<int, IntAlias>(); |
| 6416 | StaticAssertTypeEq<int*, IntAlias*>(); |
| 6417 | } |
| 6418 | |
vladlosev | f904a61 | 2008-11-20 01:40:35 +0000 | [diff] [blame] | 6419 | TEST(GetCurrentOsStackTraceExceptTopTest, ReturnsTheStackTrace) { |
| 6420 | testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); |
| 6421 | |
| 6422 | // We don't have a stack walker in Google Test yet. |
| 6423 | EXPECT_STREQ("", GetCurrentOsStackTraceExceptTop(unit_test, 0).c_str()); |
| 6424 | EXPECT_STREQ("", GetCurrentOsStackTraceExceptTop(unit_test, 1).c_str()); |
| 6425 | } |
zhanyong.wan | 1b17110 | 2009-04-07 21:03:22 +0000 | [diff] [blame] | 6426 | |
| 6427 | TEST(HasNonfatalFailureTest, ReturnsFalseWhenThereIsNoFailure) { |
| 6428 | EXPECT_FALSE(HasNonfatalFailure()); |
| 6429 | } |
| 6430 | |
| 6431 | static void FailFatally() { FAIL(); } |
| 6432 | |
| 6433 | TEST(HasNonfatalFailureTest, ReturnsFalseWhenThereIsOnlyFatalFailure) { |
| 6434 | FailFatally(); |
| 6435 | const bool has_nonfatal_failure = HasNonfatalFailure(); |
| 6436 | ClearCurrentTestPartResults(); |
| 6437 | EXPECT_FALSE(has_nonfatal_failure); |
| 6438 | } |
| 6439 | |
| 6440 | TEST(HasNonfatalFailureTest, ReturnsTrueWhenThereIsNonfatalFailure) { |
| 6441 | ADD_FAILURE(); |
| 6442 | const bool has_nonfatal_failure = HasNonfatalFailure(); |
| 6443 | ClearCurrentTestPartResults(); |
| 6444 | EXPECT_TRUE(has_nonfatal_failure); |
| 6445 | } |
| 6446 | |
| 6447 | TEST(HasNonfatalFailureTest, ReturnsTrueWhenThereAreFatalAndNonfatalFailures) { |
| 6448 | FailFatally(); |
| 6449 | ADD_FAILURE(); |
| 6450 | const bool has_nonfatal_failure = HasNonfatalFailure(); |
| 6451 | ClearCurrentTestPartResults(); |
| 6452 | EXPECT_TRUE(has_nonfatal_failure); |
| 6453 | } |
| 6454 | |
| 6455 | // A wrapper for calling HasNonfatalFailure outside of a test body. |
| 6456 | static bool HasNonfatalFailureHelper() { |
| 6457 | return testing::Test::HasNonfatalFailure(); |
| 6458 | } |
| 6459 | |
| 6460 | TEST(HasNonfatalFailureTest, WorksOutsideOfTestBody) { |
| 6461 | EXPECT_FALSE(HasNonfatalFailureHelper()); |
| 6462 | } |
| 6463 | |
| 6464 | TEST(HasNonfatalFailureTest, WorksOutsideOfTestBody2) { |
| 6465 | ADD_FAILURE(); |
| 6466 | const bool has_nonfatal_failure = HasNonfatalFailureHelper(); |
| 6467 | ClearCurrentTestPartResults(); |
| 6468 | EXPECT_TRUE(has_nonfatal_failure); |
| 6469 | } |
| 6470 | |
| 6471 | TEST(HasFailureTest, ReturnsFalseWhenThereIsNoFailure) { |
| 6472 | EXPECT_FALSE(HasFailure()); |
| 6473 | } |
| 6474 | |
| 6475 | TEST(HasFailureTest, ReturnsTrueWhenThereIsFatalFailure) { |
| 6476 | FailFatally(); |
| 6477 | const bool has_failure = HasFailure(); |
| 6478 | ClearCurrentTestPartResults(); |
| 6479 | EXPECT_TRUE(has_failure); |
| 6480 | } |
| 6481 | |
| 6482 | TEST(HasFailureTest, ReturnsTrueWhenThereIsNonfatalFailure) { |
| 6483 | ADD_FAILURE(); |
| 6484 | const bool has_failure = HasFailure(); |
| 6485 | ClearCurrentTestPartResults(); |
| 6486 | EXPECT_TRUE(has_failure); |
| 6487 | } |
| 6488 | |
| 6489 | TEST(HasFailureTest, ReturnsTrueWhenThereAreFatalAndNonfatalFailures) { |
| 6490 | FailFatally(); |
| 6491 | ADD_FAILURE(); |
| 6492 | const bool has_failure = HasFailure(); |
| 6493 | ClearCurrentTestPartResults(); |
| 6494 | EXPECT_TRUE(has_failure); |
| 6495 | } |
| 6496 | |
| 6497 | // A wrapper for calling HasFailure outside of a test body. |
| 6498 | static bool HasFailureHelper() { return testing::Test::HasFailure(); } |
| 6499 | |
| 6500 | TEST(HasFailureTest, WorksOutsideOfTestBody) { |
| 6501 | EXPECT_FALSE(HasFailureHelper()); |
| 6502 | } |
| 6503 | |
| 6504 | TEST(HasFailureTest, WorksOutsideOfTestBody2) { |
| 6505 | ADD_FAILURE(); |
| 6506 | const bool has_failure = HasFailureHelper(); |
| 6507 | ClearCurrentTestPartResults(); |
| 6508 | EXPECT_TRUE(has_failure); |
| 6509 | } |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6510 | |
| 6511 | class TestListener : public EmptyTestEventListener { |
| 6512 | public: |
| 6513 | TestListener() : on_start_counter_(NULL), is_destroyed_(NULL) {} |
| 6514 | TestListener(int* on_start_counter, bool* is_destroyed) |
| 6515 | : on_start_counter_(on_start_counter), |
| 6516 | is_destroyed_(is_destroyed) {} |
| 6517 | |
| 6518 | virtual ~TestListener() { |
| 6519 | if (is_destroyed_) |
| 6520 | *is_destroyed_ = true; |
| 6521 | } |
| 6522 | |
| 6523 | protected: |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6524 | virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6525 | if (on_start_counter_ != NULL) |
| 6526 | (*on_start_counter_)++; |
| 6527 | } |
| 6528 | |
| 6529 | private: |
| 6530 | int* on_start_counter_; |
| 6531 | bool* is_destroyed_; |
| 6532 | }; |
| 6533 | |
| 6534 | // Tests the constructor. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6535 | TEST(TestEventListenersTest, ConstructionWorks) { |
| 6536 | TestEventListeners listeners; |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6537 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6538 | EXPECT_TRUE(TestEventListenersAccessor::GetRepeater(&listeners) != NULL); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6539 | EXPECT_TRUE(listeners.default_result_printer() == NULL); |
| 6540 | EXPECT_TRUE(listeners.default_xml_generator() == NULL); |
| 6541 | } |
| 6542 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6543 | // Tests that the TestEventListeners destructor deletes all the listeners it |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6544 | // owns. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6545 | TEST(TestEventListenersTest, DestructionWorks) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6546 | bool default_result_printer_is_destroyed = false; |
| 6547 | bool default_xml_printer_is_destroyed = false; |
| 6548 | bool extra_listener_is_destroyed = false; |
| 6549 | TestListener* default_result_printer = new TestListener( |
| 6550 | NULL, &default_result_printer_is_destroyed); |
| 6551 | TestListener* default_xml_printer = new TestListener( |
| 6552 | NULL, &default_xml_printer_is_destroyed); |
| 6553 | TestListener* extra_listener = new TestListener( |
| 6554 | NULL, &extra_listener_is_destroyed); |
| 6555 | |
| 6556 | { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6557 | TestEventListeners listeners; |
| 6558 | TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, |
| 6559 | default_result_printer); |
| 6560 | TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, |
| 6561 | default_xml_printer); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6562 | listeners.Append(extra_listener); |
| 6563 | } |
| 6564 | EXPECT_TRUE(default_result_printer_is_destroyed); |
| 6565 | EXPECT_TRUE(default_xml_printer_is_destroyed); |
| 6566 | EXPECT_TRUE(extra_listener_is_destroyed); |
| 6567 | } |
| 6568 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6569 | // Tests that a listener Append'ed to a TestEventListeners list starts |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6570 | // receiving events. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6571 | TEST(TestEventListenersTest, Append) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6572 | int on_start_counter = 0; |
| 6573 | bool is_destroyed = false; |
| 6574 | TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); |
| 6575 | { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6576 | TestEventListeners listeners; |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6577 | listeners.Append(listener); |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6578 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6579 | *UnitTest::GetInstance()); |
| 6580 | EXPECT_EQ(1, on_start_counter); |
| 6581 | } |
| 6582 | EXPECT_TRUE(is_destroyed); |
| 6583 | } |
| 6584 | |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6585 | // Tests that listeners receive events in the order they were appended to |
| 6586 | // the list, except for *End requests, which must be received in the reverse |
| 6587 | // order. |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6588 | class SequenceTestingListener : public EmptyTestEventListener { |
| 6589 | public: |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6590 | SequenceTestingListener(std::vector<String>* vector, const char* id) |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6591 | : vector_(vector), id_(id) {} |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6592 | |
| 6593 | protected: |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6594 | virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) { |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6595 | vector_->push_back(GetEventDescription("OnTestProgramStart")); |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6596 | } |
| 6597 | |
| 6598 | virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) { |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6599 | vector_->push_back(GetEventDescription("OnTestProgramEnd")); |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6600 | } |
| 6601 | |
| 6602 | virtual void OnTestIterationStart(const UnitTest& /*unit_test*/, |
| 6603 | int /*iteration*/) { |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6604 | vector_->push_back(GetEventDescription("OnTestIterationStart")); |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6605 | } |
| 6606 | |
| 6607 | virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/, |
| 6608 | int /*iteration*/) { |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6609 | vector_->push_back(GetEventDescription("OnTestIterationEnd")); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6610 | } |
| 6611 | |
| 6612 | private: |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6613 | String GetEventDescription(const char* method) { |
| 6614 | Message message; |
| 6615 | message << id_ << "." << method; |
| 6616 | return message.GetString(); |
| 6617 | } |
| 6618 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6619 | std::vector<String>* vector_; |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6620 | const char* const id_; |
zhanyong.wan | d586f9f | 2009-09-18 16:35:15 +0000 | [diff] [blame] | 6621 | |
| 6622 | GTEST_DISALLOW_COPY_AND_ASSIGN_(SequenceTestingListener); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6623 | }; |
| 6624 | |
| 6625 | TEST(EventListenerTest, AppendKeepsOrder) { |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6626 | std::vector<String> vec; |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6627 | TestEventListeners listeners; |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6628 | listeners.Append(new SequenceTestingListener(&vec, "1st")); |
| 6629 | listeners.Append(new SequenceTestingListener(&vec, "2nd")); |
| 6630 | listeners.Append(new SequenceTestingListener(&vec, "3rd")); |
| 6631 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6632 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6633 | *UnitTest::GetInstance()); |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6634 | ASSERT_EQ(3U, vec.size()); |
| 6635 | EXPECT_STREQ("1st.OnTestProgramStart", vec[0].c_str()); |
| 6636 | EXPECT_STREQ("2nd.OnTestProgramStart", vec[1].c_str()); |
| 6637 | EXPECT_STREQ("3rd.OnTestProgramStart", vec[2].c_str()); |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6638 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6639 | vec.clear(); |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6640 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramEnd( |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6641 | *UnitTest::GetInstance()); |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6642 | ASSERT_EQ(3U, vec.size()); |
| 6643 | EXPECT_STREQ("3rd.OnTestProgramEnd", vec[0].c_str()); |
| 6644 | EXPECT_STREQ("2nd.OnTestProgramEnd", vec[1].c_str()); |
| 6645 | EXPECT_STREQ("1st.OnTestProgramEnd", vec[2].c_str()); |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6646 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6647 | vec.clear(); |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6648 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestIterationStart( |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6649 | *UnitTest::GetInstance(), 0); |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6650 | ASSERT_EQ(3U, vec.size()); |
| 6651 | EXPECT_STREQ("1st.OnTestIterationStart", vec[0].c_str()); |
| 6652 | EXPECT_STREQ("2nd.OnTestIterationStart", vec[1].c_str()); |
| 6653 | EXPECT_STREQ("3rd.OnTestIterationStart", vec[2].c_str()); |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6654 | |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6655 | vec.clear(); |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6656 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestIterationEnd( |
zhanyong.wan | 7dcfb6b | 2009-09-17 19:12:30 +0000 | [diff] [blame] | 6657 | *UnitTest::GetInstance(), 0); |
zhanyong.wan | 93d13a8 | 2010-02-25 01:09:07 +0000 | [diff] [blame] | 6658 | ASSERT_EQ(3U, vec.size()); |
| 6659 | EXPECT_STREQ("3rd.OnTestIterationEnd", vec[0].c_str()); |
| 6660 | EXPECT_STREQ("2nd.OnTestIterationEnd", vec[1].c_str()); |
| 6661 | EXPECT_STREQ("1st.OnTestIterationEnd", vec[2].c_str()); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6662 | } |
| 6663 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6664 | // Tests that a listener removed from a TestEventListeners list stops receiving |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6665 | // events and is not deleted when the list is destroyed. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6666 | TEST(TestEventListenersTest, Release) { |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6667 | int on_start_counter = 0; |
| 6668 | bool is_destroyed = false; |
| 6669 | // Although Append passes the ownership of this object to the list, |
| 6670 | // the following calls release it, and we need to delete it before the |
| 6671 | // test ends. |
| 6672 | TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); |
| 6673 | { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6674 | TestEventListeners listeners; |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6675 | listeners.Append(listener); |
| 6676 | EXPECT_EQ(listener, listeners.Release(listener)); |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6677 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6678 | *UnitTest::GetInstance()); |
| 6679 | EXPECT_TRUE(listeners.Release(listener) == NULL); |
| 6680 | } |
| 6681 | EXPECT_EQ(0, on_start_counter); |
| 6682 | EXPECT_FALSE(is_destroyed); |
| 6683 | delete listener; |
| 6684 | } |
| 6685 | |
| 6686 | // Tests that no events are forwarded when event forwarding is disabled. |
| 6687 | TEST(EventListenerTest, SuppressEventForwarding) { |
| 6688 | int on_start_counter = 0; |
| 6689 | TestListener* listener = new TestListener(&on_start_counter, NULL); |
| 6690 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6691 | TestEventListeners listeners; |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6692 | listeners.Append(listener); |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6693 | ASSERT_TRUE(TestEventListenersAccessor::EventForwardingEnabled(listeners)); |
| 6694 | TestEventListenersAccessor::SuppressEventForwarding(&listeners); |
| 6695 | ASSERT_FALSE(TestEventListenersAccessor::EventForwardingEnabled(listeners)); |
| 6696 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6697 | *UnitTest::GetInstance()); |
| 6698 | EXPECT_EQ(0, on_start_counter); |
| 6699 | } |
| 6700 | |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6701 | // Tests that events generated by Google Test are not forwarded in |
| 6702 | // death test subprocesses. |
| 6703 | TEST(EventListenerDeathTest, EventsNotForwardedInDeathTestSubprecesses) { |
zhanyong.wan | d541f02 | 2009-09-11 06:59:42 +0000 | [diff] [blame] | 6704 | EXPECT_DEATH_IF_SUPPORTED({ |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6705 | GTEST_CHECK_(TestEventListenersAccessor::EventForwardingEnabled( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6706 | *GetUnitTestImpl()->listeners())) << "expected failure";}, |
| 6707 | "expected failure"); |
| 6708 | } |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6709 | |
| 6710 | // Tests that a listener installed via SetDefaultResultPrinter() starts |
| 6711 | // receiving events and is returned via default_result_printer() and that |
| 6712 | // the previous default_result_printer is removed from the list and deleted. |
| 6713 | TEST(EventListenerTest, default_result_printer) { |
| 6714 | int on_start_counter = 0; |
| 6715 | bool is_destroyed = false; |
| 6716 | TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); |
| 6717 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6718 | TestEventListeners listeners; |
| 6719 | TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, listener); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6720 | |
| 6721 | EXPECT_EQ(listener, listeners.default_result_printer()); |
| 6722 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6723 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6724 | *UnitTest::GetInstance()); |
| 6725 | |
| 6726 | EXPECT_EQ(1, on_start_counter); |
| 6727 | |
| 6728 | // Replacing default_result_printer with something else should remove it |
| 6729 | // from the list and destroy it. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6730 | TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, NULL); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6731 | |
| 6732 | EXPECT_TRUE(listeners.default_result_printer() == NULL); |
| 6733 | EXPECT_TRUE(is_destroyed); |
| 6734 | |
| 6735 | // After broadcasting an event the counter is still the same, indicating |
| 6736 | // the listener is not in the list anymore. |
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 | |
| 6742 | // Tests that the default_result_printer listener stops receiving events |
| 6743 | // when removed via Release and that is not owned by the list anymore. |
| 6744 | TEST(EventListenerTest, RemovingDefaultResultPrinterWorks) { |
| 6745 | int on_start_counter = 0; |
| 6746 | bool is_destroyed = false; |
| 6747 | // Although Append passes the ownership of this object to the list, |
| 6748 | // the following calls release it, and we need to delete it before the |
| 6749 | // test ends. |
| 6750 | TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); |
| 6751 | { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6752 | TestEventListeners listeners; |
| 6753 | TestEventListenersAccessor::SetDefaultResultPrinter(&listeners, listener); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6754 | |
| 6755 | EXPECT_EQ(listener, listeners.Release(listener)); |
| 6756 | EXPECT_TRUE(listeners.default_result_printer() == NULL); |
| 6757 | EXPECT_FALSE(is_destroyed); |
| 6758 | |
| 6759 | // Broadcasting events now should not affect default_result_printer. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6760 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6761 | *UnitTest::GetInstance()); |
| 6762 | EXPECT_EQ(0, on_start_counter); |
| 6763 | } |
| 6764 | // Destroying the list should not affect the listener now, too. |
| 6765 | EXPECT_FALSE(is_destroyed); |
| 6766 | delete listener; |
| 6767 | } |
| 6768 | |
| 6769 | // Tests that a listener installed via SetDefaultXmlGenerator() starts |
| 6770 | // receiving events and is returned via default_xml_generator() and that |
| 6771 | // the previous default_xml_generator is removed from the list and deleted. |
| 6772 | TEST(EventListenerTest, default_xml_generator) { |
| 6773 | int on_start_counter = 0; |
| 6774 | bool is_destroyed = false; |
| 6775 | TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); |
| 6776 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6777 | TestEventListeners listeners; |
| 6778 | TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, listener); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6779 | |
| 6780 | EXPECT_EQ(listener, listeners.default_xml_generator()); |
| 6781 | |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6782 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6783 | *UnitTest::GetInstance()); |
| 6784 | |
| 6785 | EXPECT_EQ(1, on_start_counter); |
| 6786 | |
| 6787 | // Replacing default_xml_generator with something else should remove it |
| 6788 | // from the list and destroy it. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6789 | TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, NULL); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6790 | |
| 6791 | EXPECT_TRUE(listeners.default_xml_generator() == NULL); |
| 6792 | EXPECT_TRUE(is_destroyed); |
| 6793 | |
| 6794 | // After broadcasting an event the counter is still the same, indicating |
| 6795 | // the listener is not in the list anymore. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6796 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6797 | *UnitTest::GetInstance()); |
| 6798 | EXPECT_EQ(1, on_start_counter); |
| 6799 | } |
| 6800 | |
| 6801 | // Tests that the default_xml_generator listener stops receiving events |
| 6802 | // when removed via Release and that is not owned by the list anymore. |
| 6803 | TEST(EventListenerTest, RemovingDefaultXmlGeneratorWorks) { |
| 6804 | int on_start_counter = 0; |
| 6805 | bool is_destroyed = false; |
| 6806 | // Although Append passes the ownership of this object to the list, |
| 6807 | // the following calls release it, and we need to delete it before the |
| 6808 | // test ends. |
| 6809 | TestListener* listener = new TestListener(&on_start_counter, &is_destroyed); |
| 6810 | { |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6811 | TestEventListeners listeners; |
| 6812 | TestEventListenersAccessor::SetDefaultXmlGenerator(&listeners, listener); |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6813 | |
| 6814 | EXPECT_EQ(listener, listeners.Release(listener)); |
| 6815 | EXPECT_TRUE(listeners.default_xml_generator() == NULL); |
| 6816 | EXPECT_FALSE(is_destroyed); |
| 6817 | |
| 6818 | // Broadcasting events now should not affect default_xml_generator. |
zhanyong.wan | f6d087b | 2009-09-30 20:23:50 +0000 | [diff] [blame] | 6819 | TestEventListenersAccessor::GetRepeater(&listeners)->OnTestProgramStart( |
zhanyong.wan | f39160b | 2009-09-04 18:30:25 +0000 | [diff] [blame] | 6820 | *UnitTest::GetInstance()); |
| 6821 | EXPECT_EQ(0, on_start_counter); |
| 6822 | } |
| 6823 | // Destroying the list should not affect the listener now, too. |
| 6824 | EXPECT_FALSE(is_destroyed); |
| 6825 | delete listener; |
| 6826 | } |
zhanyong.wan | 1618bb4 | 2010-04-13 04:40:32 +0000 | [diff] [blame] | 6827 | |
| 6828 | // Sanity tests to ensure that the alternative, verbose spellings of |
| 6829 | // some of the macros work. We don't test them thoroughly as that |
| 6830 | // would be quite involved. Since their implementations are |
| 6831 | // straightforward, and they are rarely used, we'll just rely on the |
| 6832 | // users to tell us when they are broken. |
| 6833 | GTEST_TEST(AlternativeNameTest, Works) { // GTEST_TEST is the same as TEST. |
| 6834 | GTEST_SUCCEED() << "OK"; // GTEST_SUCCEED is the same as SUCCEED. |
| 6835 | |
| 6836 | // GTEST_FAIL is the same as FAIL. |
| 6837 | EXPECT_FATAL_FAILURE(GTEST_FAIL() << "An expected failure", |
| 6838 | "An expected failure"); |
| 6839 | } |
zhanyong.wan | 678f92b | 2010-05-10 17:11:58 +0000 | [diff] [blame] | 6840 | |
| 6841 | // Tests for internal utilities necessary for implementation of the universal |
| 6842 | // printing. |
| 6843 | // TODO(vladl@google.com): Find a better home for them. |
| 6844 | |
| 6845 | class ConversionHelperBase {}; |
| 6846 | class ConversionHelperDerived : public ConversionHelperBase {}; |
| 6847 | |
| 6848 | // Tests that IsAProtocolMessage<T>::value is a compile-time constant. |
| 6849 | TEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) { |
| 6850 | GTEST_COMPILE_ASSERT_(IsAProtocolMessage<ProtocolMessage>::value, |
| 6851 | const_true); |
| 6852 | GTEST_COMPILE_ASSERT_(!IsAProtocolMessage<int>::value, const_false); |
| 6853 | } |
| 6854 | |
| 6855 | // Tests that IsAProtocolMessage<T>::value is true when T is |
| 6856 | // ProtocolMessage or a sub-class of it. |
| 6857 | TEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) { |
| 6858 | EXPECT_TRUE(IsAProtocolMessage< ::proto2::Message>::value); |
| 6859 | EXPECT_TRUE(IsAProtocolMessage<ProtocolMessage>::value); |
| 6860 | #if GTEST_HAS_PROTOBUF_ |
| 6861 | EXPECT_TRUE(IsAProtocolMessage<const TestMessage>::value); |
| 6862 | #endif // GTEST_HAS_PROTOBUF_ |
| 6863 | } |
| 6864 | |
| 6865 | // Tests that IsAProtocolMessage<T>::value is false when T is neither |
| 6866 | // ProtocolMessage nor a sub-class of it. |
| 6867 | TEST(IsAProtocolMessageTest, ValueIsFalseWhenTypeIsNotAProtocolMessage) { |
| 6868 | EXPECT_FALSE(IsAProtocolMessage<int>::value); |
| 6869 | EXPECT_FALSE(IsAProtocolMessage<const ConversionHelperBase>::value); |
| 6870 | } |
| 6871 | |
| 6872 | // Tests that CompileAssertTypesEqual compiles when the type arguments are |
| 6873 | // equal. |
| 6874 | TEST(CompileAssertTypesEqual, CompilesWhenTypesAreEqual) { |
| 6875 | CompileAssertTypesEqual<void, void>(); |
| 6876 | CompileAssertTypesEqual<int*, int*>(); |
| 6877 | } |
| 6878 | |
| 6879 | // Tests that RemoveReference does not affect non-reference types. |
| 6880 | TEST(RemoveReferenceTest, DoesNotAffectNonReferenceType) { |
| 6881 | CompileAssertTypesEqual<int, RemoveReference<int>::type>(); |
| 6882 | CompileAssertTypesEqual<const char, RemoveReference<const char>::type>(); |
| 6883 | } |
| 6884 | |
| 6885 | // Tests that RemoveReference removes reference from reference types. |
| 6886 | TEST(RemoveReferenceTest, RemovesReference) { |
| 6887 | CompileAssertTypesEqual<int, RemoveReference<int&>::type>(); |
| 6888 | CompileAssertTypesEqual<const char, RemoveReference<const char&>::type>(); |
| 6889 | } |
| 6890 | |
| 6891 | // Tests GTEST_REMOVE_REFERENCE_. |
| 6892 | |
| 6893 | template <typename T1, typename T2> |
| 6894 | void TestGTestRemoveReference() { |
| 6895 | CompileAssertTypesEqual<T1, GTEST_REMOVE_REFERENCE_(T2)>(); |
| 6896 | } |
| 6897 | |
| 6898 | TEST(RemoveReferenceTest, MacroVersion) { |
| 6899 | TestGTestRemoveReference<int, int>(); |
| 6900 | TestGTestRemoveReference<const char, const char&>(); |
| 6901 | } |
| 6902 | |
| 6903 | |
| 6904 | // Tests that RemoveConst does not affect non-const types. |
| 6905 | TEST(RemoveConstTest, DoesNotAffectNonConstType) { |
| 6906 | CompileAssertTypesEqual<int, RemoveConst<int>::type>(); |
| 6907 | CompileAssertTypesEqual<char&, RemoveConst<char&>::type>(); |
| 6908 | } |
| 6909 | |
| 6910 | // Tests that RemoveConst removes const from const types. |
| 6911 | TEST(RemoveConstTest, RemovesConst) { |
| 6912 | CompileAssertTypesEqual<int, RemoveConst<const int>::type>(); |
| 6913 | CompileAssertTypesEqual<char[2], RemoveConst<const char[2]>::type>(); |
| 6914 | CompileAssertTypesEqual<char[2][3], RemoveConst<const char[2][3]>::type>(); |
| 6915 | } |
| 6916 | |
| 6917 | // Tests GTEST_REMOVE_CONST_. |
| 6918 | |
| 6919 | template <typename T1, typename T2> |
| 6920 | void TestGTestRemoveConst() { |
| 6921 | CompileAssertTypesEqual<T1, GTEST_REMOVE_CONST_(T2)>(); |
| 6922 | } |
| 6923 | |
| 6924 | TEST(RemoveConstTest, MacroVersion) { |
| 6925 | TestGTestRemoveConst<int, int>(); |
| 6926 | TestGTestRemoveConst<double&, double&>(); |
| 6927 | TestGTestRemoveConst<char, const char>(); |
| 6928 | } |
| 6929 | |
zhanyong.wan | c3ad690 | 2010-05-17 19:31:00 +0000 | [diff] [blame] | 6930 | // Tests GTEST_REMOVE_REFERENCE_AND_CONST_. |
| 6931 | |
| 6932 | template <typename T1, typename T2> |
| 6933 | void TestGTestRemoveReferenceAndConst() { |
| 6934 | CompileAssertTypesEqual<T1, GTEST_REMOVE_REFERENCE_AND_CONST_(T2)>(); |
| 6935 | } |
| 6936 | |
| 6937 | TEST(RemoveReferenceToConstTest, Works) { |
| 6938 | TestGTestRemoveReferenceAndConst<int, int>(); |
| 6939 | TestGTestRemoveReferenceAndConst<double, double&>(); |
| 6940 | TestGTestRemoveReferenceAndConst<char, const char>(); |
| 6941 | TestGTestRemoveReferenceAndConst<char, const char&>(); |
| 6942 | TestGTestRemoveReferenceAndConst<const char*, const char*>(); |
| 6943 | } |
| 6944 | |
zhanyong.wan | 678f92b | 2010-05-10 17:11:58 +0000 | [diff] [blame] | 6945 | // Tests that AddReference does not affect reference types. |
| 6946 | TEST(AddReferenceTest, DoesNotAffectReferenceType) { |
| 6947 | CompileAssertTypesEqual<int&, AddReference<int&>::type>(); |
| 6948 | CompileAssertTypesEqual<const char&, AddReference<const char&>::type>(); |
| 6949 | } |
| 6950 | |
| 6951 | // Tests that AddReference adds reference to non-reference types. |
| 6952 | TEST(AddReferenceTest, AddsReference) { |
| 6953 | CompileAssertTypesEqual<int&, AddReference<int>::type>(); |
| 6954 | CompileAssertTypesEqual<const char&, AddReference<const char>::type>(); |
| 6955 | } |
| 6956 | |
| 6957 | // Tests GTEST_ADD_REFERENCE_. |
| 6958 | |
| 6959 | template <typename T1, typename T2> |
| 6960 | void TestGTestAddReference() { |
| 6961 | CompileAssertTypesEqual<T1, GTEST_ADD_REFERENCE_(T2)>(); |
| 6962 | } |
| 6963 | |
| 6964 | TEST(AddReferenceTest, MacroVersion) { |
| 6965 | TestGTestAddReference<int&, int>(); |
| 6966 | TestGTestAddReference<const char&, const char&>(); |
| 6967 | } |
| 6968 | |
| 6969 | // Tests GTEST_REFERENCE_TO_CONST_. |
| 6970 | |
| 6971 | template <typename T1, typename T2> |
| 6972 | void TestGTestReferenceToConst() { |
| 6973 | CompileAssertTypesEqual<T1, GTEST_REFERENCE_TO_CONST_(T2)>(); |
| 6974 | } |
| 6975 | |
| 6976 | TEST(GTestReferenceToConstTest, Works) { |
| 6977 | TestGTestReferenceToConst<const char&, char>(); |
| 6978 | TestGTestReferenceToConst<const int&, const int>(); |
| 6979 | TestGTestReferenceToConst<const double&, double>(); |
| 6980 | TestGTestReferenceToConst<const String&, const String&>(); |
| 6981 | } |
| 6982 | |
| 6983 | // Tests that ImplicitlyConvertible<T1, T2>::value is a compile-time constant. |
| 6984 | TEST(ImplicitlyConvertibleTest, ValueIsCompileTimeConstant) { |
| 6985 | GTEST_COMPILE_ASSERT_((ImplicitlyConvertible<int, int>::value), const_true); |
| 6986 | GTEST_COMPILE_ASSERT_((!ImplicitlyConvertible<void*, int*>::value), |
| 6987 | const_false); |
| 6988 | } |
| 6989 | |
| 6990 | // Tests that ImplicitlyConvertible<T1, T2>::value is true when T1 can |
| 6991 | // be implicitly converted to T2. |
| 6992 | TEST(ImplicitlyConvertibleTest, ValueIsTrueWhenConvertible) { |
| 6993 | EXPECT_TRUE((ImplicitlyConvertible<int, double>::value)); |
| 6994 | EXPECT_TRUE((ImplicitlyConvertible<double, int>::value)); |
| 6995 | EXPECT_TRUE((ImplicitlyConvertible<int*, void*>::value)); |
| 6996 | EXPECT_TRUE((ImplicitlyConvertible<int*, const int*>::value)); |
| 6997 | EXPECT_TRUE((ImplicitlyConvertible<ConversionHelperDerived&, |
| 6998 | const ConversionHelperBase&>::value)); |
| 6999 | EXPECT_TRUE((ImplicitlyConvertible<const ConversionHelperBase, |
| 7000 | ConversionHelperBase>::value)); |
| 7001 | } |
| 7002 | |
| 7003 | // Tests that ImplicitlyConvertible<T1, T2>::value is false when T1 |
| 7004 | // cannot be implicitly converted to T2. |
| 7005 | TEST(ImplicitlyConvertibleTest, ValueIsFalseWhenNotConvertible) { |
| 7006 | EXPECT_FALSE((ImplicitlyConvertible<double, int*>::value)); |
| 7007 | EXPECT_FALSE((ImplicitlyConvertible<void*, int*>::value)); |
| 7008 | EXPECT_FALSE((ImplicitlyConvertible<const int*, int*>::value)); |
| 7009 | EXPECT_FALSE((ImplicitlyConvertible<ConversionHelperBase&, |
| 7010 | ConversionHelperDerived&>::value)); |
| 7011 | } |
| 7012 | |
| 7013 | // Tests IsContainerTest. |
| 7014 | |
| 7015 | class NonContainer {}; |
| 7016 | |
| 7017 | TEST(IsContainerTestTest, WorksForNonContainer) { |
| 7018 | EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<int>(0))); |
| 7019 | EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<char[5]>(0))); |
| 7020 | EXPECT_EQ(sizeof(IsNotContainer), sizeof(IsContainerTest<NonContainer>(0))); |
| 7021 | } |
| 7022 | |
| 7023 | TEST(IsContainerTestTest, WorksForContainer) { |
| 7024 | EXPECT_EQ(sizeof(IsContainer), |
| 7025 | sizeof(IsContainerTest<std::vector<bool> >(0))); |
| 7026 | EXPECT_EQ(sizeof(IsContainer), |
| 7027 | sizeof(IsContainerTest<std::map<int, double> >(0))); |
| 7028 | } |
| 7029 | |
| 7030 | // Tests ArrayEq(). |
| 7031 | |
| 7032 | TEST(ArrayEqTest, WorksForDegeneratedArrays) { |
| 7033 | EXPECT_TRUE(ArrayEq(5, 5L)); |
| 7034 | EXPECT_FALSE(ArrayEq('a', 0)); |
| 7035 | } |
| 7036 | |
| 7037 | TEST(ArrayEqTest, WorksForOneDimensionalArrays) { |
| 7038 | const int a[] = { 0, 1 }; |
| 7039 | long b[] = { 0, 1 }; |
| 7040 | EXPECT_TRUE(ArrayEq(a, b)); |
| 7041 | EXPECT_TRUE(ArrayEq(a, 2, b)); |
| 7042 | |
| 7043 | b[0] = 2; |
| 7044 | EXPECT_FALSE(ArrayEq(a, b)); |
| 7045 | EXPECT_FALSE(ArrayEq(a, 1, b)); |
| 7046 | } |
| 7047 | |
| 7048 | TEST(ArrayEqTest, WorksForTwoDimensionalArrays) { |
| 7049 | const char a[][3] = { "hi", "lo" }; |
| 7050 | const char b[][3] = { "hi", "lo" }; |
| 7051 | const char c[][3] = { "hi", "li" }; |
| 7052 | |
| 7053 | EXPECT_TRUE(ArrayEq(a, b)); |
| 7054 | EXPECT_TRUE(ArrayEq(a, 2, b)); |
| 7055 | |
| 7056 | EXPECT_FALSE(ArrayEq(a, c)); |
| 7057 | EXPECT_FALSE(ArrayEq(a, 2, c)); |
| 7058 | } |
| 7059 | |
| 7060 | // Tests ArrayAwareFind(). |
| 7061 | |
| 7062 | TEST(ArrayAwareFindTest, WorksForOneDimensionalArray) { |
| 7063 | const char a[] = "hello"; |
| 7064 | EXPECT_EQ(a + 4, ArrayAwareFind(a, a + 5, 'o')); |
| 7065 | EXPECT_EQ(a + 5, ArrayAwareFind(a, a + 5, 'x')); |
| 7066 | } |
| 7067 | |
| 7068 | TEST(ArrayAwareFindTest, WorksForTwoDimensionalArray) { |
| 7069 | int a[][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; |
| 7070 | const int b[2] = { 2, 3 }; |
| 7071 | EXPECT_EQ(a + 1, ArrayAwareFind(a, a + 3, b)); |
| 7072 | |
| 7073 | const int c[2] = { 6, 7 }; |
| 7074 | EXPECT_EQ(a + 3, ArrayAwareFind(a, a + 3, c)); |
| 7075 | } |
| 7076 | |
| 7077 | // Tests CopyArray(). |
| 7078 | |
| 7079 | TEST(CopyArrayTest, WorksForDegeneratedArrays) { |
| 7080 | int n = 0; |
| 7081 | CopyArray('a', &n); |
| 7082 | EXPECT_EQ('a', n); |
| 7083 | } |
| 7084 | |
| 7085 | TEST(CopyArrayTest, WorksForOneDimensionalArrays) { |
| 7086 | const char a[3] = "hi"; |
| 7087 | int b[3]; |
| 7088 | CopyArray(a, &b); |
| 7089 | EXPECT_TRUE(ArrayEq(a, b)); |
| 7090 | |
| 7091 | int c[3]; |
| 7092 | CopyArray(a, 3, c); |
| 7093 | EXPECT_TRUE(ArrayEq(a, c)); |
| 7094 | } |
| 7095 | |
| 7096 | TEST(CopyArrayTest, WorksForTwoDimensionalArrays) { |
| 7097 | const int a[2][3] = { { 0, 1, 2 }, { 3, 4, 5 } }; |
| 7098 | int b[2][3]; |
| 7099 | CopyArray(a, &b); |
| 7100 | EXPECT_TRUE(ArrayEq(a, b)); |
| 7101 | |
| 7102 | int c[2][3]; |
| 7103 | CopyArray(a, 2, c); |
| 7104 | EXPECT_TRUE(ArrayEq(a, c)); |
| 7105 | } |
| 7106 | |
| 7107 | // Tests NativeArray. |
| 7108 | |
| 7109 | TEST(NativeArrayTest, ConstructorFromArrayWorks) { |
| 7110 | const int a[3] = { 0, 1, 2 }; |
| 7111 | NativeArray<int> na(a, 3, kReference); |
| 7112 | EXPECT_EQ(3U, na.size()); |
| 7113 | EXPECT_EQ(a, na.begin()); |
| 7114 | } |
| 7115 | |
| 7116 | TEST(NativeArrayTest, CreatesAndDeletesCopyOfArrayWhenAskedTo) { |
| 7117 | typedef int Array[2]; |
| 7118 | Array* a = new Array[1]; |
| 7119 | (*a)[0] = 0; |
| 7120 | (*a)[1] = 1; |
| 7121 | NativeArray<int> na(*a, 2, kCopy); |
| 7122 | EXPECT_NE(*a, na.begin()); |
| 7123 | delete[] a; |
| 7124 | EXPECT_EQ(0, na.begin()[0]); |
| 7125 | EXPECT_EQ(1, na.begin()[1]); |
| 7126 | |
| 7127 | // We rely on the heap checker to verify that na deletes the copy of |
| 7128 | // array. |
| 7129 | } |
| 7130 | |
| 7131 | TEST(NativeArrayTest, TypeMembersAreCorrect) { |
| 7132 | StaticAssertTypeEq<char, NativeArray<char>::value_type>(); |
| 7133 | StaticAssertTypeEq<int[2], NativeArray<int[2]>::value_type>(); |
| 7134 | |
| 7135 | StaticAssertTypeEq<const char*, NativeArray<char>::const_iterator>(); |
| 7136 | StaticAssertTypeEq<const bool(*)[2], NativeArray<bool[2]>::const_iterator>(); |
| 7137 | } |
| 7138 | |
| 7139 | TEST(NativeArrayTest, MethodsWork) { |
| 7140 | const int a[3] = { 0, 1, 2 }; |
| 7141 | NativeArray<int> na(a, 3, kCopy); |
| 7142 | ASSERT_EQ(3U, na.size()); |
| 7143 | EXPECT_EQ(3, na.end() - na.begin()); |
| 7144 | |
| 7145 | NativeArray<int>::const_iterator it = na.begin(); |
| 7146 | EXPECT_EQ(0, *it); |
| 7147 | ++it; |
| 7148 | EXPECT_EQ(1, *it); |
| 7149 | it++; |
| 7150 | EXPECT_EQ(2, *it); |
| 7151 | ++it; |
| 7152 | EXPECT_EQ(na.end(), it); |
| 7153 | |
| 7154 | EXPECT_TRUE(na == na); |
| 7155 | |
| 7156 | NativeArray<int> na2(a, 3, kReference); |
| 7157 | EXPECT_TRUE(na == na2); |
| 7158 | |
| 7159 | const int b1[3] = { 0, 1, 1 }; |
| 7160 | const int b2[4] = { 0, 1, 2, 3 }; |
| 7161 | EXPECT_FALSE(na == NativeArray<int>(b1, 3, kReference)); |
| 7162 | EXPECT_FALSE(na == NativeArray<int>(b2, 4, kCopy)); |
| 7163 | } |
| 7164 | |
| 7165 | TEST(NativeArrayTest, WorksForTwoDimensionalArray) { |
| 7166 | const char a[2][3] = { "hi", "lo" }; |
| 7167 | NativeArray<char[3]> na(a, 2, kReference); |
| 7168 | ASSERT_EQ(2U, na.size()); |
| 7169 | EXPECT_EQ(a, na.begin()); |
| 7170 | } |
zhanyong.wan | 9748de0 | 2010-06-08 22:51:46 +0000 | [diff] [blame] | 7171 | |
| 7172 | // Tests SkipPrefix(). |
| 7173 | |
| 7174 | TEST(SkipPrefixTest, SkipsWhenPrefixMatches) { |
| 7175 | const char* const str = "hello"; |
| 7176 | |
| 7177 | const char* p = str; |
| 7178 | EXPECT_TRUE(SkipPrefix("", &p)); |
| 7179 | EXPECT_EQ(str, p); |
| 7180 | |
| 7181 | p = str; |
| 7182 | EXPECT_TRUE(SkipPrefix("hell", &p)); |
| 7183 | EXPECT_EQ(str + 4, p); |
| 7184 | } |
| 7185 | |
| 7186 | TEST(SkipPrefixTest, DoesNotSkipWhenPrefixDoesNotMatch) { |
| 7187 | const char* const str = "world"; |
| 7188 | |
| 7189 | const char* p = str; |
| 7190 | EXPECT_FALSE(SkipPrefix("W", &p)); |
| 7191 | EXPECT_EQ(str, p); |
| 7192 | |
| 7193 | p = str; |
| 7194 | EXPECT_FALSE(SkipPrefix("world!", &p)); |
| 7195 | EXPECT_EQ(str, p); |
| 7196 | } |