Commit 0d27868d by zhanyong.wan

Simplifies the implementation by using std::vector instead of Vector.

parent 3bef459e
......@@ -34,6 +34,7 @@
#define GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_
#include <iosfwd>
#include <vector>
#include <gtest/internal/gtest-internal.h>
#include <gtest/internal/gtest-string.h>
......@@ -117,15 +118,11 @@ std::ostream& operator<<(std::ostream& os, const TestPartResult& result);
// An array of TestPartResult objects.
//
// We define this class as we cannot use STL containers when compiling
// Google Test with MSVC 7.1 and exceptions disabled.
//
// Don't inherit from TestPartResultArray as its destructor is not
// virtual.
class TestPartResultArray {
public:
TestPartResultArray();
~TestPartResultArray();
TestPartResultArray() {}
// Appends the given TestPartResult to the array.
void Append(const TestPartResult& result);
......@@ -135,9 +132,9 @@ class TestPartResultArray {
// Returns the number of TestPartResult objects in the array.
int size() const;
private:
// Internally we use a Vector to implement the array.
internal::Vector<TestPartResult>* const array_;
std::vector<TestPartResult> array_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestPartResultArray);
};
......
......@@ -52,6 +52,8 @@
#define GTEST_INCLUDE_GTEST_GTEST_H_
#include <limits>
#include <vector>
#include <gtest/internal/gtest-internal.h>
#include <gtest/internal/gtest-string.h>
#include <gtest/gtest-death-test.h>
......@@ -535,13 +537,13 @@ class TestResult {
friend class internal::WindowsDeathTest;
// Gets the vector of TestPartResults.
const internal::Vector<TestPartResult>& test_part_results() const {
return *test_part_results_;
const std::vector<TestPartResult>& test_part_results() const {
return test_part_results_;
}
// Gets the vector of TestProperties.
const internal::Vector<TestProperty>& test_properties() const {
return *test_properties_;
const std::vector<TestProperty>& test_properties() const {
return test_properties_;
}
// Sets the elapsed time.
......@@ -579,9 +581,9 @@ class TestResult {
internal::Mutex test_properites_mutex_;
// The vector of TestPartResults
internal::scoped_ptr<internal::Vector<TestPartResult> > test_part_results_;
std::vector<TestPartResult> test_part_results_;
// The vector of TestProperties
internal::scoped_ptr<internal::Vector<TestProperty> > test_properties_;
std::vector<TestProperty> test_properties_;
// Running count of death tests.
int death_test_count_;
// The elapsed time, in milliseconds.
......@@ -745,11 +747,11 @@ class TestCase {
friend class internal::UnitTestImpl;
// Gets the (mutable) vector of TestInfos in this TestCase.
internal::Vector<TestInfo*>& test_info_list() { return *test_info_list_; }
std::vector<TestInfo*>& test_info_list() { return test_info_list_; }
// Gets the (immutable) vector of TestInfos in this TestCase.
const internal::Vector<TestInfo *> & test_info_list() const {
return *test_info_list_;
const std::vector<TestInfo*>& test_info_list() const {
return test_info_list_;
}
// Returns the i-th test among all the tests. i can range from 0 to
......@@ -798,11 +800,11 @@ class TestCase {
internal::String comment_;
// The vector of TestInfos in their original order. It owns the
// elements in the vector.
const internal::scoped_ptr<internal::Vector<TestInfo*> > test_info_list_;
std::vector<TestInfo*> test_info_list_;
// Provides a level of indirection for the test list to allow easy
// shuffling and restoring the test order. The i-th element in this
// vector is the index of the i-th test in the shuffled test list.
const internal::scoped_ptr<internal::Vector<int> > test_indices_;
std::vector<int> test_indices_;
// Pointer to the function that sets up the test case.
Test::SetUpTestCaseFunc set_up_tc_;
// Pointer to the function that tears down the test case.
......
......@@ -114,7 +114,6 @@ struct TraceInfo; // Information about a trace point.
class ScopedTrace; // Implements scoped trace.
class TestInfoImpl; // Opaque implementation of TestInfo
class UnitTestImpl; // Opaque implementation of UnitTest
template <typename E> class Vector; // A generic vector.
// How many times InitGoogleTest() has been called.
extern int g_init_gtest_count;
......
......@@ -13,7 +13,6 @@ EXPORTS
??0ScopedTrace@internal@testing@@QAE@PBDHABVMessage@2@@Z
??0SingleFailureChecker@internal@testing@@QAE@PBVTestPartResultArray@2@W4Type@TestPartResult@2@PBD@Z
??0Test@testing@@IAE@XZ
??0TestPartResultArray@testing@@QAE@XZ
??1AssertHelper@internal@testing@@QAE@XZ
??1GTestLog@internal@testing@@QAE@XZ
??1HasNewFatalFailureHelper@internal@testing@@UAE@XZ
......@@ -22,7 +21,6 @@ EXPORTS
??1ScopedTrace@internal@testing@@QAE@XZ
??1SingleFailureChecker@internal@testing@@QAE@XZ
??1Test@testing@@UAE@XZ
??1TestPartResultArray@testing@@QAE@XZ
??4AssertHelper@internal@testing@@QBEXABVMessage@2@@Z
??6Message@testing@@QAEAAV01@ABV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z
??7AssertionResult@testing@@QBE?AV01@XZ
......
......@@ -64,19 +64,9 @@ std::ostream& operator<<(std::ostream& os, const TestPartResult& result) {
<< result.message() << std::endl;
}
// Constructs an empty TestPartResultArray.
TestPartResultArray::TestPartResultArray()
: array_(new internal::Vector<TestPartResult>) {
}
// Destructs a TestPartResultArray.
TestPartResultArray::~TestPartResultArray() {
delete array_;
}
// Appends a TestPartResult to the array.
void TestPartResultArray::Append(const TestPartResult& result) {
array_->PushBack(result);
array_.push_back(result);
}
// Returns the TestPartResult at the given index (0-based).
......@@ -86,12 +76,12 @@ const TestPartResult& TestPartResultArray::GetTestPartResult(int index) const {
internal::posix::Abort();
}
return array_->GetElement(index);
return array_[index];
}
// Returns the number of TestPartResult objects in the array.
int TestPartResultArray::size() const {
return array_->size();
return array_.size();
}
namespace internal {
......
......@@ -34,15 +34,7 @@
// right times.
#include <gtest/gtest.h>
// Indicates that this translation unit is part of Google Test's
// implementation. It must come before gtest-internal-inl.h is
// included, or there will be a compiler error. This trick is to
// prevent a user from accidentally including gtest-internal-inl.h in
// his code.
#define GTEST_IMPLEMENTATION_ 1
#include "src/gtest-internal-inl.h" // For Vector.
#undef GTEST_IMPLEMENTATION_
#include <vector>
using ::testing::AddGlobalTestEnvironment;
using ::testing::Environment;
......@@ -54,10 +46,9 @@ using ::testing::TestInfo;
using ::testing::TestPartResult;
using ::testing::UnitTest;
using ::testing::internal::String;
using ::testing::internal::Vector;
// Used by tests to register their events.
Vector<String>* g_events = NULL;
std::vector<String>* g_events = NULL;
namespace testing {
namespace internal {
......@@ -68,7 +59,7 @@ class EventRecordingListener : public TestEventListener {
protected:
virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {
g_events->PushBack(GetFullMethodName("OnTestProgramStart"));
g_events->push_back(GetFullMethodName("OnTestProgramStart"));
}
virtual void OnTestIterationStart(const UnitTest& /*unit_test*/,
......@@ -76,43 +67,43 @@ class EventRecordingListener : public TestEventListener {
Message message;
message << GetFullMethodName("OnTestIterationStart")
<< "(" << iteration << ")";
g_events->PushBack(message.GetString());
g_events->push_back(message.GetString());
}
virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {
g_events->PushBack(GetFullMethodName("OnEnvironmentsSetUpStart"));
g_events->push_back(GetFullMethodName("OnEnvironmentsSetUpStart"));
}
virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {
g_events->PushBack(GetFullMethodName("OnEnvironmentsSetUpEnd"));
g_events->push_back(GetFullMethodName("OnEnvironmentsSetUpEnd"));
}
virtual void OnTestCaseStart(const TestCase& /*test_case*/) {
g_events->PushBack(GetFullMethodName("OnTestCaseStart"));
g_events->push_back(GetFullMethodName("OnTestCaseStart"));
}
virtual void OnTestStart(const TestInfo& /*test_info*/) {
g_events->PushBack(GetFullMethodName("OnTestStart"));
g_events->push_back(GetFullMethodName("OnTestStart"));
}
virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {
g_events->PushBack(GetFullMethodName("OnTestPartResult"));
g_events->push_back(GetFullMethodName("OnTestPartResult"));
}
virtual void OnTestEnd(const TestInfo& /*test_info*/) {
g_events->PushBack(GetFullMethodName("OnTestEnd"));
g_events->push_back(GetFullMethodName("OnTestEnd"));
}
virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {
g_events->PushBack(GetFullMethodName("OnTestCaseEnd"));
g_events->push_back(GetFullMethodName("OnTestCaseEnd"));
}
virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {
g_events->PushBack(GetFullMethodName("OnEnvironmentsTearDownStart"));
g_events->push_back(GetFullMethodName("OnEnvironmentsTearDownStart"));
}
virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {
g_events->PushBack(GetFullMethodName("OnEnvironmentsTearDownEnd"));
g_events->push_back(GetFullMethodName("OnEnvironmentsTearDownEnd"));
}
virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/,
......@@ -120,11 +111,11 @@ class EventRecordingListener : public TestEventListener {
Message message;
message << GetFullMethodName("OnTestIterationEnd")
<< "(" << iteration << ")";
g_events->PushBack(message.GetString());
g_events->push_back(message.GetString());
}
virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {
g_events->PushBack(GetFullMethodName("OnTestProgramEnd"));
g_events->push_back(GetFullMethodName("OnTestProgramEnd"));
}
private:
......@@ -140,42 +131,42 @@ class EventRecordingListener : public TestEventListener {
class EnvironmentInvocationCatcher : public Environment {
protected:
virtual void SetUp() {
g_events->PushBack(String("Environment::SetUp"));
g_events->push_back(String("Environment::SetUp"));
}
virtual void TearDown() {
g_events->PushBack(String("Environment::TearDown"));
g_events->push_back(String("Environment::TearDown"));
}
};
class ListenerTest : public Test {
protected:
static void SetUpTestCase() {
g_events->PushBack(String("ListenerTest::SetUpTestCase"));
g_events->push_back(String("ListenerTest::SetUpTestCase"));
}
static void TearDownTestCase() {
g_events->PushBack(String("ListenerTest::TearDownTestCase"));
g_events->push_back(String("ListenerTest::TearDownTestCase"));
}
virtual void SetUp() {
g_events->PushBack(String("ListenerTest::SetUp"));
g_events->push_back(String("ListenerTest::SetUp"));
}
virtual void TearDown() {
g_events->PushBack(String("ListenerTest::TearDown"));
g_events->push_back(String("ListenerTest::TearDown"));
}
};
TEST_F(ListenerTest, DoesFoo) {
// Test execution order within a test case is not guaranteed so we are not
// recording the test name.
g_events->PushBack(String("ListenerTest::* Test Body"));
g_events->push_back(String("ListenerTest::* Test Body"));
SUCCEED(); // Triggers OnTestPartResult.
}
TEST_F(ListenerTest, DoesBar) {
g_events->PushBack(String("ListenerTest::* Test Body"));
g_events->push_back(String("ListenerTest::* Test Body"));
SUCCEED(); // Triggers OnTestPartResult.
}
......@@ -186,7 +177,7 @@ TEST_F(ListenerTest, DoesBar) {
using ::testing::internal::EnvironmentInvocationCatcher;
using ::testing::internal::EventRecordingListener;
void VerifyResults(const Vector<String>& data,
void VerifyResults(const std::vector<String>& data,
const char* const* expected_data,
int expected_data_size) {
const int actual_size = data.size();
......@@ -199,18 +190,18 @@ void VerifyResults(const Vector<String>& data,
expected_data_size : actual_size;
int i = 0;
for (; i < shorter_size; ++i) {
ASSERT_STREQ(expected_data[i], data.GetElement(i).c_str())
ASSERT_STREQ(expected_data[i], data[i].c_str())
<< "at position " << i;
}
// Prints extra elements in the actual data.
for (; i < actual_size; ++i) {
printf(" Actual event #%d: %s\n", i, data.GetElement(i).c_str());
printf(" Actual event #%d: %s\n", i, data[i].c_str());
}
}
int main(int argc, char **argv) {
Vector<String> events;
std::vector<String> events;
g_events = &events;
InitGoogleTest(&argc, argv);
......
......@@ -35,6 +35,7 @@
#include <gtest/gtest.h>
#include <iostream>
#include <vector>
// We must define this macro in order to #include
// gtest-internal-inl.h. This is how Google Test prevents a user from
......@@ -51,7 +52,6 @@ namespace {
using internal::scoped_ptr;
using internal::String;
using internal::TestPropertyKeyIs;
using internal::Vector;
using internal::ThreadStartSemaphore;
using internal::ThreadWithParam;
......@@ -75,12 +75,13 @@ String IdToString(int id) {
return id_message.GetString();
}
void ExpectKeyAndValueWereRecordedForId(const Vector<TestProperty>& properties,
int id,
const char* suffix) {
void ExpectKeyAndValueWereRecordedForId(
const std::vector<TestProperty>& properties,
int id, const char* suffix) {
TestPropertyKeyIs matches_key(IdToKey(id, suffix).c_str());
const TestProperty* property = properties.FindIf(matches_key);
ASSERT_TRUE(property != NULL)
const std::vector<TestProperty>::const_iterator property =
std::find_if(properties.begin(), properties.end(), matches_key);
ASSERT_TRUE(property != properties.end())
<< "expecting " << suffix << " value for id " << id;
EXPECT_STREQ(IdToString(id).c_str(), property->value());
}
......@@ -143,11 +144,11 @@ TEST(StressTest, CanUseScopedTraceAndAssertionsInManyThreads) {
const TestInfo* const info = UnitTest::GetInstance()->current_test_info();
const TestResult* const result = info->result();
Vector<TestProperty> properties;
std::vector<TestProperty> properties;
// We have no access to the TestResult's list of properties but we can
// copy them one by one.
for (int i = 0; i < result->test_property_count(); ++i)
properties.PushBack(result->GetTestProperty(i));
properties.push_back(result->GetTestProperty(i));
EXPECT_EQ(kThreadCount * 2 + 1, result->test_property_count())
<< "String and int values recorded on each thread, "
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment