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 54fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesnamespace internal { 55fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 56fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Returns a Boolean value indicating whether the caller is currently 57fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// executing in the context of the death test child process. Tools such as 58fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Valgrind heap checkers may need this to modify their behavior in death 59fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// tests. IMPORTANT: This is an internal utility. Using it may break the 60fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// implementation of death tests. User code MUST NOT use it. 61fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesGTEST_API_ bool InDeathTestChild(); 62fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 63fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes} // namespace internal 64fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The following macros are useful for writing death tests. 661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is 681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// executed: 691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1. It generates a warning if there is more than one active 711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// thread. This is because it's safe to fork() or clone() only 721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// when there is a single thread. 731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2. The parent process clone()s a sub-process and runs the death 751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test in it; the sub-process exits with code 0 at the end of the 761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// death test, if it hasn't exited already. 771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3. The parent process waits for the sub-process to terminate. 791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 4. The parent process checks the exit code and error message of 811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the sub-process. 821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Examples: 841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ASSERT_DEATH(server.SendMessage(56, "Hello"), "Invalid port number"); 861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// for (int i = 0; i < 5; i++) { 871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_DEATH(server.ProcessRequest(i), 881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "Invalid request .* in ProcessRequest()") 89fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// << "Failed to die on request " << i; 901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), "Exiting"); 931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// bool KilledBySIGHUP(int exit_code) { 951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP; 961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, "Hanging up!"); 991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On the regular expressions used in death tests: 1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On POSIX-compliant systems (*nix), we use the <regex.h> library, 1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// which uses the POSIX extended regex syntax. 1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On other platforms (e.g. Windows), we only support a simple regex 1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// syntax implemented as part of Google Test. This limited 1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// implementation should be enough most of the time when writing 1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// death tests; though it lacks many features you can find in PCRE 1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// or POSIX extended regex syntax. For example, we don't support 1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// union ("x|y"), grouping ("(xy)"), brackets ("[xy]"), and 1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// repetition count ("x{5,7}"), among others. 1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Below is the syntax that we do support. We chose it to be a 1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// subset of both PCRE and POSIX extended regex, so it's easy to 1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// learn wherever you come from. In the following: 'A' denotes a 1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// literal character, period (.), or a single \\ escape sequence; 1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 'x' and 'y' denote regular expressions; 'm' and 'n' are for 1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// natural numbers. 1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// c matches any literal character c 1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// \\d matches any decimal digit 1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// \\D matches any character that's not a decimal digit 1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// \\f matches \f 1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// \\n matches \n 1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// \\r matches \r 1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// \\s matches any ASCII whitespace, including \n 1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// \\S matches any character that's not a whitespace 1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// \\t matches \t 1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// \\v matches \v 1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// \\w matches any letter, _, or decimal digit 1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// \\W matches any character that \\w doesn't match 1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// \\c matches any literal character c, which must be a punctuation 1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// . matches any single character except \n 1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A? matches 0 or 1 occurrences of A 1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A* matches 0 or many occurrences of A 1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A+ matches 1 or many occurrences of A 1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ^ matches the beginning of a string (not that of each line) 1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// $ matches the end of a string (not that of each line) 1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// xy matches x followed by y 1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If you accidentally use PCRE or POSIX extended regex features 1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// not implemented by us, you will get a run-time failure. In that 1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// case, please try to rewrite your regular expression within the 1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// above syntax. 1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This implementation is *not* meant to be as highly tuned or robust 1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// as a compiled regex library, but should perform well enough for a 1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// death test, which already incurs significant overhead by launching 1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// a child process. 1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Known caveats: 1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A "threadsafe" style death test obtains the path to the test 1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// program from argv[0] and re-executes it in the sub-process. For 1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// simplicity, the current implementation doesn't search the PATH 1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// when launching the sub-process. This means that the user must 1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// invoke the test program via a path that contains at least one 1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// path separator (e.g. path/to/foo_test and 1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// /absolute/path/to/bar_test are fine, but foo_test is not). This 1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is rarely a problem as people usually don't put the test binary 1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// directory in PATH. 1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TODO(wan@google.com): make thread-safe death tests search the PATH. 1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Asserts that a given statement causes the program to exit, with an 1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// integer exit status that satisfies predicate, and emitting error output 1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// that matches regex. 16841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_EXIT(statement, predicate, regex) \ 16941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_FATAL_FAILURE_) 1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Like ASSERT_EXIT, but continues on to successive tests in the 1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test case, if any: 17341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define EXPECT_EXIT(statement, predicate, regex) \ 17441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_NONFATAL_FAILURE_) 1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Asserts that a given statement causes the program to exit, either by 1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// explicitly exiting with a nonzero exit code or being killed by a 1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// signal, and emitting error output that matches regex. 17941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_DEATH(statement, regex) \ 18041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex) 1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Like ASSERT_DEATH, but continues on to successive tests in the 1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test case, if any: 18441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define EXPECT_DEATH(statement, regex) \ 18541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex) 1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*: 1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that an exit code describes a normal exit with a given exit code. 19041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ ExitedWithCode { 1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania explicit ExitedWithCode(int exit_code); 1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool operator()(int exit_status) const; 1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 19541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // No implementation - assignment is unsupported. 19641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void operator=(const ExitedWithCode& other); 19741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 1981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int exit_code_; 1991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 2001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 20141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if !GTEST_OS_WINDOWS 2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that an exit code describes an exit due to termination by a 2031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// given signal. 20441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ KilledBySignal { 2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 2061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania explicit KilledBySignal(int signum); 2071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool operator()(int exit_status) const; 2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 2091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const int signum_; 2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 21141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // !GTEST_OS_WINDOWS 2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode. 2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The death testing framework causes this to have interesting semantics, 2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// since the sideeffects of the call are only visible in opt mode, and not 2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in debug mode. 2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In practice, this can be used to test functions that utilize the 2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LOG(DFATAL) macro using the following style: 2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// int DieInDebugOr12(int* sideeffect) { 2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// if (sideeffect) { 2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// *sideeffect = 12; 2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LOG(DFATAL) << "death"; 2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// return 12; 2271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 2281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TEST(TestCase, TestDieOr12WorksInDgbAndOpt) { 2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// int sideeffect = 0; 2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// // Only asserts in dbg. 2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), "death"); 2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// #ifdef NDEBUG 2351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// // opt-mode has sideeffect visible. 2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_EQ(12, sideeffect); 2371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// #else 2381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// // dbg-mode no visible sideeffect. 2391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_EQ(0, sideeffect); 2401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// #endif 2411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// } 2421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This will assert that DieInDebugReturn12InOpt() crashes in debug 2441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// mode, usually due to a DCHECK or LOG(DFATAL), but returns the 2451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// appropriate fallback value (12 in this case) in opt mode. If you 2461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// need to test that a function has appropriate side-effects in opt 2471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// mode, include assertions against the side-effects. A general 2481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// pattern for this is: 2491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_DEBUG_DEATH({ 2511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// // Side-effects here will have an effect after this statement in 2521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// // opt mode, but none in debug mode. 2531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_EQ(12, DieInDebugOr12(&sideeffect)); 2541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// }, "death"); 2551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 25641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifdef NDEBUG 2571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 25841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define EXPECT_DEBUG_DEATH(statement, regex) \ 259fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes GTEST_EXECUTE_STATEMENT_(statement, regex) 2601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 26141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_DEBUG_DEATH(statement, regex) \ 262fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes GTEST_EXECUTE_STATEMENT_(statement, regex) 2631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 26441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else 2651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 26641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define EXPECT_DEBUG_DEATH(statement, regex) \ 2671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_DEATH(statement, regex) 2681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 26941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_DEBUG_DEATH(statement, regex) \ 2701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_DEATH(statement, regex) 2711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 27241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // NDEBUG for EXPECT_DEBUG_DEATH 2731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_DEATH_TEST 27441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 27541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// EXPECT_DEATH_IF_SUPPORTED(statement, regex) and 27641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if 27741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// death tests are supported; otherwise they just issue a warning. This is 27841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// useful when you are combining death test assertions with normal test 27941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// assertions in one test. 28041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_HAS_DEATH_TEST 28141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ 28241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_DEATH(statement, regex) 28341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \ 28441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ASSERT_DEATH(statement, regex) 28541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else 28641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \ 28741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, ) 28841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \ 28941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, return) 29041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 29141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 2921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace testing 2931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_ 295