Skip to content

Commit

Permalink
Merge pull request #555 from leapmotion/ref-testfilters
Browse files Browse the repository at this point in the history
Minor test refactors
  • Loading branch information
gtremper committed Jun 11, 2015
2 parents 6c59401 + 123dfa4 commit d16ee2d
Showing 1 changed file with 32 additions and 43 deletions.
75 changes: 32 additions & 43 deletions src/autowiring/test/AutoFilterCollapseRulesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,20 @@ class AutoFilterCollapseRulesTest:
}
};

class AcceptsConstReference {
public:
AcceptsConstReference(void) :
m_called(0)
{}

int m_called;

void AutoFilter(const int& dataIn) {
++m_called;
}
};

class AcceptsSharedPointer {
public:
AcceptsSharedPointer(void) :
m_called(0)
{}

int m_called;

void AutoFilter(std::shared_ptr<const int> dataIn) {
++m_called;
}
};

TEST_F(AutoFilterCollapseRulesTest, SharedPtrCollapse) {
AutoRequired<AutoPacketFactory> factory;
AutoRequired<AcceptsConstReference> constr_filter;
AutoRequired<AcceptsSharedPointer> shared_filter;

int contRefCallCount = 0;
*factory += [&](const int& dataIn) {
contRefCallCount++;
};

int sharedPtrRefCallCount = 0;
std::shared_ptr<const int> dataInVal;
*factory += [&](std::shared_ptr<const int> dataIn) {
sharedPtrRefCallCount++;
dataInVal = dataIn;
};

int constr_int = 222;
std::shared_ptr<int> shared_int = std::make_shared<int>(232);
Expand All @@ -52,24 +36,24 @@ TEST_F(AutoFilterCollapseRulesTest, SharedPtrCollapse) {
{
auto packet = factory->NewPacket();
packet->Decorate(constr_int);
ASSERT_EQ(1, constr_filter->m_called) << "Called const reference method " << constr_filter->m_called << " times";
ASSERT_EQ(1, shared_filter->m_called) << "Called shared pointer method " << shared_filter->m_called << " times";
ASSERT_EQ(1, contRefCallCount) << "Called const reference method " << contRefCallCount << " times";
ASSERT_EQ(1, sharedPtrRefCallCount) << "Called shared pointer method " << sharedPtrRefCallCount << " times";
}
constr_filter->m_called = 0;
shared_filter->m_called = 0;
contRefCallCount = 0;
sharedPtrRefCallCount = 0;

// Decorate(shared_ptr<type> X) calls AutoFilter(const type& X)
// Decorate(shared_ptr<type> X) calls AutoFilter(shared_ptr<type> X)
// NOTE: Decorate(shared_ptr<T> X) shares ownership of X instead of copying.
{
auto packet = factory->NewPacket();
packet->Decorate(shared_int);
ASSERT_EQ(1, constr_filter->m_called) << "Called const reference method " << constr_filter->m_called << " times";
ASSERT_EQ(1, shared_filter->m_called) << "Called shared pointer method " << shared_filter->m_called << " times";
ASSERT_EQ(1, contRefCallCount) << "Called const reference method " << contRefCallCount << " times";
ASSERT_EQ(1, sharedPtrRefCallCount) << "Called shared pointer method " << sharedPtrRefCallCount << " times";
ASSERT_FALSE(shared_int.unique()) << "Argument of Decorate should be shared, not copied, when possible";
}
constr_filter->m_called = 0;
shared_filter->m_called = 0;
contRefCallCount = 0;
sharedPtrRefCallCount = 0;

// DecorateImmediate(type X) calls AutoFilter(const type& X)
// DecorateImmediate(type X) DOES NOT CALL AutoFilter(shared_ptr<type> X)
Expand All @@ -82,32 +66,37 @@ TEST_F(AutoFilterCollapseRulesTest, SharedPtrCollapse) {
{
auto packet = factory->NewPacket();
packet->DecorateImmediate(constr_int);
ASSERT_EQ(1, constr_filter->m_called) << "Called const reference method " << constr_filter->m_called << " times";
ASSERT_EQ(1, shared_filter->m_called) << "Called shared pointer method " << shared_filter->m_called << " times";
ASSERT_EQ(1, contRefCallCount) << "Called const reference method " << contRefCallCount << " times";
ASSERT_EQ(1, sharedPtrRefCallCount) << "Shared pointer method not called as expected";
ASSERT_EQ(nullptr, dataInVal) << "A non-null shared pointer was received by an AutoFilter method during a DecorateImmediate call";
}
constr_filter->m_called = 0;
shared_filter->m_called = 0;
contRefCallCount = 0;
sharedPtrRefCallCount = 0;
}

TEST_F(AutoFilterCollapseRulesTest, ConstCollapse) {
AutoRequired<AutoPacketFactory> factory;
AutoRequired<AcceptsConstReference> filter;

int callCount = 0;
*factory += [&](const int& dataIn) {
callCount++;
};

// Test const shared_ptr
auto packet1 = factory->NewPacket();

std::shared_ptr<const int> dec1 = std::make_shared<const int>(42);
packet1->Decorate(dec1);

ASSERT_EQ(1, filter->m_called) << "'const T' decoration didn't resolve to 'T'";
ASSERT_EQ(1, callCount) << "'const T' decoration didn't resolve to 'T'";

// Test const value
auto packet2 = factory->NewPacket();

const int dec2 = 42;
packet2->Decorate(dec2);

ASSERT_EQ(2, filter->m_called) << "'const T' decoration didn't resolve to 'T'";
ASSERT_EQ(2, callCount) << "'const T' decoration didn't resolve to 'T'";
}

TEST_F(AutoFilterCollapseRulesTest, SharedPointerAliasingRules) {
Expand Down

0 comments on commit d16ee2d

Please sign in to comment.