Skip to content

Commit

Permalink
Merge pull request #25 from leapmotion/ref-testutilities
Browse files Browse the repository at this point in the history
Extracting a utilities project for testing
  • Loading branch information
codemercenary committed Aug 2, 2014
2 parents 18cffbc + f960703 commit 4ac4f37
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 63 deletions.
16 changes: 16 additions & 0 deletions CMakeLists.txt
Expand Up @@ -23,6 +23,7 @@ elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "-std=c++11")
endif()

set_property(GLOBAL PROPERTY USE_FOLDERS ON)
include(CMakeModules/pch.cmake)

# We don't build tests unless we're being built by ourselves in our own source tree
Expand Down Expand Up @@ -52,6 +53,21 @@ function(add_googletest dirname)
endif()
endfunction()

function(rewrite_header_paths target_SRC_VAR)
# All include files are located in /autowiring from here, so prepend that to all sources
set(SRCS_TEMP ${${target_SRC_VAR}})
set(SRCS)
foreach(i ${SRCS_TEMP})
string(REGEX MATCH ".*\\.h" hfile ${i})
if(hfile)
list(APPEND SRCS ${PROJECT_SOURCE_DIR}/autowiring/${i})
else()
list(APPEND SRCS ${i})
endif()
endforeach()
set(${target_SRC_VAR} ${SRCS} PARENT_SCOPE)
endfunction()

# We have unit test projects via googletest, they're added in the places where they are defined
enable_testing()

Expand Down
@@ -1,17 +1,19 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once
#include <autowiring/autowiring.h>
#include <gtest/gtest.h>
#include MEMORY_HEADER

/// <summary>
/// General purpose exception filter, used for tracking exceptions thrown from unit test CoreThreads
/// </summary>
class EnclosedContextExceptionFilter:
class AutowiringEnclosureExceptionFilter:
public ContextMember,
public ExceptionFilter
{
public:
EnclosedContextExceptionFilter(void) :
ContextMember("EnclosedContextExceptionFilter"),
AutowiringEnclosureExceptionFilter(void) :
ContextMember("AutowiringEnclosureExceptionFilter"),
m_excepted(false)
{}

Expand Down Expand Up @@ -42,7 +44,7 @@ class EnclosedContextExceptionFilter:
/// tests complete in a timely fashion. If a longer stop duration is desired, you must provide a
/// destructor which performs teardown in a different way.
/// </remarks>
class EnclosedContextTestBase:
class AutowiringEnclosure:
public testing::EmptyTestEventListener
{
private:
Expand Down
6 changes: 6 additions & 0 deletions autowiring/gtest-all-guard.h
@@ -0,0 +1,6 @@
#pragma once

/// <summary>
/// Delegating main routine, installs an enclosure filter and begins running the tests
/// </summary>
int autotesting_main(int argc, const char* argv []);
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -18,3 +18,4 @@ endif()

add_subdirectory(autonet)
add_subdirectory(autowiring)
add_subdirectory(autotesting)
@@ -1,5 +1,5 @@
#include "stdafx.h"
#include "EnclosedContextTestBase.hpp"
#include "AutowiringEnclosure.h"

struct TestInfoProxy {
TestInfoProxy(const testing::TestInfo& info) :
Expand All @@ -9,25 +9,25 @@ struct TestInfoProxy {
const testing::TestInfo& m_info;
};

void EnclosedContextTestBase::OnTestStart(const testing::TestInfo& info) {
AutoRequired<EnclosedContextExceptionFilter> filter;
void AutowiringEnclosure::OnTestStart(const testing::TestInfo& info) {
AutoRequired<AutowiringEnclosureExceptionFilter> filter;

// The context proper. This is automatically assigned as the current
// context when SetUp is invoked.
AutoCreateContext create;
create->Construct<TestInfoProxy>(info);

// Add exception filter in this context:
create->Inject<EnclosedContextExceptionFilter>();
create->Inject<AutowiringEnclosureExceptionFilter>();

// Now make it current and let the test run:
create->SetCurrent();
}

void EnclosedContextTestBase::OnTestEnd(const testing::TestInfo& info) {
void AutowiringEnclosure::OnTestEnd(const testing::TestInfo& info) {
// Verify we can grab the test case back out and that the pointer is correct:
Autowired<TestInfoProxy> ti;
Autowired<EnclosedContextExceptionFilter> ecef;
Autowired<AutowiringEnclosureExceptionFilter> ecef;
auto ctxt = ecef ? ecef->GetContext() : nullptr;

// Unconditionally reset the global context as the current context
Expand Down
11 changes: 11 additions & 0 deletions src/autotesting/CMakeLists.txt
@@ -0,0 +1,11 @@
set(AutoTesting_SOURCES
AutowiringEnclosure.h
AutowiringEnclosure.cpp
gtest-all-guard.h
gtest-all-guard.cpp
)

rewrite_header_paths(AutoTesting_SOURCES)
add_library(AutoTesting ${AutoTesting_SOURCES})
set_property(TARGET AutoTesting PROPERTY FOLDER "Autowiring")
target_link_libraries(AutoTesting Autowiring)
14 changes: 14 additions & 0 deletions src/autotesting/gtest-all-guard.cpp
@@ -0,0 +1,14 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include "AutowiringEnclosure.h"
#include <gtest/gtest-all.cc>

using namespace std;

int autotesting_main(int argc, const char* argv[])
{
auto& listeners = testing::UnitTest::GetInstance()->listeners();
listeners.Append(new AutowiringEnclosure);
testing::InitGoogleTest(&argc, (char**)argv);
return RUN_ALL_TESTS();
}
2 changes: 2 additions & 0 deletions src/autotesting/stdafx.cpp
@@ -0,0 +1,2 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
21 changes: 21 additions & 0 deletions src/autotesting/stdafx.h
@@ -0,0 +1,21 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once

// Only include these headers in cases where a pch can be generated
// Currently this is only supported on MSVC
#ifdef _MSC_VER
#include <thread>
#define NOMINMAX
#endif

#ifndef _MSC_VER
#include <stdlib.h>
#endif

// C++11 glue logic, for platforms that have incomplete C++11 support
#include "contrib/C++11/cpp11.h"


template<class T, int n>
const char(&ArraySize(const T(&vals)[n]))[n];
#define ARRAYCOUNT(x) sizeof(ArraySize(x))
14 changes: 2 additions & 12 deletions src/autowiring/CMakeLists.txt
@@ -1,4 +1,5 @@
add_googletest(test)
add_googletest(benchmark)

set(Autowiring_SRCS
at_exit.h
Expand Down Expand Up @@ -149,18 +150,7 @@ else()
set(Autowiring_SRCS ${Autowiring_Unix_SRCS} ${Autowiring_SRCS})
endif()

# All include files are located in /autowiring from here, so prepend that to all sources
set(Autowiring_SRCS_TEMP ${Autowiring_SRCS})
set(Autowiring_SRCS)
foreach(i ${Autowiring_SRCS_TEMP})
string(REGEX MATCH ".*\\.h" hfile ${i})
if(hfile)
list(APPEND Autowiring_SRCS ${PROJECT_SOURCE_DIR}/autowiring/${i})
else()
list(APPEND Autowiring_SRCS ${i})
endif()
endforeach()

rewrite_header_paths(Autowiring_SRCS)
ADD_MSVC_PRECOMPILED_HEADER("stdafx.h" "stdafx.cpp" Autowiring_SRCS)
add_library(Autowiring STATIC ${Autowiring_SRCS})
set_property(TARGET Autowiring PROPERTY FOLDER "Autowiring")
Expand Down
@@ -1,13 +1,19 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include "AutowiringBenchmarkTest.hpp"
#include "TestFixtures/SimpleObject.hpp"
#include "gtest-all-guard.h"

class Foo {};

int main(int argc, const char* argv[]) {
return autotesting_main(argc, argv);
}

TEST_F(AutowiringBenchmarkTest, VerifySimplePerformance) {
const size_t n = 10000;

// Insert the object:
AutoRequired<SimpleObject>();
AutoRequired<Foo>();

// Time n hash map hits, in order to get a baseline:
std::unordered_map<int, int> ref;
Expand All @@ -27,7 +33,7 @@ TEST_F(AutowiringBenchmarkTest, VerifySimplePerformance) {
{
auto start = std::chrono::steady_clock::now();
for(size_t i = n; i--;)
Autowired<SimpleObject>();
Autowired<Foo>();
benchmark = (std::chrono::steady_clock::now() - start) / n;
}

Expand Down
16 changes: 16 additions & 0 deletions src/autowiring/benchmark/CMakeLists.txt
@@ -0,0 +1,16 @@
if(NOT AUTOWIRING_BUILD_BENCHMARKS)
return()
endif()

set(AutowiringBenchmarkTest_SRCS
AutowiringBenchmarkTest.hpp
AutowiringBenchmarkTest.cpp
)

ADD_MSVC_PRECOMPILED_HEADER("stdafx.h" "stdafx.cpp" AutowiringBenchmarkTest_SRCS)
add_executable(AutowiringBenchmarkTest ${AutowiringBenchmarkTest_SRCS})
target_link_libraries(AutowiringBenchmarkTest Autowiring AutowiringFixture AutoTesting)
set_property(TARGET AutowiringBenchmarkTest PROPERTY FOLDER "Autowiring")

# This is a unit test, let CMake know this
add_test(NAME AutowiringBenchmarkTest COMMAND $<TARGET_FILE:AutowiringBenchmarkTest>)
2 changes: 2 additions & 0 deletions src/autowiring/benchmark/stdafx.cpp
@@ -0,0 +1,2 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
18 changes: 18 additions & 0 deletions src/autowiring/benchmark/stdafx.h
@@ -0,0 +1,18 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once

#include <C++11/cpp11.h>
#include <gtest/gtest.h>
#include <autowiring/autowiring.h>

#ifdef _MSC_VER
#include <thread>
#include <mutex>
#endif

// Very unusual syntax -- function taking an array of fixed size, and returning
// a character array of that same size
template<class T, int n>
const char(&ArraySize(const T(&vals)[n]))[n];

#define ARRAYCOUNT(x) sizeof(ArraySize(x))
5 changes: 5 additions & 0 deletions src/autowiring/test/AutowiringTest.cpp
@@ -1,11 +1,16 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#include "stdafx.h"
#include "AutowiringTest.hpp"
#include "gtest-all-guard.h"
#include "TestFixtures/SimpleObject.hpp"
#include "TestFixtures/SimpleReceiver.hpp"
#include <autowiring/Autowired.h>
#include <autowiring/CoreThread.h>

int main(int argc, const char* argv []) {
return autotesting_main(argc, argv);
}

TEST_F(AutowiringTest, VerifyAutowiredFast) {
// Add an object:
AutoCurrentContext()->Inject<SimpleObject>();
Expand Down
7 changes: 1 addition & 6 deletions src/autowiring/test/CMakeLists.txt
Expand Up @@ -9,8 +9,6 @@ set(AutowiringTest_SRCS
AutoInjectableTest.cpp
AutoPacketFactoryTest.hpp
AutoPacketFactoryTest.cpp
AutowiringBenchmarkTest.hpp
AutowiringBenchmarkTest.cpp
AutowiringTest.hpp
AutowiringTest.cpp
AutowiringUtilitiesTest.hpp
Expand Down Expand Up @@ -41,8 +39,6 @@ set(AutowiringTest_SRCS
DispatchQueueTest.cpp
DtorCorrectnessTest.hpp
DtorCorrectnessTest.cpp
EnclosedContextTestBase.hpp
EnclosedContextTestBase.cpp
ExceptionFilterTest.hpp
ExceptionFilterTest.cpp
EventReceiverTest.hpp
Expand Down Expand Up @@ -88,7 +84,6 @@ set(AutowiringTest_SRCS
TestFixtures/MultiInherit.hpp
UuidTest.hpp
UuidTest.cpp
gtest-all-guard.cpp
)

set(
Expand All @@ -105,7 +100,7 @@ set_property(TARGET AutowiringFixture PROPERTY FOLDER "Autowiring")

ADD_MSVC_PRECOMPILED_HEADER("stdafx.h" "stdafx.cpp" AutowiringTest_SRCS)
add_executable(AutowiringTest ${AutowiringTest_SRCS})
target_link_libraries(AutowiringTest Autowiring AutowiringFixture)
target_link_libraries(AutowiringTest Autowiring AutowiringFixture AutoTesting)
set_property(TARGET AutowiringTest PROPERTY FOLDER "Autowiring")

# This is a unit test, let CMake know this
Expand Down
1 change: 0 additions & 1 deletion src/autowiring/test/ContextEnumeratorTest.hpp
@@ -1,6 +1,5 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once
#include "EnclosedContextTestBase.hpp"

class ContextEnumeratorTest:
public testing::Test
Expand Down
31 changes: 0 additions & 31 deletions src/autowiring/test/gtest-all-guard.cpp

This file was deleted.

0 comments on commit 4ac4f37

Please sign in to comment.