QUDA  v1.1.0
A library for QCD on GPUs
gtest-death-test-internal.h
Go to the documentation of this file.
1 // Copyright 2005, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // The Google C++ Testing and Mocking Framework (Google Test)
31 //
32 // This header file defines internal utilities needed for implementing
33 // death tests. They are subject to change without notice.
34 // GOOGLETEST_CM0001 DO NOT DELETE
35 
36 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
37 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
38 
39 #include "gtest/gtest-matchers.h"
41 
42 #include <stdio.h>
43 #include <memory>
44 
45 namespace testing {
46 namespace internal {
47 
48 GTEST_DECLARE_string_(internal_run_death_test);
49 
50 // Names of the flags (needed for parsing Google Test flags).
51 const char kDeathTestStyleFlag[] = "death_test_style";
52 const char kDeathTestUseFork[] = "death_test_use_fork";
53 const char kInternalRunDeathTestFlag[] = "internal_run_death_test";
54 
55 #if GTEST_HAS_DEATH_TEST
56 
58 /* class A needs to have dll-interface to be used by clients of class B */)
59 
60 // DeathTest is a class that hides much of the complexity of the
61 // GTEST_DEATH_TEST_ macro. It is abstract; its static Create method
62 // returns a concrete class that depends on the prevailing death test
63 // style, as defined by the --gtest_death_test_style and/or
64 // --gtest_internal_run_death_test flags.
65 
66 // In describing the results of death tests, these terms are used with
67 // the corresponding definitions:
68 //
69 // exit status: The integer exit information in the format specified
70 // by wait(2)
71 // exit code: The integer code passed to exit(3), _exit(2), or
72 // returned from main()
73 class GTEST_API_ DeathTest {
74  public:
75  // Create returns false if there was an error determining the
76  // appropriate action to take for the current death test; for example,
77  // if the gtest_death_test_style flag is set to an invalid value.
78  // The LastMessage method will return a more detailed message in that
79  // case. Otherwise, the DeathTest pointer pointed to by the "test"
80  // argument is set. If the death test should be skipped, the pointer
81  // is set to NULL; otherwise, it is set to the address of a new concrete
82  // DeathTest object that controls the execution of the current test.
83  static bool Create(const char* statement, Matcher<const std::string&> matcher,
84  const char* file, int line, DeathTest** test);
85  DeathTest();
86  virtual ~DeathTest() { }
87 
88  // A helper class that aborts a death test when it's deleted.
89  class ReturnSentinel {
90  public:
91  explicit ReturnSentinel(DeathTest* test) : test_(test) { }
92  ~ReturnSentinel() { test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT); }
93  private:
94  DeathTest* const test_;
95  GTEST_DISALLOW_COPY_AND_ASSIGN_(ReturnSentinel);
97 
98  // An enumeration of possible roles that may be taken when a death
99  // test is encountered. EXECUTE means that the death test logic should
100  // be executed immediately. OVERSEE means that the program should prepare
101  // the appropriate environment for a child process to execute the death
102  // test, then wait for it to complete.
103  enum TestRole { OVERSEE_TEST, EXECUTE_TEST };
104 
105  // An enumeration of the three reasons that a test might be aborted.
106  enum AbortReason {
107  TEST_ENCOUNTERED_RETURN_STATEMENT,
108  TEST_THREW_EXCEPTION,
109  TEST_DID_NOT_DIE
110  };
111 
112  // Assumes one of the above roles.
113  virtual TestRole AssumeRole() = 0;
114 
115  // Waits for the death test to finish and returns its status.
116  virtual int Wait() = 0;
117 
118  // Returns true if the death test passed; that is, the test process
119  // exited during the test, its exit status matches a user-supplied
120  // predicate, and its stderr output matches a user-supplied regular
121  // expression.
122  // The user-supplied predicate may be a macro expression rather
123  // than a function pointer or functor, or else Wait and Passed could
124  // be combined.
125  virtual bool Passed(bool exit_status_ok) = 0;
126 
127  // Signals that the death test did not die as expected.
128  virtual void Abort(AbortReason reason) = 0;
129 
130  // Returns a human-readable outcome message regarding the outcome of
131  // the last death test.
132  static const char* LastMessage();
133 
134  static void set_last_death_test_message(const std::string& message);
135 
136  private:
137  // A string containing a description of the outcome of the last death test.
138  static std::string last_death_test_message_;
139 
141 };
142 
144 
145 // Factory interface for death tests. May be mocked out for testing.
146 class DeathTestFactory {
147  public:
148  virtual ~DeathTestFactory() { }
149  virtual bool Create(const char* statement,
150  Matcher<const std::string&> matcher, const char* file,
151  int line, DeathTest** test) = 0;
152 };
153 
154 // A concrete DeathTestFactory implementation for normal use.
155 class DefaultDeathTestFactory : public DeathTestFactory {
156  public:
157  bool Create(const char* statement, Matcher<const std::string&> matcher,
158  const char* file, int line, DeathTest** test) override;
159 };
160 
161 // Returns true if exit_status describes a process that was terminated
162 // by a signal, or exited normally with a nonzero exit code.
163 GTEST_API_ bool ExitedUnsuccessfully(int exit_status);
164 
165 // A string passed to EXPECT_DEATH (etc.) is caught by one of these overloads
166 // and interpreted as a regex (rather than an Eq matcher) for legacy
167 // compatibility.
168 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
169  ::testing::internal::RE regex) {
170  return ContainsRegex(regex.pattern());
171 }
172 inline Matcher<const ::std::string&> MakeDeathTestMatcher(const char* regex) {
173  return ContainsRegex(regex);
174 }
175 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
176  const ::std::string& regex) {
177  return ContainsRegex(regex);
178 }
179 #if GTEST_HAS_GLOBAL_STRING
180 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
181  const ::string& regex) {
182  return ContainsRegex(regex);
183 }
184 #endif
185 
186 // If a Matcher<const ::std::string&> is passed to EXPECT_DEATH (etc.), it's
187 // used directly.
188 inline Matcher<const ::std::string&> MakeDeathTestMatcher(
189  Matcher<const ::std::string&> matcher) {
190  return matcher;
191 }
192 
193 // Traps C++ exceptions escaping statement and reports them as test
194 // failures. Note that trapping SEH exceptions is not implemented here.
195 # if GTEST_HAS_EXCEPTIONS
196 # define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
197  try { \
198  GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
199  } catch (const ::std::exception& gtest_exception) { \
200  fprintf(\
201  stderr, \
202  "\n%s: Caught std::exception-derived exception escaping the " \
203  "death test statement. Exception message: %s\n", \
204  ::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \
205  gtest_exception.what()); \
206  fflush(stderr); \
207  death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
208  } catch (...) { \
209  death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \
210  }
211 
212 # else
213 # define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \
214  GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
215 
216 # endif
217 
218 // This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*,
219 // ASSERT_EXIT*, and EXPECT_EXIT*.
220 #define GTEST_DEATH_TEST_(statement, predicate, regex_or_matcher, fail) \
221  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
222  if (::testing::internal::AlwaysTrue()) { \
223  ::testing::internal::DeathTest* gtest_dt; \
224  if (!::testing::internal::DeathTest::Create( \
225  #statement, \
226  ::testing::internal::MakeDeathTestMatcher(regex_or_matcher), \
227  __FILE__, __LINE__, &gtest_dt)) { \
228  goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
229  } \
230  if (gtest_dt != nullptr) { \
231  std::unique_ptr< ::testing::internal::DeathTest> gtest_dt_ptr(gtest_dt); \
232  switch (gtest_dt->AssumeRole()) { \
233  case ::testing::internal::DeathTest::OVERSEE_TEST: \
234  if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \
235  goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \
236  } \
237  break; \
238  case ::testing::internal::DeathTest::EXECUTE_TEST: { \
239  ::testing::internal::DeathTest::ReturnSentinel gtest_sentinel( \
240  gtest_dt); \
241  GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \
242  gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \
243  break; \
244  } \
245  default: \
246  break; \
247  } \
248  } \
249  } else \
250  GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__) \
251  : fail(::testing::internal::DeathTest::LastMessage())
252 // The symbol "fail" here expands to something into which a message
253 // can be streamed.
254 
255 // This macro is for implementing ASSERT/EXPECT_DEBUG_DEATH when compiled in
256 // NDEBUG mode. In this case we need the statements to be executed and the macro
257 // must accept a streamed message even though the message is never printed.
258 // The regex object is not evaluated, but it is used to prevent "unused"
259 // warnings and to avoid an expression that doesn't compile in debug mode.
260 #define GTEST_EXECUTE_STATEMENT_(statement, regex_or_matcher) \
261  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
262  if (::testing::internal::AlwaysTrue()) { \
263  GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
264  } else if (!::testing::internal::AlwaysTrue()) { \
265  ::testing::internal::MakeDeathTestMatcher(regex_or_matcher); \
266  } else \
267  ::testing::Message()
268 
269 // A class representing the parsed contents of the
270 // --gtest_internal_run_death_test flag, as it existed when
271 // RUN_ALL_TESTS was called.
272 class InternalRunDeathTestFlag {
273  public:
274  InternalRunDeathTestFlag(const std::string& a_file,
275  int a_line,
276  int an_index,
277  int a_write_fd)
278  : file_(a_file), line_(a_line), index_(an_index),
279  write_fd_(a_write_fd) {}
280 
281  ~InternalRunDeathTestFlag() {
282  if (write_fd_ >= 0)
283  posix::Close(write_fd_);
284  }
285 
286  const std::string& file() const { return file_; }
287  int line() const { return line_; }
288  int index() const { return index_; }
289  int write_fd() const { return write_fd_; }
290 
291  private:
292  std::string file_;
293  int line_;
294  int index_;
295  int write_fd_;
296 
297  GTEST_DISALLOW_COPY_AND_ASSIGN_(InternalRunDeathTestFlag);
298 };
299 
300 // Returns a newly created InternalRunDeathTestFlag object with fields
301 // initialized from the GTEST_FLAG(internal_run_death_test) flag if
302 // the flag is specified; otherwise returns NULL.
303 InternalRunDeathTestFlag* ParseInternalRunDeathTestFlag();
304 
305 #endif // GTEST_HAS_DEATH_TEST
306 
307 } // namespace internal
308 } // namespace testing
309 
310 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_
double test(int data_type)
const char * pattern() const
Definition: gtest-port.h:931
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
Definition: gtest-port.h:318
#define GTEST_API_
Definition: gtest-port.h:774
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
Definition: gtest-port.h:319
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition: gtest-port.h:703
const char kInternalRunDeathTestFlag[]
::std::string string
Definition: gtest-port.h:891
GTEST_DECLARE_string_(internal_run_death_test)
class GTEST_API_ testing::ScopedTrace GTEST_ATTRIBUTE_UNUSED_