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// Tests for death tests.
331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest-death-test.h"
3541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest.h"
3641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-filepath.h"
3741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
3841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::AlwaysFalse;
3941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::AlwaysTrue;
401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_DEATH_TEST
421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS
4441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  include <direct.h>          // For chdir().
4541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else
4641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  include <unistd.h>
4741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  include <sys/wait.h>        // For waitpid.
4841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  include <limits>            // For std::numeric_limits.
4941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // GTEST_OS_WINDOWS
501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <limits.h>
5241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <signal.h>
5341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <stdio.h>
541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include "gtest/gtest-spi.h"
561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Indicates that this translation unit is part of Google Test's
581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// implementation.  It must come before gtest-internal-inl.h is
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// included, or there will be a compiler error.  This trick is to
601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// prevent a user from accidentally including gtest-internal-inl.h in
611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// his code.
6241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_IMPLEMENTATION_ 1
6341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include "src/gtest-internal-inl.h"
6441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# undef GTEST_IMPLEMENTATION_
651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotnamespace posix = ::testing::internal::posix;
671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::Message;
691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing testing::internal::DeathTest;
701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing testing::internal::DeathTestFactory;
711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing testing::internal::FilePath;
7241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::GetLastErrnoDescription;
7341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::GetUnitTestImpl;
741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing testing::internal::ParseNaturalNumber;
751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing testing::internal::String;
761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing {
781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A helper class whose objects replace the death test factory for a
811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// single UnitTest object during their lifetimes.
821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ReplaceDeathTestFactory {
831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
8441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  explicit ReplaceDeathTestFactory(DeathTestFactory* new_factory)
8541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      : unit_test_impl_(GetUnitTestImpl()) {
8641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    old_factory_ = unit_test_impl_->death_test_factory_.release();
8741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    unit_test_impl_->death_test_factory_.reset(new_factory);
881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ~ReplaceDeathTestFactory() {
9141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    unit_test_impl_->death_test_factory_.release();
9241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    unit_test_impl_->death_test_factory_.reset(old_factory_);
931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Prevents copying ReplaceDeathTestFactory objects.
961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ReplaceDeathTestFactory(const ReplaceDeathTestFactory&);
971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  void operator=(const ReplaceDeathTestFactory&);
981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  UnitTestImpl* unit_test_impl_;
1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  DeathTestFactory* old_factory_;
1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace internal
1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace testing
1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid DieWithMessage(const ::std::string& message) {
10741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  fprintf(stderr, "%s", message.c_str());
10841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  fflush(stderr);  // Make sure the text is printed before the process exits.
10941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
11041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // We call _exit() instead of exit(), as the former is a direct
11141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // system call and thus safer in the presence of threads.  exit()
11241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // will invoke user-defined exit-hooks, which may do dangerous
11341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // things that conflict with death tests.
11441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  //
11541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Some compilers can recognize that _exit() never returns and issue the
11641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // 'unreachable code' warning for code following this function, unless
11741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // fooled by a fake condition.
11841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (AlwaysTrue())
11941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    _exit(1);
12041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
12141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
12241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid DieInside(const ::std::string& function) {
12341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  DieWithMessage("death inside " + function + "().");
12441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
12541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that death tests work.
1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestForDeathTest : public testing::Test {
1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected:
1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  TestForDeathTest() : original_dir_(FilePath::GetCurrentDir()) {}
1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ~TestForDeathTest() {
13341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    posix::ChDir(original_dir_.c_str());
1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // A static member function that's expected to die.
13741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  static void StaticMemberFunction() { DieInside("StaticMemberFunction"); }
1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // A method of the test fixture that may die.
1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  void MemberFunction() {
14141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (should_die_)
14241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      DieInside("MemberFunction");
1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // True iff MemberFunction() should die.
1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool should_die_;
1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const FilePath original_dir_;
1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A class with a member function that may die.
1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass MayDie {
1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  explicit MayDie(bool should_die) : should_die_(should_die) {}
1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // A member function that may die.
1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  void MemberFunction() const {
15741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if (should_die_)
15841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      DieInside("MayDie::MemberFunction");
1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // True iff MemberFunction() should die.
1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool should_die_;
1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A global function that's expected to die.
16741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid GlobalFunction() { DieInside("GlobalFunction"); }
1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A non-void function that's expected to die.
1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint NonVoidFunction() {
17141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  DieInside("NonVoidFunction");
1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return 1;
1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A unary function that may die.
1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid DieIf(bool should_die) {
17741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (should_die)
17841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    DieInside("DieIf");
1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A binary function that may die.
1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool DieIfLessThan(int x, int y) {
1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (x < y) {
18441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    DieInside("DieIfLessThan");
1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return true;
1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture.
1901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid DeathTestSubroutine() {
1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(GlobalFunction(), "death.*GlobalFunction");
1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_DEATH(GlobalFunction(), "death.*GlobalFunction");
1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Death in dbg, not opt.
1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint DieInDebugElse12(int* sideeffect) {
1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (sideeffect) *sideeffect = 12;
19841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
19941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifndef NDEBUG
20041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
20141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  DieInside("DieInDebugElse12");
20241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
20341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // NDEBUG
20441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return 12;
2061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
20841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS
2091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the ExitedWithCode predicate.
2111be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExitStatusPredicateTest, ExitedWithCode) {
2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // On Windows, the process's exit code is the same as its exit status,
2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // so the predicate just compares the its input with its parameter.
2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(testing::ExitedWithCode(0)(0));
2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(testing::ExitedWithCode(1)(1));
2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(testing::ExitedWithCode(42)(42));
2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(testing::ExitedWithCode(0)(1));
2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(testing::ExitedWithCode(1)(0));
2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
22141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else
2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the exit status of a process that calls _exit(2) with a
2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// given exit code.  This is a helper function for the
2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ExitStatusPredicateTest test suite.
2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic int NormalExitStatus(int exit_code) {
2271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  pid_t child_pid = fork();
2281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (child_pid == 0) {
2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    _exit(exit_code);
2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int status;
2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  waitpid(child_pid, &status, 0);
2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return status;
2341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the exit status of a process that raises a given signal.
2371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// If the signal does not cause the process to die, then it returns
2381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// instead the exit status of a process that exits normally with exit
2391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// code 1.  This is a helper function for the ExitStatusPredicateTest
2401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test suite.
2411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic int KilledExitStatus(int signum) {
2421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  pid_t child_pid = fork();
2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (child_pid == 0) {
2441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    raise(signum);
2451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    _exit(1);
2461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
2471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int status;
2481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  waitpid(child_pid, &status, 0);
2491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return status;
2501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the ExitedWithCode predicate.
2531be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExitStatusPredicateTest, ExitedWithCode) {
2541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const int status0  = NormalExitStatus(0);
2551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const int status1  = NormalExitStatus(1);
2561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const int status42 = NormalExitStatus(42);
2571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const testing::ExitedWithCode pred0(0);
2581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const testing::ExitedWithCode pred1(1);
2591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const testing::ExitedWithCode pred42(42);
2601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_PRED1(pred0,  status0);
2611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_PRED1(pred1,  status1);
2621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_PRED1(pred42, status42);
2631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(pred0(status1));
2641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(pred42(status0));
2651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(pred1(status42));
2661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the KilledBySignal predicate.
2691be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExitStatusPredicateTest, KilledBySignal) {
2701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const int status_segv = KilledExitStatus(SIGSEGV);
2711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const int status_kill = KilledExitStatus(SIGKILL);
2721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const testing::KilledBySignal pred_segv(SIGSEGV);
2731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const testing::KilledBySignal pred_kill(SIGKILL);
2741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_PRED1(pred_segv, status_segv);
2751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_PRED1(pred_kill, status_kill);
2761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(pred_segv(status_kill));
2771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(pred_kill(status_segv));
2781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
2791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
28041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // GTEST_OS_WINDOWS
2811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that the death test macros expand to code which may or may not
2831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// be followed by operator<<, and that in either case the complete text
2841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// comprises only a single C++ statement.
2851be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, SingleStatement) {
28641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (AlwaysFalse())
2871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // This would fail if executed; this is a compilation test only
2881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ASSERT_DEATH(return, "");
2891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
29041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (AlwaysTrue())
2911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    EXPECT_DEATH(_exit(1), "");
2921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  else
2931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // This empty "else" branch is meant to ensure that EXPECT_DEATH
2941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // doesn't expand into an "if" statement without an "else"
2951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ;
2961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
29741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (AlwaysFalse())
2981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ASSERT_DEATH(return, "") << "did not die";
2991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
30041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (AlwaysFalse())
3011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ;
3021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  else
3031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    EXPECT_DEATH(_exit(1), "") << 1 << 2 << 3;
3041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid DieWithEmbeddedNul() {
30741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  fprintf(stderr, "Hello%cmy null world.\n", '\0');
3081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fflush(stderr);
3091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  _exit(1);
3101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
31241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_USES_PCRE
3131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_DEATH and ASSERT_DEATH work when the error
3141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// message has a NUL character in it.
3151be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, EmbeddedNulInMessage) {
3161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // TODO(wan@google.com): <regex.h> doesn't support matching strings
3171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // with embedded NUL characters - find a way to workaround it.
31841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_DEATH(DieWithEmbeddedNul(), "my null world");
31941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ASSERT_DEATH(DieWithEmbeddedNul(), "my null world");
3201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
32141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // GTEST_USES_PCRE
3221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that death test macros expand to code which interacts well with switch
3241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// statements.
3251be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, SwitchStatement) {
3261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Microsoft compiler usually complains about switch statements without
3271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// case labels. We suppress that warning for this test.
32841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifdef _MSC_VER
32941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  pragma warning(push)
33041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  pragma warning(disable: 4065)
33141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // _MSC_VER
3321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  switch (0)
3341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    default:
3351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      ASSERT_DEATH(_exit(1), "") << "exit in default switch handler";
3361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  switch (0)
3381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    case 0:
3391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      EXPECT_DEATH(_exit(1), "") << "exit in switch case";
3401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
34141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifdef _MSC_VER
34241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  pragma warning(pop)
34341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // _MSC_VER
3441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a static member function can be used in a "fast" style
3471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// death test.
3481be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, StaticMemberFunctionFastStyle) {
3491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  testing::GTEST_FLAG(death_test_style) = "fast";
3501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember");
3511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a method of the test fixture can be used in a "fast"
3541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// style death test.
3551be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, MemberFunctionFastStyle) {
3561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  testing::GTEST_FLAG(death_test_style) = "fast";
3571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  should_die_ = true;
3581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction");
3591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
36141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid ChangeToRootDir() { posix::ChDir(GTEST_PATH_SEP_); }
3621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that death tests work even if the current directory has been
3641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// changed.
3651be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, FastDeathTestInChangedDir) {
3661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  testing::GTEST_FLAG(death_test_style) = "fast";
3671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ChangeToRootDir();
3691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
3701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ChangeToRootDir();
3721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_DEATH(_exit(1), "");
3731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Repeats a representative sample of death tests in the "threadsafe" style:
3761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3771be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, StaticMemberFunctionThreadsafeStyle) {
3781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  testing::GTEST_FLAG(death_test_style) = "threadsafe";
3791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember");
3801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3821be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, MemberFunctionThreadsafeStyle) {
3831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  testing::GTEST_FLAG(death_test_style) = "threadsafe";
3841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  should_die_ = true;
3851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction");
3861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3881be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ThreadsafeDeathTestInLoop) {
3891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  testing::GTEST_FLAG(death_test_style) = "threadsafe";
3901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  for (int i = 0; i < 3; ++i)
3921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    EXPECT_EXIT(_exit(i), testing::ExitedWithCode(i), "") << ": i = " << i;
3931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
3941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3951be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ThreadsafeDeathTestInChangedDir) {
3961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  testing::GTEST_FLAG(death_test_style) = "threadsafe";
3971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ChangeToRootDir();
3991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
4001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ChangeToRootDir();
4021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_DEATH(_exit(1), "");
4031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4051be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, MixedStyles) {
4061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  testing::GTEST_FLAG(death_test_style) = "threadsafe";
4071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(_exit(1), "");
4081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  testing::GTEST_FLAG(death_test_style) = "fast";
4091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(_exit(1), "");
4101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace {
4131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool pthread_flag;
4151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid SetPthreadFlag() {
4171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  pthread_flag = true;
4181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}  // namespace
4211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
42241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
4231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4241be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, DoesNotExecuteAtforkHooks) {
4251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (!testing::GTEST_FLAG(death_test_use_fork)) {
4261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    testing::GTEST_FLAG(death_test_style) = "threadsafe";
4271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    pthread_flag = false;
4281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ASSERT_EQ(0, pthread_atfork(&SetPthreadFlag, NULL, NULL));
4291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ASSERT_DEATH(_exit(1), "");
4301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ASSERT_FALSE(pthread_flag);
4311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
4321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
43441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
4351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a method of another class can be used in a death test.
4371be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, MethodOfAnotherClass) {
4381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const MayDie x(true);
4391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_DEATH(x.MemberFunction(), "MayDie\\:\\:MemberFunction");
4401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a global function can be used in a death test.
4431be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, GlobalFunction) {
4441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(GlobalFunction(), "GlobalFunction");
4451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that any value convertible to an RE works as a second
4481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// argument to EXPECT_DEATH.
4491be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, AcceptsAnythingConvertibleToRE) {
4501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static const char regex_c_str[] = "GlobalFunction";
4511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(GlobalFunction(), regex_c_str);
4521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const testing::internal::RE regex(regex_c_str);
4541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(GlobalFunction(), regex);
4551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
45641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_HAS_GLOBAL_STRING
45741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
4581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const string regex_str(regex_c_str);
4591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(GlobalFunction(), regex_str);
4601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
46141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // GTEST_HAS_GLOBAL_STRING
46241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
4631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const ::std::string regex_std_str(regex_c_str);
4641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(GlobalFunction(), regex_std_str);
4651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a non-void function can be used in a death test.
4681be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, NonVoidFunction) {
4691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_DEATH(NonVoidFunction(), "NonVoidFunction");
4701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that functions that take parameter(s) can be used in a death test.
4731be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, FunctionWithParameter) {
4741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(DieIf(true), "DieIf\\(\\)");
4751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(DieIfLessThan(2, 3), "DieIfLessThan");
4761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture.
4791be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, OutsideFixture) {
4801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  DeathTestSubroutine();
4811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that death tests can be done inside a loop.
4841be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, InsideLoop) {
4851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  for (int i = 0; i < 5; i++) {
4861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    EXPECT_DEATH(DieIfLessThan(-1, i), "DieIfLessThan") << "where i == " << i;
4871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
4881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a compound statement can be used in a death test.
4911be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, CompoundStatement) {
4921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH({  // NOLINT
4931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const int x = 2;
4941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const int y = x + 1;
4951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    DieIfLessThan(x, y);
4961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  },
4971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  "DieIfLessThan");
4981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that code that doesn't die causes a death test to fail.
5011be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, DoesNotDie) {
5021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(DieIf(false), "DieIf"),
5031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                          "failed to die");
5041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a death test fails when the error message isn't expected.
5071be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ErrorMessageMismatch) {
5081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_NONFATAL_FAILURE({  // NOLINT
5091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    EXPECT_DEATH(DieIf(true), "DieIfLessThan") << "End of death test message.";
5101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }, "died but not with expected error");
5111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// On exit, *aborted will be true iff the EXPECT_DEATH() statement
5141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// aborted the function.
5151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ExpectDeathTestHelper(bool* aborted) {
5161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  *aborted = true;
5171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(DieIf(false), "DieIf");  // This assertion should fail.
5181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  *aborted = false;
5191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_DEATH doesn't abort the test on failure.
5221be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, EXPECT_DEATH) {
5231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool aborted = true;
5241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_NONFATAL_FAILURE(ExpectDeathTestHelper(&aborted),
5251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                          "failed to die");
5261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(aborted);
5271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that ASSERT_DEATH does abort the test on failure.
5301be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ASSERT_DEATH) {
5311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static bool aborted;
5321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FATAL_FAILURE({  // NOLINT
5331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    aborted = true;
5341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ASSERT_DEATH(DieIf(false), "DieIf");  // This assertion should fail.
5351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    aborted = false;
5361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }, "failed to die");
5371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(aborted);
5381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_DEATH evaluates the arguments exactly once.
5411be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, SingleEvaluation) {
5421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int x = 3;
5431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(DieIf((++x) == 4), "DieIf");
5441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* regex = "DieIf";
5461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* regex_save = regex;
5471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(DieIfLessThan(3, 4), regex++);
5481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(regex_save + 1, regex);
5491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that run-away death tests are reported as failures.
55241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, RunawayIsFailure) {
5531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(static_cast<void>(0), "Foo"),
5541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                          "failed to die.");
55541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
5561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
55741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that death tests report executing 'return' in the statement as
55841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// failure.
55941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, ReturnIsFailure) {
5601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FATAL_FAILURE(ASSERT_DEATH(return, "Bar"),
5611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                       "illegal return in test statement.");
5621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_DEBUG_DEATH works as expected,
5651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// that is, in debug mode, it:
5661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1. Asserts on death.
5671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2. Has no side effect.
5681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
5691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// And in opt mode, it:
5701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1.  Has side effects but does not assert.
5711be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, TestExpectDebugDeath) {
5721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int sideeffect = 0;
5731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect),
5751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                     "death.*DieInDebugElse12");
5761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
57741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifdef NDEBUG
57841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
5791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Checks that the assignment occurs in opt mode (sideeffect).
5801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(12, sideeffect);
58141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
58241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else
58341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
5841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Checks that the assignment does not occur in dbg mode (no sideeffect).
5851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(0, sideeffect);
58641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
58741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif
5881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that ASSERT_DEBUG_DEATH works as expected
5911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In debug mode:
5921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1. Asserts on debug death.
5931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2. Has no side effect.
5941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
5951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In opt mode:
5961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1. Has side effects and returns the expected value (12).
5971be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, TestAssertDebugDeath) {
5981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int sideeffect = 0;
5991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_DEBUG_DEATH({  // NOLINT
6011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Tests that the return value is 12 in opt mode.
6021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    EXPECT_EQ(12, DieInDebugElse12(&sideeffect));
6031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Tests that the side effect occurred in opt mode.
6041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    EXPECT_EQ(12, sideeffect);
6051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }, "death.*DieInDebugElse12");
6061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
60741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifdef NDEBUG
60841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
6091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Checks that the assignment occurs in opt mode (sideeffect).
6101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(12, sideeffect);
61141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
61241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else
61341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
6141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Checks that the assignment does not occur in dbg mode (no sideeffect).
6151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(0, sideeffect);
61641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
61741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif
6181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
62041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# ifndef NDEBUG
6211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid ExpectDebugDeathHelper(bool* aborted) {
6231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  *aborted = true;
6241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEBUG_DEATH(return, "") << "This is expected to fail.";
6251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  *aborted = false;
6261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
62841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  if GTEST_OS_WINDOWS
6291be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(PopUpDeathTest, DoesNotShowPopUpOnAbort) {
6301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  printf("This test should be considered failing if it shows "
6311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania         "any pop-up dialogs.\n");
6321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  fflush(stdout);
6331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH({
6351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    testing::GTEST_FLAG(catch_exceptions) = false;
6361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    abort();
6371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }, "");
6381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
63941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#  endif  // GTEST_OS_WINDOWS
6401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_DEBUG_DEATH in debug mode does not abort
6421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the function.
6431be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ExpectDebugDeathDoesNotAbort) {
6441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool aborted = true;
6451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_NONFATAL_FAILURE(ExpectDebugDeathHelper(&aborted), "");
6461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(aborted);
6471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid AssertDebugDeathHelper(bool* aborted) {
6501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  *aborted = true;
6511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_DEBUG_DEATH(return, "") << "This is expected to fail.";
6521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  *aborted = false;
6531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that ASSERT_DEBUG_DEATH in debug mode aborts the function on
6561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// failure.
6571be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, AssertDebugDeathAborts) {
6581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static bool aborted;
6591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  aborted = false;
6601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted), "");
6611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(aborted);
6621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
66441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // _NDEBUG
6651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the *_EXIT family of macros, using a variety of predicates.
6671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastatic void TestExitMacros() {
6681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EXIT(_exit(1),  testing::ExitedWithCode(1),  "");
6691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_EXIT(_exit(42), testing::ExitedWithCode(42), "");
6701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
67141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS
6721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Of all signals effects on the process exit code, only those of SIGABRT
6741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // are documented on Windows.
6751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // See http://msdn.microsoft.com/en-us/library/dwwzkt4c(VS.71).aspx.
6761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EXIT(raise(SIGABRT), testing::ExitedWithCode(3), "");
67741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
67841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else
67941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
6801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EXIT(raise(SIGKILL), testing::KilledBySignal(SIGKILL), "") << "foo";
6811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_EXIT(raise(SIGUSR2), testing::KilledBySignal(SIGUSR2), "") << "bar";
6821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FATAL_FAILURE({  // NOLINT
6841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ASSERT_EXIT(_exit(0), testing::KilledBySignal(SIGSEGV), "")
6851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        << "This failure is expected, too.";
6861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }, "This failure is expected, too.");
68741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
68841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // GTEST_OS_WINDOWS
6891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_NONFATAL_FAILURE({  // NOLINT
6911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    EXPECT_EXIT(raise(SIGSEGV), testing::ExitedWithCode(0), "")
6921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        << "This failure is expected.";
6931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }, "This failure is expected.");
6941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6961be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ExitMacros) {
6971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  TestExitMacros();
6981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
6991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7001be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, ExitMacrosUsingFork) {
7011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  testing::GTEST_FLAG(death_test_use_fork) = true;
7021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  TestExitMacros();
7031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7051be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TestForDeathTest, InvalidStyle) {
7061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  testing::GTEST_FLAG(death_test_style) = "rococo";
7071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_NONFATAL_FAILURE({  // NOLINT
7081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    EXPECT_DEATH(_exit(0), "") << "This failure is expected.";
7091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }, "This failure is expected.");
7101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
7111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
71241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, DeathTestFailedOutput) {
71341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  testing::GTEST_FLAG(death_test_style) = "fast";
71441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_NONFATAL_FAILURE(
71541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      EXPECT_DEATH(DieWithMessage("death\n"),
71641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                   "expected message"),
71741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "Actual msg:\n"
71841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "[  DEATH   ] death\n");
71941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
72041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
72141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, DeathTestUnexpectedReturnOutput) {
72241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  testing::GTEST_FLAG(death_test_style) = "fast";
72341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_NONFATAL_FAILURE(
72441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      EXPECT_DEATH({
72541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          fprintf(stderr, "returning\n");
72641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          fflush(stderr);
72741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          return;
72841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        }, ""),
72941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "    Result: illegal return in test statement.\n"
73041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      " Error msg:\n"
73141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "[  DEATH   ] returning\n");
73241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
73341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
73441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, DeathTestBadExitCodeOutput) {
73541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  testing::GTEST_FLAG(death_test_style) = "fast";
73641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_NONFATAL_FAILURE(
73741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      EXPECT_EXIT(DieWithMessage("exiting with rc 1\n"),
73841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                  testing::ExitedWithCode(3),
73941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                  "expected message"),
74041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "    Result: died but not with expected exit code:\n"
74141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "            Exited with exit status 1\n"
74241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "Actual msg:\n"
74341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "[  DEATH   ] exiting with rc 1\n");
74441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
74541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
74641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, DeathTestMultiLineMatchFail) {
74741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  testing::GTEST_FLAG(death_test_style) = "fast";
74841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_NONFATAL_FAILURE(
74941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"),
75041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                   "line 1\nxyz\nline 3\n"),
75141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "Actual msg:\n"
75241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "[  DEATH   ] line 1\n"
75341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "[  DEATH   ] line 2\n"
75441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      "[  DEATH   ] line 3\n");
75541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
75641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
75741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_F(TestForDeathTest, DeathTestMultiLineMatchPass) {
75841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  testing::GTEST_FLAG(death_test_style) = "fast";
75941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"),
76041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot               "line 1\nline 2\nline 3\n");
76141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
76241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
7631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A DeathTestFactory that returns MockDeathTests.
7641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass MockDeathTestFactory : public DeathTestFactory {
7651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
7661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  MockDeathTestFactory();
7671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual bool Create(const char* statement,
7681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      const ::testing::internal::RE* regex,
7691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      const char* file, int line, DeathTest** test);
7701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Sets the parameters for subsequent calls to Create.
7721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  void SetParameters(bool create, DeathTest::TestRole role,
7731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                     int status, bool passed);
7741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Accessors.
7761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int AssumeRoleCalls() const { return assume_role_calls_; }
7771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int WaitCalls() const { return wait_calls_; }
7781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int PassedCalls() const { return passed_args_.size(); }
7791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool PassedArgument(int n) const { return passed_args_[n]; }
7801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int AbortCalls() const { return abort_args_.size(); }
7811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  DeathTest::AbortReason AbortArgument(int n) const {
7821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return abort_args_[n];
7831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
7841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool TestDeleted() const { return test_deleted_; }
7851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
7871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  friend class MockDeathTest;
7881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // If true, Create will return a MockDeathTest; otherwise it returns
7891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // NULL.
7901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool create_;
7911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The value a MockDeathTest will return from its AssumeRole method.
7921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  DeathTest::TestRole role_;
7931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The value a MockDeathTest will return from its Wait method.
7941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int status_;
7951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The value a MockDeathTest will return from its Passed method.
7961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool passed_;
7971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Number of times AssumeRole was called.
7991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int assume_role_calls_;
8001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Number of times Wait was called.
8011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int wait_calls_;
8021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The arguments to the calls to Passed since the last call to
8031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // SetParameters.
8041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  std::vector<bool> passed_args_;
8051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The arguments to the calls to Abort since the last call to
8061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // SetParameters.
8071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  std::vector<DeathTest::AbortReason> abort_args_;
8081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // True if the last MockDeathTest returned by Create has been
8091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // deleted.
8101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool test_deleted_;
8111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
8121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A DeathTest implementation useful in testing.  It returns values set
8151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// at its creation from its various inherited DeathTest methods, and
8161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// reports calls to those methods to its parent MockDeathTestFactory
8171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// object.
8181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass MockDeathTest : public DeathTest {
8191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
8201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  MockDeathTest(MockDeathTestFactory *parent,
8211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                TestRole role, int status, bool passed) :
8221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      parent_(parent), role_(role), status_(status), passed_(passed) {
8231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
8241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ~MockDeathTest() {
8251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    parent_->test_deleted_ = true;
8261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
8271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual TestRole AssumeRole() {
8281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ++parent_->assume_role_calls_;
8291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return role_;
8301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
8311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual int Wait() {
8321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ++parent_->wait_calls_;
8331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return status_;
8341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
8351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual bool Passed(bool exit_status_ok) {
8361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    parent_->passed_args_.push_back(exit_status_ok);
8371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return passed_;
8381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
8391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual void Abort(AbortReason reason) {
8401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    parent_->abort_args_.push_back(reason);
8411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
8421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
8431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  MockDeathTestFactory* const parent_;
8441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const TestRole role_;
8451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const int status_;
8461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const bool passed_;
8471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
8481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// MockDeathTestFactory constructor.
8511be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaMockDeathTestFactory::MockDeathTestFactory()
8521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    : create_(true),
8531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      role_(DeathTest::OVERSEE_TEST),
8541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      status_(0),
8551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      passed_(true),
8561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      assume_role_calls_(0),
8571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      wait_calls_(0),
8581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      passed_args_(),
8591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      abort_args_() {
8601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
8611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Sets the parameters for subsequent calls to Create.
8641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid MockDeathTestFactory::SetParameters(bool create,
8651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                         DeathTest::TestRole role,
8661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                         int status, bool passed) {
8671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  create_ = create;
8681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  role_ = role;
8691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  status_ = status;
8701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  passed_ = passed;
8711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  assume_role_calls_ = 0;
8731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  wait_calls_ = 0;
8741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  passed_args_.clear();
8751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  abort_args_.clear();
8761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
8771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Sets test to NULL (if create_ is false) or to the address of a new
8801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// MockDeathTest object with parameters taken from the last call
8811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to SetParameters (if create_ is true).  Always returns true.
88241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool MockDeathTestFactory::Create(const char* /*statement*/,
88341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                  const ::testing::internal::RE* /*regex*/,
88441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                  const char* /*file*/,
88541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                  int /*line*/,
8861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                  DeathTest** test) {
8871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  test_deleted_ = false;
8881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (create_) {
8891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    *test = new MockDeathTest(this, role_, status_, passed_);
8901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  } else {
8911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    *test = NULL;
8921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
8931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return true;
8941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
8951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A test fixture for testing the logic of the GTEST_DEATH_TEST_ macro.
8971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// It installs a MockDeathTestFactory that is used for the duration
8981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of the test case.
8991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass MacroLogicDeathTest : public testing::Test {
9001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected:
9011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static testing::internal::ReplaceDeathTestFactory* replacer_;
9021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static MockDeathTestFactory* factory_;
9031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static void SetUpTestCase() {
9051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    factory_ = new MockDeathTestFactory;
90641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    replacer_ = new testing::internal::ReplaceDeathTestFactory(factory_);
9071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
9081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static void TearDownTestCase() {
9101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    delete replacer_;
9111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    replacer_ = NULL;
9121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    delete factory_;
9131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    factory_ = NULL;
9141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
9151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Runs a death test that breaks the rules by returning.  Such a death
9171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // test cannot be run directly from a test routine that uses a
9181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // MockDeathTest, or the remainder of the routine will not be executed.
9191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static void RunReturningDeathTest(bool* flag) {
9201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ASSERT_DEATH({  // NOLINT
9211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      *flag = true;
9221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return;
9231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }, "");
9241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
9251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
9261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatesting::internal::ReplaceDeathTestFactory* MacroLogicDeathTest::replacer_
9281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    = NULL;
9291be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaMockDeathTestFactory* MacroLogicDeathTest::factory_ = NULL;
9301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Test that nothing happens when the factory doesn't return a DeathTest:
9331be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MacroLogicDeathTest, NothingHappens) {
9341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool flag = false;
9351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  factory_->SetParameters(false, DeathTest::OVERSEE_TEST, 0, true);
9361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(flag = true, "");
9371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(flag);
9381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(0, factory_->AssumeRoleCalls());
9391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(0, factory_->WaitCalls());
9401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(0, factory_->PassedCalls());
9411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(0, factory_->AbortCalls());
9421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(factory_->TestDeleted());
9431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
9441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Test that the parent process doesn't run the death test code,
9461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and that the Passed method returns false when the (simulated)
9471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// child process exits with status 0:
9481be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MacroLogicDeathTest, ChildExitsSuccessfully) {
9491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool flag = false;
9501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  factory_->SetParameters(true, DeathTest::OVERSEE_TEST, 0, true);
9511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(flag = true, "");
9521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(flag);
9531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(1, factory_->AssumeRoleCalls());
9541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(1, factory_->WaitCalls());
9551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_EQ(1, factory_->PassedCalls());
9561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(factory_->PassedArgument(0));
9571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(0, factory_->AbortCalls());
9581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(factory_->TestDeleted());
9591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
9601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that the Passed method was given the argument "true" when
9621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the (simulated) child process exits with status 1:
9631be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MacroLogicDeathTest, ChildExitsUnsuccessfully) {
9641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool flag = false;
9651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  factory_->SetParameters(true, DeathTest::OVERSEE_TEST, 1, true);
9661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(flag = true, "");
9671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(flag);
9681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(1, factory_->AssumeRoleCalls());
9691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(1, factory_->WaitCalls());
9701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_EQ(1, factory_->PassedCalls());
9711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(factory_->PassedArgument(0));
9721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(0, factory_->AbortCalls());
9731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(factory_->TestDeleted());
9741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
9751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that the (simulated) child process executes the death test
9771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// code, and is aborted with the correct AbortReason if it
9781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// executes a return statement.
9791be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MacroLogicDeathTest, ChildPerformsReturn) {
9801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool flag = false;
9811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  factory_->SetParameters(true, DeathTest::EXECUTE_TEST, 0, true);
9821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  RunReturningDeathTest(&flag);
9831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(flag);
9841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(1, factory_->AssumeRoleCalls());
9851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(0, factory_->WaitCalls());
9861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(0, factory_->PassedCalls());
9871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(1, factory_->AbortCalls());
9881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT,
9891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania            factory_->AbortArgument(0));
9901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(factory_->TestDeleted());
9911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
9921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that the (simulated) child process is aborted with the
9941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// correct AbortReason if it does not die.
9951be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MacroLogicDeathTest, ChildDoesNotDie) {
9961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool flag = false;
9971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  factory_->SetParameters(true, DeathTest::EXECUTE_TEST, 0, true);
9981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(flag = true, "");
9991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(flag);
10001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(1, factory_->AssumeRoleCalls());
10011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(0, factory_->WaitCalls());
10021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(0, factory_->PassedCalls());
10031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // This time there are two calls to Abort: one since the test didn't
10041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // die, and another from the ReturnSentinel when it's destroyed.  The
10051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // sentinel normally isn't destroyed if a test doesn't die, since
10061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // _exit(2) is called in that case by ForkingDeathTest, but not by
10071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // our MockDeathTest.
10081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_EQ(2, factory_->AbortCalls());
10091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(DeathTest::TEST_DID_NOT_DIE,
10101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania            factory_->AbortArgument(0));
10111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT,
10121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania            factory_->AbortArgument(1));
10131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(factory_->TestDeleted());
10141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
10151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a successful death test does not register a successful
10171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test part.
10181be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(SuccessRegistrationDeathTest, NoSuccessPart) {
10191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(_exit(1), "");
102041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count());
10211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
10221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10231be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(StreamingAssertionsDeathTest, DeathTest) {
10241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_DEATH(_exit(1), "") << "unexpected failure";
10251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_DEATH(_exit(1), "") << "unexpected failure";
10261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_NONFATAL_FAILURE({  // NOLINT
10271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    EXPECT_DEATH(_exit(0), "") << "expected failure";
10281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }, "expected failure");
10291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FATAL_FAILURE({  // NOLINT
10301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ASSERT_DEATH(_exit(0), "") << "expected failure";
10311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }, "expected failure");
10321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
10331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
103441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that GetLastErrnoDescription returns an empty string when the
10351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// last error is 0 and non-empty string when it is non-zero.
103641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(GetLastErrnoDescription, GetLastErrnoDescriptionWorks) {
10371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  errno = ENOENT;
103841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_STRNE("", GetLastErrnoDescription().c_str());
10391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  errno = 0;
104041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_STREQ("", GetLastErrnoDescription().c_str());
10411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
10421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
104341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS
10441be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(AutoHandleTest, AutoHandleWorks) {
10451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  HANDLE handle = ::CreateEvent(NULL, FALSE, FALSE, NULL);
10461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_NE(INVALID_HANDLE_VALUE, handle);
10471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Tests that the AutoHandle is correctly initialized with a handle.
10491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  testing::internal::AutoHandle auto_handle(handle);
10501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(handle, auto_handle.Get());
10511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Tests that Reset assigns INVALID_HANDLE_VALUE.
10531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Note that this cannot verify whether the original handle is closed.
10541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  auto_handle.Reset();
10551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(INVALID_HANDLE_VALUE, auto_handle.Get());
10561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Tests that Reset assigns the new handle.
10581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Note that this cannot verify whether the original handle is closed.
10591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  handle = ::CreateEvent(NULL, FALSE, FALSE, NULL);
10601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_NE(INVALID_HANDLE_VALUE, handle);
10611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  auto_handle.Reset(handle);
10621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(handle, auto_handle.Get());
10631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Tests that AutoHandle contains INVALID_HANDLE_VALUE by default.
10651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  testing::internal::AutoHandle auto_handle2;
10661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(INVALID_HANDLE_VALUE, auto_handle2.Get());
10671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
106841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // GTEST_OS_WINDOWS
10691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
107041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS
10711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef unsigned __int64 BiggestParsable;
10721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef signed __int64 BiggestSignedParsable;
10731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst BiggestParsable kBiggestParsableMax = ULLONG_MAX;
107441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst BiggestSignedParsable kBiggestSignedParsableMax = LLONG_MAX;
107541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else
10761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef unsigned long long BiggestParsable;
10771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef signed long long BiggestSignedParsable;
10781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst BiggestParsable kBiggestParsableMax =
10791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ::std::numeric_limits<BiggestParsable>::max();
10801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconst BiggestSignedParsable kBiggestSignedParsableMax =
10811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ::std::numeric_limits<BiggestSignedParsable>::max();
108241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // GTEST_OS_WINDOWS
10831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10841be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ParseNaturalNumberTest, RejectsInvalidFormat) {
10851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  BiggestParsable result = 0;
10861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Rejects non-numbers.
10881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(ParseNaturalNumber(String("non-number string"), &result));
10891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Rejects numbers with whitespace prefix.
10911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(ParseNaturalNumber(String(" 123"), &result));
10921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Rejects negative numbers.
10941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(ParseNaturalNumber(String("-123"), &result));
10951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Rejects numbers starting with a plus sign.
10971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(ParseNaturalNumber(String("+123"), &result));
10981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  errno = 0;
10991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
11001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11011be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ParseNaturalNumberTest, RejectsOverflownNumbers) {
11021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  BiggestParsable result = 0;
11031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(ParseNaturalNumber(String("99999999999999999999999"), &result));
11051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  signed char char_result = 0;
11071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_FALSE(ParseNaturalNumber(String("200"), &char_result));
11081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  errno = 0;
11091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
11101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11111be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ParseNaturalNumberTest, AcceptsValidNumbers) {
11121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  BiggestParsable result = 0;
11131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  result = 0;
11151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_TRUE(ParseNaturalNumber(String("123"), &result));
111641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_EQ(123U, result);
11171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Check 0 as an edge case.
11191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  result = 1;
11201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_TRUE(ParseNaturalNumber(String("0"), &result));
112141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_EQ(0U, result);
11221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  result = 1;
11241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_TRUE(ParseNaturalNumber(String("00000"), &result));
112541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_EQ(0U, result);
11261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
11271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11281be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ParseNaturalNumberTest, AcceptsTypeLimits) {
11291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message msg;
11301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  msg << kBiggestParsableMax;
11311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  BiggestParsable result = 0;
11331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(ParseNaturalNumber(msg.GetString(), &result));
11341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(kBiggestParsableMax, result);
11351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message msg2;
11371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  msg2 << kBiggestSignedParsableMax;
11381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  BiggestSignedParsable signed_result = 0;
11401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(ParseNaturalNumber(msg2.GetString(), &signed_result));
11411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(kBiggestSignedParsableMax, signed_result);
11421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message msg3;
11441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  msg3 << INT_MAX;
11451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  int int_result = 0;
11471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(ParseNaturalNumber(msg3.GetString(), &int_result));
11481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(INT_MAX, int_result);
11491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  Message msg4;
11511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  msg4 << UINT_MAX;
11521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  unsigned int uint_result = 0;
11541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_TRUE(ParseNaturalNumber(msg4.GetString(), &uint_result));
11551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(UINT_MAX, uint_result);
11561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
11571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11581be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ParseNaturalNumberTest, WorksForShorterIntegers) {
11591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  short short_result = 0;
11601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_TRUE(ParseNaturalNumber(String("123"), &short_result));
11611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(123, short_result);
11621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  signed char char_result = 0;
11641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_TRUE(ParseNaturalNumber(String("123"), &char_result));
11651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  EXPECT_EQ(123, char_result);
11661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
11671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
116841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS
11691be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(EnvironmentTest, HandleFitsIntoSizeT) {
11701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // TODO(vladl@google.com): Remove this test after this condition is verified
11711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // in a static assertion in gtest-death-test.cc in the function
11721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // GetStatusFileDescriptor.
11731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ASSERT_TRUE(sizeof(HANDLE) <= sizeof(size_t));
11741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
117541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif  // GTEST_OS_WINDOWS
117641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
117741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED trigger
117841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// failures when death tests are available on the system.
117941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(ConditionalDeathMacrosDeathTest, ExpectsDeathWhenDeathTestsAvailable) {
118041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestExpectMacro"),
118141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                            "death inside CondDeathTestExpectMacro");
118241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ASSERT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestAssertMacro"),
118341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                            "death inside CondDeathTestAssertMacro");
118441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
118541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Empty statement will not crash, which must trigger a failure.
118641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_NONFATAL_FAILURE(EXPECT_DEATH_IF_SUPPORTED(;, ""), "");
118741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_FATAL_FAILURE(ASSERT_DEATH_IF_SUPPORTED(;, ""), "");
118841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
11891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
119041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else
119141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
119241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::CaptureStderr;
119341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::GetCapturedStderr;
119441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::String;
119541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
119641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED are still
119741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// defined but do not trigger failures when death tests are not available on
119841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the system.
119941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(ConditionalDeathMacrosTest, WarnsWhenDeathTestsNotAvailable) {
120041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // Empty statement will not crash, but that should not trigger a failure
120141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // when death tests are not supported.
120241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  CaptureStderr();
120341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_DEATH_IF_SUPPORTED(;, "");
120441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  String output = GetCapturedStderr();
120541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ASSERT_TRUE(NULL != strstr(output.c_str(),
120641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                             "Death tests are not supported on this platform"));
120741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ASSERT_TRUE(NULL != strstr(output.c_str(), ";"));
120841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
120941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // The streamed message should not be printed as there is no test failure.
121041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  CaptureStderr();
121141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_DEATH_IF_SUPPORTED(;, "") << "streamed message";
121241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  output = GetCapturedStderr();
121341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ASSERT_TRUE(NULL == strstr(output.c_str(), "streamed message"));
121441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
121541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  CaptureStderr();
121641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ASSERT_DEATH_IF_SUPPORTED(;, "");  // NOLINT
121741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  output = GetCapturedStderr();
121841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ASSERT_TRUE(NULL != strstr(output.c_str(),
121941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                             "Death tests are not supported on this platform"));
122041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ASSERT_TRUE(NULL != strstr(output.c_str(), ";"));
122141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
122241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  CaptureStderr();
122341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ASSERT_DEATH_IF_SUPPORTED(;, "") << "streamed message";  // NOLINT
122441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  output = GetCapturedStderr();
122541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ASSERT_TRUE(NULL == strstr(output.c_str(), "streamed message"));
122641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
122741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
122841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid FuncWithAssert(int* n) {
122941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ASSERT_DEATH_IF_SUPPORTED(return;, "");
123041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  (*n)++;
123141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
123241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
123341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that ASSERT_DEATH_IF_SUPPORTED does not return from the current
123441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// function (as ASSERT_DEATH does) if death tests are not supported.
123541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(ConditionalDeathMacrosTest, AssertDeatDoesNotReturnhIfUnsupported) {
123641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  int n = 0;
123741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  FuncWithAssert(&n);
123841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  EXPECT_EQ(1, n);
123941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
12401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_DEATH_TEST
12411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
124241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that the death test macros expand to code which may or may not
124341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// be followed by operator<<, and that in either case the complete text
124441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// comprises only a single C++ statement.
124541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot//
124641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The syntax should work whether death tests are available or not.
124741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(ConditionalDeathMacrosSyntaxDeathTest, SingleStatement) {
124841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (AlwaysFalse())
124941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // This would fail if executed; this is a compilation test only
125041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    ASSERT_DEATH_IF_SUPPORTED(return, "");
125141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
125241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (AlwaysTrue())
125341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    EXPECT_DEATH_IF_SUPPORTED(_exit(1), "");
125441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  else
125541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // This empty "else" branch is meant to ensure that EXPECT_DEATH
125641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    // doesn't expand into an "if" statement without an "else"
125741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    ;  // NOLINT
125841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
125941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (AlwaysFalse())
126041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    ASSERT_DEATH_IF_SUPPORTED(return, "") << "did not die";
126141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
126241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if (AlwaysFalse())
126341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    ;  // NOLINT
126441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  else
126541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << 1 << 2 << 3;
126641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
126741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
126841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that conditional death test macros expand to code which interacts
126941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// well with switch statements.
127041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) {
127141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Microsoft compiler usually complains about switch statements without
127241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// case labels. We suppress that warning for this test.
127341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#ifdef _MSC_VER
127441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(push)
127541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(disable: 4065)
127641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // _MSC_VER
127741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
127841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  switch (0)
127941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    default:
128041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      ASSERT_DEATH_IF_SUPPORTED(_exit(1), "")
128141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          << "exit in default switch handler";
128241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
128341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  switch (0)
128441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    case 0:
128541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << "exit in switch case";
128641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
128741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#ifdef _MSC_VER
128841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(pop)
128941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // _MSC_VER
129041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}
129141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
12921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that a test case whose name ends with "DeathTest" works fine
12931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// on Windows.
12941be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(NotADeathTest, Test) {
12951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  SUCCEED();
12961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
1297