Skip to content

Commit

Permalink
Added EventRegistryTest
Browse files Browse the repository at this point in the history
  • Loading branch information
gtremper committed Aug 1, 2014
1 parent c8d5b1b commit 9223ac8
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 55 deletions.
15 changes: 3 additions & 12 deletions autowiring/Autowired.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "Decompose.h"
#include "Deferred.h"
#include "GlobalCoreContext.h"
#include "TypeRegistry.h"
#include MEMORY_HEADER
#include ATOMIC_HEADER

Expand Down Expand Up @@ -121,7 +120,6 @@ class Autowired:
AutowirableSlot<T>(ctxt ? ctxt->template ResolveAnchor<T>() : ctxt),
m_pFirstChild(nullptr)
{
(void) RegType<T>::r;
if(ctxt)
ctxt->Autowire(*this);
}
Expand Down Expand Up @@ -253,9 +251,7 @@ class AutoRequired:
// !!!!! Read comment in Autowired if you get a compiler error here !!!!!
AutoRequired(const std::shared_ptr<CoreContext>& ctxt = CoreContext::CurrentContext()):
std::shared_ptr<T>(ctxt->template Inject<T>())
{
(void)RegType<T>::r;
}
{}

/// <summary>
/// Construct overload, for types which take constructor arguments
Expand Down Expand Up @@ -299,19 +295,14 @@ template<class T>
public:
AutoFired(const std::shared_ptr<CoreContext>& ctxt = CoreContext::CurrentContext()):
m_junctionBox(ctxt->GetJunctionBox<T>())
{
// Add an utterance of the TypeRegistry so we can add this AutoFired type to our collection
(void) RegType<T>::r;
}
{}

/// <summary>
/// Utility constructor, used when the receiver is already known
/// </summary>
AutoFired(const std::shared_ptr<JunctionBox<T>>& junctionBox) :
m_junctionBox(junctionBox)
{
(void) RegType<T>::r;
}
{}

/// <summary>
/// Utility constructor, used to support movement operations
Expand Down
21 changes: 8 additions & 13 deletions autowiring/CoreContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "EventInputStream.h"
#include "ExceptionFilter.h"
#include "TeardownNotifier.h"
#include "TypeRegistry.h"
#include "TypeUnifier.h"

#include <list>
Expand Down Expand Up @@ -115,8 +116,7 @@ class CoreContext:
struct MemoEntry {
MemoEntry(void) :
pFirst(nullptr)
{
}
{}

// The first deferrable autowiring which requires this type, if one exists:
DeferrableAutowiring* pFirst;
Expand Down Expand Up @@ -512,6 +512,9 @@ class CoreContext:
/// </summary>
template<typename T, typename... Args>
std::shared_ptr<T> Construct(Args&&... args) {
// Add this type to the TypeRegistry
(void) RegType<T>::r;

// If T doesn't inherit Object, then we need to compose a unifying type which does
typedef typename SelectTypeUnifier<T>::type TActual;
static_assert(std::is_base_of<Object, TActual>::value, "Constructive type does not implement Object as expected");
Expand Down Expand Up @@ -786,12 +789,8 @@ class CoreContext:
/// </remarks>
template<class T>
void Snoop(const std::shared_ptr<T>& pSnooper) {
// GRAHAM
//static_assert(std::is_base_of<EventReceiver, T>::value ||
// has_autofilter<T>::value,
// "Cannot snoop on a type which is not an EventReceiver or implements AutoFilter");

const AddInternalTraits traits(AutoFilterDescriptorSelect<T>(pSnooper), pSnooper);

// Add to collections of snoopers
InsertSnooper(pSnooper);

Expand All @@ -812,14 +811,10 @@ class CoreContext:
/// </remarks>
template<class T>
void Unsnoop(const std::shared_ptr<T>& pSnooper) {
//GRAHAM
//static_assert(std::is_base_of<EventReceiver, T>::value ||
// has_autofilter<T>::value,
// "Cannot snoop on a type which is not an EventReceiver or implements AutoFilter");

const AddInternalTraits traits(AutoFilterDescriptorSelect<T>(pSnooper), pSnooper);

RemoveSnooper(pSnooper);

auto oSnooper = std::static_pointer_cast<Object>(pSnooper);

// Cleanup if its an EventReceiver
Expand Down
2 changes: 1 addition & 1 deletion autowiring/JunctionBoxManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class JunctionBoxManager {
const std::type_index& pTypeIndex = typeid(T);

auto box = m_junctionBoxes.find(pTypeIndex);
assert(box != m_junctionBoxes.end() && "If junction box isn't found, EventRegistry isn't working");
assert(box != m_junctionBoxes.end() && "If JunctionBox isn't found, EventRegistry isn't working");

//Check here if any listening marshals might be interested in receiving the fired args
auto mapfinditerator = m_eventOutputStreams.find(pTypeIndex);
Expand Down
10 changes: 3 additions & 7 deletions autowiring/TypeRegistry.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once
#include "CreationRules.h"
#include "CoreContext.h"
#include <typeinfo>
#include STL_TUPLE_HEADER
#include MEMORY_HEADER
Expand Down Expand Up @@ -84,9 +83,6 @@ struct TypeRegistryEntryT:

virtual const std::type_info& GetTypeInfo(void) const override { return typeid(T); }

//GRAHAM
//bool IsEventReceiver(void) const override { return std::is_base_of<EventReceiver, T>::value; }

virtual std::shared_ptr<JunctionBoxBase> NewJunctionBox(void) const override {
return std::static_pointer_cast<JunctionBoxBase>(
std::make_shared<JunctionBox<T>>()
Expand All @@ -105,7 +101,7 @@ struct TypeRegistryEntryT:

template<typename U>
typename std::enable_if<is_injectable<U>::value>::type AnyInject(void) const {
CoreContext::CurrentContext()->Inject<U>();
//CoreContext::CurrentContext()->Inject<U>();
}

template<typename U>
Expand All @@ -116,8 +112,8 @@ struct TypeRegistryEntryT:
virtual void Inject(void) const override { AnyInject<T>(); }
};

extern const TypeRegistryEntry* g_pFirstEntry;
extern size_t g_entryCount;
extern const TypeRegistryEntry* g_pFirstTypeEntry;
extern size_t g_typeEntryCount;

/// <summary>
/// Adds the specified type to the universal type registry
Expand Down
1 change: 0 additions & 1 deletion src/autowiring/CoreContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "JunctionBox.h"
#include "MicroBolt.h"
#include "NewAutoFilter.h"
#include "TypeRegistry.h"
#include <algorithm>
#include <stack>
#include <iostream>
Expand Down
10 changes: 5 additions & 5 deletions src/autowiring/TypeRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#include "JunctionBox.h"

// Head of a linked list which will have node for every event type
const TypeRegistryEntry* g_pFirstEntry = nullptr;
size_t g_entryCount = 0;
const TypeRegistryEntry* g_pFirstTypeEntry = nullptr;
size_t g_typeEntryCount = 0;

TypeRegistryEntry::TypeRegistryEntry(const std::type_info& ti) :
pFlink(g_pFirstEntry),
pFlink(g_pFirstTypeEntry),
ti(ti)
{
g_entryCount++;
g_pFirstEntry = this;
g_typeEntryCount++;
g_pFirstTypeEntry = this;
}
2 changes: 2 additions & 0 deletions src/autowiring/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ set(AutowiringTest_SRCS
ExceptionFilterTest.cpp
EventReceiverTest.hpp
EventReceiverTest.cpp
EventRegistryTest.hpp
EventRegistryTest.cpp
FactoryTest.hpp
FactoryTest.cpp
GlobalInitTest.hpp
Expand Down
67 changes: 67 additions & 0 deletions src/autowiring/test/EventRegistryTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include "EventRegistryTest.hpp"
#include <autowiring/Autowired.h>
#include <autowiring/GlobalCoreContext.h>
#include <autowiring/JunctionBox.h>
#include <autowiring/TypeRegistry.h>

template<int>
class Registered{};

TEST_F(EventRegistryTest, VerifySimpleLocalRegistration) {
// Register two entries statically by uttering the static member's name:
(void)RegEvent<Registered<1>>::r;
(void)RegEvent<Registered<2>>::r;

// Verify that both types exist:
bool exists1 = false;
bool exists2 = false;

// Enumerate, skipping both bounds on this set. Note that the set will contain ALL utterances
// of RegType, which means we must enumerate everything, and have to expect a lot of negative
// matches.
for(auto p = g_pFirstEventEntry; p; p = p->pFlink) {
if(p->GetTypeInfo() == typeid(Registered<1>))
exists1 = true;
if(p->GetTypeInfo() == typeid(Registered<2>))
exists2 = true;

// Negative assertion:
ASSERT_NE(p->ti, typeid(Registered<3>)) << "Obtained a registration for a type which was not explicitly registered";
}

ASSERT_TRUE(exists1) << "Type one was registered but not found";
ASSERT_TRUE(exists2) << "Type two was registered but not found";
}

TEST_F(EventRegistryTest, VerifyExteriorModuleRegistration) {
// Verify that there are a bunch of registered types in the test application by looking
// at the length of the linked list. We expect that there will be at least 2, because of
// our declarations in the earlier test in this module, but we also expect there to be
// (to choose arbitrarily) around 8, because this is a number of autofired fields known
// to exist at one point.
//
// If the number of autowired members in this unit test drops below 10 at some point, it
// might be necessary to reduce the count from 8 to something lower.
size_t nTypes = 0;

for(auto p = g_pFirstEventEntry; p; p = p->pFlink)
nTypes++;

ASSERT_LT(8UL, nTypes) << "Registration failed to pick up the expected minimum number of types in this test";
ASSERT_EQ(g_eventEntryCount, nTypes) << "Linked list did not contain the same number of entries as reported in g_entryCount";
}

class OnlyUsedForRegistrationChecking {};

TEST_F(EventRegistryTest, AutoFiredShouldRegister) {
AutoFired<OnlyUsedForRegistrationChecking> af;

// Now that we've got an AutoFired declaration, check for membership:
for(auto p = g_pFirstEventEntry; p; p = p->pFlink)
if(typeid(OnlyUsedForRegistrationChecking) == p->ti)
return;

FAIL() << "Failed to find a type which should have been present in a type registry collection";
}
6 changes: 6 additions & 0 deletions src/autowiring/test/EventRegistryTest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once

class EventRegistryTest:
public testing::Test
{};
19 changes: 3 additions & 16 deletions src/autowiring/test/TypeRegistryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ TEST_F(TypeRegistryTest, VerifySimpleLocalRegistration) {
// Enumerate, skipping both bounds on this set. Note that the set will contain ALL utterances
// of RegType, which means we must enumerate everything, and have to expect a lot of negative
// matches.
for(auto p = g_pFirstEntry; p; p = p->pFlink) {
for(auto p = g_pFirstTypeEntry; p; p = p->pFlink) {
if(p->GetTypeInfo() == typeid(Registered<1>))
exists1 = true;
if(p->GetTypeInfo() == typeid(Registered<2>))
Expand All @@ -46,22 +46,9 @@ TEST_F(TypeRegistryTest, VerifyExteriorModuleRegistration) {
// might be necessary to reduce the count from 8 to something lower.
size_t nTypes = 0;

for(auto p = g_pFirstEntry; p; p = p->pFlink)
for(auto p = g_pFirstTypeEntry; p; p = p->pFlink)
nTypes++;

ASSERT_LT(8UL, nTypes) << "Registration failed to pick up the expected minimum number of types in this test";
ASSERT_EQ(g_entryCount, nTypes) << "Linked list did not contain the same number of entries as reported in g_entryCount";
}

class OnlyUsedForRegistrationChecking {};

TEST_F(TypeRegistryTest, AutoFiredShouldRegister) {
AutoFired<OnlyUsedForRegistrationChecking> af;

// Now that we've got an AutoFired declaration, check for membership:
for(auto p = g_pFirstEntry; p; p = p->pFlink)
if(typeid(OnlyUsedForRegistrationChecking) == p->ti)
return;

FAIL() << "Failed to find a type which should have been present in a type registry collection";
ASSERT_EQ(g_typeEntryCount, nTypes) << "Linked list did not contain the same number of entries as reported in g_entryCount";
}

0 comments on commit 9223ac8

Please sign in to comment.