This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- ref
- TEST
#include "config.h"
#include "wtf/ListHashSet.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefCounted.h"
#include "wtf/RefPtr.h"
#include <gtest/gtest.h>
namespace {
TEST(WTF, ListHashSetRemoveFirst)
{
ListHashSet<int> list;
list.add(1);
list.add(2);
list.add(3);
ASSERT_EQ(1, list.first());
list.removeFirst();
ASSERT_EQ(2, list.first());
list.removeFirst();
ASSERT_EQ(3, list.first());
list.removeFirst();
ASSERT_TRUE(list.isEmpty());
}
TEST(WTF, ListHashSetAppendOrMoveToLastNewItems)
{
ListHashSet<int> list;
ListHashSet<int>::AddResult result = list.appendOrMoveToLast(1);
ASSERT_TRUE(result.isNewEntry);
result = list.add(2);
ASSERT_TRUE(result.isNewEntry);
result = list.appendOrMoveToLast(3);
ASSERT_TRUE(result.isNewEntry);
ASSERT_EQ(list.size(), 3UL);
ListHashSet<int>::iterator iterator = list.begin();
ASSERT_EQ(1, *iterator);
++iterator;
ASSERT_EQ(2, *iterator);
++iterator;
ASSERT_EQ(3, *iterator);
++iterator;
}
TEST(WTF, ListHashSetAppendOrMoveToLastWithDuplicates)
{
ListHashSet<int> list;
ListHashSet<int>::AddResult result = list.add(1);
ASSERT_TRUE(result.isNewEntry);
result = list.appendOrMoveToLast(1);
ASSERT_FALSE(result.isNewEntry);
ASSERT_EQ(1UL, list.size());
list.add(2);
list.add(3);
ASSERT_EQ(3UL, list.size());
ASSERT_EQ(3, list.last());
result = list.appendOrMoveToLast(2);
ASSERT_FALSE(result.isNewEntry);
ASSERT_EQ(2, list.last());
result = list.appendOrMoveToLast(3);
ASSERT_FALSE(result.isNewEntry);
result = list.appendOrMoveToLast(2);
ASSERT_FALSE(result.isNewEntry);
result = list.appendOrMoveToLast(1);
ASSERT_FALSE(result.isNewEntry);
ASSERT_EQ(3UL, list.size());
ListHashSet<int>::iterator iterator = list.begin();
ASSERT_EQ(3, *iterator);
++iterator;
ASSERT_EQ(2, *iterator);
++iterator;
ASSERT_EQ(1, *iterator);
++iterator;
}
TEST(WTF, ListHashSetPrependOrMoveToLastNewItems)
{
ListHashSet<int> list;
ListHashSet<int>::AddResult result = list.prependOrMoveToFirst(1);
ASSERT_TRUE(result.isNewEntry);
result = list.add(2);
ASSERT_TRUE(result.isNewEntry);
result = list.prependOrMoveToFirst(3);
ASSERT_TRUE(result.isNewEntry);
ASSERT_EQ(list.size(), 3UL);
ListHashSet<int>::iterator iterator = list.begin();
ASSERT_EQ(3, *iterator);
++iterator;
ASSERT_EQ(1, *iterator);
++iterator;
ASSERT_EQ(2, *iterator);
++iterator;
}
TEST(WTF, ListHashSetPrependOrMoveToLastWithDuplicates)
{
ListHashSet<int> list;
ListHashSet<int>::AddResult result = list.add(1);
ASSERT_TRUE(result.isNewEntry);
result = list.prependOrMoveToFirst(1);
ASSERT_FALSE(result.isNewEntry);
ASSERT_EQ(1UL, list.size());
list.add(2);
list.add(3);
ASSERT_EQ(3UL, list.size());
ASSERT_EQ(1, list.first());
result = list.prependOrMoveToFirst(2);
ASSERT_FALSE(result.isNewEntry);
ASSERT_EQ(2, list.first());
result = list.prependOrMoveToFirst(1);
ASSERT_FALSE(result.isNewEntry);
result = list.prependOrMoveToFirst(2);
ASSERT_FALSE(result.isNewEntry);
result = list.prependOrMoveToFirst(3);
ASSERT_FALSE(result.isNewEntry);
ASSERT_EQ(3UL, list.size());
ListHashSet<int>::iterator iterator = list.begin();
ASSERT_EQ(3, *iterator);
++iterator;
ASSERT_EQ(2, *iterator);
++iterator;
ASSERT_EQ(1, *iterator);
++iterator;
}
class DummyRefCounted: public WTF::RefCounted<DummyRefCounted> {
public:
DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) { m_isDeleted = false; }
~DummyRefCounted() { m_isDeleted = true; }
void ref()
{
WTF::RefCounted<DummyRefCounted>::ref();
++m_refInvokesCount;
}
static int m_refInvokesCount;
private:
bool& m_isDeleted;
};
int DummyRefCounted::m_refInvokesCount = 0;
TEST(WTF, ListHashSetWithRefPtr)
{
bool isDeleted;
RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted));
ASSERT_EQ(0, DummyRefCounted::m_refInvokesCount);
ListHashSet<RefPtr<DummyRefCounted> > list;
list.add(ptr);
ASSERT_EQ(1, DummyRefCounted::m_refInvokesCount);
ASSERT_EQ(ptr, list.first());
DummyRefCounted* rawPtr = ptr.get();
ASSERT_TRUE(list.contains(ptr));
ASSERT_TRUE(list.contains(rawPtr));
ptr.clear();
ASSERT_FALSE(isDeleted);
list.remove(rawPtr);
ASSERT_TRUE(isDeleted);
ASSERT_EQ(1, DummyRefCounted::m_refInvokesCount);
}
}