Reformatted list classes.

BUG=
TEST=Trybots

Review URL: https://webrtc-codereview.appspot.com/995004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3291 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
phoglund@webrtc.org 2012-12-14 09:52:34 +00:00
parent 32519398b6
commit 52d981f60c
6 changed files with 822 additions and 913 deletions

View File

@ -11,97 +11,97 @@
#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_LIST_WRAPPER_H_
#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_LIST_WRAPPER_H_
#include "constructor_magic.h"
#include "webrtc/system_wrappers/interface/constructor_magic.h"
namespace webrtc {
class CriticalSectionWrapper;
class ListItem
{
friend class ListWrapper;
class ListItem {
friend class ListWrapper;
public:
ListItem(const void* ptr);
ListItem(const unsigned int item);
virtual ~ListItem();
void* GetItem() const;
unsigned int GetUnsignedItem() const;
public:
ListItem(const void* ptr);
ListItem(const unsigned int item);
virtual ~ListItem();
void* GetItem() const;
unsigned int GetUnsignedItem() const;
protected:
ListItem* next_;
ListItem* prev_;
protected:
ListItem* next_;
ListItem* prev_;
private:
const void* item_ptr_;
const unsigned int item_;
private:
const void* item_ptr_;
const unsigned int item_;
};
class ListWrapper
{
public:
ListWrapper();
virtual ~ListWrapper();
class ListWrapper {
public:
ListWrapper();
virtual ~ListWrapper();
// Returns the number of elements stored in the list.
unsigned int GetSize() const;
// Returns the number of elements stored in the list.
unsigned int GetSize() const;
// Puts a pointer to anything last in the list.
int PushBack(const void* ptr);
// Puts a pointer to anything first in the list.
int PushFront(const void* ptr);
// Puts a pointer to anything last in the list.
int PushBack(const void* ptr);
// Puts a pointer to anything first in the list.
int PushFront(const void* ptr);
// Puts a copy of the specified integer last in the list.
int PushBack(const unsigned int item_id);
// Puts a copy of the specified integer first in the list.
int PushFront(const unsigned int item_id);
// Puts a copy of the specified integer last in the list.
int PushBack(const unsigned int item_id);
// Puts a copy of the specified integer first in the list.
int PushFront(const unsigned int item_id);
// Pops the first ListItem from the list
int PopFront();
// Pops the first ListItem from the list
int PopFront();
// Pops the last ListItem from the list
int PopBack();
// Pops the last ListItem from the list
int PopBack();
// Returns true if the list is empty
bool Empty() const;
// Returns true if the list is empty
bool Empty() const;
// Returns a pointer to the first ListItem in the list.
ListItem* First() const;
// Returns a pointer to the first ListItem in the list.
ListItem* First() const;
// Returns a pointer to the last ListItem in the list.
ListItem* Last() const;
// Returns a pointer to the last ListItem in the list.
ListItem* Last() const;
// Returns a pointer to the ListItem stored after item in the list.
ListItem* Next(ListItem* item) const;
// Returns a pointer to the ListItem stored after item in the list.
ListItem* Next(ListItem* item) const;
// Returns a pointer to the ListItem stored before item in the list.
ListItem* Previous(ListItem* item) const;
// Returns a pointer to the ListItem stored before item in the list.
ListItem* Previous(ListItem* item) const;
// Removes item from the list.
int Erase(ListItem* item);
// Removes item from the list.
int Erase(ListItem* item);
// Insert list item after existing_previous_item. Please note that new_item
// must be created using new ListItem(). The map will take ownership of
// new_item following a successfull insert. If insert fails new_item will
// not be released by the List
int Insert(ListItem* existing_previous_item,
ListItem* new_item);
// Insert list item after existing_previous_item. Please note that new_item
// must be created using new ListItem(). The map will take ownership of
// new_item following a successfull insert. If insert fails new_item will
// not be released by the List
int Insert(ListItem* existing_previous_item,
ListItem* new_item);
// Insert list item before existing_next_item. Please note that new_item
// must be created using new ListItem(). The map will take ownership of
// new_item following a successfull insert. If insert fails new_item will
// not be released by the List
int InsertBefore(ListItem* existing_next_item,
ListItem* new_item);
// Insert list item before existing_next_item. Please note that new_item
// must be created using new ListItem(). The map will take ownership of
// new_item following a successfull insert. If insert fails new_item will
// not be released by the List
int InsertBefore(ListItem* existing_next_item,
ListItem* new_item);
private:
void PushBackImpl(ListItem* item);
void PushFrontImpl(ListItem* item);
private:
void PushBackImpl(ListItem* item);
void PushFrontImpl(ListItem* item);
CriticalSectionWrapper* critical_section_;
ListItem* first_;
ListItem* last_;
unsigned int size_;
CriticalSectionWrapper* critical_section_;
ListItem* first_;
ListItem* last_;
unsigned int size_;
};
} //namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_LIST_WRAPPER_H_
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_LIST_WRAPPER_H_

View File

@ -8,282 +8,234 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "list_wrapper.h"
#include "webrtc/system_wrappers/interface/list_wrapper.h"
#include "critical_section_wrapper.h"
#include "trace.h"
#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
#include "webrtc/system_wrappers/interface/trace.h"
namespace webrtc {
ListItem::ListItem(const void* item)
: next_(0),
prev_(0),
item_ptr_(item),
item_(0)
{
item_(0) {
}
ListItem::ListItem(const unsigned int item)
: next_(0),
prev_(0),
item_ptr_(0),
item_(item)
{
item_(item) {
}
ListItem::~ListItem()
{
ListItem::~ListItem() {
}
void* ListItem::GetItem() const
{
return const_cast<void*>(item_ptr_);
void* ListItem::GetItem() const {
return const_cast<void*>(item_ptr_);
}
unsigned int ListItem::GetUnsignedItem() const
{
return item_;
unsigned int ListItem::GetUnsignedItem() const {
return item_;
}
ListWrapper::ListWrapper()
: critical_section_(CriticalSectionWrapper::CreateCriticalSection()),
first_(0),
last_(0),
size_(0)
{
size_(0) {
}
ListWrapper::~ListWrapper()
{
if (!Empty())
{
// TODO (hellner) I'm not sure this loggin is useful.
WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1,
"Potential memory leak in ListWrapper");
// Remove all remaining list items.
while (Erase(First()) == 0)
{}
}
delete critical_section_;
ListWrapper::~ListWrapper() {
if (!Empty()) {
// TODO(hellner) I'm not sure this loggin is useful.
WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1,
"Potential memory leak in ListWrapper");
// Remove all remaining list items.
while (Erase(First()) == 0)
{}
}
delete critical_section_;
}
bool ListWrapper::Empty() const
{
return !first_ && !last_;
bool ListWrapper::Empty() const {
return !first_ && !last_;
}
unsigned int ListWrapper::GetSize() const
{
return size_;
unsigned int ListWrapper::GetSize() const {
return size_;
}
int ListWrapper::PushBack(const void* ptr)
{
ListItem* item = new ListItem(ptr);
CriticalSectionScoped lock(critical_section_);
PushBackImpl(item);
int ListWrapper::PushBack(const void* ptr) {
ListItem* item = new ListItem(ptr);
CriticalSectionScoped lock(critical_section_);
PushBackImpl(item);
return 0;
}
int ListWrapper::PushBack(const unsigned int item_id) {
ListItem* item = new ListItem(item_id);
CriticalSectionScoped lock(critical_section_);
PushBackImpl(item);
return 0;
}
int ListWrapper::PushFront(const unsigned int item_id) {
ListItem* item = new ListItem(item_id);
CriticalSectionScoped lock(critical_section_);
PushFrontImpl(item);
return 0;
}
int ListWrapper::PushFront(const void* ptr) {
ListItem* item = new ListItem(ptr);
CriticalSectionScoped lock(critical_section_);
PushFrontImpl(item);
return 0;
}
int ListWrapper::PopFront() {
return Erase(first_);
}
int ListWrapper::PopBack() {
return Erase(last_);
}
ListItem* ListWrapper::First() const {
return first_;
}
ListItem* ListWrapper::Last() const {
return last_;
}
ListItem* ListWrapper::Next(ListItem* item) const {
if (!item) {
return 0;
}
return item->next_;
}
int ListWrapper::PushBack(const unsigned int item_id)
{
ListItem* item = new ListItem(item_id);
CriticalSectionScoped lock(critical_section_);
PushBackImpl(item);
ListItem* ListWrapper::Previous(ListItem* item) const {
if (!item) {
return 0;
}
return item->prev_;
}
int ListWrapper::PushFront(const unsigned int item_id)
{
ListItem* item = new ListItem(item_id);
CriticalSectionScoped lock(critical_section_);
PushFrontImpl(item);
return 0;
}
int ListWrapper::PushFront(const void* ptr)
{
ListItem* item = new ListItem(ptr);
CriticalSectionScoped lock(critical_section_);
PushFrontImpl(item);
return 0;
}
int ListWrapper::PopFront()
{
return Erase(first_);
}
int ListWrapper::PopBack()
{
return Erase(last_);
}
ListItem* ListWrapper::First() const
{
return first_;
}
ListItem* ListWrapper::Last() const
{
return last_;
}
ListItem* ListWrapper::Next(ListItem* item) const
{
if(!item)
{
return 0;
}
return item->next_;
}
ListItem* ListWrapper::Previous(ListItem* item) const
{
if (!item)
{
return 0;
}
return item->prev_;
}
int ListWrapper::Insert(ListItem* existing_previous_item, ListItem* new_item)
{
if (!new_item)
{
return -1;
}
// Allow existing_previous_item to be NULL if the list is empty.
// TODO (hellner) why allow this? Keep it as is for now to avoid
// breaking API contract.
if (!existing_previous_item && !Empty())
{
return -1;
}
CriticalSectionScoped lock(critical_section_);
if (!existing_previous_item)
{
PushBackImpl(new_item);
return 0;
}
ListItem* next_item = existing_previous_item->next_;
new_item->next_ = existing_previous_item->next_;
new_item->prev_ = existing_previous_item;
existing_previous_item->next_ = new_item;
if (next_item)
{
next_item->prev_ = new_item;
}
else
{
last_ = new_item;
}
size_++;
int ListWrapper::Insert(ListItem* existing_previous_item, ListItem* new_item) {
if (!new_item) {
return -1;
}
// Allow existing_previous_item to be NULL if the list is empty.
// TODO(hellner) why allow this? Keep it as is for now to avoid
// breaking API contract.
if (!existing_previous_item && !Empty()) {
return -1;
}
CriticalSectionScoped lock(critical_section_);
if (!existing_previous_item) {
PushBackImpl(new_item);
return 0;
}
ListItem* next_item = existing_previous_item->next_;
new_item->next_ = existing_previous_item->next_;
new_item->prev_ = existing_previous_item;
existing_previous_item->next_ = new_item;
if (next_item) {
next_item->prev_ = new_item;
} else {
last_ = new_item;
}
size_++;
return 0;
}
int ListWrapper::InsertBefore(ListItem* existing_next_item,
ListItem* new_item)
{
if (!new_item)
{
return -1;
}
// Allow existing_next_item to be NULL if the list is empty.
// Todo: why allow this? Keep it as is for now to avoid breaking API
// contract.
if (!existing_next_item && !Empty())
{
return -1;
}
CriticalSectionScoped lock(critical_section_);
if (!existing_next_item)
{
PushBackImpl(new_item);
return 0;
}
ListItem* previous_item = existing_next_item->prev_;
new_item->next_ = existing_next_item;
new_item->prev_ = previous_item;
existing_next_item->prev_ = new_item;
if (previous_item)
{
previous_item->next_ = new_item;
}
else
{
first_ = new_item;
}
size_++;
ListItem* new_item) {
if (!new_item) {
return -1;
}
// Allow existing_next_item to be NULL if the list is empty.
// Todo: why allow this? Keep it as is for now to avoid breaking API
// contract.
if (!existing_next_item && !Empty()) {
return -1;
}
CriticalSectionScoped lock(critical_section_);
if (!existing_next_item) {
PushBackImpl(new_item);
return 0;
}
ListItem* previous_item = existing_next_item->prev_;
new_item->next_ = existing_next_item;
new_item->prev_ = previous_item;
existing_next_item->prev_ = new_item;
if (previous_item) {
previous_item->next_ = new_item;
} else {
first_ = new_item;
}
size_++;
return 0;
}
int ListWrapper::Erase(ListItem* item)
{
if (!item)
{
return -1;
int ListWrapper::Erase(ListItem* item) {
if (!item) {
return -1;
}
size_--;
ListItem* previous_item = item->prev_;
ListItem* next_item = item->next_;
if (!previous_item) {
if (next_item) {
next_item->prev_ = 0;
}
size_--;
ListItem* previous_item = item->prev_;
ListItem* next_item = item->next_;
if (!previous_item)
{
if(next_item)
{
next_item->prev_ = 0;
}
first_ = next_item;
first_ = next_item;
} else {
previous_item->next_ = next_item;
}
if (!next_item) {
if (previous_item) {
previous_item->next_ = 0;
}
else
{
previous_item->next_ = next_item;
}
if (!next_item)
{
if(previous_item)
{
previous_item->next_ = 0;
}
last_ = previous_item;
}
else
{
next_item->prev_ = previous_item;
}
delete item;
return 0;
last_ = previous_item;
} else {
next_item->prev_ = previous_item;
}
delete item;
return 0;
}
void ListWrapper::PushBackImpl(ListItem* item)
{
if (Empty())
{
first_ = item;
last_ = item;
size_++;
return;
}
item->prev_ = last_;
last_->next_ = item;
void ListWrapper::PushBackImpl(ListItem* item) {
if (Empty()) {
first_ = item;
last_ = item;
size_++;
return;
}
item->prev_ = last_;
last_->next_ = item;
last_ = item;
size_++;
}
void ListWrapper::PushFrontImpl(ListItem* item)
{
if (Empty())
{
first_ = item;
last_ = item;
size_++;
return;
}
item->next_ = first_;
first_->prev_ = item;
void ListWrapper::PushFrontImpl(ListItem* item) {
if (Empty()) {
first_ = item;
last_ = item;
size_++;
return;
}
item->next_ = first_;
first_->prev_ = item;
first_ = item;
size_++;
}
} //namespace webrtc

View File

@ -11,69 +11,68 @@
#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_LIST_NO_STL_H_
#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_LIST_NO_STL_H_
#include "constructor_magic.h"
#include "webrtc/system_wrappers/interface/constructor_magic.h"
namespace webrtc {
class CriticalSectionWrapper;
class ListNoStlItem
{
public:
ListNoStlItem(const void* ptr);
ListNoStlItem(const unsigned int item);
virtual ~ListNoStlItem();
void* GetItem() const;
unsigned int GetUnsignedItem() const;
class ListNoStlItem {
public:
ListNoStlItem(const void* ptr);
ListNoStlItem(const unsigned int item);
virtual ~ListNoStlItem();
void* GetItem() const;
unsigned int GetUnsignedItem() const;
protected:
ListNoStlItem* next_;
ListNoStlItem* prev_;
protected:
ListNoStlItem* next_;
ListNoStlItem* prev_;
private:
friend class ListNoStl;
private:
friend class ListNoStl;
const void* item_ptr_;
const unsigned int item_;
DISALLOW_COPY_AND_ASSIGN(ListNoStlItem);
const void* item_ptr_;
const unsigned int item_;
DISALLOW_COPY_AND_ASSIGN(ListNoStlItem);
};
class ListNoStl {
public:
ListNoStl();
virtual ~ListNoStl();
class ListNoStl
{
public:
ListNoStl();
virtual ~ListNoStl();
// ListWrapper functions
unsigned int GetSize() const;
int PushBack(const void* ptr);
int PushBack(const unsigned int item_id);
int PushFront(const void* ptr);
int PushFront(const unsigned int item_id);
int PopFront();
int PopBack();
bool Empty() const;
ListNoStlItem* First() const;
ListNoStlItem* Last() const;
ListNoStlItem* Next(ListNoStlItem* item) const;
ListNoStlItem* Previous(ListNoStlItem* item) const;
int Erase(ListNoStlItem* item);
int Insert(ListNoStlItem* existing_previous_item,
ListNoStlItem* new_item);
// ListWrapper functions
unsigned int GetSize() const;
int PushBack(const void* ptr);
int PushBack(const unsigned int item_id);
int PushFront(const void* ptr);
int PushFront(const unsigned int item_id);
int PopFront();
int PopBack();
bool Empty() const;
ListNoStlItem* First() const;
ListNoStlItem* Last() const;
ListNoStlItem* Next(ListNoStlItem* item) const;
ListNoStlItem* Previous(ListNoStlItem* item) const;
int Erase(ListNoStlItem* item);
int Insert(ListNoStlItem* existing_previous_item,
ListNoStlItem* new_item);
int InsertBefore(ListNoStlItem* existing_next_item,
ListNoStlItem* new_item);
int InsertBefore(ListNoStlItem* existing_next_item,
ListNoStlItem* new_item);
private:
void PushBack(ListNoStlItem* item);
void PushFront(ListNoStlItem* item);
private:
void PushBack(ListNoStlItem* item);
void PushFront(ListNoStlItem* item);
CriticalSectionWrapper* critical_section_;
ListNoStlItem* first_;
ListNoStlItem* last_;
unsigned int size_;
DISALLOW_COPY_AND_ASSIGN(ListNoStl);
CriticalSectionWrapper* critical_section_;
ListNoStlItem* first_;
ListNoStlItem* last_;
unsigned int size_;
DISALLOW_COPY_AND_ASSIGN(ListNoStl);
};
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_LIST_NO_STL_H_
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_LIST_NO_STL_H_

View File

@ -8,237 +8,200 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "list_wrapper.h"
#include "webrtc/system_wrappers/interface/list_wrapper.h"
#include "trace.h"
#include "webrtc/system_wrappers/interface/trace.h"
namespace webrtc {
ListItem::ListItem(const void* item)
: this_iter_(),
item_ptr_(item),
item_(0)
{
item_(0) {
}
ListItem::ListItem(const unsigned int item)
: this_iter_(),
item_ptr_(0),
item_(item)
{
item_(item) {
}
ListItem::~ListItem()
{
ListItem::~ListItem() {
}
void* ListItem::GetItem() const
{
return const_cast<void*>(item_ptr_);
void* ListItem::GetItem() const {
return const_cast<void*>(item_ptr_);
}
unsigned int ListItem::GetUnsignedItem() const
{
return item_;
unsigned int ListItem::GetUnsignedItem() const {
return item_;
}
ListWrapper::ListWrapper() : list_()
{
ListWrapper::ListWrapper()
: list_() {
}
ListWrapper::~ListWrapper()
{
if (!Empty())
{
// TODO (hellner) I'm not sure this loggin is useful.
WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1,
"Potential memory leak in ListWrapper");
// Remove all remaining list items.
while (Erase(First()) == 0)
{}
}
ListWrapper::~ListWrapper() {
if (!Empty()) {
// TODO(hellner) I'm not sure this loggin is useful.
WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1,
"Potential memory leak in ListWrapper");
// Remove all remaining list items.
while (Erase(First()) == 0) {}
}
}
bool ListWrapper::Empty() const
{
return list_.empty();
bool ListWrapper::Empty() const {
return list_.empty();
}
unsigned int ListWrapper::GetSize() const
{
return list_.size();
unsigned int ListWrapper::GetSize() const {
return list_.size();
}
int ListWrapper::PushBack(const void* ptr)
{
ListItem* item = new ListItem(ptr);
list_.push_back(item);
return 0;
int ListWrapper::PushBack(const void* ptr) {
ListItem* item = new ListItem(ptr);
list_.push_back(item);
return 0;
}
int ListWrapper::PushBack(const unsigned int item_id)
{
ListItem* item = new ListItem(item_id);
list_.push_back(item);
return 0;
int ListWrapper::PushBack(const unsigned int item_id) {
ListItem* item = new ListItem(item_id);
list_.push_back(item);
return 0;
}
int ListWrapper::PushFront(const unsigned int item_id)
{
ListItem* item = new ListItem(item_id);
list_.push_front(item);
return 0;
int ListWrapper::PushFront(const unsigned int item_id) {
ListItem* item = new ListItem(item_id);
list_.push_front(item);
return 0;
}
int ListWrapper::PushFront(const void* ptr)
{
ListItem* item = new ListItem(ptr);
list_.push_front(item);
return 0;
int ListWrapper::PushFront(const void* ptr) {
ListItem* item = new ListItem(ptr);
list_.push_front(item);
return 0;
}
int ListWrapper::PopFront()
{
if(list_.empty())
{
return -1;
}
list_.pop_front();
return 0;
int ListWrapper::PopFront() {
if (list_.empty()) {
return -1;
}
list_.pop_front();
return 0;
}
int ListWrapper::PopBack()
{
if(list_.empty())
{
return -1;
}
list_.pop_back();
return 0;
int ListWrapper::PopBack() {
if (list_.empty()) {
return -1;
}
list_.pop_back();
return 0;
}
ListItem* ListWrapper::First() const
{
if(list_.empty())
{
return NULL;
}
std::list<ListItem*>::iterator item_iter = list_.begin();
ListItem* return_item = (*item_iter);
return_item->this_iter_ = item_iter;
return return_item;
ListItem* ListWrapper::First() const {
if (list_.empty()) {
return NULL;
}
std::list<ListItem*>::iterator item_iter = list_.begin();
ListItem* return_item = (*item_iter);
return_item->this_iter_ = item_iter;
return return_item;
}
ListItem* ListWrapper::Last() const
{
if(list_.empty())
{
return NULL;
}
// std::list::end() addresses the last item + 1. Decrement so that the
// actual last is accessed.
std::list<ListItem*>::iterator item_iter = list_.end();
--item_iter;
ListItem* return_item = (*item_iter);
return_item->this_iter_ = item_iter;
return return_item;
ListItem* ListWrapper::Last() const {
if (list_.empty()) {
return NULL;
}
// std::list::end() addresses the last item + 1. Decrement so that the
// actual last is accessed.
std::list<ListItem*>::iterator item_iter = list_.end();
--item_iter;
ListItem* return_item = (*item_iter);
return_item->this_iter_ = item_iter;
return return_item;
}
ListItem* ListWrapper::Next(ListItem* item) const
{
if(item == NULL)
{
return NULL;
}
std::list<ListItem*>::iterator item_iter = item->this_iter_;
++item_iter;
if (item_iter == list_.end())
{
return NULL;
}
ListItem* return_item = (*item_iter);
return_item->this_iter_ = item_iter;
return return_item;
ListItem* ListWrapper::Next(ListItem* item) const {
if (item == NULL) {
return NULL;
}
std::list<ListItem*>::iterator item_iter = item->this_iter_;
++item_iter;
if (item_iter == list_.end()) {
return NULL;
}
ListItem* return_item = (*item_iter);
return_item->this_iter_ = item_iter;
return return_item;
}
ListItem* ListWrapper::Previous(ListItem* item) const
{
if(item == NULL)
{
return NULL;
}
std::list<ListItem*>::iterator item_iter = item->this_iter_;
if (item_iter == list_.begin())
{
return NULL;
}
--item_iter;
ListItem* return_item = (*item_iter);
return_item->this_iter_ = item_iter;
return return_item;
ListItem* ListWrapper::Previous(ListItem* item) const {
if (item == NULL) {
return NULL;
}
std::list<ListItem*>::iterator item_iter = item->this_iter_;
if (item_iter == list_.begin()) {
return NULL;
}
--item_iter;
ListItem* return_item = (*item_iter);
return_item->this_iter_ = item_iter;
return return_item;
}
int ListWrapper::Insert(ListItem* existing_previous_item,
ListItem* new_item)
{
// Allow existingPreviousItem to be NULL if the list is empty.
// TODO (hellner) why allow this? Keep it as is for now to avoid
// breaking API contract.
if (!existing_previous_item && !Empty())
{
return -1;
}
ListItem* new_item) {
// Allow existing_previous_item to be NULL if the list is empty.
// TODO(hellner) why allow this? Keep it as is for now to avoid
// breaking API contract.
if (!existing_previous_item && !Empty()) {
return -1;
}
if (!new_item)
{
return -1;
}
if (!new_item) {
return -1;
}
std::list<ListItem*>::iterator insert_location = list_.begin();
if (!Empty())
{
insert_location = existing_previous_item->this_iter_;
if(insert_location != list_.end())
{
++insert_location;
}
std::list<ListItem*>::iterator insert_location = list_.begin();
if (!Empty()) {
insert_location = existing_previous_item->this_iter_;
if (insert_location != list_.end()) {
++insert_location;
}
}
list_.insert(insert_location,new_item);
return 0;
list_.insert(insert_location, new_item);
return 0;
}
int ListWrapper::InsertBefore(ListItem* existing_next_item,
ListItem* new_item)
{
// Allow existing_next_item to be NULL if the list is empty.
// Todo: why allow this? Keep it as is for now to avoid breaking API
// contract.
if (!existing_next_item && !Empty())
{
return -1;
}
if (!new_item)
{
return -1;
}
ListItem* new_item) {
// Allow existing_next_item to be NULL if the list is empty.
// Todo: why allow this? Keep it as is for now to avoid breaking API
// contract.
if (!existing_next_item && !Empty()) {
return -1;
}
if (!new_item) {
return -1;
}
std::list<ListItem*>::iterator insert_location = list_.begin();
if (!Empty())
{
insert_location = existing_next_item->this_iter_;
}
std::list<ListItem*>::iterator insert_location = list_.begin();
if (!Empty()) {
insert_location = existing_next_item->this_iter_;
}
list_.insert(insert_location,new_item);
return 0;
list_.insert(insert_location, new_item);
return 0;
}
int ListWrapper::Erase(ListItem* item)
{
if(item == NULL)
{
return -1;
}
list_.erase(item->this_iter_);
return 0;
int ListWrapper::Erase(ListItem* item) {
if (item == NULL) {
return -1;
}
list_.erase(item->this_iter_);
return 0;
}
} // namespace webrtc

View File

@ -13,54 +13,53 @@
#include <list>
#include "constructor_magic.h"
#include "webrtc/system_wrappers/interface/constructor_magic.h"
namespace webrtc {
class ListItem
{
friend class ListWrapper;
public:
ListItem(const void* ptr);
ListItem(const unsigned int item);
virtual ~ListItem();
void* GetItem() const;
unsigned int GetUnsignedItem() const;
class ListItem {
public:
ListItem(const void* ptr);
ListItem(const unsigned int item);
virtual ~ListItem();
void* GetItem() const;
unsigned int GetUnsignedItem() const;
private:
mutable std::list<ListItem*>::iterator this_iter_;
const void* item_ptr_;
const unsigned int item_;
DISALLOW_COPY_AND_ASSIGN(ListItem);
private:
friend class ListWrapper;
mutable std::list<ListItem*>::iterator this_iter_;
const void* item_ptr_;
const unsigned int item_;
DISALLOW_COPY_AND_ASSIGN(ListItem);
};
class ListWrapper
{
public:
ListWrapper();
~ListWrapper();
class ListWrapper {
public:
ListWrapper();
~ListWrapper();
// ListWrapper functions
unsigned int GetSize() const;
int PushBack(const void* ptr);
int PushBack(const unsigned int item_id);
int PushFront(const void* ptr);
int PushFront(const unsigned int item_id);
int PopFront();
int PopBack();
bool Empty() const;
ListItem* First() const;
ListItem* Last() const;
ListItem* Next(ListItem* item) const;
ListItem* Previous(ListItem* item) const;
int Erase(ListItem* item);
int Insert(ListItem* existing_previous_item, ListItem* new_item);
int InsertBefore(ListItem* existing_next_item, ListItem* new_item);
// ListWrapper functions
unsigned int GetSize() const;
int PushBack(const void* ptr);
int PushBack(const unsigned int item_id);
int PushFront(const void* ptr);
int PushFront(const unsigned int item_id);
int PopFront();
int PopBack();
bool Empty() const;
ListItem* First() const;
ListItem* Last() const;
ListItem* Next(ListItem* item) const;
ListItem* Previous(ListItem* item) const;
int Erase(ListItem* item);
int Insert(ListItem* existing_previous_item, ListItem* new_item);
int InsertBefore(ListItem* existing_next_item, ListItem* new_item);
private:
mutable std::list<ListItem*> list_;
DISALLOW_COPY_AND_ASSIGN(ListWrapper);
private:
mutable std::list<ListItem*> list_;
DISALLOW_COPY_AND_ASSIGN(ListWrapper);
};
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_LIST_STL_H_
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_LIST_STL_H_

View File

@ -8,10 +8,10 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "gtest/gtest.h"
#include "webrtc/system_wrappers/interface/list_wrapper.h"
#include "system_wrappers/interface/list_wrapper.h"
#include "system_wrappers/interface/scoped_ptr.h"
#include "gtest/gtest.h"
#include "webrtc/system_wrappers/interface/scoped_ptr.h"
using ::webrtc::ListWrapper;
using ::webrtc::ListItem;
@ -32,70 +32,70 @@ const unsigned int kNumberOfElements = 10;
// Note: the non-virtual fuctions behave the same for both versions.
class ListWrapperSimple {
public:
static ListWrapperSimple* Create(bool static_allocation);
virtual ~ListWrapperSimple() {}
static ListWrapperSimple* Create(bool static_allocation);
virtual ~ListWrapperSimple() {}
// These three functions should be used for manipulating ListItems so that
// they are the type corresponding to the underlying implementation.
virtual unsigned int GetUnsignedItem(
const ListItem* item) const = 0;
virtual ListItem* CreateListItem(unsigned int item_id) = 0;
unsigned int GetSize() const {
return list_.GetSize();
}
virtual int PushBack(const unsigned int item_id) = 0;
virtual int PushFront(const unsigned int item_id) = 0;
virtual int PopFront() = 0;
virtual int PopBack() = 0;
bool Empty() const {
return list_.Empty();
}
ListItem* First() const {
return list_.First();
}
ListItem* Last() const {
return list_.Last();
}
ListItem* Next(ListItem* item) const {
return list_.Next(item);
}
ListItem* Previous(ListItem* item) const {
return list_.Previous(item);
}
virtual int Erase(ListItem* item) = 0;
int Insert(ListItem* existing_previous_item,
ListItem* new_item) {
const int retval = list_.Insert(existing_previous_item, new_item);
if (retval != 0) {
EXPECT_TRUE(DestroyListItem(new_item));
}
return retval;
// These three functions should be used for manipulating ListItems so that
// they are the type corresponding to the underlying implementation.
virtual unsigned int GetUnsignedItem(
const ListItem* item) const = 0;
virtual ListItem* CreateListItem(unsigned int item_id) = 0;
unsigned int GetSize() const {
return list_.GetSize();
}
virtual int PushBack(const unsigned int item_id) = 0;
virtual int PushFront(const unsigned int item_id) = 0;
virtual int PopFront() = 0;
virtual int PopBack() = 0;
bool Empty() const {
return list_.Empty();
}
ListItem* First() const {
return list_.First();
}
ListItem* Last() const {
return list_.Last();
}
ListItem* Next(ListItem* item) const {
return list_.Next(item);
}
ListItem* Previous(ListItem* item) const {
return list_.Previous(item);
}
virtual int Erase(ListItem* item) = 0;
int Insert(ListItem* existing_previous_item,
ListItem* new_item) {
const int retval = list_.Insert(existing_previous_item, new_item);
if (retval != 0) {
EXPECT_TRUE(DestroyListItem(new_item));
}
return retval;
}
int InsertBefore(ListItem* existing_next_item,
ListItem* new_item) {
const int retval = list_.InsertBefore(existing_next_item, new_item);
if (retval != 0) {
EXPECT_TRUE(DestroyListItem(new_item));
}
return retval;
int InsertBefore(ListItem* existing_next_item,
ListItem* new_item) {
const int retval = list_.InsertBefore(existing_next_item, new_item);
if (retval != 0) {
EXPECT_TRUE(DestroyListItem(new_item));
}
return retval;
}
protected:
ListWrapperSimple() {}
ListWrapperSimple() {}
virtual bool DestroyListItemContent(ListItem* item) = 0;
bool DestroyListItem(ListItem* item) {
const bool retval = DestroyListItemContent(item);
delete item;
return retval;
}
virtual bool DestroyListItemContent(ListItem* item) = 0;
bool DestroyListItem(ListItem* item) {
const bool retval = DestroyListItemContent(item);
delete item;
return retval;
}
ListWrapper list_;
ListWrapper list_;
};
void ClearList(ListWrapperSimple* list_wrapper) {
if (list_wrapper == NULL) {
return;
return;
}
ListItem* list_item = list_wrapper->First();
while (list_item != NULL) {
@ -106,374 +106,370 @@ void ClearList(ListWrapperSimple* list_wrapper) {
class ListWrapperStatic : public ListWrapperSimple {
public:
ListWrapperStatic() {}
virtual ~ListWrapperStatic() {
ClearList(this);
}
ListWrapperStatic() {}
virtual ~ListWrapperStatic() {
ClearList(this);
}
virtual unsigned int GetUnsignedItem(const ListItem* item) const {
return item->GetUnsignedItem();
}
virtual ListItem* CreateListItem(unsigned int item_id) {
return new ListItem(item_id);
}
virtual bool DestroyListItemContent(ListItem* item) {
return true;
}
virtual int PushBack(const unsigned int item_id) {
return list_.PushBack(item_id);
}
virtual int PushFront(const unsigned int item_id) {
return list_.PushFront(item_id);
}
virtual int PopFront() {
return list_.PopFront();
}
virtual int PopBack() {
return list_.PopBack();
}
virtual int Erase(ListItem* item) {
return list_.Erase(item);
}
virtual unsigned int GetUnsignedItem(const ListItem* item) const {
return item->GetUnsignedItem();
}
virtual ListItem* CreateListItem(unsigned int item_id) {
return new ListItem(item_id);
}
virtual bool DestroyListItemContent(ListItem* item) {
return true;
}
virtual int PushBack(const unsigned int item_id) {
return list_.PushBack(item_id);
}
virtual int PushFront(const unsigned int item_id) {
return list_.PushFront(item_id);
}
virtual int PopFront() {
return list_.PopFront();
}
virtual int PopBack() {
return list_.PopBack();
}
virtual int Erase(ListItem* item) {
return list_.Erase(item);
}
};
class ListWrapperDynamic : public ListWrapperSimple {
public:
ListWrapperDynamic() {}
virtual ~ListWrapperDynamic() {
ClearList(this);
}
ListWrapperDynamic() {}
virtual ~ListWrapperDynamic() {
ClearList(this);
}
virtual unsigned int GetUnsignedItem(const ListItem* item) const {
const unsigned int* return_value_pointer =
reinterpret_cast<unsigned int*> (item->GetItem());
if (return_value_pointer == NULL) {
return -1;
}
return *return_value_pointer;
virtual unsigned int GetUnsignedItem(const ListItem* item) const {
const unsigned int* return_value_pointer =
reinterpret_cast<unsigned int*>(item->GetItem());
if (return_value_pointer == NULL) {
return -1;
}
virtual ListItem* CreateListItem(unsigned int item_id) {
unsigned int* item_id_pointer = new unsigned int;
if (item_id_pointer == NULL) {
return NULL;
}
*item_id_pointer = item_id;
ListItem* return_value = new ListItem(
reinterpret_cast<void*>(item_id_pointer));
if (return_value == NULL) {
delete item_id_pointer;
return NULL;
}
return return_value;
return *return_value_pointer;
}
virtual ListItem* CreateListItem(unsigned int item_id) {
unsigned int* item_id_pointer = new unsigned int;
if (item_id_pointer == NULL) {
return NULL;
}
virtual bool DestroyListItemContent(ListItem* item) {
if (item == NULL) {
return false;
}
bool return_value = false;
unsigned int* item_id_ptr = reinterpret_cast<unsigned int*>(
item->GetItem());
if (item_id_ptr != NULL) {
return_value = true;
delete item_id_ptr;
}
return return_value;
*item_id_pointer = item_id;
ListItem* return_value = new ListItem(
reinterpret_cast<void*>(item_id_pointer));
if (return_value == NULL) {
delete item_id_pointer;
return NULL;
}
virtual int PushBack(const unsigned int item_id) {
unsigned int* item_id_ptr = new unsigned int;
if (item_id_ptr == NULL) {
return -1;
}
*item_id_ptr = item_id;
const int return_value = list_.PushBack(
reinterpret_cast<void*>(item_id_ptr));
if (return_value != 0) {
delete item_id_ptr;
}
return return_value;
return return_value;
}
virtual bool DestroyListItemContent(ListItem* item) {
if (item == NULL) {
return false;
}
virtual int PushFront(const unsigned int item_id) {
unsigned int* item_id_ptr = new unsigned int;
if (item_id_ptr == NULL) {
return -1;
}
*item_id_ptr = item_id;
const int return_value = list_.PushFront(
reinterpret_cast<void*>(item_id_ptr));
if (return_value != 0) {
delete item_id_ptr;
}
return return_value;
bool return_value = false;
unsigned int* item_id_ptr = reinterpret_cast<unsigned int*>(
item->GetItem());
if (item_id_ptr != NULL) {
return_value = true;
delete item_id_ptr;
}
virtual int PopFront() {
return Erase(list_.First());
return return_value;
}
virtual int PushBack(const unsigned int item_id) {
unsigned int* item_id_ptr = new unsigned int;
if (item_id_ptr == NULL) {
return -1;
}
virtual int PopBack() {
return Erase(list_.Last());
*item_id_ptr = item_id;
const int return_value = list_.PushBack(
reinterpret_cast<void*>(item_id_ptr));
if (return_value != 0) {
delete item_id_ptr;
}
virtual int Erase(ListItem* item) {
if (item == NULL) {
return -1;
}
int retval = 0;
if (!DestroyListItemContent(item)) {
retval = -1;
ADD_FAILURE();
}
if (list_.Erase(item) != 0) {
retval = -1;
}
return retval;
return return_value;
}
virtual int PushFront(const unsigned int item_id) {
unsigned int* item_id_ptr = new unsigned int;
if (item_id_ptr == NULL) {
return -1;
}
*item_id_ptr = item_id;
const int return_value = list_.PushFront(
reinterpret_cast<void*>(item_id_ptr));
if (return_value != 0) {
delete item_id_ptr;
}
return return_value;
}
virtual int PopFront() {
return Erase(list_.First());
}
virtual int PopBack() {
return Erase(list_.Last());
}
virtual int Erase(ListItem* item) {
if (item == NULL) {
return -1;
}
int retval = 0;
if (!DestroyListItemContent(item)) {
retval = -1;
ADD_FAILURE();
}
if (list_.Erase(item) != 0) {
retval = -1;
}
return retval;
}
};
ListWrapperSimple* ListWrapperSimple::Create(bool static_allocation) {
if(static_allocation)
{
return new ListWrapperStatic();
}
return new ListWrapperDynamic();
if (static_allocation) {
return new ListWrapperStatic();
}
return new ListWrapperDynamic();
}
ListWrapperSimple* CreateAscendingList(bool static_allocation) {
ListWrapperSimple* return_value = ListWrapperSimple::Create(
static_allocation);
if (return_value == NULL) {
return NULL;
ListWrapperSimple* return_value = ListWrapperSimple::Create(
static_allocation);
if (return_value == NULL) {
return NULL;
}
for (unsigned int i = 0; i < kNumberOfElements; ++i) {
if (return_value->PushBack(i) == -1) {
ClearList(return_value);
delete return_value;
return NULL;
}
for (unsigned int i = 0; i < kNumberOfElements; ++i) {
if (return_value->PushBack(i) == -1) {
ClearList(return_value);
delete return_value;
return NULL;
}
}
return return_value;
}
return return_value;
}
ListWrapperSimple* CreateDescendingList(bool static_allocation) {
ListWrapperSimple* return_value = ListWrapperSimple::Create(
static_allocation);
if (return_value == NULL) {
return NULL;
ListWrapperSimple* return_value = ListWrapperSimple::Create(
static_allocation);
if (return_value == NULL) {
return NULL;
}
for (unsigned int i = 0; i < kNumberOfElements; ++i) {
if (return_value->PushBack(kNumberOfElements - i - 1) == -1) {
ClearList(return_value);
delete return_value;
return NULL;
}
for (unsigned int i = 0; i < kNumberOfElements; ++i) {
if (return_value->PushBack(kNumberOfElements - i - 1) == -1) {
ClearList(return_value);
delete return_value;
return NULL;
}
}
return return_value;
}
return return_value;
}
// [0,kNumberOfElements - 1,1,kNumberOfElements - 2,...] (this is why
// kNumberOfElements need to be even)
ListWrapperSimple* CreateInterleavedList(bool static_allocation) {
ListWrapperSimple* return_value = ListWrapperSimple::Create(
static_allocation);
if (return_value == NULL) {
return NULL;
ListWrapperSimple* return_value = ListWrapperSimple::Create(
static_allocation);
if (return_value == NULL) {
return NULL;
}
unsigned int uneven_count = 0;
unsigned int even_count = 0;
for (unsigned int i = 0; i < kNumberOfElements; i++) {
unsigned int push_value = 0;
if ((i % 2) == 0) {
push_value = even_count;
even_count++;
} else {
push_value = kNumberOfElements - uneven_count - 1;
uneven_count++;
}
unsigned int uneven_count = 0;
unsigned int even_count = 0;
for (unsigned int i = 0; i < kNumberOfElements; i++) {
unsigned int push_value = 0;
if ((i % 2) == 0) {
push_value = even_count;
even_count++;
} else {
push_value = kNumberOfElements - uneven_count - 1;
uneven_count++;
}
if (return_value->PushBack(push_value) == -1) {
ClearList(return_value);
delete return_value;
return NULL;
}
if (return_value->PushBack(push_value) == -1) {
ClearList(return_value);
delete return_value;
return NULL;
}
return return_value;
}
return return_value;
}
void PrintList(const ListWrapperSimple* list) {
ListItem* list_item = list->First();
printf("[");
while (list_item != NULL)
{
printf("%3u", list->GetUnsignedItem(list_item));
list_item = list->Next(list_item);
}
printf("]\n");
ListItem* list_item = list->First();
printf("[");
while (list_item != NULL) {
printf("%3u", list->GetUnsignedItem(list_item));
list_item = list->Next(list_item);
}
printf("]\n");
}
bool CompareLists(const ListWrapperSimple* lhs, const ListWrapperSimple* rhs) {
const unsigned int list_size = lhs->GetSize();
if (lhs->GetSize() != rhs->GetSize()) {
return false;
const unsigned int list_size = lhs->GetSize();
if (lhs->GetSize() != rhs->GetSize()) {
return false;
}
if (lhs->Empty()) {
return rhs->Empty();
}
unsigned int i = 0;
ListItem* lhs_item = lhs->First();
ListItem* rhs_item = rhs->First();
while (i < list_size) {
if (lhs_item == NULL) {
return false;
}
if (lhs->Empty()) {
return rhs->Empty();
if (rhs_item == NULL) {
return false;
}
unsigned int i = 0;
ListItem* lhs_item = lhs->First();
ListItem* rhs_item = rhs->First();
while (i < list_size) {
if (lhs_item == NULL) {
return false;
}
if (rhs_item == NULL) {
return false;
}
if (lhs->GetUnsignedItem(lhs_item) != rhs->GetUnsignedItem(rhs_item)) {
return false;
}
i++;
lhs_item = lhs->Next(lhs_item);
rhs_item = rhs->Next(rhs_item);
if (lhs->GetUnsignedItem(lhs_item) != rhs->GetUnsignedItem(rhs_item)) {
return false;
}
return true;
i++;
lhs_item = lhs->Next(lhs_item);
rhs_item = rhs->Next(rhs_item);
}
return true;
}
TEST(ListWrapperTest,ReverseNewIntList) {
// Create a new temporary list with elements reversed those of
// new_int_list_
const scoped_ptr<ListWrapperSimple> descending_list(
CreateDescendingList(rand()%2));
ASSERT_FALSE(descending_list.get() == NULL);
ASSERT_FALSE(descending_list->Empty());
ASSERT_EQ(kNumberOfElements,descending_list->GetSize());
TEST(ListWrapperTest, ReverseNewIntList) {
// Create a new temporary list with elements reversed those of
// new_int_list_
const scoped_ptr<ListWrapperSimple> descending_list(
CreateDescendingList(rand() % 2));
ASSERT_FALSE(descending_list.get() == NULL);
ASSERT_FALSE(descending_list->Empty());
ASSERT_EQ(kNumberOfElements, descending_list->GetSize());
const scoped_ptr<ListWrapperSimple> ascending_list(
CreateAscendingList(rand()%2));
ASSERT_FALSE(ascending_list.get() == NULL);
ASSERT_FALSE(ascending_list->Empty());
ASSERT_EQ(kNumberOfElements,ascending_list->GetSize());
const scoped_ptr<ListWrapperSimple> ascending_list(
CreateAscendingList(rand() % 2));
ASSERT_FALSE(ascending_list.get() == NULL);
ASSERT_FALSE(ascending_list->Empty());
ASSERT_EQ(kNumberOfElements, ascending_list->GetSize());
scoped_ptr<ListWrapperSimple> list_to_reverse(
ListWrapperSimple::Create(rand()%2));
scoped_ptr<ListWrapperSimple> list_to_reverse(
ListWrapperSimple::Create(rand() % 2));
// Reverse the list using PushBack and Previous.
for (ListItem* item = ascending_list->Last(); item != NULL;
item = ascending_list->Previous(item)) {
list_to_reverse->PushBack(ascending_list->GetUnsignedItem(item));
}
// Reverse the list using PushBack and Previous.
for (ListItem* item = ascending_list->Last(); item != NULL;
item = ascending_list->Previous(item)) {
list_to_reverse->PushBack(ascending_list->GetUnsignedItem(item));
}
ASSERT_TRUE(CompareLists(descending_list.get(), list_to_reverse.get()));
ASSERT_TRUE(CompareLists(descending_list.get(), list_to_reverse.get()));
scoped_ptr<ListWrapperSimple> list_to_un_reverse(
ListWrapperSimple::Create(rand()%2));
ASSERT_FALSE(list_to_un_reverse.get() == NULL);
// Reverse the reversed list using PushFront and Next.
for (ListItem* item = list_to_reverse->First(); item != NULL;
item = list_to_reverse->Next(item)) {
list_to_un_reverse->PushFront(list_to_reverse->GetUnsignedItem(item));
}
ASSERT_TRUE(CompareLists(ascending_list.get(), list_to_un_reverse.get()));
scoped_ptr<ListWrapperSimple> list_to_un_reverse(
ListWrapperSimple::Create(rand() % 2));
ASSERT_FALSE(list_to_un_reverse.get() == NULL);
// Reverse the reversed list using PushFront and Next.
for (ListItem* item = list_to_reverse->First(); item != NULL;
item = list_to_reverse->Next(item)) {
list_to_un_reverse->PushFront(list_to_reverse->GetUnsignedItem(item));
}
ASSERT_TRUE(CompareLists(ascending_list.get(), list_to_un_reverse.get()));
}
TEST(ListWrapperTest,PopTest) {
scoped_ptr<ListWrapperSimple> ascending_list(CreateAscendingList(rand()%2));
ASSERT_FALSE(ascending_list.get() == NULL);
ASSERT_FALSE(ascending_list->Empty());
EXPECT_EQ(0, ascending_list->PopFront());
EXPECT_EQ(1U, ascending_list->GetUnsignedItem(ascending_list->First()));
TEST(ListWrapperTest, PopTest) {
scoped_ptr<ListWrapperSimple> ascending_list(CreateAscendingList(rand() % 2));
ASSERT_FALSE(ascending_list.get() == NULL);
ASSERT_FALSE(ascending_list->Empty());
EXPECT_EQ(0, ascending_list->PopFront());
EXPECT_EQ(1U, ascending_list->GetUnsignedItem(ascending_list->First()));
EXPECT_EQ(0, ascending_list->PopBack());
EXPECT_EQ(kNumberOfElements - 2, ascending_list->GetUnsignedItem(
EXPECT_EQ(0, ascending_list->PopBack());
EXPECT_EQ(kNumberOfElements - 2, ascending_list->GetUnsignedItem(
ascending_list->Last()));
EXPECT_EQ(kNumberOfElements - 2, ascending_list->GetSize());
EXPECT_EQ(kNumberOfElements - 2, ascending_list->GetSize());
}
// Use Insert to interleave two lists.
TEST(ListWrapperTest,InterLeaveTest) {
scoped_ptr<ListWrapperSimple> interleave_list(
CreateAscendingList(rand()%2));
ASSERT_FALSE(interleave_list.get() == NULL);
ASSERT_FALSE(interleave_list->Empty());
TEST(ListWrapperTest, InterLeaveTest) {
scoped_ptr<ListWrapperSimple> interleave_list(
CreateAscendingList(rand() % 2));
ASSERT_FALSE(interleave_list.get() == NULL);
ASSERT_FALSE(interleave_list->Empty());
scoped_ptr<ListWrapperSimple> descending_list(
CreateDescendingList(rand()%2));
ASSERT_FALSE(descending_list.get() == NULL);
scoped_ptr<ListWrapperSimple> descending_list(
CreateDescendingList(rand() % 2));
ASSERT_FALSE(descending_list.get() == NULL);
for (unsigned int i = 0; i < kNumberOfElements/2; ++i) {
ASSERT_EQ(0, interleave_list->PopBack());
ASSERT_EQ(0, descending_list->PopBack());
for (unsigned int i = 0; i < kNumberOfElements / 2; ++i) {
ASSERT_EQ(0, interleave_list->PopBack());
ASSERT_EQ(0, descending_list->PopBack());
}
ASSERT_EQ(kNumberOfElements / 2, interleave_list->GetSize());
ASSERT_EQ(kNumberOfElements / 2, descending_list->GetSize());
unsigned int insert_position = kNumberOfElements / 2;
ASSERT_EQ(insert_position * 2, kNumberOfElements);
while (!descending_list->Empty()) {
ListItem* item = descending_list->Last();
ASSERT_FALSE(item == NULL);
const unsigned int item_id = descending_list->GetUnsignedItem(item);
ASSERT_EQ(0, descending_list->Erase(item));
ListItem* insert_item = interleave_list->CreateListItem(item_id);
ASSERT_FALSE(insert_item == NULL);
item = interleave_list->First();
ASSERT_FALSE(item == NULL);
for (unsigned int j = 0; j < insert_position - 1; ++j) {
item = interleave_list->Next(item);
ASSERT_FALSE(item == NULL);
}
ASSERT_EQ(kNumberOfElements/2, interleave_list->GetSize());
ASSERT_EQ(kNumberOfElements/2, descending_list->GetSize());
EXPECT_EQ(0, interleave_list->Insert(item, insert_item));
--insert_position;
}
unsigned int insert_position = kNumberOfElements/2;
ASSERT_EQ(insert_position * 2, kNumberOfElements);
while (!descending_list->Empty())
{
ListItem* item = descending_list->Last();
ASSERT_FALSE(item == NULL);
const unsigned int item_id = descending_list->GetUnsignedItem(item);
ASSERT_EQ(0, descending_list->Erase(item));
ListItem* insert_item = interleave_list->CreateListItem(item_id);
ASSERT_FALSE(insert_item == NULL);
item = interleave_list->First();
ASSERT_FALSE(item == NULL);
for (unsigned int j = 0; j < insert_position - 1; ++j) {
item = interleave_list->Next(item);
ASSERT_FALSE(item == NULL);
}
EXPECT_EQ(0, interleave_list->Insert(item, insert_item));
--insert_position;
}
scoped_ptr<ListWrapperSimple> interleaved_list(
CreateInterleavedList(rand()%2));
ASSERT_FALSE(interleaved_list.get() == NULL);
ASSERT_FALSE(interleaved_list->Empty());
ASSERT_TRUE(CompareLists(interleaved_list.get(), interleave_list.get()));
scoped_ptr<ListWrapperSimple> interleaved_list(
CreateInterleavedList(rand() % 2));
ASSERT_FALSE(interleaved_list.get() == NULL);
ASSERT_FALSE(interleaved_list->Empty());
ASSERT_TRUE(CompareLists(interleaved_list.get(), interleave_list.get()));
}
// Use InsertBefore to interleave two lists.
TEST(ListWrapperTest,InterLeaveTestII) {
scoped_ptr<ListWrapperSimple> interleave_list(
CreateDescendingList(rand()%2));
ASSERT_FALSE(interleave_list.get() == NULL);
ASSERT_FALSE(interleave_list->Empty());
TEST(ListWrapperTest, InterLeaveTestII) {
scoped_ptr<ListWrapperSimple> interleave_list(
CreateDescendingList(rand() % 2));
ASSERT_FALSE(interleave_list.get() == NULL);
ASSERT_FALSE(interleave_list->Empty());
scoped_ptr<ListWrapperSimple> ascending_list(CreateAscendingList(rand()%2));
ASSERT_FALSE(ascending_list.get() == NULL);
scoped_ptr<ListWrapperSimple> ascending_list(CreateAscendingList(rand() % 2));
ASSERT_FALSE(ascending_list.get() == NULL);
for (unsigned int i = 0; i < kNumberOfElements/2; ++i) {
ASSERT_EQ(0, interleave_list->PopBack());
ASSERT_EQ(0, ascending_list->PopBack());
for (unsigned int i = 0; i < kNumberOfElements / 2; ++i) {
ASSERT_EQ(0, interleave_list->PopBack());
ASSERT_EQ(0, ascending_list->PopBack());
}
ASSERT_EQ(kNumberOfElements / 2, interleave_list->GetSize());
ASSERT_EQ(kNumberOfElements / 2, ascending_list->GetSize());
unsigned int insert_position = kNumberOfElements / 2;
ASSERT_EQ(insert_position * 2, kNumberOfElements);
while (!ascending_list->Empty()) {
ListItem* item = ascending_list->Last();
ASSERT_FALSE(item == NULL);
const unsigned int item_id = ascending_list->GetUnsignedItem(item);
ASSERT_EQ(0, ascending_list->Erase(item));
ListItem* insert_item = interleave_list->CreateListItem(item_id);
ASSERT_FALSE(insert_item == NULL);
item = interleave_list->First();
ASSERT_FALSE(item == NULL);
for (unsigned int j = 0; j < insert_position - 1; ++j) {
item = interleave_list->Next(item);
ASSERT_FALSE(item == NULL);
}
ASSERT_EQ(kNumberOfElements/2, interleave_list->GetSize());
ASSERT_EQ(kNumberOfElements/2, ascending_list->GetSize());
EXPECT_EQ(interleave_list->InsertBefore(item, insert_item), 0);
--insert_position;
}
unsigned int insert_position = kNumberOfElements/2;
ASSERT_EQ(insert_position * 2, kNumberOfElements);
while (!ascending_list->Empty())
{
ListItem* item = ascending_list->Last();
ASSERT_FALSE(item == NULL);
scoped_ptr<ListWrapperSimple> interleaved_list(
CreateInterleavedList(rand() % 2));
ASSERT_FALSE(interleaved_list.get() == NULL);
ASSERT_FALSE(interleaved_list->Empty());
const unsigned int item_id = ascending_list->GetUnsignedItem(item);
ASSERT_EQ(0,ascending_list->Erase(item));
ListItem* insert_item = interleave_list->CreateListItem(item_id);
ASSERT_FALSE(insert_item == NULL);
item = interleave_list->First();
ASSERT_FALSE(item == NULL);
for (unsigned int j = 0; j < insert_position - 1; ++j) {
item = interleave_list->Next(item);
ASSERT_FALSE(item == NULL);
}
EXPECT_EQ(interleave_list->InsertBefore(item, insert_item), 0);
--insert_position;
}
scoped_ptr<ListWrapperSimple> interleaved_list(
CreateInterleavedList(rand()%2));
ASSERT_FALSE(interleaved_list.get() == NULL);
ASSERT_FALSE(interleaved_list->Empty());
ASSERT_TRUE(CompareLists(interleaved_list.get(), interleave_list.get()));
ASSERT_TRUE(CompareLists(interleaved_list.get(), interleave_list.get()));
}