Skip to content

Commit

Permalink
Fixed test accidentally checking for destruction of temporaries inste…
Browse files Browse the repository at this point in the history
…ad of destruction of queue contents (discovered thanks to issue cameron314#101)
  • Loading branch information
cameron314 committed Aug 18, 2020
1 parent dc62468 commit 11da28e
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions tests/unittests/unittests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,18 @@ struct Foo
{
Foo() : copied(false) { id = _id()++; }
Foo(Foo const& other) : id(other.id), copied(true) { }
~Foo()
Foo(Foo&& other) : id(other.id), copied(other.copied) { other.copied = true; }
Foo& operator=(Foo&& other)
{
verify();
id = other.id, copied = other.copied;
other.copied = true;
return *this;
}
~Foo() { verify(); }

private:
void verify()
{
if (copied) return;
if (id != _last_destroyed_id() + 1) {
Expand All @@ -28,6 +39,8 @@ struct Foo
_last_destroyed_id() = id;
++_destroy_count();
}

public:
static void reset() { _destroy_count() = 0; _id() = 0; _destroyed_in_order() = true; _last_destroyed_id() = -1; }
static int destroy_count() { return _destroy_count(); }
static bool destroyed_in_order() { return _destroyed_in_order(); }
Expand Down Expand Up @@ -166,15 +179,14 @@ class ReaderWriterQueueTests : public TestClass<ReaderWriterQueueTests>

bool nonempty_destroy()
{
Foo item;

// Some elements at beginning
Foo::reset();
{
ReaderWriterQueue<Foo> q(31);
for (int i = 0; i != 10; ++i) {
q.enqueue(Foo());
}
ASSERT_OR_FAIL(Foo::destroy_count() == 0);
}
ASSERT_OR_FAIL(Foo::destroy_count() == 10);
ASSERT_OR_FAIL(Foo::destroyed_in_order());
Expand All @@ -186,6 +198,7 @@ class ReaderWriterQueueTests : public TestClass<ReaderWriterQueueTests>
for (int i = 0; i != 31; ++i) {
q.enqueue(Foo());
}
ASSERT_OR_FAIL(Foo::destroy_count() == 0);
}
ASSERT_OR_FAIL(Foo::destroy_count() == 31);
ASSERT_OR_FAIL(Foo::destroyed_in_order());
Expand All @@ -197,6 +210,7 @@ class ReaderWriterQueueTests : public TestClass<ReaderWriterQueueTests>
for (int i = 0; i != 94; ++i) {
q.enqueue(Foo());
}
ASSERT_OR_FAIL(Foo::destroy_count() == 0);
}
ASSERT_OR_FAIL(Foo::destroy_count() == 94);
ASSERT_OR_FAIL(Foo::destroyed_in_order());
Expand All @@ -205,20 +219,24 @@ class ReaderWriterQueueTests : public TestClass<ReaderWriterQueueTests>
Foo::reset();
{
ReaderWriterQueue<Foo> q(31);
Foo item;
for (int i = 0; i != 42; ++i) {
q.enqueue(Foo());
}
ASSERT_OR_FAIL(Foo::destroy_count() == 0);
for (int i = 0; i != 31; ++i) {
ASSERT_OR_FAIL(q.try_dequeue(item));
}
ASSERT_OR_FAIL(Foo::destroy_count() == 31);
}
ASSERT_OR_FAIL(Foo::destroy_count() == 42);
ASSERT_OR_FAIL(Foo::destroy_count() == 43);
ASSERT_OR_FAIL(Foo::destroyed_in_order());

// Some elements in multiple blocks
Foo::reset();
{
ReaderWriterQueue<Foo> q(31);
Foo item;
for (int i = 0; i != 123; ++i) {
q.enqueue(Foo());
}
Expand All @@ -241,7 +259,7 @@ class ReaderWriterQueueTests : public TestClass<ReaderWriterQueueTests>
q.enqueue(Foo());
}
}
ASSERT_OR_FAIL(Foo::destroy_count() == 500);
ASSERT_OR_FAIL(Foo::destroy_count() == 501);
ASSERT_OR_FAIL(Foo::destroyed_in_order());

return true;
Expand Down

0 comments on commit 11da28e

Please sign in to comment.