| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| // Copyright 2005, Google Inc. | ||
| // All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| // | ||
| // Author: wan@google.com (Zhanyong Wan) | ||
| // | ||
| // The Google C++ Testing Framework (Google Test) | ||
| // | ||
| // This header file defines the Message class. | ||
| // | ||
| // IMPORTANT NOTE: Due to limitation of the C++ language, we have to | ||
| // leave some internal implementation details in this header file. | ||
| // They are clearly marked by comments like this: | ||
| // | ||
| // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | ||
| // | ||
| // Such code is NOT meant to be used by a user directly, and is subject | ||
| // to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user | ||
| // program! | ||
|
|
||
| #ifndef GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ | ||
| #define GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ | ||
|
|
||
| #include <limits> | ||
|
|
||
| #include "gtest/internal/gtest-port.h" | ||
|
|
||
| // Ensures that there is at least one operator<< in the global namespace. | ||
| // See Message& operator<<(...) below for why. | ||
| void operator<<(const testing::internal::Secret&, int); | ||
|
|
||
| namespace testing { | ||
|
|
||
| // The Message class works like an ostream repeater. | ||
| // | ||
| // Typical usage: | ||
| // | ||
| // 1. You stream a bunch of values to a Message object. | ||
| // It will remember the text in a stringstream. | ||
| // 2. Then you stream the Message object to an ostream. | ||
| // This causes the text in the Message to be streamed | ||
| // to the ostream. | ||
| // | ||
| // For example; | ||
| // | ||
| // testing::Message foo; | ||
| // foo << 1 << " != " << 2; | ||
| // std::cout << foo; | ||
| // | ||
| // will print "1 != 2". | ||
| // | ||
| // Message is not intended to be inherited from. In particular, its | ||
| // destructor is not virtual. | ||
| // | ||
| // Note that stringstream behaves differently in gcc and in MSVC. You | ||
| // can stream a NULL char pointer to it in the former, but not in the | ||
| // latter (it causes an access violation if you do). The Message | ||
| // class hides this difference by treating a NULL char pointer as | ||
| // "(null)". | ||
| class GTEST_API_ Message { | ||
| private: | ||
| // The type of basic IO manipulators (endl, ends, and flush) for | ||
| // narrow streams. | ||
| typedef std::ostream& (*BasicNarrowIoManip)(std::ostream&); | ||
|
|
||
| public: | ||
| // Constructs an empty Message. | ||
| Message(); | ||
|
|
||
| // Copy constructor. | ||
| Message(const Message& msg) : ss_(new ::std::stringstream) { // NOLINT | ||
| *ss_ << msg.GetString(); | ||
| } | ||
|
|
||
| // Constructs a Message from a C-string. | ||
| explicit Message(const char* str) : ss_(new ::std::stringstream) { | ||
| *ss_ << str; | ||
| } | ||
|
|
||
| #if GTEST_OS_SYMBIAN | ||
| // Streams a value (either a pointer or not) to this object. | ||
| template <typename T> | ||
| inline Message& operator <<(const T& value) { | ||
| StreamHelper(typename internal::is_pointer<T>::type(), value); | ||
| return *this; | ||
| } | ||
| #else | ||
| // Streams a non-pointer value to this object. | ||
| template <typename T> | ||
| inline Message& operator <<(const T& val) { | ||
| // Some libraries overload << for STL containers. These | ||
| // overloads are defined in the global namespace instead of ::std. | ||
| // | ||
| // C++'s symbol lookup rule (i.e. Koenig lookup) says that these | ||
| // overloads are visible in either the std namespace or the global | ||
| // namespace, but not other namespaces, including the testing | ||
| // namespace which Google Test's Message class is in. | ||
| // | ||
| // To allow STL containers (and other types that has a << operator | ||
| // defined in the global namespace) to be used in Google Test | ||
| // assertions, testing::Message must access the custom << operator | ||
| // from the global namespace. With this using declaration, | ||
| // overloads of << defined in the global namespace and those | ||
| // visible via Koenig lookup are both exposed in this function. | ||
| using ::operator <<; | ||
| *ss_ << val; | ||
| return *this; | ||
| } | ||
|
|
||
| // Streams a pointer value to this object. | ||
| // | ||
| // This function is an overload of the previous one. When you | ||
| // stream a pointer to a Message, this definition will be used as it | ||
| // is more specialized. (The C++ Standard, section | ||
| // [temp.func.order].) If you stream a non-pointer, then the | ||
| // previous definition will be used. | ||
| // | ||
| // The reason for this overload is that streaming a NULL pointer to | ||
| // ostream is undefined behavior. Depending on the compiler, you | ||
| // may get "0", "(nil)", "(null)", or an access violation. To | ||
| // ensure consistent result across compilers, we always treat NULL | ||
| // as "(null)". | ||
| template <typename T> | ||
| inline Message& operator <<(T* const& pointer) { // NOLINT | ||
| if (pointer == NULL) { | ||
| *ss_ << "(null)"; | ||
| } else { | ||
| *ss_ << pointer; | ||
| } | ||
| return *this; | ||
| } | ||
| #endif // GTEST_OS_SYMBIAN | ||
|
|
||
| // Since the basic IO manipulators are overloaded for both narrow | ||
| // and wide streams, we have to provide this specialized definition | ||
| // of operator <<, even though its body is the same as the | ||
| // templatized version above. Without this definition, streaming | ||
| // endl or other basic IO manipulators to Message will confuse the | ||
| // compiler. | ||
| Message& operator <<(BasicNarrowIoManip val) { | ||
| *ss_ << val; | ||
| return *this; | ||
| } | ||
|
|
||
| // Instead of 1/0, we want to see true/false for bool values. | ||
| Message& operator <<(bool b) { | ||
| return *this << (b ? "true" : "false"); | ||
| } | ||
|
|
||
| // These two overloads allow streaming a wide C string to a Message | ||
| // using the UTF-8 encoding. | ||
| Message& operator <<(const wchar_t* wide_c_str); | ||
| Message& operator <<(wchar_t* wide_c_str); | ||
|
|
||
| #if GTEST_HAS_STD_WSTRING | ||
| // Converts the given wide string to a narrow string using the UTF-8 | ||
| // encoding, and streams the result to this Message object. | ||
| Message& operator <<(const ::std::wstring& wstr); | ||
| #endif // GTEST_HAS_STD_WSTRING | ||
|
|
||
| #if GTEST_HAS_GLOBAL_WSTRING | ||
| // Converts the given wide string to a narrow string using the UTF-8 | ||
| // encoding, and streams the result to this Message object. | ||
| Message& operator <<(const ::wstring& wstr); | ||
| #endif // GTEST_HAS_GLOBAL_WSTRING | ||
|
|
||
| // Gets the text streamed to this object so far as an std::string. | ||
| // Each '\0' character in the buffer is replaced with "\\0". | ||
| // | ||
| // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | ||
| std::string GetString() const; | ||
|
|
||
| private: | ||
|
|
||
| #if GTEST_OS_SYMBIAN | ||
| // These are needed as the Nokia Symbian Compiler cannot decide between | ||
| // const T& and const T* in a function template. The Nokia compiler _can_ | ||
| // decide between class template specializations for T and T*, so a | ||
| // tr1::type_traits-like is_pointer works, and we can overload on that. | ||
| template <typename T> | ||
| inline void StreamHelper(internal::true_type /*is_pointer*/, T* pointer) { | ||
| if (pointer == NULL) { | ||
| *ss_ << "(null)"; | ||
| } else { | ||
| *ss_ << pointer; | ||
| } | ||
| } | ||
| template <typename T> | ||
| inline void StreamHelper(internal::false_type /*is_pointer*/, | ||
| const T& value) { | ||
| // See the comments in Message& operator <<(const T&) above for why | ||
| // we need this using statement. | ||
| using ::operator <<; | ||
| *ss_ << value; | ||
| } | ||
| #endif // GTEST_OS_SYMBIAN | ||
|
|
||
| // We'll hold the text streamed to this object here. | ||
| const internal::scoped_ptr< ::std::stringstream> ss_; | ||
|
|
||
| // We declare (but don't implement) this to prevent the compiler | ||
| // from implementing the assignment operator. | ||
| void operator=(const Message&); | ||
| }; | ||
|
|
||
| // Streams a Message to an ostream. | ||
| inline std::ostream& operator <<(std::ostream& os, const Message& sb) { | ||
| return os << sb.GetString(); | ||
| } | ||
|
|
||
| namespace internal { | ||
|
|
||
| // Converts a streamable value to an std::string. A NULL pointer is | ||
| // converted to "(null)". When the input value is a ::string, | ||
| // ::std::string, ::wstring, or ::std::wstring object, each NUL | ||
| // character in it is replaced with "\\0". | ||
| template <typename T> | ||
| std::string StreamableToString(const T& streamable) { | ||
| return (Message() << streamable).GetString(); | ||
| } | ||
|
|
||
| } // namespace internal | ||
| } // namespace testing | ||
|
|
||
| #endif // GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| // Copyright 2007, Google Inc. | ||
| // All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| // | ||
| // Author: wan@google.com (Zhanyong Wan) | ||
| // | ||
| // Utilities for testing Google Test itself and code that uses Google Test | ||
| // (e.g. frameworks built on top of Google Test). | ||
|
|
||
| #ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_ | ||
| #define GTEST_INCLUDE_GTEST_GTEST_SPI_H_ | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace testing { | ||
|
|
||
| // This helper class can be used to mock out Google Test failure reporting | ||
| // so that we can test Google Test or code that builds on Google Test. | ||
| // | ||
| // An object of this class appends a TestPartResult object to the | ||
| // TestPartResultArray object given in the constructor whenever a Google Test | ||
| // failure is reported. It can either intercept only failures that are | ||
| // generated in the same thread that created this object or it can intercept | ||
| // all generated failures. The scope of this mock object can be controlled with | ||
| // the second argument to the two arguments constructor. | ||
| class GTEST_API_ ScopedFakeTestPartResultReporter | ||
| : public TestPartResultReporterInterface { | ||
| public: | ||
| // The two possible mocking modes of this object. | ||
| enum InterceptMode { | ||
| INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures. | ||
| INTERCEPT_ALL_THREADS // Intercepts all failures. | ||
| }; | ||
|
|
||
| // The c'tor sets this object as the test part result reporter used | ||
| // by Google Test. The 'result' parameter specifies where to report the | ||
| // results. This reporter will only catch failures generated in the current | ||
| // thread. DEPRECATED | ||
| explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result); | ||
|
|
||
| // Same as above, but you can choose the interception scope of this object. | ||
| ScopedFakeTestPartResultReporter(InterceptMode intercept_mode, | ||
| TestPartResultArray* result); | ||
|
|
||
| // The d'tor restores the previous test part result reporter. | ||
| virtual ~ScopedFakeTestPartResultReporter(); | ||
|
|
||
| // Appends the TestPartResult object to the TestPartResultArray | ||
| // received in the constructor. | ||
| // | ||
| // This method is from the TestPartResultReporterInterface | ||
| // interface. | ||
| virtual void ReportTestPartResult(const TestPartResult& result); | ||
| private: | ||
| void Init(); | ||
|
|
||
| const InterceptMode intercept_mode_; | ||
| TestPartResultReporterInterface* old_reporter_; | ||
| TestPartResultArray* const result_; | ||
|
|
||
| GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter); | ||
| }; | ||
|
|
||
| namespace internal { | ||
|
|
||
| // A helper class for implementing EXPECT_FATAL_FAILURE() and | ||
| // EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given | ||
| // TestPartResultArray contains exactly one failure that has the given | ||
| // type and contains the given substring. If that's not the case, a | ||
| // non-fatal failure will be generated. | ||
| class GTEST_API_ SingleFailureChecker { | ||
| public: | ||
| // The constructor remembers the arguments. | ||
| SingleFailureChecker(const TestPartResultArray* results, | ||
| TestPartResult::Type type, | ||
| const string& substr); | ||
| ~SingleFailureChecker(); | ||
| private: | ||
| const TestPartResultArray* const results_; | ||
| const TestPartResult::Type type_; | ||
| const string substr_; | ||
|
|
||
| GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker); | ||
| }; | ||
|
|
||
| } // namespace internal | ||
|
|
||
| } // namespace testing | ||
|
|
||
| // A set of macros for testing Google Test assertions or code that's expected | ||
| // to generate Google Test fatal failures. It verifies that the given | ||
| // statement will cause exactly one fatal Google Test failure with 'substr' | ||
| // being part of the failure message. | ||
| // | ||
| // There are two different versions of this macro. EXPECT_FATAL_FAILURE only | ||
| // affects and considers failures generated in the current thread and | ||
| // EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. | ||
| // | ||
| // The verification of the assertion is done correctly even when the statement | ||
| // throws an exception or aborts the current function. | ||
| // | ||
| // Known restrictions: | ||
| // - 'statement' cannot reference local non-static variables or | ||
| // non-static members of the current object. | ||
| // - 'statement' cannot return a value. | ||
| // - You cannot stream a failure message to this macro. | ||
| // | ||
| // Note that even though the implementations of the following two | ||
| // macros are much alike, we cannot refactor them to use a common | ||
| // helper macro, due to some peculiarity in how the preprocessor | ||
| // works. The AcceptsMacroThatExpandsToUnprotectedComma test in | ||
| // gtest_unittest.cc will fail to compile if we do that. | ||
| #define EXPECT_FATAL_FAILURE(statement, substr) \ | ||
| do { \ | ||
| class GTestExpectFatalFailureHelper {\ | ||
| public:\ | ||
| static void Execute() { statement; }\ | ||
| };\ | ||
| ::testing::TestPartResultArray gtest_failures;\ | ||
| ::testing::internal::SingleFailureChecker gtest_checker(\ | ||
| >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\ | ||
| {\ | ||
| ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ | ||
| ::testing::ScopedFakeTestPartResultReporter:: \ | ||
| INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\ | ||
| GTestExpectFatalFailureHelper::Execute();\ | ||
| }\ | ||
| } while (::testing::internal::AlwaysFalse()) | ||
|
|
||
| #define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ | ||
| do { \ | ||
| class GTestExpectFatalFailureHelper {\ | ||
| public:\ | ||
| static void Execute() { statement; }\ | ||
| };\ | ||
| ::testing::TestPartResultArray gtest_failures;\ | ||
| ::testing::internal::SingleFailureChecker gtest_checker(\ | ||
| >est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\ | ||
| {\ | ||
| ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ | ||
| ::testing::ScopedFakeTestPartResultReporter:: \ | ||
| INTERCEPT_ALL_THREADS, >est_failures);\ | ||
| GTestExpectFatalFailureHelper::Execute();\ | ||
| }\ | ||
| } while (::testing::internal::AlwaysFalse()) | ||
|
|
||
| // A macro for testing Google Test assertions or code that's expected to | ||
| // generate Google Test non-fatal failures. It asserts that the given | ||
| // statement will cause exactly one non-fatal Google Test failure with 'substr' | ||
| // being part of the failure message. | ||
| // | ||
| // There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only | ||
| // affects and considers failures generated in the current thread and | ||
| // EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads. | ||
| // | ||
| // 'statement' is allowed to reference local variables and members of | ||
| // the current object. | ||
| // | ||
| // The verification of the assertion is done correctly even when the statement | ||
| // throws an exception or aborts the current function. | ||
| // | ||
| // Known restrictions: | ||
| // - You cannot stream a failure message to this macro. | ||
| // | ||
| // Note that even though the implementations of the following two | ||
| // macros are much alike, we cannot refactor them to use a common | ||
| // helper macro, due to some peculiarity in how the preprocessor | ||
| // works. If we do that, the code won't compile when the user gives | ||
| // EXPECT_NONFATAL_FAILURE() a statement that contains a macro that | ||
| // expands to code containing an unprotected comma. The | ||
| // AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc | ||
| // catches that. | ||
| // | ||
| // For the same reason, we have to write | ||
| // if (::testing::internal::AlwaysTrue()) { statement; } | ||
| // instead of | ||
| // GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) | ||
| // to avoid an MSVC warning on unreachable code. | ||
| #define EXPECT_NONFATAL_FAILURE(statement, substr) \ | ||
| do {\ | ||
| ::testing::TestPartResultArray gtest_failures;\ | ||
| ::testing::internal::SingleFailureChecker gtest_checker(\ | ||
| >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ | ||
| (substr));\ | ||
| {\ | ||
| ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ | ||
| ::testing::ScopedFakeTestPartResultReporter:: \ | ||
| INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\ | ||
| if (::testing::internal::AlwaysTrue()) { statement; }\ | ||
| }\ | ||
| } while (::testing::internal::AlwaysFalse()) | ||
|
|
||
| #define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \ | ||
| do {\ | ||
| ::testing::TestPartResultArray gtest_failures;\ | ||
| ::testing::internal::SingleFailureChecker gtest_checker(\ | ||
| >est_failures, ::testing::TestPartResult::kNonFatalFailure, \ | ||
| (substr));\ | ||
| {\ | ||
| ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ | ||
| ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \ | ||
| >est_failures);\ | ||
| if (::testing::internal::AlwaysTrue()) { statement; }\ | ||
| }\ | ||
| } while (::testing::internal::AlwaysFalse()) | ||
|
|
||
| #endif // GTEST_INCLUDE_GTEST_GTEST_SPI_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| // Copyright 2008, Google Inc. | ||
| // All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| // | ||
| // Author: mheule@google.com (Markus Heule) | ||
| // | ||
|
|
||
| #ifndef GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ | ||
| #define GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ | ||
|
|
||
| #include <iosfwd> | ||
| #include <vector> | ||
| #include "gtest/internal/gtest-internal.h" | ||
| #include "gtest/internal/gtest-string.h" | ||
|
|
||
| namespace testing { | ||
|
|
||
| // A copyable object representing the result of a test part (i.e. an | ||
| // assertion or an explicit FAIL(), ADD_FAILURE(), or SUCCESS()). | ||
| // | ||
| // Don't inherit from TestPartResult as its destructor is not virtual. | ||
| class GTEST_API_ TestPartResult { | ||
| public: | ||
| // The possible outcomes of a test part (i.e. an assertion or an | ||
| // explicit SUCCEED(), FAIL(), or ADD_FAILURE()). | ||
| enum Type { | ||
| kSuccess, // Succeeded. | ||
| kNonFatalFailure, // Failed but the test can continue. | ||
| kFatalFailure // Failed and the test should be terminated. | ||
| }; | ||
|
|
||
| // C'tor. TestPartResult does NOT have a default constructor. | ||
| // Always use this constructor (with parameters) to create a | ||
| // TestPartResult object. | ||
| TestPartResult(Type a_type, | ||
| const char* a_file_name, | ||
| int a_line_number, | ||
| const char* a_message) | ||
| : type_(a_type), | ||
| file_name_(a_file_name == NULL ? "" : a_file_name), | ||
| line_number_(a_line_number), | ||
| summary_(ExtractSummary(a_message)), | ||
| message_(a_message) { | ||
| } | ||
|
|
||
| // Gets the outcome of the test part. | ||
| Type type() const { return type_; } | ||
|
|
||
| // Gets the name of the source file where the test part took place, or | ||
| // NULL if it's unknown. | ||
| const char* file_name() const { | ||
| return file_name_.empty() ? NULL : file_name_.c_str(); | ||
| } | ||
|
|
||
| // Gets the line in the source file where the test part took place, | ||
| // or -1 if it's unknown. | ||
| int line_number() const { return line_number_; } | ||
|
|
||
| // Gets the summary of the failure message. | ||
| const char* summary() const { return summary_.c_str(); } | ||
|
|
||
| // Gets the message associated with the test part. | ||
| const char* message() const { return message_.c_str(); } | ||
|
|
||
| // Returns true iff the test part passed. | ||
| bool passed() const { return type_ == kSuccess; } | ||
|
|
||
| // Returns true iff the test part failed. | ||
| bool failed() const { return type_ != kSuccess; } | ||
|
|
||
| // Returns true iff the test part non-fatally failed. | ||
| bool nonfatally_failed() const { return type_ == kNonFatalFailure; } | ||
|
|
||
| // Returns true iff the test part fatally failed. | ||
| bool fatally_failed() const { return type_ == kFatalFailure; } | ||
|
|
||
| private: | ||
| Type type_; | ||
|
|
||
| // Gets the summary of the failure message by omitting the stack | ||
| // trace in it. | ||
| static std::string ExtractSummary(const char* message); | ||
|
|
||
| // The name of the source file where the test part took place, or | ||
| // "" if the source file is unknown. | ||
| std::string file_name_; | ||
| // The line in the source file where the test part took place, or -1 | ||
| // if the line number is unknown. | ||
| int line_number_; | ||
| std::string summary_; // The test failure summary. | ||
| std::string message_; // The test failure message. | ||
| }; | ||
|
|
||
| // Prints a TestPartResult object. | ||
| std::ostream& operator<<(std::ostream& os, const TestPartResult& result); | ||
|
|
||
| // An array of TestPartResult objects. | ||
| // | ||
| // Don't inherit from TestPartResultArray as its destructor is not | ||
| // virtual. | ||
| class GTEST_API_ TestPartResultArray { | ||
| public: | ||
| TestPartResultArray() {} | ||
|
|
||
| // Appends the given TestPartResult to the array. | ||
| void Append(const TestPartResult& result); | ||
|
|
||
| // Returns the TestPartResult at the given index (0-based). | ||
| const TestPartResult& GetTestPartResult(int index) const; | ||
|
|
||
| // Returns the number of TestPartResult objects in the array. | ||
| int size() const; | ||
|
|
||
| private: | ||
| std::vector<TestPartResult> array_; | ||
|
|
||
| GTEST_DISALLOW_COPY_AND_ASSIGN_(TestPartResultArray); | ||
| }; | ||
|
|
||
| // This interface knows how to report a test part result. | ||
| class TestPartResultReporterInterface { | ||
| public: | ||
| virtual ~TestPartResultReporterInterface() {} | ||
|
|
||
| virtual void ReportTestPartResult(const TestPartResult& result) = 0; | ||
| }; | ||
|
|
||
| namespace internal { | ||
|
|
||
| // This helper class is used by {ASSERT|EXPECT}_NO_FATAL_FAILURE to check if a | ||
| // statement generates new fatal failures. To do so it registers itself as the | ||
| // current test part result reporter. Besides checking if fatal failures were | ||
| // reported, it only delegates the reporting to the former result reporter. | ||
| // The original result reporter is restored in the destructor. | ||
| // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. | ||
| class GTEST_API_ HasNewFatalFailureHelper | ||
| : public TestPartResultReporterInterface { | ||
| public: | ||
| HasNewFatalFailureHelper(); | ||
| virtual ~HasNewFatalFailureHelper(); | ||
| virtual void ReportTestPartResult(const TestPartResult& result); | ||
| bool has_new_fatal_failure() const { return has_new_fatal_failure_; } | ||
| private: | ||
| bool has_new_fatal_failure_; | ||
| TestPartResultReporterInterface* original_reporter_; | ||
|
|
||
| GTEST_DISALLOW_COPY_AND_ASSIGN_(HasNewFatalFailureHelper); | ||
| }; | ||
|
|
||
| } // namespace internal | ||
|
|
||
| } // namespace testing | ||
|
|
||
| #endif // GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,259 @@ | ||
| // Copyright 2008 Google Inc. | ||
| // All Rights Reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| // | ||
| // Author: wan@google.com (Zhanyong Wan) | ||
|
|
||
| #ifndef GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_ | ||
| #define GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_ | ||
|
|
||
| // This header implements typed tests and type-parameterized tests. | ||
|
|
||
| // Typed (aka type-driven) tests repeat the same test for types in a | ||
| // list. You must know which types you want to test with when writing | ||
| // typed tests. Here's how you do it: | ||
|
|
||
| #if 0 | ||
|
|
||
| // First, define a fixture class template. It should be parameterized | ||
| // by a type. Remember to derive it from testing::Test. | ||
| template <typename T> | ||
| class FooTest : public testing::Test { | ||
| public: | ||
| ... | ||
| typedef std::list<T> List; | ||
| static T shared_; | ||
| T value_; | ||
| }; | ||
|
|
||
| // Next, associate a list of types with the test case, which will be | ||
| // repeated for each type in the list. The typedef is necessary for | ||
| // the macro to parse correctly. | ||
| typedef testing::Types<char, int, unsigned int> MyTypes; | ||
| TYPED_TEST_CASE(FooTest, MyTypes); | ||
|
|
||
| // If the type list contains only one type, you can write that type | ||
| // directly without Types<...>: | ||
| // TYPED_TEST_CASE(FooTest, int); | ||
|
|
||
| // Then, use TYPED_TEST() instead of TEST_F() to define as many typed | ||
| // tests for this test case as you want. | ||
| TYPED_TEST(FooTest, DoesBlah) { | ||
| // Inside a test, refer to TypeParam to get the type parameter. | ||
| // Since we are inside a derived class template, C++ requires use to | ||
| // visit the members of FooTest via 'this'. | ||
| TypeParam n = this->value_; | ||
|
|
||
| // To visit static members of the fixture, add the TestFixture:: | ||
| // prefix. | ||
| n += TestFixture::shared_; | ||
|
|
||
| // To refer to typedefs in the fixture, add the "typename | ||
| // TestFixture::" prefix. | ||
| typename TestFixture::List values; | ||
| values.push_back(n); | ||
| ... | ||
| } | ||
|
|
||
| TYPED_TEST(FooTest, HasPropertyA) { ... } | ||
|
|
||
| #endif // 0 | ||
|
|
||
| // Type-parameterized tests are abstract test patterns parameterized | ||
| // by a type. Compared with typed tests, type-parameterized tests | ||
| // allow you to define the test pattern without knowing what the type | ||
| // parameters are. The defined pattern can be instantiated with | ||
| // different types any number of times, in any number of translation | ||
| // units. | ||
| // | ||
| // If you are designing an interface or concept, you can define a | ||
| // suite of type-parameterized tests to verify properties that any | ||
| // valid implementation of the interface/concept should have. Then, | ||
| // each implementation can easily instantiate the test suite to verify | ||
| // that it conforms to the requirements, without having to write | ||
| // similar tests repeatedly. Here's an example: | ||
|
|
||
| #if 0 | ||
|
|
||
| // First, define a fixture class template. It should be parameterized | ||
| // by a type. Remember to derive it from testing::Test. | ||
| template <typename T> | ||
| class FooTest : public testing::Test { | ||
| ... | ||
| }; | ||
|
|
||
| // Next, declare that you will define a type-parameterized test case | ||
| // (the _P suffix is for "parameterized" or "pattern", whichever you | ||
| // prefer): | ||
| TYPED_TEST_CASE_P(FooTest); | ||
|
|
||
| // Then, use TYPED_TEST_P() to define as many type-parameterized tests | ||
| // for this type-parameterized test case as you want. | ||
| TYPED_TEST_P(FooTest, DoesBlah) { | ||
| // Inside a test, refer to TypeParam to get the type parameter. | ||
| TypeParam n = 0; | ||
| ... | ||
| } | ||
|
|
||
| TYPED_TEST_P(FooTest, HasPropertyA) { ... } | ||
|
|
||
| // Now the tricky part: you need to register all test patterns before | ||
| // you can instantiate them. The first argument of the macro is the | ||
| // test case name; the rest are the names of the tests in this test | ||
| // case. | ||
| REGISTER_TYPED_TEST_CASE_P(FooTest, | ||
| DoesBlah, HasPropertyA); | ||
|
|
||
| // Finally, you are free to instantiate the pattern with the types you | ||
| // want. If you put the above code in a header file, you can #include | ||
| // it in multiple C++ source files and instantiate it multiple times. | ||
| // | ||
| // To distinguish different instances of the pattern, the first | ||
| // argument to the INSTANTIATE_* macro is a prefix that will be added | ||
| // to the actual test case name. Remember to pick unique prefixes for | ||
| // different instances. | ||
| typedef testing::Types<char, int, unsigned int> MyTypes; | ||
| INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, MyTypes); | ||
|
|
||
| // If the type list contains only one type, you can write that type | ||
| // directly without Types<...>: | ||
| // INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, int); | ||
|
|
||
| #endif // 0 | ||
|
|
||
| #include "gtest/internal/gtest-port.h" | ||
| #include "gtest/internal/gtest-type-util.h" | ||
|
|
||
| // Implements typed tests. | ||
|
|
||
| #if GTEST_HAS_TYPED_TEST | ||
|
|
||
| // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. | ||
| // | ||
| // Expands to the name of the typedef for the type parameters of the | ||
| // given test case. | ||
| # define GTEST_TYPE_PARAMS_(TestCaseName) gtest_type_params_##TestCaseName##_ | ||
|
|
||
| // The 'Types' template argument below must have spaces around it | ||
| // since some compilers may choke on '>>' when passing a template | ||
| // instance (e.g. Types<int>) | ||
| # define TYPED_TEST_CASE(CaseName, Types) \ | ||
| typedef ::testing::internal::TypeList< Types >::type \ | ||
| GTEST_TYPE_PARAMS_(CaseName) | ||
|
|
||
| # define TYPED_TEST(CaseName, TestName) \ | ||
| template <typename gtest_TypeParam_> \ | ||
| class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \ | ||
| : public CaseName<gtest_TypeParam_> { \ | ||
| private: \ | ||
| typedef CaseName<gtest_TypeParam_> TestFixture; \ | ||
| typedef gtest_TypeParam_ TypeParam; \ | ||
| virtual void TestBody(); \ | ||
| }; \ | ||
| bool gtest_##CaseName##_##TestName##_registered_ GTEST_ATTRIBUTE_UNUSED_ = \ | ||
| ::testing::internal::TypeParameterizedTest< \ | ||
| CaseName, \ | ||
| ::testing::internal::TemplateSel< \ | ||
| GTEST_TEST_CLASS_NAME_(CaseName, TestName)>, \ | ||
| GTEST_TYPE_PARAMS_(CaseName)>::Register(\ | ||
| "", #CaseName, #TestName, 0); \ | ||
| template <typename gtest_TypeParam_> \ | ||
| void GTEST_TEST_CLASS_NAME_(CaseName, TestName)<gtest_TypeParam_>::TestBody() | ||
|
|
||
| #endif // GTEST_HAS_TYPED_TEST | ||
|
|
||
| // Implements type-parameterized tests. | ||
|
|
||
| #if GTEST_HAS_TYPED_TEST_P | ||
|
|
||
| // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. | ||
| // | ||
| // Expands to the namespace name that the type-parameterized tests for | ||
| // the given type-parameterized test case are defined in. The exact | ||
| // name of the namespace is subject to change without notice. | ||
| # define GTEST_CASE_NAMESPACE_(TestCaseName) \ | ||
| gtest_case_##TestCaseName##_ | ||
|
|
||
| // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. | ||
| // | ||
| // Expands to the name of the variable used to remember the names of | ||
| // the defined tests in the given test case. | ||
| # define GTEST_TYPED_TEST_CASE_P_STATE_(TestCaseName) \ | ||
| gtest_typed_test_case_p_state_##TestCaseName##_ | ||
|
|
||
| // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE DIRECTLY. | ||
| // | ||
| // Expands to the name of the variable used to remember the names of | ||
| // the registered tests in the given test case. | ||
| # define GTEST_REGISTERED_TEST_NAMES_(TestCaseName) \ | ||
| gtest_registered_test_names_##TestCaseName##_ | ||
|
|
||
| // The variables defined in the type-parameterized test macros are | ||
| // static as typically these macros are used in a .h file that can be | ||
| // #included in multiple translation units linked together. | ||
| # define TYPED_TEST_CASE_P(CaseName) \ | ||
| static ::testing::internal::TypedTestCasePState \ | ||
| GTEST_TYPED_TEST_CASE_P_STATE_(CaseName) | ||
|
|
||
| # define TYPED_TEST_P(CaseName, TestName) \ | ||
| namespace GTEST_CASE_NAMESPACE_(CaseName) { \ | ||
| template <typename gtest_TypeParam_> \ | ||
| class TestName : public CaseName<gtest_TypeParam_> { \ | ||
| private: \ | ||
| typedef CaseName<gtest_TypeParam_> TestFixture; \ | ||
| typedef gtest_TypeParam_ TypeParam; \ | ||
| virtual void TestBody(); \ | ||
| }; \ | ||
| static bool gtest_##TestName##_defined_ GTEST_ATTRIBUTE_UNUSED_ = \ | ||
| GTEST_TYPED_TEST_CASE_P_STATE_(CaseName).AddTestName(\ | ||
| __FILE__, __LINE__, #CaseName, #TestName); \ | ||
| } \ | ||
| template <typename gtest_TypeParam_> \ | ||
| void GTEST_CASE_NAMESPACE_(CaseName)::TestName<gtest_TypeParam_>::TestBody() | ||
|
|
||
| # define REGISTER_TYPED_TEST_CASE_P(CaseName, ...) \ | ||
| namespace GTEST_CASE_NAMESPACE_(CaseName) { \ | ||
| typedef ::testing::internal::Templates<__VA_ARGS__>::type gtest_AllTests_; \ | ||
| } \ | ||
| static const char* const GTEST_REGISTERED_TEST_NAMES_(CaseName) = \ | ||
| GTEST_TYPED_TEST_CASE_P_STATE_(CaseName).VerifyRegisteredTestNames(\ | ||
| __FILE__, __LINE__, #__VA_ARGS__) | ||
|
|
||
| // The 'Types' template argument below must have spaces around it | ||
| // since some compilers may choke on '>>' when passing a template | ||
| // instance (e.g. Types<int>) | ||
| # define INSTANTIATE_TYPED_TEST_CASE_P(Prefix, CaseName, Types) \ | ||
| bool gtest_##Prefix##_##CaseName GTEST_ATTRIBUTE_UNUSED_ = \ | ||
| ::testing::internal::TypeParameterizedTestCase<CaseName, \ | ||
| GTEST_CASE_NAMESPACE_(CaseName)::gtest_AllTests_, \ | ||
| ::testing::internal::TypeList< Types >::type>::Register(\ | ||
| #Prefix, #CaseName, GTEST_REGISTERED_TEST_NAMES_(CaseName)) | ||
|
|
||
| #endif // GTEST_HAS_TYPED_TEST_P | ||
|
|
||
| #endif // GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,358 @@ | ||
| // Copyright 2006, Google Inc. | ||
| // All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| // This file is AUTOMATICALLY GENERATED on 10/31/2011 by command | ||
| // 'gen_gtest_pred_impl.py 5'. DO NOT EDIT BY HAND! | ||
| // | ||
| // Implements a family of generic predicate assertion macros. | ||
|
|
||
| #ifndef GTEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_ | ||
| #define GTEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_ | ||
|
|
||
| // Makes sure this header is not included before gtest.h. | ||
| #ifndef GTEST_INCLUDE_GTEST_GTEST_H_ | ||
| # error Do not include gtest_pred_impl.h directly. Include gtest.h instead. | ||
| #endif // GTEST_INCLUDE_GTEST_GTEST_H_ | ||
|
|
||
| // This header implements a family of generic predicate assertion | ||
| // macros: | ||
| // | ||
| // ASSERT_PRED_FORMAT1(pred_format, v1) | ||
| // ASSERT_PRED_FORMAT2(pred_format, v1, v2) | ||
| // ... | ||
| // | ||
| // where pred_format is a function or functor that takes n (in the | ||
| // case of ASSERT_PRED_FORMATn) values and their source expression | ||
| // text, and returns a testing::AssertionResult. See the definition | ||
| // of ASSERT_EQ in gtest.h for an example. | ||
| // | ||
| // If you don't care about formatting, you can use the more | ||
| // restrictive version: | ||
| // | ||
| // ASSERT_PRED1(pred, v1) | ||
| // ASSERT_PRED2(pred, v1, v2) | ||
| // ... | ||
| // | ||
| // where pred is an n-ary function or functor that returns bool, | ||
| // and the values v1, v2, ..., must support the << operator for | ||
| // streaming to std::ostream. | ||
| // | ||
| // We also define the EXPECT_* variations. | ||
| // | ||
| // For now we only support predicates whose arity is at most 5. | ||
| // Please email googletestframework@googlegroups.com if you need | ||
| // support for higher arities. | ||
|
|
||
| // GTEST_ASSERT_ is the basic statement to which all of the assertions | ||
| // in this file reduce. Don't use this in your code. | ||
|
|
||
| #define GTEST_ASSERT_(expression, on_failure) \ | ||
| GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ | ||
| if (const ::testing::AssertionResult gtest_ar = (expression)) \ | ||
| ; \ | ||
| else \ | ||
| on_failure(gtest_ar.failure_message()) | ||
|
|
||
|
|
||
| // Helper function for implementing {EXPECT|ASSERT}_PRED1. Don't use | ||
| // this in your code. | ||
| template <typename Pred, | ||
| typename T1> | ||
| AssertionResult AssertPred1Helper(const char* pred_text, | ||
| const char* e1, | ||
| Pred pred, | ||
| const T1& v1) { | ||
| if (pred(v1)) return AssertionSuccess(); | ||
|
|
||
| return AssertionFailure() << pred_text << "(" | ||
| << e1 << ") evaluates to false, where" | ||
| << "\n" << e1 << " evaluates to " << v1; | ||
| } | ||
|
|
||
| // Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT1. | ||
| // Don't use this in your code. | ||
| #define GTEST_PRED_FORMAT1_(pred_format, v1, on_failure)\ | ||
| GTEST_ASSERT_(pred_format(#v1, v1), \ | ||
| on_failure) | ||
|
|
||
| // Internal macro for implementing {EXPECT|ASSERT}_PRED1. Don't use | ||
| // this in your code. | ||
| #define GTEST_PRED1_(pred, v1, on_failure)\ | ||
| GTEST_ASSERT_(::testing::AssertPred1Helper(#pred, \ | ||
| #v1, \ | ||
| pred, \ | ||
| v1), on_failure) | ||
|
|
||
| // Unary predicate assertion macros. | ||
| #define EXPECT_PRED_FORMAT1(pred_format, v1) \ | ||
| GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_NONFATAL_FAILURE_) | ||
| #define EXPECT_PRED1(pred, v1) \ | ||
| GTEST_PRED1_(pred, v1, GTEST_NONFATAL_FAILURE_) | ||
| #define ASSERT_PRED_FORMAT1(pred_format, v1) \ | ||
| GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_FATAL_FAILURE_) | ||
| #define ASSERT_PRED1(pred, v1) \ | ||
| GTEST_PRED1_(pred, v1, GTEST_FATAL_FAILURE_) | ||
|
|
||
|
|
||
|
|
||
| // Helper function for implementing {EXPECT|ASSERT}_PRED2. Don't use | ||
| // this in your code. | ||
| template <typename Pred, | ||
| typename T1, | ||
| typename T2> | ||
| AssertionResult AssertPred2Helper(const char* pred_text, | ||
| const char* e1, | ||
| const char* e2, | ||
| Pred pred, | ||
| const T1& v1, | ||
| const T2& v2) { | ||
| if (pred(v1, v2)) return AssertionSuccess(); | ||
|
|
||
| return AssertionFailure() << pred_text << "(" | ||
| << e1 << ", " | ||
| << e2 << ") evaluates to false, where" | ||
| << "\n" << e1 << " evaluates to " << v1 | ||
| << "\n" << e2 << " evaluates to " << v2; | ||
| } | ||
|
|
||
| // Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT2. | ||
| // Don't use this in your code. | ||
| #define GTEST_PRED_FORMAT2_(pred_format, v1, v2, on_failure)\ | ||
| GTEST_ASSERT_(pred_format(#v1, #v2, v1, v2), \ | ||
| on_failure) | ||
|
|
||
| // Internal macro for implementing {EXPECT|ASSERT}_PRED2. Don't use | ||
| // this in your code. | ||
| #define GTEST_PRED2_(pred, v1, v2, on_failure)\ | ||
| GTEST_ASSERT_(::testing::AssertPred2Helper(#pred, \ | ||
| #v1, \ | ||
| #v2, \ | ||
| pred, \ | ||
| v1, \ | ||
| v2), on_failure) | ||
|
|
||
| // Binary predicate assertion macros. | ||
| #define EXPECT_PRED_FORMAT2(pred_format, v1, v2) \ | ||
| GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_NONFATAL_FAILURE_) | ||
| #define EXPECT_PRED2(pred, v1, v2) \ | ||
| GTEST_PRED2_(pred, v1, v2, GTEST_NONFATAL_FAILURE_) | ||
| #define ASSERT_PRED_FORMAT2(pred_format, v1, v2) \ | ||
| GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_FATAL_FAILURE_) | ||
| #define ASSERT_PRED2(pred, v1, v2) \ | ||
| GTEST_PRED2_(pred, v1, v2, GTEST_FATAL_FAILURE_) | ||
|
|
||
|
|
||
|
|
||
| // Helper function for implementing {EXPECT|ASSERT}_PRED3. Don't use | ||
| // this in your code. | ||
| template <typename Pred, | ||
| typename T1, | ||
| typename T2, | ||
| typename T3> | ||
| AssertionResult AssertPred3Helper(const char* pred_text, | ||
| const char* e1, | ||
| const char* e2, | ||
| const char* e3, | ||
| Pred pred, | ||
| const T1& v1, | ||
| const T2& v2, | ||
| const T3& v3) { | ||
| if (pred(v1, v2, v3)) return AssertionSuccess(); | ||
|
|
||
| return AssertionFailure() << pred_text << "(" | ||
| << e1 << ", " | ||
| << e2 << ", " | ||
| << e3 << ") evaluates to false, where" | ||
| << "\n" << e1 << " evaluates to " << v1 | ||
| << "\n" << e2 << " evaluates to " << v2 | ||
| << "\n" << e3 << " evaluates to " << v3; | ||
| } | ||
|
|
||
| // Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT3. | ||
| // Don't use this in your code. | ||
| #define GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, on_failure)\ | ||
| GTEST_ASSERT_(pred_format(#v1, #v2, #v3, v1, v2, v3), \ | ||
| on_failure) | ||
|
|
||
| // Internal macro for implementing {EXPECT|ASSERT}_PRED3. Don't use | ||
| // this in your code. | ||
| #define GTEST_PRED3_(pred, v1, v2, v3, on_failure)\ | ||
| GTEST_ASSERT_(::testing::AssertPred3Helper(#pred, \ | ||
| #v1, \ | ||
| #v2, \ | ||
| #v3, \ | ||
| pred, \ | ||
| v1, \ | ||
| v2, \ | ||
| v3), on_failure) | ||
|
|
||
| // Ternary predicate assertion macros. | ||
| #define EXPECT_PRED_FORMAT3(pred_format, v1, v2, v3) \ | ||
| GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, GTEST_NONFATAL_FAILURE_) | ||
| #define EXPECT_PRED3(pred, v1, v2, v3) \ | ||
| GTEST_PRED3_(pred, v1, v2, v3, GTEST_NONFATAL_FAILURE_) | ||
| #define ASSERT_PRED_FORMAT3(pred_format, v1, v2, v3) \ | ||
| GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, GTEST_FATAL_FAILURE_) | ||
| #define ASSERT_PRED3(pred, v1, v2, v3) \ | ||
| GTEST_PRED3_(pred, v1, v2, v3, GTEST_FATAL_FAILURE_) | ||
|
|
||
|
|
||
|
|
||
| // Helper function for implementing {EXPECT|ASSERT}_PRED4. Don't use | ||
| // this in your code. | ||
| template <typename Pred, | ||
| typename T1, | ||
| typename T2, | ||
| typename T3, | ||
| typename T4> | ||
| AssertionResult AssertPred4Helper(const char* pred_text, | ||
| const char* e1, | ||
| const char* e2, | ||
| const char* e3, | ||
| const char* e4, | ||
| Pred pred, | ||
| const T1& v1, | ||
| const T2& v2, | ||
| const T3& v3, | ||
| const T4& v4) { | ||
| if (pred(v1, v2, v3, v4)) return AssertionSuccess(); | ||
|
|
||
| return AssertionFailure() << pred_text << "(" | ||
| << e1 << ", " | ||
| << e2 << ", " | ||
| << e3 << ", " | ||
| << e4 << ") evaluates to false, where" | ||
| << "\n" << e1 << " evaluates to " << v1 | ||
| << "\n" << e2 << " evaluates to " << v2 | ||
| << "\n" << e3 << " evaluates to " << v3 | ||
| << "\n" << e4 << " evaluates to " << v4; | ||
| } | ||
|
|
||
| // Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT4. | ||
| // Don't use this in your code. | ||
| #define GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, on_failure)\ | ||
| GTEST_ASSERT_(pred_format(#v1, #v2, #v3, #v4, v1, v2, v3, v4), \ | ||
| on_failure) | ||
|
|
||
| // Internal macro for implementing {EXPECT|ASSERT}_PRED4. Don't use | ||
| // this in your code. | ||
| #define GTEST_PRED4_(pred, v1, v2, v3, v4, on_failure)\ | ||
| GTEST_ASSERT_(::testing::AssertPred4Helper(#pred, \ | ||
| #v1, \ | ||
| #v2, \ | ||
| #v3, \ | ||
| #v4, \ | ||
| pred, \ | ||
| v1, \ | ||
| v2, \ | ||
| v3, \ | ||
| v4), on_failure) | ||
|
|
||
| // 4-ary predicate assertion macros. | ||
| #define EXPECT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \ | ||
| GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, GTEST_NONFATAL_FAILURE_) | ||
| #define EXPECT_PRED4(pred, v1, v2, v3, v4) \ | ||
| GTEST_PRED4_(pred, v1, v2, v3, v4, GTEST_NONFATAL_FAILURE_) | ||
| #define ASSERT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \ | ||
| GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, GTEST_FATAL_FAILURE_) | ||
| #define ASSERT_PRED4(pred, v1, v2, v3, v4) \ | ||
| GTEST_PRED4_(pred, v1, v2, v3, v4, GTEST_FATAL_FAILURE_) | ||
|
|
||
|
|
||
|
|
||
| // Helper function for implementing {EXPECT|ASSERT}_PRED5. Don't use | ||
| // this in your code. | ||
| template <typename Pred, | ||
| typename T1, | ||
| typename T2, | ||
| typename T3, | ||
| typename T4, | ||
| typename T5> | ||
| AssertionResult AssertPred5Helper(const char* pred_text, | ||
| const char* e1, | ||
| const char* e2, | ||
| const char* e3, | ||
| const char* e4, | ||
| const char* e5, | ||
| Pred pred, | ||
| const T1& v1, | ||
| const T2& v2, | ||
| const T3& v3, | ||
| const T4& v4, | ||
| const T5& v5) { | ||
| if (pred(v1, v2, v3, v4, v5)) return AssertionSuccess(); | ||
|
|
||
| return AssertionFailure() << pred_text << "(" | ||
| << e1 << ", " | ||
| << e2 << ", " | ||
| << e3 << ", " | ||
| << e4 << ", " | ||
| << e5 << ") evaluates to false, where" | ||
| << "\n" << e1 << " evaluates to " << v1 | ||
| << "\n" << e2 << " evaluates to " << v2 | ||
| << "\n" << e3 << " evaluates to " << v3 | ||
| << "\n" << e4 << " evaluates to " << v4 | ||
| << "\n" << e5 << " evaluates to " << v5; | ||
| } | ||
|
|
||
| // Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT5. | ||
| // Don't use this in your code. | ||
| #define GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, on_failure)\ | ||
| GTEST_ASSERT_(pred_format(#v1, #v2, #v3, #v4, #v5, v1, v2, v3, v4, v5), \ | ||
| on_failure) | ||
|
|
||
| // Internal macro for implementing {EXPECT|ASSERT}_PRED5. Don't use | ||
| // this in your code. | ||
| #define GTEST_PRED5_(pred, v1, v2, v3, v4, v5, on_failure)\ | ||
| GTEST_ASSERT_(::testing::AssertPred5Helper(#pred, \ | ||
| #v1, \ | ||
| #v2, \ | ||
| #v3, \ | ||
| #v4, \ | ||
| #v5, \ | ||
| pred, \ | ||
| v1, \ | ||
| v2, \ | ||
| v3, \ | ||
| v4, \ | ||
| v5), on_failure) | ||
|
|
||
| // 5-ary predicate assertion macros. | ||
| #define EXPECT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \ | ||
| GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, GTEST_NONFATAL_FAILURE_) | ||
| #define EXPECT_PRED5(pred, v1, v2, v3, v4, v5) \ | ||
| GTEST_PRED5_(pred, v1, v2, v3, v4, v5, GTEST_NONFATAL_FAILURE_) | ||
| #define ASSERT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \ | ||
| GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, GTEST_FATAL_FAILURE_) | ||
| #define ASSERT_PRED5(pred, v1, v2, v3, v4, v5) \ | ||
| GTEST_PRED5_(pred, v1, v2, v3, v4, v5, GTEST_FATAL_FAILURE_) | ||
|
|
||
|
|
||
|
|
||
| #endif // GTEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright 2006, Google Inc. | ||
| // All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| // | ||
| // Author: wan@google.com (Zhanyong Wan) | ||
| // | ||
| // Google C++ Testing Framework definitions useful in production code. | ||
|
|
||
| #ifndef GTEST_INCLUDE_GTEST_GTEST_PROD_H_ | ||
| #define GTEST_INCLUDE_GTEST_GTEST_PROD_H_ | ||
|
|
||
| // When you need to test the private or protected members of a class, | ||
| // use the FRIEND_TEST macro to declare your tests as friends of the | ||
| // class. For example: | ||
| // | ||
| // class MyClass { | ||
| // private: | ||
| // void MyMethod(); | ||
| // FRIEND_TEST(MyClassTest, MyMethod); | ||
| // }; | ||
| // | ||
| // class MyClassTest : public testing::Test { | ||
| // // ... | ||
| // }; | ||
| // | ||
| // TEST_F(MyClassTest, MyMethod) { | ||
| // // Can call MyClass::MyMethod() here. | ||
| // } | ||
|
|
||
| #define FRIEND_TEST(test_case_name, test_name)\ | ||
| friend class test_case_name##_##test_name##_Test | ||
|
|
||
| #endif // GTEST_INCLUDE_GTEST_GTEST_PROD_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,319 @@ | ||
| // Copyright 2005, Google Inc. | ||
| // All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| // | ||
| // Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee) | ||
| // | ||
| // The Google C++ Testing Framework (Google Test) | ||
| // | ||
| // This header file defines internal utilities needed for implementing | ||
| // death tests. They are subject to change without notice. | ||
|
|
||
| #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ | ||
| #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ | ||
|
|
||
| #include "gtest/internal/gtest-internal.h" | ||
|
|
||
| #include <stdio.h> | ||
|
|
||
| namespace testing { | ||
| namespace internal { | ||
|
|
||
| GTEST_DECLARE_string_(internal_run_death_test); | ||
|
|
||
| // Names of the flags (needed for parsing Google Test flags). | ||
| const char kDeathTestStyleFlag[] = "death_test_style"; | ||
| const char kDeathTestUseFork[] = "death_test_use_fork"; | ||
| const char kInternalRunDeathTestFlag[] = "internal_run_death_test"; | ||
|
|
||
| #if GTEST_HAS_DEATH_TEST | ||
|
|
||
| // DeathTest is a class that hides much of the complexity of the | ||
| // GTEST_DEATH_TEST_ macro. It is abstract; its static Create method | ||
| // returns a concrete class that depends on the prevailing death test | ||
| // style, as defined by the --gtest_death_test_style and/or | ||
| // --gtest_internal_run_death_test flags. | ||
|
|
||
| // In describing the results of death tests, these terms are used with | ||
| // the corresponding definitions: | ||
| // | ||
| // exit status: The integer exit information in the format specified | ||
| // by wait(2) | ||
| // exit code: The integer code passed to exit(3), _exit(2), or | ||
| // returned from main() | ||
| class GTEST_API_ DeathTest { | ||
| public: | ||
| // Create returns false if there was an error determining the | ||
| // appropriate action to take for the current death test; for example, | ||
| // if the gtest_death_test_style flag is set to an invalid value. | ||
| // The LastMessage method will return a more detailed message in that | ||
| // case. Otherwise, the DeathTest pointer pointed to by the "test" | ||
| // argument is set. If the death test should be skipped, the pointer | ||
| // is set to NULL; otherwise, it is set to the address of a new concrete | ||
| // DeathTest object that controls the execution of the current test. | ||
| static bool Create(const char* statement, const RE* regex, | ||
| const char* file, int line, DeathTest** test); | ||
| DeathTest(); | ||
| virtual ~DeathTest() { } | ||
|
|
||
| // A helper class that aborts a death test when it's deleted. | ||
| class ReturnSentinel { | ||
| public: | ||
| explicit ReturnSentinel(DeathTest* test) : test_(test) { } | ||
| ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); } | ||
| private: | ||
| DeathTest* const test_; | ||
| GTEST_DISALLOW_COPY_AND_ASSIGN_(ReturnSentinel); | ||
| } GTEST_ATTRIBUTE_UNUSED_; | ||
|
|
||
| // An enumeration of possible roles that may be taken when a death | ||
| // test is encountered. EXECUTE means that the death test logic should | ||
| // be executed immediately. OVERSEE means that the program should prepare | ||
| // the appropriate environment for a child process to execute the death | ||
| // test, then wait for it to complete. | ||
| enum TestRole { OVERSEE_TEST, EXECUTE_TEST }; | ||
|
|
||
| // An enumeration of the three reasons that a test might be aborted. | ||
| enum AbortReason { | ||
| TEST_ENCOUNTERED_RETURN_STATEMENT, | ||
| TEST_THREW_EXCEPTION, | ||
| TEST_DID_NOT_DIE | ||
| }; | ||
|
|
||
| // Assumes one of the above roles. | ||
| virtual TestRole AssumeRole() = 0; | ||
|
|
||
| // Waits for the death test to finish and returns its status. | ||
| virtual int Wait() = 0; | ||
|
|
||
| // Returns true if the death test passed; that is, the test process | ||
| // exited during the test, its exit status matches a user-supplied | ||
| // predicate, and its stderr output matches a user-supplied regular | ||
| // expression. | ||
| // The user-supplied predicate may be a macro expression rather | ||
| // than a function pointer or functor, or else Wait and Passed could | ||
| // be combined. | ||
| virtual bool Passed(bool exit_status_ok) = 0; | ||
|
|
||
| // Signals that the death test did not die as expected. | ||
| virtual void Abort(AbortReason reason) = 0; | ||
|
|
||
| // Returns a human-readable outcome message regarding the outcome of | ||
| // the last death test. | ||
| static const char* LastMessage(); | ||
|
|
||
| static void set_last_death_test_message(const std::string& message); | ||
|
|
||
| private: | ||
| // A string containing a description of the outcome of the last death test. | ||
| static std::string last_death_test_message_; | ||
|
|
||
| GTEST_DISALLOW_COPY_AND_ASSIGN_(DeathTest); | ||
| }; | ||
|
|
||
| // Factory interface for death tests. May be mocked out for testing. | ||
| class DeathTestFactory { | ||
| public: | ||
| virtual ~DeathTestFactory() { } | ||
| virtual bool Create(const char* statement, const RE* regex, | ||
| const char* file, int line, DeathTest** test) = 0; | ||
| }; | ||
|
|
||
| // A concrete DeathTestFactory implementation for normal use. | ||
| class DefaultDeathTestFactory : public DeathTestFactory { | ||
| public: | ||
| virtual bool Create(const char* statement, const RE* regex, | ||
| const char* file, int line, DeathTest** test); | ||
| }; | ||
|
|
||
| // Returns true if exit_status describes a process that was terminated | ||
| // by a signal, or exited normally with a nonzero exit code. | ||
| GTEST_API_ bool ExitedUnsuccessfully(int exit_status); | ||
|
|
||
| // Traps C++ exceptions escaping statement and reports them as test | ||
| // failures. Note that trapping SEH exceptions is not implemented here. | ||
| # if GTEST_HAS_EXCEPTIONS | ||
| # define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \ | ||
| try { \ | ||
| GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ | ||
| } catch (const ::std::exception& gtest_exception) { \ | ||
| fprintf(\ | ||
| stderr, \ | ||
| "\n%s: Caught std::exception-derived exception escaping the " \ | ||
| "death test statement. Exception message: %s\n", \ | ||
| ::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \ | ||
| gtest_exception.what()); \ | ||
| fflush(stderr); \ | ||
| death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \ | ||
| } catch (...) { \ | ||
| death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \ | ||
| } | ||
|
|
||
| # else | ||
| # define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \ | ||
| GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) | ||
|
|
||
| # endif | ||
|
|
||
| // This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*, | ||
| // ASSERT_EXIT*, and EXPECT_EXIT*. | ||
| # define GTEST_DEATH_TEST_(statement, predicate, regex, fail) \ | ||
| GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ | ||
| if (::testing::internal::AlwaysTrue()) { \ | ||
| const ::testing::internal::RE& gtest_regex = (regex); \ | ||
| ::testing::internal::DeathTest* gtest_dt; \ | ||
| if (!::testing::internal::DeathTest::Create(#statement, >est_regex, \ | ||
| __FILE__, __LINE__, >est_dt)) { \ | ||
| goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \ | ||
| } \ | ||
| if (gtest_dt != NULL) { \ | ||
| ::testing::internal::scoped_ptr< ::testing::internal::DeathTest> \ | ||
| gtest_dt_ptr(gtest_dt); \ | ||
| switch (gtest_dt->AssumeRole()) { \ | ||
| case ::testing::internal::DeathTest::OVERSEE_TEST: \ | ||
| if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \ | ||
| goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \ | ||
| } \ | ||
| break; \ | ||
| case ::testing::internal::DeathTest::EXECUTE_TEST: { \ | ||
| ::testing::internal::DeathTest::ReturnSentinel \ | ||
| gtest_sentinel(gtest_dt); \ | ||
| GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \ | ||
| gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \ | ||
| break; \ | ||
| } \ | ||
| default: \ | ||
| break; \ | ||
| } \ | ||
| } \ | ||
| } else \ | ||
| GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__): \ | ||
| fail(::testing::internal::DeathTest::LastMessage()) | ||
| // The symbol "fail" here expands to something into which a message | ||
| // can be streamed. | ||
|
|
||
| // This macro is for implementing ASSERT/EXPECT_DEBUG_DEATH when compiled in | ||
| // NDEBUG mode. In this case we need the statements to be executed, the regex is | ||
| // ignored, and the macro must accept a streamed message even though the message | ||
| // is never printed. | ||
| # define GTEST_EXECUTE_STATEMENT_(statement, regex) \ | ||
| GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ | ||
| if (::testing::internal::AlwaysTrue()) { \ | ||
| GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ | ||
| } else \ | ||
| ::testing::Message() | ||
|
|
||
| // A class representing the parsed contents of the | ||
| // --gtest_internal_run_death_test flag, as it existed when | ||
| // RUN_ALL_TESTS was called. | ||
| class InternalRunDeathTestFlag { | ||
| public: | ||
| InternalRunDeathTestFlag(const std::string& a_file, | ||
| int a_line, | ||
| int an_index, | ||
| int a_write_fd) | ||
| : file_(a_file), line_(a_line), index_(an_index), | ||
| write_fd_(a_write_fd) {} | ||
|
|
||
| ~InternalRunDeathTestFlag() { | ||
| if (write_fd_ >= 0) | ||
| posix::Close(write_fd_); | ||
| } | ||
|
|
||
| const std::string& file() const { return file_; } | ||
| int line() const { return line_; } | ||
| int index() const { return index_; } | ||
| int write_fd() const { return write_fd_; } | ||
|
|
||
| private: | ||
| std::string file_; | ||
| int line_; | ||
| int index_; | ||
| int write_fd_; | ||
|
|
||
| GTEST_DISALLOW_COPY_AND_ASSIGN_(InternalRunDeathTestFlag); | ||
| }; | ||
|
|
||
| // Returns a newly created InternalRunDeathTestFlag object with fields | ||
| // initialized from the GTEST_FLAG(internal_run_death_test) flag if | ||
| // the flag is specified; otherwise returns NULL. | ||
| InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag(); | ||
|
|
||
| #else // GTEST_HAS_DEATH_TEST | ||
|
|
||
| // This macro is used for implementing macros such as | ||
| // EXPECT_DEATH_IF_SUPPORTED and ASSERT_DEATH_IF_SUPPORTED on systems where | ||
| // death tests are not supported. Those macros must compile on such systems | ||
| // iff EXPECT_DEATH and ASSERT_DEATH compile with the same parameters on | ||
| // systems that support death tests. This allows one to write such a macro | ||
| // on a system that does not support death tests and be sure that it will | ||
| // compile on a death-test supporting system. | ||
| // | ||
| // Parameters: | ||
| // statement - A statement that a macro such as EXPECT_DEATH would test | ||
| // for program termination. This macro has to make sure this | ||
| // statement is compiled but not executed, to ensure that | ||
| // EXPECT_DEATH_IF_SUPPORTED compiles with a certain | ||
| // parameter iff EXPECT_DEATH compiles with it. | ||
| // regex - A regex that a macro such as EXPECT_DEATH would use to test | ||
| // the output of statement. This parameter has to be | ||
| // compiled but not evaluated by this macro, to ensure that | ||
| // this macro only accepts expressions that a macro such as | ||
| // EXPECT_DEATH would accept. | ||
| // terminator - Must be an empty statement for EXPECT_DEATH_IF_SUPPORTED | ||
| // and a return statement for ASSERT_DEATH_IF_SUPPORTED. | ||
| // This ensures that ASSERT_DEATH_IF_SUPPORTED will not | ||
| // compile inside functions where ASSERT_DEATH doesn't | ||
| // compile. | ||
| // | ||
| // The branch that has an always false condition is used to ensure that | ||
| // statement and regex are compiled (and thus syntactically correct) but | ||
| // never executed. The unreachable code macro protects the terminator | ||
| // statement from generating an 'unreachable code' warning in case | ||
| // statement unconditionally returns or throws. The Message constructor at | ||
| // the end allows the syntax of streaming additional messages into the | ||
| // macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH. | ||
| # define GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, terminator) \ | ||
| GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ | ||
| if (::testing::internal::AlwaysTrue()) { \ | ||
| GTEST_LOG_(WARNING) \ | ||
| << "Death tests are not supported on this platform.\n" \ | ||
| << "Statement '" #statement "' cannot be verified."; \ | ||
| } else if (::testing::internal::AlwaysFalse()) { \ | ||
| ::testing::internal::RE::PartialMatch(".*", (regex)); \ | ||
| GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ | ||
| terminator; \ | ||
| } else \ | ||
| ::testing::Message() | ||
|
|
||
| #endif // GTEST_HAS_DEATH_TEST | ||
|
|
||
| } // namespace internal | ||
| } // namespace testing | ||
|
|
||
| #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| // Copyright 2008, Google Inc. | ||
| // All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| // | ||
| // Author: keith.ray@gmail.com (Keith Ray) | ||
| // | ||
| // Google Test filepath utilities | ||
| // | ||
| // This header file declares classes and functions used internally by | ||
| // Google Test. They are subject to change without notice. | ||
| // | ||
| // This file is #included in <gtest/internal/gtest-internal.h>. | ||
| // Do not include this header file separately! | ||
|
|
||
| #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ | ||
| #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ | ||
|
|
||
| #include "gtest/internal/gtest-string.h" | ||
|
|
||
| namespace testing { | ||
| namespace internal { | ||
|
|
||
| // FilePath - a class for file and directory pathname manipulation which | ||
| // handles platform-specific conventions (like the pathname separator). | ||
| // Used for helper functions for naming files in a directory for xml output. | ||
| // Except for Set methods, all methods are const or static, which provides an | ||
| // "immutable value object" -- useful for peace of mind. | ||
| // A FilePath with a value ending in a path separator ("like/this/") represents | ||
| // a directory, otherwise it is assumed to represent a file. In either case, | ||
| // it may or may not represent an actual file or directory in the file system. | ||
| // Names are NOT checked for syntax correctness -- no checking for illegal | ||
| // characters, malformed paths, etc. | ||
|
|
||
| class GTEST_API_ FilePath { | ||
| public: | ||
| FilePath() : pathname_("") { } | ||
| FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) { } | ||
|
|
||
| explicit FilePath(const std::string& pathname) : pathname_(pathname) { | ||
| Normalize(); | ||
| } | ||
|
|
||
| FilePath& operator=(const FilePath& rhs) { | ||
| Set(rhs); | ||
| return *this; | ||
| } | ||
|
|
||
| void Set(const FilePath& rhs) { | ||
| pathname_ = rhs.pathname_; | ||
| } | ||
|
|
||
| const std::string& string() const { return pathname_; } | ||
| const char* c_str() const { return pathname_.c_str(); } | ||
|
|
||
| // Returns the current working directory, or "" if unsuccessful. | ||
| static FilePath GetCurrentDir(); | ||
|
|
||
| // Given directory = "dir", base_name = "test", number = 0, | ||
| // extension = "xml", returns "dir/test.xml". If number is greater | ||
| // than zero (e.g., 12), returns "dir/test_12.xml". | ||
| // On Windows platform, uses \ as the separator rather than /. | ||
| static FilePath MakeFileName(const FilePath& directory, | ||
| const FilePath& base_name, | ||
| int number, | ||
| const char* extension); | ||
|
|
||
| // Given directory = "dir", relative_path = "test.xml", | ||
| // returns "dir/test.xml". | ||
| // On Windows, uses \ as the separator rather than /. | ||
| static FilePath ConcatPaths(const FilePath& directory, | ||
| const FilePath& relative_path); | ||
|
|
||
| // Returns a pathname for a file that does not currently exist. The pathname | ||
| // will be directory/base_name.extension or | ||
| // directory/base_name_<number>.extension if directory/base_name.extension | ||
| // already exists. The number will be incremented until a pathname is found | ||
| // that does not already exist. | ||
| // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'. | ||
| // There could be a race condition if two or more processes are calling this | ||
| // function at the same time -- they could both pick the same filename. | ||
| static FilePath GenerateUniqueFileName(const FilePath& directory, | ||
| const FilePath& base_name, | ||
| const char* extension); | ||
|
|
||
| // Returns true iff the path is "". | ||
| bool IsEmpty() const { return pathname_.empty(); } | ||
|
|
||
| // If input name has a trailing separator character, removes it and returns | ||
| // the name, otherwise return the name string unmodified. | ||
| // On Windows platform, uses \ as the separator, other platforms use /. | ||
| FilePath RemoveTrailingPathSeparator() const; | ||
|
|
||
| // Returns a copy of the FilePath with the directory part removed. | ||
| // Example: FilePath("path/to/file").RemoveDirectoryName() returns | ||
| // FilePath("file"). If there is no directory part ("just_a_file"), it returns | ||
| // the FilePath unmodified. If there is no file part ("just_a_dir/") it | ||
| // returns an empty FilePath (""). | ||
| // On Windows platform, '\' is the path separator, otherwise it is '/'. | ||
| FilePath RemoveDirectoryName() const; | ||
|
|
||
| // RemoveFileName returns the directory path with the filename removed. | ||
| // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/". | ||
| // If the FilePath is "a_file" or "/a_file", RemoveFileName returns | ||
| // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does | ||
| // not have a file, like "just/a/dir/", it returns the FilePath unmodified. | ||
| // On Windows platform, '\' is the path separator, otherwise it is '/'. | ||
| FilePath RemoveFileName() const; | ||
|
|
||
| // Returns a copy of the FilePath with the case-insensitive extension removed. | ||
| // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns | ||
| // FilePath("dir/file"). If a case-insensitive extension is not | ||
| // found, returns a copy of the original FilePath. | ||
| FilePath RemoveExtension(const char* extension) const; | ||
|
|
||
| // Creates directories so that path exists. Returns true if successful or if | ||
| // the directories already exist; returns false if unable to create | ||
| // directories for any reason. Will also return false if the FilePath does | ||
| // not represent a directory (that is, it doesn't end with a path separator). | ||
| bool CreateDirectoriesRecursively() const; | ||
|
|
||
| // Create the directory so that path exists. Returns true if successful or | ||
| // if the directory already exists; returns false if unable to create the | ||
| // directory for any reason, including if the parent directory does not | ||
| // exist. Not named "CreateDirectory" because that's a macro on Windows. | ||
| bool CreateFolder() const; | ||
|
|
||
| // Returns true if FilePath describes something in the file-system, | ||
| // either a file, directory, or whatever, and that something exists. | ||
| bool FileOrDirectoryExists() const; | ||
|
|
||
| // Returns true if pathname describes a directory in the file-system | ||
| // that exists. | ||
| bool DirectoryExists() const; | ||
|
|
||
| // Returns true if FilePath ends with a path separator, which indicates that | ||
| // it is intended to represent a directory. Returns false otherwise. | ||
| // This does NOT check that a directory (or file) actually exists. | ||
| bool IsDirectory() const; | ||
|
|
||
| // Returns true if pathname describes a root directory. (Windows has one | ||
| // root directory per disk drive.) | ||
| bool IsRootDirectory() const; | ||
|
|
||
| // Returns true if pathname describes an absolute path. | ||
| bool IsAbsolutePath() const; | ||
|
|
||
| private: | ||
| // Replaces multiple consecutive separators with a single separator. | ||
| // For example, "bar///foo" becomes "bar/foo". Does not eliminate other | ||
| // redundancies that might be in a pathname involving "." or "..". | ||
| // | ||
| // A pathname with multiple consecutive separators may occur either through | ||
| // user error or as a result of some scripts or APIs that generate a pathname | ||
| // with a trailing separator. On other platforms the same API or script | ||
| // may NOT generate a pathname with a trailing "/". Then elsewhere that | ||
| // pathname may have another "/" and pathname components added to it, | ||
| // without checking for the separator already being there. | ||
| // The script language and operating system may allow paths like "foo//bar" | ||
| // but some of the functions in FilePath will not handle that correctly. In | ||
| // particular, RemoveTrailingPathSeparator() only removes one separator, and | ||
| // it is called in CreateDirectoriesRecursively() assuming that it will change | ||
| // a pathname from directory syntax (trailing separator) to filename syntax. | ||
| // | ||
| // On Windows this method also replaces the alternate path separator '/' with | ||
| // the primary path separator '\\', so that for example "bar\\/\\foo" becomes | ||
| // "bar\\foo". | ||
|
|
||
| void Normalize(); | ||
|
|
||
| // Returns a pointer to the last occurence of a valid path separator in | ||
| // the FilePath. On Windows, for example, both '/' and '\' are valid path | ||
| // separators. Returns NULL if no path separator was found. | ||
| const char* FindLastPathSeparator() const; | ||
|
|
||
| std::string pathname_; | ||
| }; // class FilePath | ||
|
|
||
| } // namespace internal | ||
| } // namespace testing | ||
|
|
||
| #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| // Copyright 2003 Google Inc. | ||
| // All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| // | ||
| // Authors: Dan Egnor (egnor@google.com) | ||
| // | ||
| // A "smart" pointer type with reference tracking. Every pointer to a | ||
| // particular object is kept on a circular linked list. When the last pointer | ||
| // to an object is destroyed or reassigned, the object is deleted. | ||
| // | ||
| // Used properly, this deletes the object when the last reference goes away. | ||
| // There are several caveats: | ||
| // - Like all reference counting schemes, cycles lead to leaks. | ||
| // - Each smart pointer is actually two pointers (8 bytes instead of 4). | ||
| // - Every time a pointer is assigned, the entire list of pointers to that | ||
| // object is traversed. This class is therefore NOT SUITABLE when there | ||
| // will often be more than two or three pointers to a particular object. | ||
| // - References are only tracked as long as linked_ptr<> objects are copied. | ||
| // If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS | ||
| // will happen (double deletion). | ||
| // | ||
| // A good use of this class is storing object references in STL containers. | ||
| // You can safely put linked_ptr<> in a vector<>. | ||
| // Other uses may not be as good. | ||
| // | ||
| // Note: If you use an incomplete type with linked_ptr<>, the class | ||
| // *containing* linked_ptr<> must have a constructor and destructor (even | ||
| // if they do nothing!). | ||
| // | ||
| // Bill Gibbons suggested we use something like this. | ||
| // | ||
| // Thread Safety: | ||
| // Unlike other linked_ptr implementations, in this implementation | ||
| // a linked_ptr object is thread-safe in the sense that: | ||
| // - it's safe to copy linked_ptr objects concurrently, | ||
| // - it's safe to copy *from* a linked_ptr and read its underlying | ||
| // raw pointer (e.g. via get()) concurrently, and | ||
| // - it's safe to write to two linked_ptrs that point to the same | ||
| // shared object concurrently. | ||
| // TODO(wan@google.com): rename this to safe_linked_ptr to avoid | ||
| // confusion with normal linked_ptr. | ||
|
|
||
| #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_ | ||
| #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_ | ||
|
|
||
| #include <stdlib.h> | ||
| #include <assert.h> | ||
|
|
||
| #include "gtest/internal/gtest-port.h" | ||
|
|
||
| namespace testing { | ||
| namespace internal { | ||
|
|
||
| // Protects copying of all linked_ptr objects. | ||
| GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_linked_ptr_mutex); | ||
|
|
||
| // This is used internally by all instances of linked_ptr<>. It needs to be | ||
| // a non-template class because different types of linked_ptr<> can refer to | ||
| // the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)). | ||
| // So, it needs to be possible for different types of linked_ptr to participate | ||
| // in the same circular linked list, so we need a single class type here. | ||
| // | ||
| // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>. | ||
| class linked_ptr_internal { | ||
| public: | ||
| // Create a new circle that includes only this instance. | ||
| void join_new() { | ||
| next_ = this; | ||
| } | ||
|
|
||
| // Many linked_ptr operations may change p.link_ for some linked_ptr | ||
| // variable p in the same circle as this object. Therefore we need | ||
| // to prevent two such operations from occurring concurrently. | ||
| // | ||
| // Note that different types of linked_ptr objects can coexist in a | ||
| // circle (e.g. linked_ptr<Base>, linked_ptr<Derived1>, and | ||
| // linked_ptr<Derived2>). Therefore we must use a single mutex to | ||
| // protect all linked_ptr objects. This can create serious | ||
| // contention in production code, but is acceptable in a testing | ||
| // framework. | ||
|
|
||
| // Join an existing circle. | ||
| void join(linked_ptr_internal const* ptr) | ||
| GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) { | ||
| MutexLock lock(&g_linked_ptr_mutex); | ||
|
|
||
| linked_ptr_internal const* p = ptr; | ||
| while (p->next_ != ptr) p = p->next_; | ||
| p->next_ = this; | ||
| next_ = ptr; | ||
| } | ||
|
|
||
| // Leave whatever circle we're part of. Returns true if we were the | ||
| // last member of the circle. Once this is done, you can join() another. | ||
| bool depart() | ||
| GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex) { | ||
| MutexLock lock(&g_linked_ptr_mutex); | ||
|
|
||
| if (next_ == this) return true; | ||
| linked_ptr_internal const* p = next_; | ||
| while (p->next_ != this) p = p->next_; | ||
| p->next_ = next_; | ||
| return false; | ||
| } | ||
|
|
||
| private: | ||
| mutable linked_ptr_internal const* next_; | ||
| }; | ||
|
|
||
| template <typename T> | ||
| class linked_ptr { | ||
| public: | ||
| typedef T element_type; | ||
|
|
||
| // Take over ownership of a raw pointer. This should happen as soon as | ||
| // possible after the object is created. | ||
| explicit linked_ptr(T* ptr = NULL) { capture(ptr); } | ||
| ~linked_ptr() { depart(); } | ||
|
|
||
| // Copy an existing linked_ptr<>, adding ourselves to the list of references. | ||
| template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); } | ||
| linked_ptr(linked_ptr const& ptr) { // NOLINT | ||
| assert(&ptr != this); | ||
| copy(&ptr); | ||
| } | ||
|
|
||
| // Assignment releases the old value and acquires the new. | ||
| template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) { | ||
| depart(); | ||
| copy(&ptr); | ||
| return *this; | ||
| } | ||
|
|
||
| linked_ptr& operator=(linked_ptr const& ptr) { | ||
| if (&ptr != this) { | ||
| depart(); | ||
| copy(&ptr); | ||
| } | ||
| return *this; | ||
| } | ||
|
|
||
| // Smart pointer members. | ||
| void reset(T* ptr = NULL) { | ||
| depart(); | ||
| capture(ptr); | ||
| } | ||
| T* get() const { return value_; } | ||
| T* operator->() const { return value_; } | ||
| T& operator*() const { return *value_; } | ||
|
|
||
| bool operator==(T* p) const { return value_ == p; } | ||
| bool operator!=(T* p) const { return value_ != p; } | ||
| template <typename U> | ||
| bool operator==(linked_ptr<U> const& ptr) const { | ||
| return value_ == ptr.get(); | ||
| } | ||
| template <typename U> | ||
| bool operator!=(linked_ptr<U> const& ptr) const { | ||
| return value_ != ptr.get(); | ||
| } | ||
|
|
||
| private: | ||
| template <typename U> | ||
| friend class linked_ptr; | ||
|
|
||
| T* value_; | ||
| linked_ptr_internal link_; | ||
|
|
||
| void depart() { | ||
| if (link_.depart()) delete value_; | ||
| } | ||
|
|
||
| void capture(T* ptr) { | ||
| value_ = ptr; | ||
| link_.join_new(); | ||
| } | ||
|
|
||
| template <typename U> void copy(linked_ptr<U> const* ptr) { | ||
| value_ = ptr->get(); | ||
| if (value_) | ||
| link_.join(&ptr->link_); | ||
| else | ||
| link_.join_new(); | ||
| } | ||
| }; | ||
|
|
||
| template<typename T> inline | ||
| bool operator==(T* ptr, const linked_ptr<T>& x) { | ||
| return ptr == x.get(); | ||
| } | ||
|
|
||
| template<typename T> inline | ||
| bool operator!=(T* ptr, const linked_ptr<T>& x) { | ||
| return ptr != x.get(); | ||
| } | ||
|
|
||
| // A function to convert T* into linked_ptr<T> | ||
| // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation | ||
| // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | ||
| template <typename T> | ||
| linked_ptr<T> make_linked_ptr(T* ptr) { | ||
| return linked_ptr<T>(ptr); | ||
| } | ||
|
|
||
| } // namespace internal | ||
| } // namespace testing | ||
|
|
||
| #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,301 @@ | ||
| $$ -*- mode: c++; -*- | ||
| $var n = 50 $$ Maximum length of Values arguments we want to support. | ||
| $var maxtuple = 10 $$ Maximum number of Combine arguments we want to support. | ||
| // Copyright 2008 Google Inc. | ||
| // All Rights Reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| // | ||
| // Author: vladl@google.com (Vlad Losev) | ||
|
|
||
| // Type and function utilities for implementing parameterized tests. | ||
| // This file is generated by a SCRIPT. DO NOT EDIT BY HAND! | ||
| // | ||
| // Currently Google Test supports at most $n arguments in Values, | ||
| // and at most $maxtuple arguments in Combine. Please contact | ||
| // googletestframework@googlegroups.com if you need more. | ||
| // Please note that the number of arguments to Combine is limited | ||
| // by the maximum arity of the implementation of tr1::tuple which is | ||
| // currently set at $maxtuple. | ||
|
|
||
| #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_GENERATED_H_ | ||
| #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_GENERATED_H_ | ||
|
|
||
| // scripts/fuse_gtest.py depends on gtest's own header being #included | ||
| // *unconditionally*. Therefore these #includes cannot be moved | ||
| // inside #if GTEST_HAS_PARAM_TEST. | ||
| #include "gtest/internal/gtest-param-util.h" | ||
| #include "gtest/internal/gtest-port.h" | ||
|
|
||
| #if GTEST_HAS_PARAM_TEST | ||
|
|
||
| namespace testing { | ||
|
|
||
| // Forward declarations of ValuesIn(), which is implemented in | ||
| // include/gtest/gtest-param-test.h. | ||
| template <typename ForwardIterator> | ||
| internal::ParamGenerator< | ||
| typename ::testing::internal::IteratorTraits<ForwardIterator>::value_type> | ||
| ValuesIn(ForwardIterator begin, ForwardIterator end); | ||
|
|
||
| template <typename T, size_t N> | ||
| internal::ParamGenerator<T> ValuesIn(const T (&array)[N]); | ||
|
|
||
| template <class Container> | ||
| internal::ParamGenerator<typename Container::value_type> ValuesIn( | ||
| const Container& container); | ||
|
|
||
| namespace internal { | ||
|
|
||
| // Used in the Values() function to provide polymorphic capabilities. | ||
| template <typename T1> | ||
| class ValueArray1 { | ||
| public: | ||
| explicit ValueArray1(T1 v1) : v1_(v1) {} | ||
|
|
||
| template <typename T> | ||
| operator ParamGenerator<T>() const { return ValuesIn(&v1_, &v1_ + 1); } | ||
|
|
||
| private: | ||
| // No implementation - assignment is unsupported. | ||
| void operator=(const ValueArray1& other); | ||
|
|
||
| const T1 v1_; | ||
| }; | ||
|
|
||
| $range i 2..n | ||
| $for i [[ | ||
| $range j 1..i | ||
|
|
||
| template <$for j, [[typename T$j]]> | ||
| class ValueArray$i { | ||
| public: | ||
| ValueArray$i($for j, [[T$j v$j]]) : $for j, [[v$(j)_(v$j)]] {} | ||
|
|
||
| template <typename T> | ||
| operator ParamGenerator<T>() const { | ||
| const T array[] = {$for j, [[static_cast<T>(v$(j)_)]]}; | ||
| return ValuesIn(array); | ||
| } | ||
|
|
||
| private: | ||
| // No implementation - assignment is unsupported. | ||
| void operator=(const ValueArray$i& other); | ||
|
|
||
| $for j [[ | ||
|
|
||
| const T$j v$(j)_; | ||
| ]] | ||
|
|
||
| }; | ||
|
|
||
| ]] | ||
|
|
||
| # if GTEST_HAS_COMBINE | ||
| // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. | ||
| // | ||
| // Generates values from the Cartesian product of values produced | ||
| // by the argument generators. | ||
| // | ||
| $range i 2..maxtuple | ||
| $for i [[ | ||
| $range j 1..i | ||
| $range k 2..i | ||
|
|
||
| template <$for j, [[typename T$j]]> | ||
| class CartesianProductGenerator$i | ||
| : public ParamGeneratorInterface< ::std::tr1::tuple<$for j, [[T$j]]> > { | ||
| public: | ||
| typedef ::std::tr1::tuple<$for j, [[T$j]]> ParamType; | ||
|
|
||
| CartesianProductGenerator$i($for j, [[const ParamGenerator<T$j>& g$j]]) | ||
| : $for j, [[g$(j)_(g$j)]] {} | ||
| virtual ~CartesianProductGenerator$i() {} | ||
|
|
||
| virtual ParamIteratorInterface<ParamType>* Begin() const { | ||
| return new Iterator(this, $for j, [[g$(j)_, g$(j)_.begin()]]); | ||
| } | ||
| virtual ParamIteratorInterface<ParamType>* End() const { | ||
| return new Iterator(this, $for j, [[g$(j)_, g$(j)_.end()]]); | ||
| } | ||
|
|
||
| private: | ||
| class Iterator : public ParamIteratorInterface<ParamType> { | ||
| public: | ||
| Iterator(const ParamGeneratorInterface<ParamType>* base, $for j, [[ | ||
|
|
||
| const ParamGenerator<T$j>& g$j, | ||
| const typename ParamGenerator<T$j>::iterator& current$(j)]]) | ||
| : base_(base), | ||
| $for j, [[ | ||
|
|
||
| begin$(j)_(g$j.begin()), end$(j)_(g$j.end()), current$(j)_(current$j) | ||
| ]] { | ||
| ComputeCurrentValue(); | ||
| } | ||
| virtual ~Iterator() {} | ||
|
|
||
| virtual const ParamGeneratorInterface<ParamType>* BaseGenerator() const { | ||
| return base_; | ||
| } | ||
| // Advance should not be called on beyond-of-range iterators | ||
| // so no component iterators must be beyond end of range, either. | ||
| virtual void Advance() { | ||
| assert(!AtEnd()); | ||
| ++current$(i)_; | ||
|
|
||
| $for k [[ | ||
| if (current$(i+2-k)_ == end$(i+2-k)_) { | ||
| current$(i+2-k)_ = begin$(i+2-k)_; | ||
| ++current$(i+2-k-1)_; | ||
| } | ||
|
|
||
| ]] | ||
| ComputeCurrentValue(); | ||
| } | ||
| virtual ParamIteratorInterface<ParamType>* Clone() const { | ||
| return new Iterator(*this); | ||
| } | ||
| virtual const ParamType* Current() const { return ¤t_value_; } | ||
| virtual bool Equals(const ParamIteratorInterface<ParamType>& other) const { | ||
| // Having the same base generator guarantees that the other | ||
| // iterator is of the same type and we can downcast. | ||
| GTEST_CHECK_(BaseGenerator() == other.BaseGenerator()) | ||
| << "The program attempted to compare iterators " | ||
| << "from different generators." << std::endl; | ||
| const Iterator* typed_other = | ||
| CheckedDowncastToActualType<const Iterator>(&other); | ||
| // We must report iterators equal if they both point beyond their | ||
| // respective ranges. That can happen in a variety of fashions, | ||
| // so we have to consult AtEnd(). | ||
| return (AtEnd() && typed_other->AtEnd()) || | ||
| ($for j && [[ | ||
|
|
||
| current$(j)_ == typed_other->current$(j)_ | ||
| ]]); | ||
| } | ||
|
|
||
| private: | ||
| Iterator(const Iterator& other) | ||
| : base_(other.base_), $for j, [[ | ||
|
|
||
| begin$(j)_(other.begin$(j)_), | ||
| end$(j)_(other.end$(j)_), | ||
| current$(j)_(other.current$(j)_) | ||
| ]] { | ||
| ComputeCurrentValue(); | ||
| } | ||
|
|
||
| void ComputeCurrentValue() { | ||
| if (!AtEnd()) | ||
| current_value_ = ParamType($for j, [[*current$(j)_]]); | ||
| } | ||
| bool AtEnd() const { | ||
| // We must report iterator past the end of the range when either of the | ||
| // component iterators has reached the end of its range. | ||
| return | ||
| $for j || [[ | ||
|
|
||
| current$(j)_ == end$(j)_ | ||
| ]]; | ||
| } | ||
|
|
||
| // No implementation - assignment is unsupported. | ||
| void operator=(const Iterator& other); | ||
|
|
||
| const ParamGeneratorInterface<ParamType>* const base_; | ||
| // begin[i]_ and end[i]_ define the i-th range that Iterator traverses. | ||
| // current[i]_ is the actual traversing iterator. | ||
| $for j [[ | ||
|
|
||
| const typename ParamGenerator<T$j>::iterator begin$(j)_; | ||
| const typename ParamGenerator<T$j>::iterator end$(j)_; | ||
| typename ParamGenerator<T$j>::iterator current$(j)_; | ||
| ]] | ||
|
|
||
| ParamType current_value_; | ||
| }; // class CartesianProductGenerator$i::Iterator | ||
|
|
||
| // No implementation - assignment is unsupported. | ||
| void operator=(const CartesianProductGenerator$i& other); | ||
|
|
||
|
|
||
| $for j [[ | ||
| const ParamGenerator<T$j> g$(j)_; | ||
|
|
||
| ]] | ||
| }; // class CartesianProductGenerator$i | ||
|
|
||
|
|
||
| ]] | ||
|
|
||
| // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. | ||
| // | ||
| // Helper classes providing Combine() with polymorphic features. They allow | ||
| // casting CartesianProductGeneratorN<T> to ParamGenerator<U> if T is | ||
| // convertible to U. | ||
| // | ||
| $range i 2..maxtuple | ||
| $for i [[ | ||
| $range j 1..i | ||
|
|
||
| template <$for j, [[class Generator$j]]> | ||
| class CartesianProductHolder$i { | ||
| public: | ||
| CartesianProductHolder$i($for j, [[const Generator$j& g$j]]) | ||
| : $for j, [[g$(j)_(g$j)]] {} | ||
| template <$for j, [[typename T$j]]> | ||
| operator ParamGenerator< ::std::tr1::tuple<$for j, [[T$j]]> >() const { | ||
| return ParamGenerator< ::std::tr1::tuple<$for j, [[T$j]]> >( | ||
| new CartesianProductGenerator$i<$for j, [[T$j]]>( | ||
| $for j,[[ | ||
|
|
||
| static_cast<ParamGenerator<T$j> >(g$(j)_) | ||
| ]])); | ||
| } | ||
|
|
||
| private: | ||
| // No implementation - assignment is unsupported. | ||
| void operator=(const CartesianProductHolder$i& other); | ||
|
|
||
|
|
||
| $for j [[ | ||
| const Generator$j g$(j)_; | ||
|
|
||
| ]] | ||
| }; // class CartesianProductHolder$i | ||
|
|
||
| ]] | ||
|
|
||
| # endif // GTEST_HAS_COMBINE | ||
|
|
||
| } // namespace internal | ||
| } // namespace testing | ||
|
|
||
| #endif // GTEST_HAS_PARAM_TEST | ||
|
|
||
| #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_GENERATED_H_ |