Adds color support for TERM=linux (by Alexander Demin); renames List to Vector (by Zhanyong Wan); implements Vector::Erase (by Vlad Losev).

This commit is contained in:
zhanyong.wan
2009-07-13 19:25:02 +00:00
parent 600105ee3a
commit 89080477ae
10 changed files with 208 additions and 132 deletions

View File

@@ -199,7 +199,7 @@ Int32 Int32FromEnvOrDie(const char* env_var, Int32 default_val);
// method. Assumes that 0 <= shard_index < total_shards.
bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id);
// List is an ordered container that supports random access to the
// Vector is an ordered container that supports random access to the
// elements.
//
// We cannot use std::vector, as Visual C++ 7.1's implementation of
@@ -209,15 +209,15 @@ bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id);
//
// The element type must support copy constructor and operator=.
template <typename E> // E is the element type.
class List {
class Vector {
public:
// Creates an empty list.
List() : elements_(NULL), capacity_(0), size_(0) {}
// Creates an empty Vector.
Vector() : elements_(NULL), capacity_(0), size_(0) {}
// D'tor.
virtual ~List() { Clear(); }
virtual ~Vector() { Clear(); }
// Clears the list.
// Clears the Vector.
void Clear() {
if (elements_ != NULL) {
for (int i = 0; i < size_; i++) {
@@ -233,29 +233,27 @@ class List {
// Gets the number of elements.
int size() const { return size_; }
// Adds an element to the end of the list. A copy of the element is
// created using the copy constructor, and then stored in the list.
// Changes made to the element in the list doesn't affect the source
// object, and vice versa.
// Adds an element to the end of the Vector. A copy of the element
// is created using the copy constructor, and then stored in the
// Vector. Changes made to the element in the Vector doesn't affect
// the source object, and vice versa.
void PushBack(const E & element) { Insert(element, size_); }
// Adds an element to the beginning of this list.
// Adds an element to the beginning of this Vector.
void PushFront(const E& element) { Insert(element, 0); }
// Removes an element from the beginning of this list. If the
// Removes an element from the beginning of this Vector. If the
// result argument is not NULL, the removed element is stored in the
// memory it points to. Otherwise the element is thrown away.
// Returns true iff the list wasn't empty before the operation.
// Returns true iff the vector wasn't empty before the operation.
bool PopFront(E* result) {
if (size_ == 0)
return false;
if (result != NULL)
*result = *(elements_[0]);
*result = GetElement(0);
delete elements_[0];
size_--;
MoveElements(1, size_, 0);
Erase(0);
return true;
}
@@ -269,6 +267,18 @@ class List {
size_++;
}
// Erases the element at the specified index, or aborts the program if the
// index is not in range [0, size()).
void Erase(int index) {
GTEST_CHECK_(0 <= index && index < size_)
<< "Invalid Vector index " << index << ": must be in range [0, "
<< (size_ - 1) << "].";
delete elements_[index];
MoveElements(index + 1, size_ - index - 1, index);
size_--;
}
// Returns the number of elements that satisfy a given predicate.
// The parameter 'predicate' is a Boolean function or functor that
// accepts a 'const E &', where E is the element type.
@@ -284,7 +294,7 @@ class List {
return count;
}
// Applies a function/functor to each element in the list. The
// Applies a function/functor to each element in the Vector. The
// parameter 'functor' is a function/functor that accepts a 'const
// E &', where E is the element type. This method does not change
// the elements.
@@ -323,7 +333,7 @@ class List {
// is not in range [0, size()).
const E& GetElement(int i) const {
GTEST_CHECK_(0 <= i && i < size_)
<< "Invalid list index " << i << ": must be in range [0, "
<< "Invalid Vector index " << i << ": must be in range [0, "
<< (size_ - 1) << "].";
return *(elements_[i]);
@@ -346,13 +356,13 @@ class List {
// no more than 1/3 of the slots are wasted.
const int new_capacity = 3*(capacity_/2 + 1);
GTEST_CHECK_(new_capacity > capacity_) // Does the new capacity overflow?
<< "Cannot grow a list with " << capacity_ << " elements already.";
<< "Cannot grow a Vector with " << capacity_ << " elements already.";
capacity_ = new_capacity;
elements_ = static_cast<E**>(
realloc(elements_, capacity_*sizeof(elements_[0])));
}
// Moves the give consecutive elements to a new index in the list.
// Moves the give consecutive elements to a new index in the Vector.
void MoveElements(int source, int count, int dest) {
memmove(elements_ + dest, elements_ + source, count*sizeof(elements_[0]));
}
@@ -361,9 +371,9 @@ class List {
int capacity_; // The number of elements allocated for elements_.
int size_; // The number of elements; in the range [0, capacity_].
// We disallow copying List.
GTEST_DISALLOW_COPY_AND_ASSIGN_(List);
}; // class List
// We disallow copying Vector.
GTEST_DISALLOW_COPY_AND_ASSIGN_(Vector);
}; // class Vector
// A function for deleting an object. Handy for being used as a
// functor.
@@ -840,21 +850,21 @@ class UnitTestImpl {
TestInfo* current_test_info() { return current_test_info_; }
const TestInfo* current_test_info() const { return current_test_info_; }
// Returns the list of environments that need to be set-up/torn-down
// Returns the vector of environments that need to be set-up/torn-down
// before/after the tests are run.
internal::List<Environment*>* environments() { return &environments_; }
internal::List<Environment*>* environments_in_reverse_order() {
internal::Vector<Environment*>* environments() { return &environments_; }
internal::Vector<Environment*>* environments_in_reverse_order() {
return &environments_in_reverse_order_;
}
internal::List<TestCase*>* test_cases() { return &test_cases_; }
const internal::List<TestCase*>* test_cases() const { return &test_cases_; }
internal::Vector<TestCase*>* test_cases() { return &test_cases_; }
const internal::Vector<TestCase*>* test_cases() const { return &test_cases_; }
// Getters for the per-thread Google Test trace stack.
internal::List<TraceInfo>* gtest_trace_stack() {
internal::Vector<TraceInfo>* gtest_trace_stack() {
return gtest_trace_stack_.pointer();
}
const internal::List<TraceInfo>* gtest_trace_stack() const {
const internal::Vector<TraceInfo>* gtest_trace_stack() const {
return gtest_trace_stack_.pointer();
}
@@ -899,13 +909,13 @@ class UnitTestImpl {
internal::ThreadLocal<TestPartResultReporterInterface*>
per_thread_test_part_result_reporter_;
// The list of environments that need to be set-up/torn-down
// The vector of environments that need to be set-up/torn-down
// before/after the tests are run. environments_in_reverse_order_
// simply mirrors environments_ in reverse order.
internal::List<Environment*> environments_;
internal::List<Environment*> environments_in_reverse_order_;
internal::Vector<Environment*> environments_;
internal::Vector<Environment*> environments_in_reverse_order_;
internal::List<TestCase*> test_cases_; // The list of TestCases.
internal::Vector<TestCase*> test_cases_; // The vector of TestCases.
#if GTEST_HAS_PARAM_TEST
// ParameterizedTestRegistry object used to register value-parameterized
@@ -964,7 +974,7 @@ class UnitTestImpl {
#endif // GTEST_HAS_DEATH_TEST
// A per-thread stack of traces created by the SCOPED_TRACE() macro.
internal::ThreadLocal<internal::List<TraceInfo> > gtest_trace_stack_;
internal::ThreadLocal<internal::Vector<TraceInfo> > gtest_trace_stack_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl);
}; // class UnitTestImpl

View File

@@ -66,17 +66,17 @@ std::ostream& operator<<(std::ostream& os, const TestPartResult& result) {
// Constructs an empty TestPartResultArray.
TestPartResultArray::TestPartResultArray()
: list_(new internal::List<TestPartResult>) {
: array_(new internal::Vector<TestPartResult>) {
}
// Destructs a TestPartResultArray.
TestPartResultArray::~TestPartResultArray() {
delete list_;
delete array_;
}
// Appends a TestPartResult to the array.
void TestPartResultArray::Append(const TestPartResult& result) {
list_->PushBack(result);
array_->PushBack(result);
}
// Returns the TestPartResult at the given index (0-based).
@@ -86,12 +86,12 @@ const TestPartResult& TestPartResultArray::GetTestPartResult(int index) const {
internal::posix::Abort();
}
return list_->GetElement(index);
return array_->GetElement(index);
}
// Returns the number of TestPartResult objects in the array.
int TestPartResultArray::size() const {
return list_->size();
return array_->size();
}
namespace internal {

View File

@@ -185,7 +185,7 @@ GTEST_DEFINE_string_(
"Whether to use colors in the output. Valid values: yes, no, "
"and auto. 'auto' means to use colors if the output is "
"being sent to a terminal and the TERM environment variable "
"is set to xterm, xterm-color, xterm-256color or cygwin.");
"is set to xterm, xterm-color, xterm-256color, linux or cygwin.");
GTEST_DEFINE_string_(
filter,
@@ -258,10 +258,10 @@ static bool g_help_flag = false;
int g_init_gtest_count = 0;
static bool GTestIsInitialized() { return g_init_gtest_count != 0; }
// Iterates over a list of TestCases, keeping a running sum of the
// Iterates over a vector of TestCases, keeping a running sum of the
// results of calling a given int-returning method on each.
// Returns the sum.
static int SumOverTestCaseList(const internal::List<TestCase*>& case_list,
static int SumOverTestCaseList(const internal::Vector<TestCase*>& case_list,
int (TestCase::*method)() const) {
int sum = 0;
for (int i = 0; i < case_list.size(); i++) {
@@ -1818,8 +1818,8 @@ String AppendUserMessage(const String& gtest_msg,
// Creates an empty TestResult.
TestResult::TestResult()
: test_part_results_(new List<TestPartResult>),
test_properties_(new List<TestProperty>),
: test_part_results_(new Vector<TestPartResult>),
test_properties_(new Vector<TestProperty>),
death_test_count_(0),
elapsed_time_(0) {
}
@@ -2407,7 +2407,7 @@ TestCase::TestCase(const char* name, const char* comment,
tear_down_tc_(tear_down_tc),
should_run_(false),
elapsed_time_(0) {
test_info_list_ = new internal::List<TestInfo *>;
test_info_list_ = new internal::Vector<TestInfo *>;
}
// Destructor of TestCase.
@@ -2603,6 +2603,7 @@ bool ShouldUseColor(bool stdout_is_tty) {
String::CStringEquals(term, "xterm") ||
String::CStringEquals(term, "xterm-color") ||
String::CStringEquals(term, "xterm-256color") ||
String::CStringEquals(term, "linux") ||
String::CStringEquals(term, "cygwin");
return stdout_is_tty && term_supports_color;
#endif // GTEST_OS_WINDOWS
@@ -2881,7 +2882,7 @@ void PrettyUnitTestResultPrinter::OnUnitTestEnd(const UnitTest& unit_test) {
// This class forwards events to other event listeners.
class UnitTestEventsRepeater : public UnitTestEventListenerInterface {
public:
typedef internal::List<UnitTestEventListenerInterface *> Listeners;
typedef internal::Vector<UnitTestEventListenerInterface *> Listeners;
UnitTestEventsRepeater() {}
virtual ~UnitTestEventsRepeater();
void AddListener(UnitTestEventListenerInterface *listener);
@@ -3685,7 +3686,7 @@ TestCase* UnitTestImpl::GetTestCase(const char* test_case_name,
}
// Helpers for setting up / tearing down the given environment. They
// are for use in the List::ForEach() method.
// are for use in the Vector::ForEach() method.
static void SetUpEnvironment(Environment* env) { env->SetUp(); }
static void TearDownEnvironment(Environment* env) { env->TearDown(); }
@@ -3739,7 +3740,7 @@ int UnitTestImpl::RunAllTests() {
? HONOR_SHARDING_PROTOCOL
: IGNORE_SHARDING_PROTOCOL) > 0;
// List the tests and exit if the --gtest_list_tests flag was specified.
// Lists the tests and exits if the --gtest_list_tests flag was specified.
if (GTEST_FLAG(list_tests)) {
// This must be called *after* FilterTests() has been called.
ListTestsMatchingFilter();