/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/************************************************************** Now define the token deallocator class...
**************************************************************/ namespace TestNsDeque {
template <typename T> class _Dealloc : public nsDequeFunctor<T> { virtualvoidoperator()(T* aObject) {}
};
staticbool VerifyContents(const nsDeque<int>& aDeque, constint* aContents,
size_t aLength) { for (size_t i = 0; i < aLength; ++i) { if (*aDeque.ObjectAt(i) != aContents[i]) { returnfalse;
}
} returntrue;
}
class Deallocator : public nsDequeFunctor<int> { virtualvoidoperator()(int* aObject) { if (aObject) { // Set value to -1, to use in test function.
*(aObject) = -1;
}
}
};
class ForEachAdder : public nsDequeFunctor<int> { virtualvoidoperator()(int* aObject) { if (aObject) {
sum += *(int*)aObject;
}
}
private: int sum = 0;
public: int GetSum() { return sum; }
};
static uint32_t sFreeCount = 0;
class RefCountedClass { public:
RefCountedClass() : mRefCnt(0), mVal(0) {}
class ForEachRefPtr : public nsDequeFunctor<RefCountedClass> { virtualvoidoperator()(RefCountedClass* aObject) { if (aObject) {
aObject->SetVal(mVal);
}
}
TEST(NsDeque, OriginalTest)
{ const size_t size = 200; int ints[size];
size_t i = 0; int temp;
nsDeque<int> theDeque(new _Dealloc<int>); // construct a simple one...
// ints = [0...199] for (i = 0; i < size; i++) { // initialize'em
ints[i] = static_cast<int>(i);
} // queue = [0...69] for (i = 0; i < 70; i++) {
theDeque.Push(&ints[i]);
temp = *theDeque.Peek();
EXPECT_EQ(static_cast<int>(i), temp) << "Verify end after push #1";
EXPECT_EQ(i + 1, theDeque.GetSize()) << "Verify size after push #1";
}
EXPECT_EQ(70u, theDeque.GetSize()) << "Verify overall size after pushes #1";
// queue = [0...14] for (i = 1; i <= 55; i++) {
temp = *theDeque.Pop();
EXPECT_EQ(70 - static_cast<int>(i), temp) << "Verify end after pop # 1";
EXPECT_EQ(70u - i, theDeque.GetSize()) << "Verify size after pop # 1";
}
EXPECT_EQ(15u, theDeque.GetSize()) << "Verify overall size after pops";
// queue = [0...14,0...54] for (i = 0; i < 55; i++) {
theDeque.Push(&ints[i]);
temp = *theDeque.Peek();
EXPECT_EQ(static_cast<int>(i), temp) << "Verify end after push #2";
EXPECT_EQ(i + 15u + 1, theDeque.GetSize()) << "Verify size after push # 2";
}
EXPECT_EQ(70u, theDeque.GetSize())
<< "Verify size after end of all pushes #2";
// queue = [0...14,0...19] for (i = 1; i <= 35; i++) {
temp = *theDeque.Pop();
EXPECT_EQ(55 - static_cast<int>(i), temp) << "Verify end after pop # 2";
EXPECT_EQ(70u - i, theDeque.GetSize()) << "Verify size after pop #2";
}
EXPECT_EQ(35u, theDeque.GetSize())
<< "Verify overall size after end of all pops #2";
// queue = [0...14,0...19,0...34] for (i = 0; i < 35; i++) {
theDeque.Push(&ints[i]);
temp = *theDeque.Peek();
EXPECT_EQ(static_cast<int>(i), temp) << "Verify end after push # 3";
EXPECT_EQ(35u + 1u + i, theDeque.GetSize()) << "Verify size after push #3";
}
// queue = [0...14,0...19] for (i = 0; i < 35; i++) {
temp = *theDeque.Pop();
EXPECT_EQ(34 - static_cast<int>(i), temp) << "Verify end after pop # 3";
}
// queue = [0...14] for (i = 0; i < 20; i++) {
temp = *theDeque.Pop();
EXPECT_EQ(19 - static_cast<int>(i), temp) << "Verify end after pop # 4";
}
// queue = [] for (i = 0; i < 15; i++) {
temp = *theDeque.Pop();
EXPECT_EQ(14 - static_cast<int>(i), temp) << "Verify end after pop # 5";
}
EXPECT_EQ(0u, theDeque.GetSize()) << "Deque should finish empty.";
}
TEST(NsDeque, OriginalFlaw)
{ int ints[200]; int i = 0; int temp;
nsDeque<int> d(new _Dealloc<int>); /** * Test 1. Origin near end, semi full, call Peek(). * you start, mCapacity is 8
*/ for (i = 0; i < 30; i++) ints[i] = i;
for (i = 0; i < 6; i++) {
d.Push(&ints[i]);
temp = *d.Peek();
EXPECT_EQ(i, temp) << "OriginalFlaw push #1";
}
EXPECT_EQ(6u, d.GetSize()) << "OriginalFlaw size check #1";
for (i = 0; i < 4; i++) {
temp = *d.PopFront();
EXPECT_EQ(i, temp) << "PopFront test";
} // d = [4,5]
EXPECT_EQ(2u, d.GetSize()) << "OriginalFlaw size check #2";
for (i = 0; i < 4; i++) {
d.Push(&ints[6 + i]);
}
// d = [4...9] for (i = 4; i <= 9; i++) {
temp = *d.PopFront();
EXPECT_EQ(i, temp) << "OriginalFlaw empty check";
}
}
TEST(NsDeque, TestObjectAt)
{
nsDeque<int> d; constint count = 10; int ints[count]; for (int i = 0; i < count; i++) {
ints[i] = i;
}
for (int i = 0; i < 6; i++) {
d.Push(&ints[i]);
} // d = [0...5]
d.PopFront();
d.PopFront();
// d = [2..5] for (size_t i = 2; i <= 5; i++) { int t = *d.ObjectAt(i - 2);
EXPECT_EQ(static_cast<int>(i), t) << "Verify ObjectAt()";
}
}
TEST(NsDeque, TestPushFront)
{ // PushFront has some interesting corner cases, primarily we're interested in // whether: // - wrapping around works properly // - growing works properly
// And one more so that it wraps again
d.PushFront(pool + kMaxSizeBeforeGrowth + 1);
EXPECT_EQ(kMaxSizeBeforeGrowth + 2, d.GetSize()) << "verify size";
const size_t NumTestValues = 3; for (size_t i = 0; i < NumTestValues; ++i) {
d.Push(newint(i + 1));
}
static_assert(
std::is_same_v<nsDeque<int>::ConstDequeIterator,
decltype(std::declval<const nsDeque<int>&>().begin())>, "(const nsDeque).begin() should return ConstDequeIterator");
static_assert(
std::is_same_v<nsDeque<int>::ConstDequeIterator,
decltype(std::declval<const nsDeque<int>&>().end())>, "(const nsDeque).end() should return ConstDequeIterator");
int sum = 0; for (int* ob : const_cast<const nsDeque<int>&>(d)) {
sum += *ob;
}
EXPECT_EQ(1 + 2 + 3, sum) << "Const-range-for should iterate over values";
}
TEST(NsDeque, TestRangeFor)
{ const size_t NumTestValues = 3; struct Test {
size_t runAfterLoopCount;
std::function<void(nsDeque<int>&)> function; int expectedSum; constchar* description;
}; // Note: All tests start with a deque containing 3 pointers to ints 1, 2, 3.
Test tests[] = {
{0, [](nsDeque<int>& d) {}, 1 + 2 + 3, "no changes"},
for (const Test& test : tests) {
nsDeque<int> d(new Deallocator());
for (size_t i = 0; i < NumTestValues; ++i) {
d.Push(newint(i + 1));
}
static_assert(
std::is_same_v<nsDeque<int>::ConstIterator, decltype(d.begin())>, "(non-const nsDeque).begin() should return ConstIterator");
static_assert(
std::is_same_v<nsDeque<int>::ConstIterator, decltype(d.end())>, "(non-const nsDeque).end() should return ConstIterator");
int sum = 0;
size_t loopCount = 0; for (int* ob : d) {
sum += *ob; if (++loopCount == test.runAfterLoopCount) {
test.function(d);
}
}
EXPECT_EQ(test.expectedSum, sum)
<< "Range-for should iterate over values in test '" << test.description
<< "'";
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.