tr00per / googletest

Automatically exported from code.google.com/p/googletest
0 stars 0 forks source link

Need death tests on Mac OS X #31

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Reported by Juanjo:

The only problem I found so far is that there are no death tests for
OS X. I know that we do not have clone and that fork by itself is not
thread safe, but my library is also not thread safe and, lacking
exceptions by design, relies on abort()/exit() for signalling errors.

I have come up with a simple solution which consists on activating
death tests but not implementing the ExecDeath... class. Furthermore,
in order to avoid those annoying warnings (couldn't you just print
them once?) about the thread number, I have implemented also
GetThreadCount() for OS X.

The code is very simple and since it may be useful for somebody else,
I have attached it to the end of this message. Please consider
including a variant of it in the next release

Juanjo

Index: include/gtest/internal/gtest-port.h
===================================================================
--- include/gtest/internal/gtest-port.h (revision 83)
+++ include/gtest/internal/gtest-port.h (working copy)
@@ -213,7 +213,7 @@
 #endif  // GTEST_HAS_STD_STRING

 // Determines whether to support death tests.
-#if GTEST_HAS_STD_STRING && defined(GTEST_OS_LINUX)
+#if GTEST_HAS_STD_STRING && (defined(GTEST_OS_LINUX) ||
defined(GTEST_OS_MAC))
 #define GTEST_HAS_DEATH_TEST
 // On some platforms, <regex.h> needs someone to define size_t, and
 // won't compile otherwise.  We can #include it here as we already
@@ -464,7 +464,7 @@

 // There's no portable way to detect the number of threads, so we
just
 // return 0 to indicate that we cannot detect it.
-inline size_t GetThreadCount() { return 0; }
+size_t GetThreadCount();

 // Defines tr1::is_pointer (only needed for Symbian).

Index: src/gtest-death-test.cc
===================================================================
--- src/gtest-death-test.cc     (revision 83)
+++ src/gtest-death-test.cc     (working copy)
@@ -38,6 +38,13 @@
 #include <errno.h>
 #include <limits.h>
 #include <stdarg.h>
+#include <unistd.h>
+extern "C" char **environ;
+#include <sys/types.h>
+#include <sys/mman.h>
+#ifndef MAP_ANONYMOUS
+# define MAP_ANONYMOUS MAP_ANON
+#endif
 #endif  // GTEST_HAS_DEATH_TEST

 #include <gtest/gtest-message.h>
@@ -538,6 +545,8 @@
  std::vector<char*> args_;
 };

+#ifdef GTEST_OS_LINUX
+
 // A struct that encompasses the arguments to the child process of a
 // threadsafe-style death test process.
 struct ExecDeathTestArgs {
@@ -639,6 +648,8 @@
  return OVERSEE_TEST;
 }

+#endif // !GTEST_OS_LINUX
+
 // Creates a concrete DeathTest-derived class that depends on the
 // --gtest_death_test_style flag, and sets the pointer pointed to
 // by the "test" argument to its address.  If the test should be
@@ -669,7 +680,11 @@
  }

  if (GTEST_FLAG(death_test_style) == "threadsafe") {
+#ifdef GTEST_OS_LINUX
    *test = new ExecDeathTest(statement, regex, file, line);
+#else
+    *test = new NoExecDeathTest(statement, regex);
+#endif
  } else if (GTEST_FLAG(death_test_style) == "fast") {
    *test = new NoExecDeathTest(statement, regex);
  } else {
Index: src/gtest-port.cc
===================================================================
--- src/gtest-port.cc   (revision 83)
+++ src/gtest-port.cc   (working copy)
@@ -47,6 +47,10 @@
 #include <gtest/gtest-message.h>
 #include <gtest/internal/gtest-string.h>

+#ifdef GTEST_OS_MAC
+#include <mach/mach_init.h>
+#include <mach/task.h>
+#endif

 namespace testing {
 namespace internal {
@@ -55,6 +59,20 @@

 // Implements RE.  Currently only needed for death tests.

+#ifdef GTEST_OS_MAC
+size_t GetThreadCount() {
+  mach_msg_type_number_t thread_count;
+  thread_act_port_array_t thread_list;
+  task_t task = current_task();
+  task_threads(task, &thread_list, &thread_count);
+  return thread_count;
+}
+#else
+size_t GetThreadCount() {
+  return 0;
+}
+#endif // !GTEST_OS_MAC
+
 RE::~RE() {
  regfree(&regex_);
  free(const_cast<char*>(pattern_));

Original issue reported on code.google.com by shiq...@gmail.com on 8 Sep 2008 at 11:41

GoogleCodeExporter commented 9 years ago
Implemented by Vlad Losev.

Original comment by zhanyong...@gmail.com on 20 Mar 2009 at 12:20