Adds sample4_unittest to scons (by Vlad Losev); adds logic for getting the thread count on Mac (by Vlad Losev); adds HasFailure() and HasNonfatalFailure() (by Zhanyong Wan).
This commit is contained in:
		@@ -32,6 +32,11 @@
 | 
			
		||||
// This file tests the internal cross-platform support utilities.
 | 
			
		||||
 | 
			
		||||
#include <gtest/internal/gtest-port.h>
 | 
			
		||||
 | 
			
		||||
#if GTEST_OS_MAC
 | 
			
		||||
#include <pthread.h>
 | 
			
		||||
#endif  // GTEST_OS_MAC
 | 
			
		||||
 | 
			
		||||
#include <gtest/gtest.h>
 | 
			
		||||
#include <gtest/gtest-spi.h>
 | 
			
		||||
 | 
			
		||||
@@ -76,6 +81,44 @@ TEST(GtestCheckSyntaxTest, WorksWithSwitch) {
 | 
			
		||||
      GTEST_CHECK_(true) << "Check failed in switch case";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if GTEST_OS_MAC
 | 
			
		||||
void* ThreadFunc(void* data) {
 | 
			
		||||
  pthread_mutex_t* mutex = reinterpret_cast<pthread_mutex_t*>(data);
 | 
			
		||||
  pthread_mutex_lock(mutex);
 | 
			
		||||
  pthread_mutex_unlock(mutex);
 | 
			
		||||
  return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(GetThreadCountTest, ReturnsCorrectValue) {
 | 
			
		||||
  EXPECT_EQ(1, GetThreadCount());
 | 
			
		||||
  pthread_mutex_t mutex;
 | 
			
		||||
  pthread_attr_t  attr;
 | 
			
		||||
  pthread_t       thread_id;
 | 
			
		||||
 | 
			
		||||
  // TODO(vladl@google.com): turn mutex into internal::Mutex for automatic
 | 
			
		||||
  // destruction.
 | 
			
		||||
  pthread_mutex_init(&mutex, NULL);
 | 
			
		||||
  pthread_mutex_lock(&mutex);
 | 
			
		||||
  ASSERT_EQ(0, pthread_attr_init(&attr));
 | 
			
		||||
  ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
 | 
			
		||||
 | 
			
		||||
  const int status = pthread_create(&thread_id, &attr, &ThreadFunc, &mutex);
 | 
			
		||||
  ASSERT_EQ(0, pthread_attr_destroy(&attr));
 | 
			
		||||
  ASSERT_EQ(0, status);
 | 
			
		||||
  EXPECT_EQ(2, GetThreadCount());
 | 
			
		||||
  pthread_mutex_unlock(&mutex);
 | 
			
		||||
 | 
			
		||||
  void* dummy;
 | 
			
		||||
  ASSERT_EQ(0, pthread_join(thread_id, &dummy));
 | 
			
		||||
  EXPECT_EQ(1, GetThreadCount());
 | 
			
		||||
  pthread_mutex_destroy(&mutex);
 | 
			
		||||
}
 | 
			
		||||
#else
 | 
			
		||||
TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
 | 
			
		||||
  EXPECT_EQ(0, GetThreadCount());
 | 
			
		||||
}
 | 
			
		||||
#endif  // GTEST_OS_MAC
 | 
			
		||||
 | 
			
		||||
#if GTEST_HAS_DEATH_TEST
 | 
			
		||||
 | 
			
		||||
TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
 | 
			
		||||
 
 | 
			
		||||
@@ -131,6 +131,7 @@ using testing::TPRT_SUCCESS;
 | 
			
		||||
using testing::UnitTest;
 | 
			
		||||
using testing::internal::kTestTypeIdInGoogleTest;
 | 
			
		||||
using testing::internal::AppendUserMessage;
 | 
			
		||||
using testing::internal::ClearCurrentTestPartResults;
 | 
			
		||||
using testing::internal::CodePointToUtf8;
 | 
			
		||||
using testing::internal::EqFailure;
 | 
			
		||||
using testing::internal::FloatingPoint;
 | 
			
		||||
@@ -5456,3 +5457,87 @@ TEST(GetCurrentOsStackTraceExceptTopTest, ReturnsTheStackTrace) {
 | 
			
		||||
  EXPECT_STREQ("", GetCurrentOsStackTraceExceptTop(unit_test, 0).c_str());
 | 
			
		||||
  EXPECT_STREQ("", GetCurrentOsStackTraceExceptTop(unit_test, 1).c_str());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(HasNonfatalFailureTest, ReturnsFalseWhenThereIsNoFailure) {
 | 
			
		||||
  EXPECT_FALSE(HasNonfatalFailure());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void FailFatally() { FAIL(); }
 | 
			
		||||
 | 
			
		||||
TEST(HasNonfatalFailureTest, ReturnsFalseWhenThereIsOnlyFatalFailure) {
 | 
			
		||||
  FailFatally();
 | 
			
		||||
  const bool has_nonfatal_failure = HasNonfatalFailure();
 | 
			
		||||
  ClearCurrentTestPartResults();
 | 
			
		||||
  EXPECT_FALSE(has_nonfatal_failure);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(HasNonfatalFailureTest, ReturnsTrueWhenThereIsNonfatalFailure) {
 | 
			
		||||
  ADD_FAILURE();
 | 
			
		||||
  const bool has_nonfatal_failure = HasNonfatalFailure();
 | 
			
		||||
  ClearCurrentTestPartResults();
 | 
			
		||||
  EXPECT_TRUE(has_nonfatal_failure);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(HasNonfatalFailureTest, ReturnsTrueWhenThereAreFatalAndNonfatalFailures) {
 | 
			
		||||
  FailFatally();
 | 
			
		||||
  ADD_FAILURE();
 | 
			
		||||
  const bool has_nonfatal_failure = HasNonfatalFailure();
 | 
			
		||||
  ClearCurrentTestPartResults();
 | 
			
		||||
  EXPECT_TRUE(has_nonfatal_failure);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// A wrapper for calling HasNonfatalFailure outside of a test body.
 | 
			
		||||
static bool HasNonfatalFailureHelper() {
 | 
			
		||||
  return testing::Test::HasNonfatalFailure();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(HasNonfatalFailureTest, WorksOutsideOfTestBody) {
 | 
			
		||||
  EXPECT_FALSE(HasNonfatalFailureHelper());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(HasNonfatalFailureTest, WorksOutsideOfTestBody2) {
 | 
			
		||||
  ADD_FAILURE();
 | 
			
		||||
  const bool has_nonfatal_failure = HasNonfatalFailureHelper();
 | 
			
		||||
  ClearCurrentTestPartResults();
 | 
			
		||||
  EXPECT_TRUE(has_nonfatal_failure);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(HasFailureTest, ReturnsFalseWhenThereIsNoFailure) {
 | 
			
		||||
  EXPECT_FALSE(HasFailure());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(HasFailureTest, ReturnsTrueWhenThereIsFatalFailure) {
 | 
			
		||||
  FailFatally();
 | 
			
		||||
  const bool has_failure = HasFailure();
 | 
			
		||||
  ClearCurrentTestPartResults();
 | 
			
		||||
  EXPECT_TRUE(has_failure);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(HasFailureTest, ReturnsTrueWhenThereIsNonfatalFailure) {
 | 
			
		||||
  ADD_FAILURE();
 | 
			
		||||
  const bool has_failure = HasFailure();
 | 
			
		||||
  ClearCurrentTestPartResults();
 | 
			
		||||
  EXPECT_TRUE(has_failure);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(HasFailureTest, ReturnsTrueWhenThereAreFatalAndNonfatalFailures) {
 | 
			
		||||
  FailFatally();
 | 
			
		||||
  ADD_FAILURE();
 | 
			
		||||
  const bool has_failure = HasFailure();
 | 
			
		||||
  ClearCurrentTestPartResults();
 | 
			
		||||
  EXPECT_TRUE(has_failure);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// A wrapper for calling HasFailure outside of a test body.
 | 
			
		||||
static bool HasFailureHelper() { return testing::Test::HasFailure(); }
 | 
			
		||||
 | 
			
		||||
TEST(HasFailureTest, WorksOutsideOfTestBody) {
 | 
			
		||||
  EXPECT_FALSE(HasFailureHelper());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(HasFailureTest, WorksOutsideOfTestBody2) {
 | 
			
		||||
  ADD_FAILURE();
 | 
			
		||||
  const bool has_failure = HasFailureHelper();
 | 
			
		||||
  ClearCurrentTestPartResults();
 | 
			
		||||
  EXPECT_TRUE(has_failure);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user