Commit 0d27868d by zhanyong.wan

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

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