1 // Copyright 2005, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: wan@google.com (Zhanyong Wan)
31 //
32 // The Google C++ Testing Framework (Google Test)
33 //
34 // This header file defines the public API for Google Test.  It should be
35 // included by any test program that uses Google Test.
36 //
37 // IMPORTANT NOTE: Due to limitation of the C++ language, we have to
38 // leave some internal implementation details in this header file.
39 // They are clearly marked by comments like this:
40 //
41 //   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
42 //
43 // Such code is NOT meant to be used by a user directly, and is subject
44 // to CHANGE WITHOUT NOTICE.  Therefore DO NOT DEPEND ON IT in a user
45 // program!
46 //
47 // Acknowledgment: Google Test borrowed the idea of automatic test
48 // registration from Barthelemy Dagenais' (barthelemy@prologique.com)
49 // easyUnit framework.
50 
51 #ifndef GTEST_INCLUDE_GTEST_GTEST_H_
52 #define GTEST_INCLUDE_GTEST_GTEST_H_
53 
54 // The following platform macro is used throughout Google Test:
55 //   _WIN32_WCE      Windows CE     (set in project files)
56 
57 #include <limits>
58 #include <gtest/internal/gtest-internal.h>
59 #include <gtest/internal/gtest-string.h>
60 #include <gtest/gtest-death-test.h>
61 #include <gtest/gtest-message.h>
62 #include <gtest/gtest-param-test.h>
63 #include <gtest/gtest_prod.h>
64 #include <gtest/gtest-test-part.h>
65 #include <gtest/gtest-typed-test.h>
66 
67 // Depending on the platform, different string classes are available.
68 // On Windows, ::std::string compiles only when exceptions are
69 // enabled.  On Linux, in addition to ::std::string, Google also makes
70 // use of class ::string, which has the same interface as
71 // ::std::string, but has a different implementation.
72 //
73 // The user can tell us whether ::std::string is available in his
74 // environment by defining the macro GTEST_HAS_STD_STRING to either 1
75 // or 0 on the compiler command line.  He can also define
76 // GTEST_HAS_GLOBAL_STRING to 1 to indicate that ::string is available
77 // AND is a distinct type to ::std::string, or define it to 0 to
78 // indicate otherwise.
79 //
80 // If the user's ::std::string and ::string are the same class due to
81 // aliasing, he should define GTEST_HAS_STD_STRING to 1 and
82 // GTEST_HAS_GLOBAL_STRING to 0.
83 //
84 // If the user doesn't define GTEST_HAS_STD_STRING and/or
85 // GTEST_HAS_GLOBAL_STRING, they are defined heuristically.
86 
87 namespace testing {
88 
89 // Declares the flags.
90 
91 // This flag temporary enables the disabled tests.
92 GTEST_DECLARE_bool_(also_run_disabled_tests);
93 
94 // This flag brings the debugger on an assertion failure.
95 GTEST_DECLARE_bool_(break_on_failure);
96 
97 // This flag controls whether Google Test catches all test-thrown exceptions
98 // and logs them as failures.
99 GTEST_DECLARE_bool_(catch_exceptions);
100 
101 // This flag enables using colors in terminal output. Available values are
102 // "yes" to enable colors, "no" (disable colors), or "auto" (the default)
103 // to let Google Test decide.
104 GTEST_DECLARE_string_(color);
105 
106 // This flag sets up the filter to select by name using a glob pattern
107 // the tests to run. If the filter is not given all tests are executed.
108 GTEST_DECLARE_string_(filter);
109 
110 // This flag causes the Google Test to list tests. None of the tests listed
111 // are actually run if the flag is provided.
112 GTEST_DECLARE_bool_(list_tests);
113 
114 // This flag controls whether Google Test emits a detailed XML report to a file
115 // in addition to its normal textual output.
116 GTEST_DECLARE_string_(output);
117 
118 // This flags control whether Google Test prints the elapsed time for each
119 // test.
120 GTEST_DECLARE_bool_(print_time);
121 
122 // This flag sets how many times the tests are repeated. The default value
123 // is 1. If the value is -1 the tests are repeating forever.
124 GTEST_DECLARE_int32_(repeat);
125 
126 // This flag controls whether Google Test includes Google Test internal
127 // stack frames in failure stack traces.
128 GTEST_DECLARE_bool_(show_internal_stack_frames);
129 
130 // This flag specifies the maximum number of stack frames to be
131 // printed in a failure message.
132 GTEST_DECLARE_int32_(stack_trace_depth);
133 
134 // When this flag is specified, a failed assertion will throw an
135 // exception if exceptions are enabled, or exit the program with a
136 // non-zero code otherwise.
137 GTEST_DECLARE_bool_(throw_on_failure);
138 
139 // The upper limit for valid stack trace depths.
140 const int kMaxStackTraceDepth = 100;
141 
142 namespace internal {
143 
144 class GTestFlagSaver;
145 
146 // Converts a streamable value to a String.  A NULL pointer is
147 // converted to "(null)".  When the input value is a ::string,
148 // ::std::string, ::wstring, or ::std::wstring object, each NUL
149 // character in it is replaced with "\\0".
150 // Declared in gtest-internal.h but defined here, so that it has access
151 // to the definition of the Message class, required by the ARM
152 // compiler.
153 template <typename T>
StreamableToString(const T & streamable)154 String StreamableToString(const T& streamable) {
155   return (Message() << streamable).GetString();
156 }
157 
158 }  // namespace internal
159 
160 // A class for indicating whether an assertion was successful.  When
161 // the assertion wasn't successful, the AssertionResult object
162 // remembers a non-empty message that described how it failed.
163 //
164 // This class is useful for defining predicate-format functions to be
165 // used with predicate assertions (ASSERT_PRED_FORMAT*, etc).
166 //
167 // The constructor of AssertionResult is private.  To create an
168 // instance of this class, use one of the factory functions
169 // (AssertionSuccess() and AssertionFailure()).
170 //
171 // For example, in order to be able to write:
172 //
173 //   // Verifies that Foo() returns an even number.
174 //   EXPECT_PRED_FORMAT1(IsEven, Foo());
175 //
176 // you just need to define:
177 //
178 //   testing::AssertionResult IsEven(const char* expr, int n) {
179 //     if ((n % 2) == 0) return testing::AssertionSuccess();
180 //
181 //     Message msg;
182 //     msg << "Expected: " << expr << " is even\n"
183 //         << "  Actual: it's " << n;
184 //     return testing::AssertionFailure(msg);
185 //   }
186 //
187 // If Foo() returns 5, you will see the following message:
188 //
189 //   Expected: Foo() is even
190 //     Actual: it's 5
191 class AssertionResult {
192  public:
193   // Declares factory functions for making successful and failed
194   // assertion results as friends.
195   friend AssertionResult AssertionSuccess();
196   friend AssertionResult AssertionFailure(const Message&);
197 
198   // Returns true iff the assertion succeeded.
199   operator bool() const { return failure_message_.c_str() == NULL; }  // NOLINT
200 
201   // Returns the assertion's failure message.
failure_message()202   const char* failure_message() const { return failure_message_.c_str(); }
203 
204  private:
205   // The default constructor.  It is used when the assertion succeeded.
AssertionResult()206   AssertionResult() {}
207 
208   // The constructor used when the assertion failed.
209   explicit AssertionResult(const internal::String& failure_message);
210 
211   // Stores the assertion's failure message.
212   internal::String failure_message_;
213 };
214 
215 // Makes a successful assertion result.
216 AssertionResult AssertionSuccess();
217 
218 // Makes a failed assertion result with the given failure message.
219 AssertionResult AssertionFailure(const Message& msg);
220 
221 // The abstract class that all tests inherit from.
222 //
223 // In Google Test, a unit test program contains one or many TestCases, and
224 // each TestCase contains one or many Tests.
225 //
226 // When you define a test using the TEST macro, you don't need to
227 // explicitly derive from Test - the TEST macro automatically does
228 // this for you.
229 //
230 // The only time you derive from Test is when defining a test fixture
231 // to be used a TEST_F.  For example:
232 //
233 //   class FooTest : public testing::Test {
234 //    protected:
235 //     virtual void SetUp() { ... }
236 //     virtual void TearDown() { ... }
237 //     ...
238 //   };
239 //
240 //   TEST_F(FooTest, Bar) { ... }
241 //   TEST_F(FooTest, Baz) { ... }
242 //
243 // Test is not copyable.
244 class Test {
245  public:
246   friend class internal::TestInfoImpl;
247 
248   // Defines types for pointers to functions that set up and tear down
249   // a test case.
250   typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc;
251   typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc;
252 
253   // The d'tor is virtual as we intend to inherit from Test.
254   virtual ~Test();
255 
256   // Sets up the stuff shared by all tests in this test case.
257   //
258   // Google Test will call Foo::SetUpTestCase() before running the first
259   // test in test case Foo.  Hence a sub-class can define its own
260   // SetUpTestCase() method to shadow the one defined in the super
261   // class.
SetUpTestCase()262   static void SetUpTestCase() {}
263 
264   // Tears down the stuff shared by all tests in this test case.
265   //
266   // Google Test will call Foo::TearDownTestCase() after running the last
267   // test in test case Foo.  Hence a sub-class can define its own
268   // TearDownTestCase() method to shadow the one defined in the super
269   // class.
TearDownTestCase()270   static void TearDownTestCase() {}
271 
272   // Returns true iff the current test has a fatal failure.
273   static bool HasFatalFailure();
274 
275   // Returns true iff the current test has a non-fatal failure.
276   static bool HasNonfatalFailure();
277 
278   // Returns true iff the current test has a (either fatal or
279   // non-fatal) failure.
HasFailure()280   static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); }
281 
282   // Logs a property for the current test.  Only the last value for a given
283   // key is remembered.
284   // These are public static so they can be called from utility functions
285   // that are not members of the test fixture.
286   // The arguments are const char* instead strings, as Google Test is used
287   // on platforms where string doesn't compile.
288   //
289   // Note that a driving consideration for these RecordProperty methods
290   // was to produce xml output suited to the Greenspan charting utility,
291   // which at present will only chart values that fit in a 32-bit int. It
292   // is the user's responsibility to restrict their values to 32-bit ints
293   // if they intend them to be used with Greenspan.
294   static void RecordProperty(const char* key, const char* value);
295   static void RecordProperty(const char* key, int value);
296 
297  protected:
298   // Creates a Test object.
299   Test();
300 
301   // Sets up the test fixture.
302   virtual void SetUp();
303 
304   // Tears down the test fixture.
305   virtual void TearDown();
306 
307  private:
308   // Returns true iff the current test has the same fixture class as
309   // the first test in the current test case.
310   static bool HasSameFixtureClass();
311 
312   // Runs the test after the test fixture has been set up.
313   //
314   // A sub-class must implement this to define the test logic.
315   //
316   // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM.
317   // Instead, use the TEST or TEST_F macro.
318   virtual void TestBody() = 0;
319 
320   // Sets up, executes, and tears down the test.
321   void Run();
322 
323   // Uses a GTestFlagSaver to save and restore all Google Test flags.
324   const internal::GTestFlagSaver* const gtest_flag_saver_;
325 
326   // Often a user mis-spells SetUp() as Setup() and spends a long time
327   // wondering why it is never called by Google Test.  The declaration of
328   // the following method is solely for catching such an error at
329   // compile time:
330   //
331   //   - The return type is deliberately chosen to be not void, so it
332   //   will be a conflict if a user declares void Setup() in his test
333   //   fixture.
334   //
335   //   - This method is private, so it will be another compiler error
336   //   if a user calls it from his test fixture.
337   //
338   // DO NOT OVERRIDE THIS FUNCTION.
339   //
340   // If you see an error about overriding the following function or
341   // about it being private, you have mis-spelled SetUp() as Setup().
342   struct Setup_should_be_spelled_SetUp {};
Setup()343   virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; }
344 
345   // We disallow copying Tests.
346   GTEST_DISALLOW_COPY_AND_ASSIGN_(Test);
347 };
348 
349 
350 // A TestInfo object stores the following information about a test:
351 //
352 //   Test case name
353 //   Test name
354 //   Whether the test should be run
355 //   A function pointer that creates the test object when invoked
356 //   Test result
357 //
358 // The constructor of TestInfo registers itself with the UnitTest
359 // singleton such that the RUN_ALL_TESTS() macro knows which tests to
360 // run.
361 class TestInfo {
362  public:
363   // Destructs a TestInfo object.  This function is not virtual, so
364   // don't inherit from TestInfo.
365   ~TestInfo();
366 
367   // Returns the test case name.
368   const char* test_case_name() const;
369 
370   // Returns the test name.
371   const char* name() const;
372 
373   // Returns the test case comment.
374   const char* test_case_comment() const;
375 
376   // Returns the test comment.
377   const char* comment() const;
378 
379   // Returns true if this test matches the user-specified filter.
380   bool matches_filter() const;
381 
382   // Returns true if this test should run, that is if the test is not disabled
383   // (or it is disabled but the also_run_disabled_tests flag has been specified)
384   // and its full name matches the user-specified filter.
385   //
386   // Google Test allows the user to filter the tests by their full names.
387   // The full name of a test Bar in test case Foo is defined as
388   // "Foo.Bar".  Only the tests that match the filter will run.
389   //
390   // A filter is a colon-separated list of glob (not regex) patterns,
391   // optionally followed by a '-' and a colon-separated list of
392   // negative patterns (tests to exclude).  A test is run if it
393   // matches one of the positive patterns and does not match any of
394   // the negative patterns.
395   //
396   // For example, *A*:Foo.* is a filter that matches any string that
397   // contains the character 'A' or starts with "Foo.".
398   bool should_run() const;
399 
400   // Returns the result of the test.
401   const internal::TestResult* result() const;
402  private:
403 #if GTEST_HAS_DEATH_TEST
404   friend class internal::DefaultDeathTestFactory;
405 #endif  // GTEST_HAS_DEATH_TEST
406   friend class internal::TestInfoImpl;
407   friend class internal::UnitTestImpl;
408   friend class Test;
409   friend class TestCase;
410   friend TestInfo* internal::MakeAndRegisterTestInfo(
411       const char* test_case_name, const char* name,
412       const char* test_case_comment, const char* comment,
413       internal::TypeId fixture_class_id,
414       Test::SetUpTestCaseFunc set_up_tc,
415       Test::TearDownTestCaseFunc tear_down_tc,
416       internal::TestFactoryBase* factory);
417 
418   // Increments the number of death tests encountered in this test so
419   // far.
420   int increment_death_test_count();
421 
422   // Accessors for the implementation object.
impl()423   internal::TestInfoImpl* impl() { return impl_; }
impl()424   const internal::TestInfoImpl* impl() const { return impl_; }
425 
426   // Constructs a TestInfo object. The newly constructed instance assumes
427   // ownership of the factory object.
428   TestInfo(const char* test_case_name, const char* name,
429            const char* test_case_comment, const char* comment,
430            internal::TypeId fixture_class_id,
431            internal::TestFactoryBase* factory);
432 
433   // An opaque implementation object.
434   internal::TestInfoImpl* impl_;
435 
436   GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo);
437 };
438 
439 // An Environment object is capable of setting up and tearing down an
440 // environment.  The user should subclass this to define his own
441 // environment(s).
442 //
443 // An Environment object does the set-up and tear-down in virtual
444 // methods SetUp() and TearDown() instead of the constructor and the
445 // destructor, as:
446 //
447 //   1. You cannot safely throw from a destructor.  This is a problem
448 //      as in some cases Google Test is used where exceptions are enabled, and
449 //      we may want to implement ASSERT_* using exceptions where they are
450 //      available.
451 //   2. You cannot use ASSERT_* directly in a constructor or
452 //      destructor.
453 class Environment {
454  public:
455   // The d'tor is virtual as we need to subclass Environment.
~Environment()456   virtual ~Environment() {}
457 
458   // Override this to define how to set up the environment.
SetUp()459   virtual void SetUp() {}
460 
461   // Override this to define how to tear down the environment.
TearDown()462   virtual void TearDown() {}
463  private:
464   // If you see an error about overriding the following function or
465   // about it being private, you have mis-spelled SetUp() as Setup().
466   struct Setup_should_be_spelled_SetUp {};
Setup()467   virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; }
468 };
469 
470 // A UnitTest consists of a list of TestCases.
471 //
472 // This is a singleton class.  The only instance of UnitTest is
473 // created when UnitTest::GetInstance() is first called.  This
474 // instance is never deleted.
475 //
476 // UnitTest is not copyable.
477 //
478 // This class is thread-safe as long as the methods are called
479 // according to their specification.
480 class UnitTest {
481  public:
482   // Gets the singleton UnitTest object.  The first time this method
483   // is called, a UnitTest object is constructed and returned.
484   // Consecutive calls will return the same object.
485   static UnitTest* GetInstance();
486 
487   // Registers and returns a global test environment.  When a test
488   // program is run, all global test environments will be set-up in
489   // the order they were registered.  After all tests in the program
490   // have finished, all global test environments will be torn-down in
491   // the *reverse* order they were registered.
492   //
493   // The UnitTest object takes ownership of the given environment.
494   //
495   // This method can only be called from the main thread.
496   Environment* AddEnvironment(Environment* env);
497 
498   // Adds a TestPartResult to the current TestResult object.  All
499   // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc)
500   // eventually call this to report their results.  The user code
501   // should use the assertion macros instead of calling this directly.
502   //
503   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
504   void AddTestPartResult(TestPartResultType result_type,
505                          const char* file_name,
506                          int line_number,
507                          const internal::String& message,
508                          const internal::String& os_stack_trace);
509 
510   // Adds a TestProperty to the current TestResult object. If the result already
511   // contains a property with the same key, the value will be updated.
512   void RecordPropertyForCurrentTest(const char* key, const char* value);
513 
514   // Runs all tests in this UnitTest object and prints the result.
515   // Returns 0 if successful, or 1 otherwise.
516   //
517   // This method can only be called from the main thread.
518   //
519   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
520   int Run() GTEST_MUST_USE_RESULT_;
521 
522   // Returns the working directory when the first TEST() or TEST_F()
523   // was executed.  The UnitTest object owns the string.
524   const char* original_working_dir() const;
525 
526   // Returns the TestCase object for the test that's currently running,
527   // or NULL if no test is running.
528   const TestCase* current_test_case() const;
529 
530   // Returns the TestInfo object for the test that's currently running,
531   // or NULL if no test is running.
532   const TestInfo* current_test_info() const;
533 
534 #if GTEST_HAS_PARAM_TEST
535   // Returns the ParameterizedTestCaseRegistry object used to keep track of
536   // value-parameterized tests and instantiate and register them.
537   internal::ParameterizedTestCaseRegistry& parameterized_test_registry();
538 #endif  // GTEST_HAS_PARAM_TEST
539 
540   // Accessors for the implementation object.
impl()541   internal::UnitTestImpl* impl() { return impl_; }
impl()542   const internal::UnitTestImpl* impl() const { return impl_; }
543  private:
544   // ScopedTrace is a friend as it needs to modify the per-thread
545   // trace stack, which is a private member of UnitTest.
546   friend class internal::ScopedTrace;
547 
548   // Creates an empty UnitTest.
549   UnitTest();
550 
551   // D'tor
552   virtual ~UnitTest();
553 
554   // Pushes a trace defined by SCOPED_TRACE() on to the per-thread
555   // Google Test trace stack.
556   void PushGTestTrace(const internal::TraceInfo& trace);
557 
558   // Pops a trace from the per-thread Google Test trace stack.
559   void PopGTestTrace();
560 
561   // Protects mutable state in *impl_.  This is mutable as some const
562   // methods need to lock it too.
563   mutable internal::Mutex mutex_;
564 
565   // Opaque implementation object.  This field is never changed once
566   // the object is constructed.  We don't mark it as const here, as
567   // doing so will cause a warning in the constructor of UnitTest.
568   // Mutable state in *impl_ is protected by mutex_.
569   internal::UnitTestImpl* impl_;
570 
571   // We disallow copying UnitTest.
572   GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTest);
573 };
574 
575 // A convenient wrapper for adding an environment for the test
576 // program.
577 //
578 // You should call this before RUN_ALL_TESTS() is called, probably in
579 // main().  If you use gtest_main, you need to call this before main()
580 // starts for it to take effect.  For example, you can define a global
581 // variable like this:
582 //
583 //   testing::Environment* const foo_env =
584 //       testing::AddGlobalTestEnvironment(new FooEnvironment);
585 //
586 // However, we strongly recommend you to write your own main() and
587 // call AddGlobalTestEnvironment() there, as relying on initialization
588 // of global variables makes the code harder to read and may cause
589 // problems when you register multiple environments from different
590 // translation units and the environments have dependencies among them
591 // (remember that the compiler doesn't guarantee the order in which
592 // global variables from different translation units are initialized).
AddGlobalTestEnvironment(Environment * env)593 inline Environment* AddGlobalTestEnvironment(Environment* env) {
594   return UnitTest::GetInstance()->AddEnvironment(env);
595 }
596 
597 // Initializes Google Test.  This must be called before calling
598 // RUN_ALL_TESTS().  In particular, it parses a command line for the
599 // flags that Google Test recognizes.  Whenever a Google Test flag is
600 // seen, it is removed from argv, and *argc is decremented.
601 //
602 // No value is returned.  Instead, the Google Test flag variables are
603 // updated.
604 //
605 // Calling the function for the second time has no user-visible effect.
606 void InitGoogleTest(int* argc, char** argv);
607 
608 // This overloaded version can be used in Windows programs compiled in
609 // UNICODE mode.
610 void InitGoogleTest(int* argc, wchar_t** argv);
611 
612 namespace internal {
613 
614 // These overloaded versions handle ::std::string and ::std::wstring.
615 #if GTEST_HAS_STD_STRING
FormatForFailureMessage(const::std::string & str)616 inline String FormatForFailureMessage(const ::std::string& str) {
617   return (Message() << '"' << str << '"').GetString();
618 }
619 #endif  // GTEST_HAS_STD_STRING
620 
621 #if GTEST_HAS_STD_WSTRING
FormatForFailureMessage(const::std::wstring & wstr)622 inline String FormatForFailureMessage(const ::std::wstring& wstr) {
623   return (Message() << "L\"" << wstr << '"').GetString();
624 }
625 #endif  // GTEST_HAS_STD_WSTRING
626 
627 // These overloaded versions handle ::string and ::wstring.
628 #if GTEST_HAS_GLOBAL_STRING
FormatForFailureMessage(const::string & str)629 inline String FormatForFailureMessage(const ::string& str) {
630   return (Message() << '"' << str << '"').GetString();
631 }
632 #endif  // GTEST_HAS_GLOBAL_STRING
633 
634 #if GTEST_HAS_GLOBAL_WSTRING
FormatForFailureMessage(const::wstring & wstr)635 inline String FormatForFailureMessage(const ::wstring& wstr) {
636   return (Message() << "L\"" << wstr << '"').GetString();
637 }
638 #endif  // GTEST_HAS_GLOBAL_WSTRING
639 
640 // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
641 // operand to be used in a failure message.  The type (but not value)
642 // of the other operand may affect the format.  This allows us to
643 // print a char* as a raw pointer when it is compared against another
644 // char*, and print it as a C string when it is compared against an
645 // std::string object, for example.
646 //
647 // The default implementation ignores the type of the other operand.
648 // Some specialized versions are used to handle formatting wide or
649 // narrow C strings.
650 //
651 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
652 template <typename T1, typename T2>
FormatForComparisonFailureMessage(const T1 & value,const T2 &)653 String FormatForComparisonFailureMessage(const T1& value,
654                                          const T2& /* other_operand */) {
655   return FormatForFailureMessage(value);
656 }
657 
658 // The helper function for {ASSERT|EXPECT}_EQ.
659 template <typename T1, typename T2>
CmpHelperEQ(const char * expected_expression,const char * actual_expression,const T1 & expected,const T2 & actual)660 AssertionResult CmpHelperEQ(const char* expected_expression,
661                             const char* actual_expression,
662                             const T1& expected,
663                             const T2& actual) {
664 #ifdef _MSC_VER
665 #pragma warning(push)          // Saves the current warning state.
666 #pragma warning(disable:4389)  // Temporarily disables warning on
667                                // signed/unsigned mismatch.
668 #endif
669 
670   if (expected == actual) {
671     return AssertionSuccess();
672   }
673 
674 #ifdef _MSC_VER
675 #pragma warning(pop)          // Restores the warning state.
676 #endif
677 
678   return EqFailure(expected_expression,
679                    actual_expression,
680                    FormatForComparisonFailureMessage(expected, actual),
681                    FormatForComparisonFailureMessage(actual, expected),
682                    false);
683 }
684 
685 // With this overloaded version, we allow anonymous enums to be used
686 // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums
687 // can be implicitly cast to BiggestInt.
688 AssertionResult CmpHelperEQ(const char* expected_expression,
689                             const char* actual_expression,
690                             BiggestInt expected,
691                             BiggestInt actual);
692 
693 // The helper class for {ASSERT|EXPECT}_EQ.  The template argument
694 // lhs_is_null_literal is true iff the first argument to ASSERT_EQ()
695 // is a null pointer literal.  The following default implementation is
696 // for lhs_is_null_literal being false.
697 template <bool lhs_is_null_literal>
698 class EqHelper {
699  public:
700   // This templatized version is for the general case.
701   template <typename T1, typename T2>
Compare(const char * expected_expression,const char * actual_expression,const T1 & expected,const T2 & actual)702   static AssertionResult Compare(const char* expected_expression,
703                                  const char* actual_expression,
704                                  const T1& expected,
705                                  const T2& actual) {
706     return CmpHelperEQ(expected_expression, actual_expression, expected,
707                        actual);
708   }
709 
710   // With this overloaded version, we allow anonymous enums to be used
711   // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous
712   // enums can be implicitly cast to BiggestInt.
713   //
714   // Even though its body looks the same as the above version, we
715   // cannot merge the two, as it will make anonymous enums unhappy.
Compare(const char * expected_expression,const char * actual_expression,BiggestInt expected,BiggestInt actual)716   static AssertionResult Compare(const char* expected_expression,
717                                  const char* actual_expression,
718                                  BiggestInt expected,
719                                  BiggestInt actual) {
720     return CmpHelperEQ(expected_expression, actual_expression, expected,
721                        actual);
722   }
723 };
724 
725 // This specialization is used when the first argument to ASSERT_EQ()
726 // is a null pointer literal.
727 template <>
728 class EqHelper<true> {
729  public:
730   // We define two overloaded versions of Compare().  The first
731   // version will be picked when the second argument to ASSERT_EQ() is
732   // NOT a pointer, e.g. ASSERT_EQ(0, AnIntFunction()) or
733   // EXPECT_EQ(false, a_bool).
734   template <typename T1, typename T2>
Compare(const char * expected_expression,const char * actual_expression,const T1 & expected,const T2 & actual)735   static AssertionResult Compare(const char* expected_expression,
736                                  const char* actual_expression,
737                                  const T1& expected,
738                                  const T2& actual) {
739     return CmpHelperEQ(expected_expression, actual_expression, expected,
740                        actual);
741   }
742 
743   // This version will be picked when the second argument to
744   // ASSERT_EQ() is a pointer, e.g. ASSERT_EQ(NULL, a_pointer).
745   template <typename T1, typename T2>
Compare(const char * expected_expression,const char * actual_expression,const T1 &,T2 * actual)746   static AssertionResult Compare(const char* expected_expression,
747                                  const char* actual_expression,
748                                  const T1& /* expected */,
749                                  T2* actual) {
750     // We already know that 'expected' is a null pointer.
751     return CmpHelperEQ(expected_expression, actual_expression,
752                        static_cast<T2*>(NULL), actual);
753   }
754 };
755 
756 // A macro for implementing the helper functions needed to implement
757 // ASSERT_?? and EXPECT_??.  It is here just to avoid copy-and-paste
758 // of similar code.
759 //
760 // For each templatized helper function, we also define an overloaded
761 // version for BiggestInt in order to reduce code bloat and allow
762 // anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled
763 // with gcc 4.
764 //
765 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
766 #define GTEST_IMPL_CMP_HELPER_(op_name, op)\
767 template <typename T1, typename T2>\
768 AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
769                                    const T1& val1, const T2& val2) {\
770   if (val1 op val2) {\
771     return AssertionSuccess();\
772   } else {\
773     Message msg;\
774     msg << "Expected: (" << expr1 << ") " #op " (" << expr2\
775         << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\
776         << " vs " << FormatForComparisonFailureMessage(val2, val1);\
777     return AssertionFailure(msg);\
778   }\
779 }\
780 AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \
781                                    BiggestInt val1, BiggestInt val2);
782 
783 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
784 
785 // Implements the helper function for {ASSERT|EXPECT}_NE
786 GTEST_IMPL_CMP_HELPER_(NE, !=)
787 // Implements the helper function for {ASSERT|EXPECT}_LE
788 GTEST_IMPL_CMP_HELPER_(LE, <=)
789 // Implements the helper function for {ASSERT|EXPECT}_LT
790 GTEST_IMPL_CMP_HELPER_(LT, < )
791 // Implements the helper function for {ASSERT|EXPECT}_GE
792 GTEST_IMPL_CMP_HELPER_(GE, >=)
793 // Implements the helper function for {ASSERT|EXPECT}_GT
794 GTEST_IMPL_CMP_HELPER_(GT, > )
795 
796 #undef GTEST_IMPL_CMP_HELPER_
797 
798 // The helper function for {ASSERT|EXPECT}_STREQ.
799 //
800 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
801 AssertionResult CmpHelperSTREQ(const char* expected_expression,
802                                const char* actual_expression,
803                                const char* expected,
804                                const char* actual);
805 
806 // The helper function for {ASSERT|EXPECT}_STRCASEEQ.
807 //
808 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
809 AssertionResult CmpHelperSTRCASEEQ(const char* expected_expression,
810                                    const char* actual_expression,
811                                    const char* expected,
812                                    const char* actual);
813 
814 // The helper function for {ASSERT|EXPECT}_STRNE.
815 //
816 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
817 AssertionResult CmpHelperSTRNE(const char* s1_expression,
818                                const char* s2_expression,
819                                const char* s1,
820                                const char* s2);
821 
822 // The helper function for {ASSERT|EXPECT}_STRCASENE.
823 //
824 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
825 AssertionResult CmpHelperSTRCASENE(const char* s1_expression,
826                                    const char* s2_expression,
827                                    const char* s1,
828                                    const char* s2);
829 
830 
831 // Helper function for *_STREQ on wide strings.
832 //
833 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
834 AssertionResult CmpHelperSTREQ(const char* expected_expression,
835                                const char* actual_expression,
836                                const wchar_t* expected,
837                                const wchar_t* actual);
838 
839 // Helper function for *_STRNE on wide strings.
840 //
841 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
842 AssertionResult CmpHelperSTRNE(const char* s1_expression,
843                                const char* s2_expression,
844                                const wchar_t* s1,
845                                const wchar_t* s2);
846 
847 }  // namespace internal
848 
849 // IsSubstring() and IsNotSubstring() are intended to be used as the
850 // first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by
851 // themselves.  They check whether needle is a substring of haystack
852 // (NULL is considered a substring of itself only), and return an
853 // appropriate error message when they fail.
854 //
855 // The {needle,haystack}_expr arguments are the stringified
856 // expressions that generated the two real arguments.
857 AssertionResult IsSubstring(
858     const char* needle_expr, const char* haystack_expr,
859     const char* needle, const char* haystack);
860 AssertionResult IsSubstring(
861     const char* needle_expr, const char* haystack_expr,
862     const wchar_t* needle, const wchar_t* haystack);
863 AssertionResult IsNotSubstring(
864     const char* needle_expr, const char* haystack_expr,
865     const char* needle, const char* haystack);
866 AssertionResult IsNotSubstring(
867     const char* needle_expr, const char* haystack_expr,
868     const wchar_t* needle, const wchar_t* haystack);
869 #if GTEST_HAS_STD_STRING
870 AssertionResult IsSubstring(
871     const char* needle_expr, const char* haystack_expr,
872     const ::std::string& needle, const ::std::string& haystack);
873 AssertionResult IsNotSubstring(
874     const char* needle_expr, const char* haystack_expr,
875     const ::std::string& needle, const ::std::string& haystack);
876 #endif  // GTEST_HAS_STD_STRING
877 
878 #if GTEST_HAS_STD_WSTRING
879 AssertionResult IsSubstring(
880     const char* needle_expr, const char* haystack_expr,
881     const ::std::wstring& needle, const ::std::wstring& haystack);
882 AssertionResult IsNotSubstring(
883     const char* needle_expr, const char* haystack_expr,
884     const ::std::wstring& needle, const ::std::wstring& haystack);
885 #endif  // GTEST_HAS_STD_WSTRING
886 
887 namespace internal {
888 
889 // Helper template function for comparing floating-points.
890 //
891 // Template parameter:
892 //
893 //   RawType: the raw floating-point type (either float or double)
894 //
895 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
896 template <typename RawType>
CmpHelperFloatingPointEQ(const char * expected_expression,const char * actual_expression,RawType expected,RawType actual)897 AssertionResult CmpHelperFloatingPointEQ(const char* expected_expression,
898                                          const char* actual_expression,
899                                          RawType expected,
900                                          RawType actual) {
901   const FloatingPoint<RawType> lhs(expected), rhs(actual);
902 
903   if (lhs.AlmostEquals(rhs)) {
904     return AssertionSuccess();
905   }
906 
907   StrStream expected_ss;
908   expected_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
909               << expected;
910 
911   StrStream actual_ss;
912   actual_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
913             << actual;
914 
915   return EqFailure(expected_expression,
916                    actual_expression,
917                    StrStreamToString(&expected_ss),
918                    StrStreamToString(&actual_ss),
919                    false);
920 }
921 
922 // Helper function for implementing ASSERT_NEAR.
923 //
924 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
925 AssertionResult DoubleNearPredFormat(const char* expr1,
926                                      const char* expr2,
927                                      const char* abs_error_expr,
928                                      double val1,
929                                      double val2,
930                                      double abs_error);
931 
932 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
933 // A class that enables one to stream messages to assertion macros
934 class AssertHelper {
935  public:
936   // Constructor.
937   AssertHelper(TestPartResultType type, const char* file, int line,
938                const char* message);
939   // Message assignment is a semantic trick to enable assertion
940   // streaming; see the GTEST_MESSAGE_ macro below.
941   void operator=(const Message& message) const;
942  private:
943   TestPartResultType const type_;
944   const char*        const file_;
945   int                const line_;
946   String             const message_;
947 
948   GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelper);
949 };
950 
951 }  // namespace internal
952 
953 #if GTEST_HAS_PARAM_TEST
954 // The abstract base class that all value-parameterized tests inherit from.
955 //
956 // This class adds support for accessing the test parameter value via
957 // the GetParam() method.
958 //
959 // Use it with one of the parameter generator defining functions, like Range(),
960 // Values(), ValuesIn(), Bool(), and Combine().
961 //
962 // class FooTest : public ::testing::TestWithParam<int> {
963 //  protected:
964 //   FooTest() {
965 //     // Can use GetParam() here.
966 //   }
967 //   virtual ~FooTest() {
968 //     // Can use GetParam() here.
969 //   }
970 //   virtual void SetUp() {
971 //     // Can use GetParam() here.
972 //   }
973 //   virtual void TearDown {
974 //     // Can use GetParam() here.
975 //   }
976 // };
977 // TEST_P(FooTest, DoesBar) {
978 //   // Can use GetParam() method here.
979 //   Foo foo;
980 //   ASSERT_TRUE(foo.DoesBar(GetParam()));
981 // }
982 // INSTANTIATE_TEST_CASE_P(OneToTenRange, FooTest, ::testing::Range(1, 10));
983 
984 template <typename T>
985 class TestWithParam : public Test {
986  public:
987   typedef T ParamType;
988 
989   // The current parameter value. Is also available in the test fixture's
990   // constructor.
GetParam()991   const ParamType& GetParam() const { return *parameter_; }
992 
993  private:
994   // Sets parameter value. The caller is responsible for making sure the value
995   // remains alive and unchanged throughout the current test.
SetParam(const ParamType * parameter)996   static void SetParam(const ParamType* parameter) {
997     parameter_ = parameter;
998   }
999 
1000   // Static value used for accessing parameter during a test lifetime.
1001   static const ParamType* parameter_;
1002 
1003   // TestClass must be a subclass of TestWithParam<T>.
1004   template <class TestClass> friend class internal::ParameterizedTestFactory;
1005 };
1006 
1007 template <typename T>
1008 const T* TestWithParam<T>::parameter_ = NULL;
1009 
1010 #endif  // GTEST_HAS_PARAM_TEST
1011 
1012 // Macros for indicating success/failure in test code.
1013 
1014 // ADD_FAILURE unconditionally adds a failure to the current test.
1015 // SUCCEED generates a success - it doesn't automatically make the
1016 // current test successful, as a test is only successful when it has
1017 // no failure.
1018 //
1019 // EXPECT_* verifies that a certain condition is satisfied.  If not,
1020 // it behaves like ADD_FAILURE.  In particular:
1021 //
1022 //   EXPECT_TRUE  verifies that a Boolean condition is true.
1023 //   EXPECT_FALSE verifies that a Boolean condition is false.
1024 //
1025 // FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except
1026 // that they will also abort the current function on failure.  People
1027 // usually want the fail-fast behavior of FAIL and ASSERT_*, but those
1028 // writing data-driven tests often find themselves using ADD_FAILURE
1029 // and EXPECT_* more.
1030 //
1031 // Examples:
1032 //
1033 //   EXPECT_TRUE(server.StatusIsOK());
1034 //   ASSERT_FALSE(server.HasPendingRequest(port))
1035 //       << "There are still pending requests " << "on port " << port;
1036 
1037 // Generates a nonfatal failure with a generic message.
1038 #define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed")
1039 
1040 // Generates a fatal failure with a generic message.
1041 #define FAIL() GTEST_FATAL_FAILURE_("Failed")
1042 
1043 // Generates a success with a generic message.
1044 #define SUCCEED() GTEST_SUCCESS_("Succeeded")
1045 
1046 // Macros for testing exceptions.
1047 //
1048 //    * {ASSERT|EXPECT}_THROW(statement, expected_exception):
1049 //         Tests that the statement throws the expected exception.
1050 //    * {ASSERT|EXPECT}_NO_THROW(statement):
1051 //         Tests that the statement doesn't throw any exception.
1052 //    * {ASSERT|EXPECT}_ANY_THROW(statement):
1053 //         Tests that the statement throws an exception.
1054 
1055 #define EXPECT_THROW(statement, expected_exception) \
1056   GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_)
1057 #define EXPECT_NO_THROW(statement) \
1058   GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_)
1059 #define EXPECT_ANY_THROW(statement) \
1060   GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_)
1061 #define ASSERT_THROW(statement, expected_exception) \
1062   GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_)
1063 #define ASSERT_NO_THROW(statement) \
1064   GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_)
1065 #define ASSERT_ANY_THROW(statement) \
1066   GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_)
1067 
1068 // Boolean assertions.
1069 #define EXPECT_TRUE(condition) \
1070   GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \
1071                       GTEST_NONFATAL_FAILURE_)
1072 #define EXPECT_FALSE(condition) \
1073   GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
1074                       GTEST_NONFATAL_FAILURE_)
1075 #define ASSERT_TRUE(condition) \
1076   GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \
1077                       GTEST_FATAL_FAILURE_)
1078 #define ASSERT_FALSE(condition) \
1079   GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \
1080                       GTEST_FATAL_FAILURE_)
1081 
1082 // Includes the auto-generated header that implements a family of
1083 // generic predicate assertion macros.
1084 #include <gtest/gtest_pred_impl.h>
1085 
1086 // Macros for testing equalities and inequalities.
1087 //
1088 //    * {ASSERT|EXPECT}_EQ(expected, actual): Tests that expected == actual
1089 //    * {ASSERT|EXPECT}_NE(v1, v2):           Tests that v1 != v2
1090 //    * {ASSERT|EXPECT}_LT(v1, v2):           Tests that v1 < v2
1091 //    * {ASSERT|EXPECT}_LE(v1, v2):           Tests that v1 <= v2
1092 //    * {ASSERT|EXPECT}_GT(v1, v2):           Tests that v1 > v2
1093 //    * {ASSERT|EXPECT}_GE(v1, v2):           Tests that v1 >= v2
1094 //
1095 // When they are not, Google Test prints both the tested expressions and
1096 // their actual values.  The values must be compatible built-in types,
1097 // or you will get a compiler error.  By "compatible" we mean that the
1098 // values can be compared by the respective operator.
1099 //
1100 // Note:
1101 //
1102 //   1. It is possible to make a user-defined type work with
1103 //   {ASSERT|EXPECT}_??(), but that requires overloading the
1104 //   comparison operators and is thus discouraged by the Google C++
1105 //   Usage Guide.  Therefore, you are advised to use the
1106 //   {ASSERT|EXPECT}_TRUE() macro to assert that two objects are
1107 //   equal.
1108 //
1109 //   2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on
1110 //   pointers (in particular, C strings).  Therefore, if you use it
1111 //   with two C strings, you are testing how their locations in memory
1112 //   are related, not how their content is related.  To compare two C
1113 //   strings by content, use {ASSERT|EXPECT}_STR*().
1114 //
1115 //   3. {ASSERT|EXPECT}_EQ(expected, actual) is preferred to
1116 //   {ASSERT|EXPECT}_TRUE(expected == actual), as the former tells you
1117 //   what the actual value is when it fails, and similarly for the
1118 //   other comparisons.
1119 //
1120 //   4. Do not depend on the order in which {ASSERT|EXPECT}_??()
1121 //   evaluate their arguments, which is undefined.
1122 //
1123 //   5. These macros evaluate their arguments exactly once.
1124 //
1125 // Examples:
1126 //
1127 //   EXPECT_NE(5, Foo());
1128 //   EXPECT_EQ(NULL, a_pointer);
1129 //   ASSERT_LT(i, array_size);
1130 //   ASSERT_GT(records.size(), 0) << "There is no record left.";
1131 
1132 #define EXPECT_EQ(expected, actual) \
1133   EXPECT_PRED_FORMAT2(::testing::internal:: \
1134                       EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \
1135                       expected, actual)
1136 #define EXPECT_NE(expected, actual) \
1137   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, expected, actual)
1138 #define EXPECT_LE(val1, val2) \
1139   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
1140 #define EXPECT_LT(val1, val2) \
1141   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
1142 #define EXPECT_GE(val1, val2) \
1143   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
1144 #define EXPECT_GT(val1, val2) \
1145   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
1146 
1147 #define ASSERT_EQ(expected, actual) \
1148   ASSERT_PRED_FORMAT2(::testing::internal:: \
1149                       EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \
1150                       expected, actual)
1151 #define ASSERT_NE(val1, val2) \
1152   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)
1153 #define ASSERT_LE(val1, val2) \
1154   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)
1155 #define ASSERT_LT(val1, val2) \
1156   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)
1157 #define ASSERT_GE(val1, val2) \
1158   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)
1159 #define ASSERT_GT(val1, val2) \
1160   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)
1161 
1162 // C String Comparisons.  All tests treat NULL and any non-NULL string
1163 // as different.  Two NULLs are equal.
1164 //
1165 //    * {ASSERT|EXPECT}_STREQ(s1, s2):     Tests that s1 == s2
1166 //    * {ASSERT|EXPECT}_STRNE(s1, s2):     Tests that s1 != s2
1167 //    * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case
1168 //    * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case
1169 //
1170 // For wide or narrow string objects, you can use the
1171 // {ASSERT|EXPECT}_??() macros.
1172 //
1173 // Don't depend on the order in which the arguments are evaluated,
1174 // which is undefined.
1175 //
1176 // These macros evaluate their arguments exactly once.
1177 
1178 #define EXPECT_STREQ(expected, actual) \
1179   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual)
1180 #define EXPECT_STRNE(s1, s2) \
1181   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
1182 #define EXPECT_STRCASEEQ(expected, actual) \
1183   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual)
1184 #define EXPECT_STRCASENE(s1, s2)\
1185   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
1186 
1187 #define ASSERT_STREQ(expected, actual) \
1188   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual)
1189 #define ASSERT_STRNE(s1, s2) \
1190   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)
1191 #define ASSERT_STRCASEEQ(expected, actual) \
1192   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual)
1193 #define ASSERT_STRCASENE(s1, s2)\
1194   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)
1195 
1196 // Macros for comparing floating-point numbers.
1197 //
1198 //    * {ASSERT|EXPECT}_FLOAT_EQ(expected, actual):
1199 //         Tests that two float values are almost equal.
1200 //    * {ASSERT|EXPECT}_DOUBLE_EQ(expected, actual):
1201 //         Tests that two double values are almost equal.
1202 //    * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error):
1203 //         Tests that v1 and v2 are within the given distance to each other.
1204 //
1205 // Google Test uses ULP-based comparison to automatically pick a default
1206 // error bound that is appropriate for the operands.  See the
1207 // FloatingPoint template class in gtest-internal.h if you are
1208 // interested in the implementation details.
1209 
1210 #define EXPECT_FLOAT_EQ(expected, actual)\
1211   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
1212                       expected, actual)
1213 
1214 #define EXPECT_DOUBLE_EQ(expected, actual)\
1215   EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
1216                       expected, actual)
1217 
1218 #define ASSERT_FLOAT_EQ(expected, actual)\
1219   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \
1220                       expected, actual)
1221 
1222 #define ASSERT_DOUBLE_EQ(expected, actual)\
1223   ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
1224                       expected, actual)
1225 
1226 #define EXPECT_NEAR(val1, val2, abs_error)\
1227   EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \
1228                       val1, val2, abs_error)
1229 
1230 #define ASSERT_NEAR(val1, val2, abs_error)\
1231   ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \
1232                       val1, val2, abs_error)
1233 
1234 // These predicate format functions work on floating-point values, and
1235 // can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g.
1236 //
1237 //   EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0);
1238 
1239 // Asserts that val1 is less than, or almost equal to, val2.  Fails
1240 // otherwise.  In particular, it fails if either val1 or val2 is NaN.
1241 AssertionResult FloatLE(const char* expr1, const char* expr2,
1242                         float val1, float val2);
1243 AssertionResult DoubleLE(const char* expr1, const char* expr2,
1244                          double val1, double val2);
1245 
1246 
1247 #if GTEST_OS_WINDOWS
1248 
1249 // Macros that test for HRESULT failure and success, these are only useful
1250 // on Windows, and rely on Windows SDK macros and APIs to compile.
1251 //
1252 //    * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr)
1253 //
1254 // When expr unexpectedly fails or succeeds, Google Test prints the
1255 // expected result and the actual result with both a human-readable
1256 // string representation of the error, if available, as well as the
1257 // hex result code.
1258 #define EXPECT_HRESULT_SUCCEEDED(expr) \
1259     EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
1260 
1261 #define ASSERT_HRESULT_SUCCEEDED(expr) \
1262     ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))
1263 
1264 #define EXPECT_HRESULT_FAILED(expr) \
1265     EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
1266 
1267 #define ASSERT_HRESULT_FAILED(expr) \
1268     ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))
1269 
1270 #endif  // GTEST_OS_WINDOWS
1271 
1272 // Macros that execute statement and check that it doesn't generate new fatal
1273 // failures in the current thread.
1274 //
1275 //   * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement);
1276 //
1277 // Examples:
1278 //
1279 //   EXPECT_NO_FATAL_FAILURE(Process());
1280 //   ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed";
1281 //
1282 #define ASSERT_NO_FATAL_FAILURE(statement) \
1283     GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_)
1284 #define EXPECT_NO_FATAL_FAILURE(statement) \
1285     GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_)
1286 
1287 // Causes a trace (including the source file path, the current line
1288 // number, and the given message) to be included in every test failure
1289 // message generated by code in the current scope.  The effect is
1290 // undone when the control leaves the current scope.
1291 //
1292 // The message argument can be anything streamable to std::ostream.
1293 //
1294 // In the implementation, we include the current line number as part
1295 // of the dummy variable name, thus allowing multiple SCOPED_TRACE()s
1296 // to appear in the same block - as long as they are on different
1297 // lines.
1298 #define SCOPED_TRACE(message) \
1299   ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\
1300     __FILE__, __LINE__, ::testing::Message() << (message))
1301 
1302 namespace internal {
1303 
1304 // This template is declared, but intentionally undefined.
1305 template <typename T1, typename T2>
1306 struct StaticAssertTypeEqHelper;
1307 
1308 template <typename T>
1309 struct StaticAssertTypeEqHelper<T, T> {};
1310 
1311 }  // namespace internal
1312 
1313 // Compile-time assertion for type equality.
1314 // StaticAssertTypeEq<type1, type2>() compiles iff type1 and type2 are
1315 // the same type.  The value it returns is not interesting.
1316 //
1317 // Instead of making StaticAssertTypeEq a class template, we make it a
1318 // function template that invokes a helper class template.  This
1319 // prevents a user from misusing StaticAssertTypeEq<T1, T2> by
1320 // defining objects of that type.
1321 //
1322 // CAVEAT:
1323 //
1324 // When used inside a method of a class template,
1325 // StaticAssertTypeEq<T1, T2>() is effective ONLY IF the method is
1326 // instantiated.  For example, given:
1327 //
1328 //   template <typename T> class Foo {
1329 //    public:
1330 //     void Bar() { testing::StaticAssertTypeEq<int, T>(); }
1331 //   };
1332 //
1333 // the code:
1334 //
1335 //   void Test1() { Foo<bool> foo; }
1336 //
1337 // will NOT generate a compiler error, as Foo<bool>::Bar() is never
1338 // actually instantiated.  Instead, you need:
1339 //
1340 //   void Test2() { Foo<bool> foo; foo.Bar(); }
1341 //
1342 // to cause a compiler error.
1343 template <typename T1, typename T2>
1344 bool StaticAssertTypeEq() {
1345   internal::StaticAssertTypeEqHelper<T1, T2>();
1346   return true;
1347 }
1348 
1349 // Defines a test.
1350 //
1351 // The first parameter is the name of the test case, and the second
1352 // parameter is the name of the test within the test case.
1353 //
1354 // The convention is to end the test case name with "Test".  For
1355 // example, a test case for the Foo class can be named FooTest.
1356 //
1357 // The user should put his test code between braces after using this
1358 // macro.  Example:
1359 //
1360 //   TEST(FooTest, InitializesCorrectly) {
1361 //     Foo foo;
1362 //     EXPECT_TRUE(foo.StatusIsOK());
1363 //   }
1364 
1365 // Note that we call GetTestTypeId() instead of GetTypeId<
1366 // ::testing::Test>() here to get the type ID of testing::Test.  This
1367 // is to work around a suspected linker bug when using Google Test as
1368 // a framework on Mac OS X.  The bug causes GetTypeId<
1369 // ::testing::Test>() to return different values depending on whether
1370 // the call is from the Google Test framework itself or from user test
1371 // code.  GetTestTypeId() is guaranteed to always return the same
1372 // value, as it always calls GetTypeId<>() from the Google Test
1373 // framework.
1374 #define TEST(test_case_name, test_name)\
1375   GTEST_TEST_(test_case_name, test_name, \
1376               ::testing::Test, ::testing::internal::GetTestTypeId())
1377 
1378 
1379 // Defines a test that uses a test fixture.
1380 //
1381 // The first parameter is the name of the test fixture class, which
1382 // also doubles as the test case name.  The second parameter is the
1383 // name of the test within the test case.
1384 //
1385 // A test fixture class must be declared earlier.  The user should put
1386 // his test code between braces after using this macro.  Example:
1387 //
1388 //   class FooTest : public testing::Test {
1389 //    protected:
1390 //     virtual void SetUp() { b_.AddElement(3); }
1391 //
1392 //     Foo a_;
1393 //     Foo b_;
1394 //   };
1395 //
1396 //   TEST_F(FooTest, InitializesCorrectly) {
1397 //     EXPECT_TRUE(a_.StatusIsOK());
1398 //   }
1399 //
1400 //   TEST_F(FooTest, ReturnsElementCountCorrectly) {
1401 //     EXPECT_EQ(0, a_.size());
1402 //     EXPECT_EQ(1, b_.size());
1403 //   }
1404 
1405 #define TEST_F(test_fixture, test_name)\
1406   GTEST_TEST_(test_fixture, test_name, test_fixture, \
1407               ::testing::internal::GetTypeId<test_fixture>())
1408 
1409 // Use this macro in main() to run all tests.  It returns 0 if all
1410 // tests are successful, or 1 otherwise.
1411 //
1412 // RUN_ALL_TESTS() should be invoked after the command line has been
1413 // parsed by InitGoogleTest().
1414 
1415 #define RUN_ALL_TESTS()\
1416   (::testing::UnitTest::GetInstance()->Run())
1417 
1418 }  // namespace testing
1419 
1420 #endif  // GTEST_INCLUDE_GTEST_GTEST_H_
1421