11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2005, Google Inc.
21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All rights reserved.
31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Redistribution and use in source and binary forms, with or without
51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// modification, are permitted provided that the following conditions are
61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// met:
71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions of source code must retain the above copyright
91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// notice, this list of conditions and the following disclaimer.
101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions in binary form must reproduce the above
111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// copyright notice, this list of conditions and the following disclaimer
121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the documentation and/or other materials provided with the
131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// distribution.
141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Neither the name of Google Inc. nor the names of its
151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// contributors may be used to endorse or promote products derived from
161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this software without specific prior written permission.
171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Author: wan@google.com (Zhanyong Wan)
311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The Google C++ Testing Framework (Google Test)
331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This header file defines the public API for death tests.  It is
351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// #included by gtest.h so a user doesn't need to include this
361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// directly.
371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifndef GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-death-test-internal.h"
421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing {
441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This flag controls the style of death tests.  Valid values are "threadsafe",
461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// meaning that the death test child process will re-execute the test binary
471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// from the start, running only a single death test, or "fast",
481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// meaning that the child process will execute the test logic immediately
491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// after forking.
501be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_DECLARE_string_(death_test_style);
511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_DEATH_TEST
531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The following macros are useful for writing death tests.
551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is
571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// executed:
581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   1. It generates a warning if there is more than one active
601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   thread.  This is because it's safe to fork() or clone() only
611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   when there is a single thread.
621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   2. The parent process clone()s a sub-process and runs the death
641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   test in it; the sub-process exits with code 0 at the end of the
651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   death test, if it hasn't exited already.
661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   3. The parent process waits for the sub-process to terminate.
681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   4. The parent process checks the exit code and error message of
701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   the sub-process.
711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Examples:
731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number");
751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   for (int i = 0; i < 5; i++) {
761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     EXPECT_DEATH(server.ProcessRequest(i),
771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//                  "Invalid request .* in ProcessRequest()")
781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//         << "Failed to die on request " << i);
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   }
801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting");
821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   bool KilledBySIGHUP(int exit_code) {
841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP;
851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   }
861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!");
881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On the regular expressions used in death tests:
901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   On POSIX-compliant systems (*nix), we use the <regex.h> library,
921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   which uses the POSIX extended regex syntax.
931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   On other platforms (e.g. Windows), we only support a simple regex
951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   syntax implemented as part of Google Test.  This limited
961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   implementation should be enough most of the time when writing
971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   death tests; though it lacks many features you can find in PCRE
981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   or POSIX extended regex syntax.  For example, we don't support
991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and
1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   repetition count ("x{5,7}"), among others.
1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   Below is the syntax that we do support.  We chose it to be a
1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   subset of both PCRE and POSIX extended regex, so it's easy to
1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   learn wherever you come from.  In the following: 'A' denotes a
1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   literal character, period (.), or a single \\ escape sequence;
1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   'x' and 'y' denote regular expressions; 'm' and 'n' are for
1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   natural numbers.
1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     c     matches any literal character c
1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     \\d   matches any decimal digit
1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     \\D   matches any character that's not a decimal digit
1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     \\f   matches \f
1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     \\n   matches \n
1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     \\r   matches \r
1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     \\s   matches any ASCII whitespace, including \n
1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     \\S   matches any character that's not a whitespace
1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     \\t   matches \t
1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     \\v   matches \v
1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     \\w   matches any letter, _, or decimal digit
1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     \\W   matches any character that \\w doesn't match
1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     \\c   matches any literal character c, which must be a punctuation
1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     .     matches any single character except \n
1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     A?    matches 0 or 1 occurrences of A
1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     A*    matches 0 or many occurrences of A
1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     A+    matches 1 or many occurrences of A
1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     ^     matches the beginning of a string (not that of each line)
1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     $     matches the end of a string (not that of each line)
1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     xy    matches x followed by y
1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   If you accidentally use PCRE or POSIX extended regex features
1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   not implemented by us, you will get a run-time failure.  In that
1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   case, please try to rewrite your regular expression within the
1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   above syntax.
1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   This implementation is *not* meant to be as highly tuned or robust
1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   as a compiled regex library, but should perform well enough for a
1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   death test, which already incurs significant overhead by launching
1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   a child process.
1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Known caveats:
1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   A "threadsafe" style death test obtains the path to the test
1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   program from argv[0] and re-executes it in the sub-process.  For
1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   simplicity, the current implementation doesn't search the PATH
1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   when launching the sub-process.  This means that the user must
1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   invoke the test program via a path that contains at least one
1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   path separator (e.g. path/to/foo_test and
1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   /absolute/path/to/bar_test are fine, but foo_test is not).  This
1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   is rarely a problem as people usually don't put the test binary
1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   directory in PATH.
1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(wan@google.com): make thread-safe death tests search the PATH.
1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Asserts that a given statement causes the program to exit, with an
1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// integer exit status that satisfies predicate, and emitting error output
1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// that matches regex.
15741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_EXIT(statement, predicate, regex) \
15841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_FATAL_FAILURE_)
1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Like ASSERT_EXIT, but continues on to successive tests in the
1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test case, if any:
16241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define EXPECT_EXIT(statement, predicate, regex) \
16341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_NONFATAL_FAILURE_)
1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Asserts that a given statement causes the program to exit, either by
1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// explicitly exiting with a nonzero exit code or being killed by a
1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// signal, and emitting error output that matches regex.
16841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_DEATH(statement, regex) \
16941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Like ASSERT_DEATH, but continues on to successive tests in the
1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test case, if any:
17341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define EXPECT_DEATH(statement, regex) \
17441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)
1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*:
1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that an exit code describes a normal exit with a given exit code.
17941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ ExitedWithCode {
1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  explicit ExitedWithCode(int exit_code);
1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool operator()(int exit_status) const;
1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
18441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // No implementation - assignment is unsupported.
18541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  void operator=(const ExitedWithCode& other);
18641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const int exit_code_;
1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
19041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if !GTEST_OS_WINDOWS
1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that an exit code describes an exit due to termination by a
1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// given signal.
19341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ KilledBySignal {
1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
1951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  explicit KilledBySignal(int signum);
1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool operator()(int exit_status) const;
1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
1981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const int signum_;
1991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
20041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // !GTEST_OS_WINDOWS
2011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode.
2031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The death testing framework causes this to have interesting semantics,
2041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// since the sideeffects of the call are only visible in opt mode, and not
2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in debug mode.
2061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In practice, this can be used to test functions that utilize the
2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LOG(DFATAL) macro using the following style:
2091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// int DieInDebugOr12(int* sideeffect) {
2111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   if (sideeffect) {
2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     *sideeffect = 12;
2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   }
2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   LOG(DFATAL) << "death";
2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   return 12;
2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// }
2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TEST(TestCase, TestDieOr12WorksInDgbAndOpt) {
2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   int sideeffect = 0;
2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   // Only asserts in dbg.
2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death");
2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// #ifdef NDEBUG
2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   // opt-mode has sideeffect visible.
2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   EXPECT_EQ(12, sideeffect);
2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// #else
2271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   // dbg-mode no visible sideeffect.
2281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   EXPECT_EQ(0, sideeffect);
2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// #endif
2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// }
2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This will assert that DieInDebugReturn12InOpt() crashes in debug
2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// mode, usually due to a DCHECK or LOG(DFATAL), but returns the
2341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// appropriate fallback value (12 in this case) in opt mode. If you
2351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// need to test that a function has appropriate side-effects in opt
2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// mode, include assertions against the side-effects.  A general
2371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// pattern for this is:
2381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_DEBUG_DEATH({
2401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   // Side-effects here will have an effect after this statement in
2411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   // opt mode, but none in debug mode.
2421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   EXPECT_EQ(12, DieInDebugOr12(&sideeffect));
2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// }, "death");
2441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
24541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifdef NDEBUG
2461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
24741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  define EXPECT_DEBUG_DEATH(statement, regex) \
24841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  do { statement; } while (::testing::internal::AlwaysFalse())
2491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
25041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  define ASSERT_DEBUG_DEATH(statement, regex) \
25141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  do { statement; } while (::testing::internal::AlwaysFalse())
2521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
25341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else
2541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
25541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  define EXPECT_DEBUG_DEATH(statement, regex) \
2561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(statement, regex)
2571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
25841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  define ASSERT_DEBUG_DEATH(statement, regex) \
2591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_DEATH(statement, regex)
2601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
26141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // NDEBUG for EXPECT_DEBUG_DEATH
2621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_DEATH_TEST
26341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
26441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// EXPECT_DEATH_IF_SUPPORTED(statement, regex) and
26541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if
26641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// death tests are supported; otherwise they just issue a warning.  This is
26741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// useful when you are combining death test assertions with normal test
26841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// assertions in one test.
26941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_DEATH_TEST
27041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
27141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    EXPECT_DEATH(statement, regex)
27241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
27341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    ASSERT_DEATH(statement, regex)
27441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else
27541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
27641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, )
27741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \
27841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, return)
27941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif
28041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
2811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace testing
2821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_
284