Skip to content

Commit

Permalink
Bundle GTest (#8)
Browse files Browse the repository at this point in the history
Bundle GTest - also fixes some sign warnings in comparisons for tests, and updates README.md to the most recent version
  • Loading branch information
SgtCoDFish authored Oct 29, 2017
1 parent 991dd55 commit 2847fa6
Show file tree
Hide file tree
Showing 210 changed files with 87,863 additions and 99 deletions.
9 changes: 0 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ matrix:
env: COMPILER=clang++-3.6 C_COMPILER=clang-3.6

before_install:
- sudo apt-get update -qq
- sudo apt-get install -y libgtest-dev
- sudo wget https://github.com/google/googletest/archive/release-1.7.0.tar.gz
- sudo tar xf release-1.7.0.tar.gz
- cd googletest-release-1.7.0
- sudo cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_C_COMPILER=$C_COMPILER -DCMAKE_CXX_COMPILER=$COMPILER .
- sudo make
- sudo cp -a include/gtest /usr/include
- sudo cp -a libgtest_main.so libgtest.so /usr/lib/
- cd "${TRAVIS_BUILD_DIR}"

script:
Expand Down
38 changes: 18 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ endif()

set(CMAKE_CXX_FLAGS_BASE "${CMAKE_CXX_FLAGS_BASE} ${dialect} ${warnings} ${flags}")

include_directories(include test)
include_directories("include")

file(GLOB_RECURSE ASHLEY_CPP_SOURCES ${PROJECT_SOURCE_DIR}/src/*.cpp)
file(GLOB ASHLEY_CPP_HEADERS ${PROJECT_SOURCE_DIR}/include/*.hpp)
Expand All @@ -67,26 +67,24 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_BASE} ${ASHLEY_RELEASE_FLAGS}")
add_library(${ASHLEY_LIB_NAME} ${ASHLEY_CPP_SOURCES})

if (NOT EXCLUDE_TESTS)
find_package(GTest)

if( NOT GTEST_FOUND )
message ("Skipping tests; GTest was not found")
if ( NOT MSVC )
set(ASHLEY_TEST_FLAGS "-pthread")
else ()
file (GLOB_RECURSE ASHLEY_TEST_SOURCES ${PROJECT_SOURCE_DIR}/test/*.cpp)
if ( NOT MSVC )
set(ASHLEY_TEST_FLAGS "-pthread")
else ()
set(ASHLEY_TEST_FLAGS "")
endif ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ASHLEY_TEST_FLAGS}")
include_directories(${GTEST_INCLUDE_DIRS})
add_executable(AshleyCPPTest ${ASHLEY_TEST_SOURCES})
target_link_libraries(AshleyCPPTest ${ASHLEY_LIB_NAME} ${GTEST_BOTH_LIBRARIES})
endif()
set(ASHLEY_TEST_FLAGS "")
endif ()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ASHLEY_TEST_FLAGS}")

add_subdirectory("test")

include_directories("test" "test/googletest/include")
file (GLOB_RECURSE ASHLEY_TEST_SOURCES
${PROJECT_SOURCE_DIR}/test/core/*.cpp
${PROJECT_SOURCE_DIR}/test/signals/*.cpp
${PROJECT_SOURCE_DIR}/test/systems/*.cpp)

add_executable(AshleyCPPTest ${ASHLEY_TEST_SOURCES})
target_link_libraries(AshleyCPPTest ${ASHLEY_LIB_NAME} gtest_main gtest)
endif (NOT EXCLUDE_TESTS)

install (TARGETS ${ASHLEY_LIB_NAME} DESTINATION lib)
Expand Down
42 changes: 20 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,45 @@ AshleyCPP is a lightweight, cross-platform and easy to use Entity framework for
get started making a game using a pure, ECS-oriented style.

It prioritises speed and simplicity and mirrors the public API of the original where possible to make it easy to learn,
quick to run and simple to debug, doubly so for users of the Java original.
quick to run and simple to debug - and doubly so for users of the Java original.

The original is inspired by the [Ash](http://www.ashframework.org/) and
[Artemis](http://gamadu.com/artemis/) frameworks.
The original is inspired by the [Ash](http://www.ashframework.org/) and [Artemis](http://gamadu.com/artemis/) frameworks.

AshleyCPP is a personal project and while there are no guarantees that it will compile and work, the master branch
on GitHub should compile and function properly and should be usable for making projects right now.
on GitHub should compile and function properly and should be usable for making projects right now. There is currently no
guarantee of API stability between versions, or indeed any meaningful versioning.

### Building

AshleyCPP uses the CMake build system and comes with three build targets; the debug library, "AshleyCPP-d", the release library "AshleyCPP" and the imaginatively named executable "AshleyCPPTest". The test program has a hard dependency on Google Test.
AshleyCPP uses the CMake build system and comes with three build targets; the debug library, "AshleyCPP-d", the release library "AshleyCPP" and the (imaginatively named) executable "AshleyCPPTest". The test program uses GTest, which is bundled.

To build the library from source, install a relatively recent version of CMake, navigate to a copy of the source and
do the following:

mkdir build
cd build
cmake ..
cmake -DCMAKE_BUILD_TYPE=<CHOOSE ONE: Debug/Release> ..

and proceed as normal for your environment. If you don't care about the tests, you can run `cmake -DEXCLUDE_TESTS=TRUE ..` and the dependency on GTest will be removed, just giving you the libraries.
and proceed as normal for your environment. If you don't care about the tests, you can run `cmake -DEXCLUDE_TESTS=TRUE ..` leaving you with just the library.

### Usage Notes and API Changes
While AshleyCPP strives to match the exported public API of the Java original, differences in the languages mean that
some differences exist. Such changes are listed in detail in APICHANGES.md, but a quickstart is given below.

Note: In situations where you'd use `.class` to get the type of a Java class, in C++ you can `#include <typeinfo>` and:
First in foremost: In situations where in Java you'd use `.class` to get a Java class, in C++ you can `#include <typeinfo>` and:

typeid(ComponentClass); // Equivalent to ComponentClass.class in Java.

// e.g. get a Family* that matches Entity having both ComponentA and ComponentB
ashley::Family::getFor({typeid(ComponentA), typeid(ComponentB)});
- General
- The whole library is enclosed in the `ashley` namespace. Use `using namespace ashley;` to save typing if you want.
- You can `#include` individual headers (organised similarly to the Java packages) or `#include "Ashley/AshleyCore.hpp"`
- Java generic functions that take vararg lists of `Class<? extends Component>` are replaced by
`std::initializer_list<std::type_index>`. Some functions also provide template overloads which may be easier and
faster to use; check the documentation for specific occurrences.
- The library enforces ownership using std::unique_ptr; the engine owns all entities and systems that are passed into
it and unless they're removed, all entities and systems die with the engine. Likewise, each Entity strictly owns
its attached components. This minimises the risk of leaking memory.
- The whole library is enclosed in the `ashley` namespace. Use `using namespace ashley;` to save typing if you want.
- You can `#include` individual headers (organised similarly to the Java packages) or `#include "Ashley/Ashley.hpp"`
- Java generic functions that take vararg lists of `Class<? extends Component>` are replaced by
`std::initializer_list<std::type_index>`. Some functions also provide template overloads which may be easier and
faster to use; check the documentation for specific occurrences.
- The library enforces ownership using `std::unique_ptr`; the engine owns all entities and systems that are passed into
it and unless they're removed, all entities and systems die with the engine. Likewise, each Entity strictly owns
its attached components. This minimises the risk of leaking memory.

For more specific changes, see the [APICHANGES.md](https://github.com/SgtCoDFish/AshleyCPP/blob/master/APICHANGES.md) file.

Expand All @@ -70,18 +68,18 @@ Ticked classes have both their implementation and tests complete.
- [x] IteratingSystem
- [x] IntervalSystem
- [x] IntervalIteratingSystem
- [ ] SortedIteratingSystem
- [x] SortedIteratingSystem
- Util
- [x] ObjectPool
- [x] Poolable

*NB:*
- Bag and ImmutableArray will not be implemented, there are better native C++ choices without me reinventing the wheel.
- `Bag` and `ImmutableArray` will probably not be implemented, there are better native C++ choices without reinventing the wheel.
- Currently implements as far as Ashley commit [#a276fe45](https://github.com/libgdx/ashley/commit/a276fe45c81d450f305ce1b5b0bd0fe837207a70).

Features to be implemented:
Nice to have features:

- Refactor entity ids, using a C++11 constexpr compile time hash and a templated Component class to prevent using a virtual method and vtable. Will lead to smaller components and better cache coherency.
- Refactor/rewrite handling of entity ids, using a C++11 constexpr compile time hash and a templated Component class to prevent using a virtual method and vtable. Will lead to smaller components and better cache coherency.

### License

Expand Down
2 changes: 1 addition & 1 deletion src/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ std::unique_ptr<ashley::Component> ashley::Entity::removeImpl(std::type_index ty

std::unique_ptr<ashley::Component> ashley::Entity::removeInternal(std::type_index typeIndex) {
const auto id = ashley::ComponentType::getIndexFor(typeIndex);
assert(id >= 0 && id < componentBits.size() && "invalid component index; you might have too many component types");
assert(id < componentBits.size() && "invalid component index; you might have too many component types");

std::unique_ptr<ashley::Component> ret { nullptr };

Expand Down
9 changes: 9 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 2.6.2)

project( googletest-distribution )

enable_testing()

option(BUILD_GTEST "Builds the googletest subproject" ON)

add_subdirectory( googletest )
2 changes: 1 addition & 1 deletion test/core/ComponentTypeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

// Ensures that we don't waste any memory by having a vtable on Components; that is, tests that a Component has a size of 1 (the minimum)
TEST(ComponentTypeTest, MinSizeComponents) {
ASSERT_EQ(sizeof(ashley::Component), 1);
ASSERT_EQ(sizeof(ashley::Component), 1u);
}

// Ensures a valid type is returned when a call is made to the various getFor() functions, and that this type is the same in the various functions.
Expand Down
48 changes: 24 additions & 24 deletions test/core/EngineTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ class EntityListenerMock : public EntityListener {

class EntitySystemMock : public EntitySystem {
public:
uint64_t updateCalls = { 0 };
uint64_t addedCalls = { 0 };
uint64_t removedCalls = { 0 };
int64_t updateCalls = { 0 };
int64_t addedCalls = { 0 };
int64_t removedCalls = { 0 };

EntitySystemMock() :
EntitySystemMock(nullptr) {
Expand Down Expand Up @@ -142,22 +142,22 @@ class EngineTest : public ::testing::Test {
TEST_F(EngineTest, AddAndRemoveEntities) {
engine.addEntity();

ASSERT_EQ(1, listenerA.addedCount);
ASSERT_EQ(1, listenerB.addedCount);
ASSERT_EQ(1u, listenerA.addedCount);
ASSERT_EQ(1u, listenerB.addedCount);

engine.removeEntityListener(&listenerB);

engine.addEntity();

ASSERT_EQ(2, listenerA.addedCount);
ASSERT_EQ(1, listenerB.addedCount);
ASSERT_EQ(2u, listenerA.addedCount);
ASSERT_EQ(1u, listenerB.addedCount);

engine.addEntityListener(&listenerB);

engine.removeAllEntities(); // calls removeEntity(shared_ptr) on each added entity

ASSERT_EQ(2, listenerA.removedCount);
ASSERT_EQ(2, listenerB.removedCount);
ASSERT_EQ(2u, listenerA.removedCount);
ASSERT_EQ(2u, listenerB.removedCount);
}

// Test the addSystem(EntitySystem*) getSystem() and removeSystem(typeID) methods
Expand All @@ -173,8 +173,8 @@ TEST_F(EngineTest, AddGetAndRemoveSystem) {

ASSERT_FALSE(engine.getSystem(typeid(EntitySystemMockA)) == nullptr);
ASSERT_FALSE(engine.getSystem(typeid(EntitySystemMockB)) == nullptr);
ASSERT_EQ(1, sA->addedCalls);
ASSERT_EQ(1, sB->addedCalls);
ASSERT_EQ(1u, sA->addedCalls);
ASSERT_EQ(1u, sB->addedCalls);

engine.removeSystem(typeid(EntitySystemMockA));
engine.removeSystem(typeid(EntitySystemMockB));
Expand Down Expand Up @@ -275,7 +275,7 @@ TEST_F(EngineTest, EntitiesForFamily) {
auto family = Family::getFor( { typeid(ComponentA), typeid(ComponentB) });
auto familyEntities = engine.getEntitiesFor(family);

ASSERT_EQ(0, familyEntities->size());
ASSERT_EQ(0u, familyEntities->size());

auto e1 = engine.addEntity();
auto e2 = engine.addEntity();
Expand All @@ -287,7 +287,7 @@ TEST_F(EngineTest, EntitiesForFamily) {
e3->add<ComponentA>().add<ComponentB>().add<ComponentC>();
e4->add<ComponentA>().add<ComponentB>().add<ComponentC>();

ASSERT_EQ(3, familyEntities->size());
ASSERT_EQ(3u, familyEntities->size());

bool e1Found = false, e2Found = false, e3Found = false, e4Found = false;

Expand All @@ -308,14 +308,14 @@ TEST_F(EngineTest, EntityForFamilyWithRemoval) {
e->add<ComponentA>();
auto entities = engine.getEntitiesFor(Family::getFor( { typeid(ComponentA) }));

ASSERT_EQ(entities->size(), 1);
ASSERT_EQ(1u, entities->size());
ASSERT_TRUE(
std::find_if(entities->begin(), entities->end(), [&](Entity *&found) {return found == e;})
!= entities->end());

engine.removeEntity(e);

ASSERT_EQ(0, entities->size());
ASSERT_EQ(0u, entities->size());
ASSERT_FALSE(
std::find_if(entities->begin(), entities->end(), [&](Entity *&found) {return found == e;})
!= entities->end());
Expand All @@ -325,7 +325,7 @@ TEST_F(EngineTest, EntitiesForFamilyWithRemoval) {
auto family = Family::getFor( { typeid(ComponentA), typeid(ComponentB) });
auto familyEntities = engine.getEntitiesFor(family);

ASSERT_EQ(0, familyEntities->size());
ASSERT_EQ(0u, familyEntities->size());

auto e1u = std::unique_ptr<Entity>(new Entity());
auto e2u = std::unique_ptr<Entity>(new Entity());
Expand All @@ -342,7 +342,7 @@ TEST_F(EngineTest, EntitiesForFamilyWithRemoval) {
auto e3 = engine.addEntity(std::move(e3u));
auto e4 = engine.addEntity(std::move(e4u));

ASSERT_EQ(3, familyEntities->size());
ASSERT_EQ(3u, familyEntities->size());

bool e1Found = false, e2Found = false, e3Found = false, e4Found = false;

Expand Down Expand Up @@ -372,7 +372,7 @@ TEST_F(EngineTest, EntitiesForFamilyWithRemoval) {
EXPECT_EQ(e2Found, false);
EXPECT_EQ(e3Found, false);
EXPECT_EQ(e4Found, true);
ASSERT_EQ(1, familyEntities->size());
ASSERT_EQ(1u, familyEntities->size());
}

TEST_F(EngineTest, EntitiesForFamilyWithRemovalAndFiltering) {
Expand All @@ -393,24 +393,24 @@ TEST_F(EngineTest, EntitiesForFamilyWithRemovalAndFiltering) {
e2->add<ComponentA>();
e2->add<ComponentB>();

ASSERT_EQ(1, entsWithAOnly->size());
ASSERT_EQ(1, entsWithB->size());
ASSERT_EQ(1u, entsWithAOnly->size());
ASSERT_EQ(1u, entsWithB->size());

e2->remove<ComponentB>();

ASSERT_EQ(2, entsWithAOnly->size());
ASSERT_EQ(0, entsWithB->size());
ASSERT_EQ(2u, entsWithAOnly->size());
ASSERT_EQ(0u, entsWithB->size());
}

TEST_F(EngineTest, GetSystems) {
auto sys = engine.getSystems();

ASSERT_EQ(sys.size(), 0);
ASSERT_EQ(sys.size(), 0u);

engine.addSystem(std::unique_ptr<EntitySystemMockA>(new EntitySystemMockA()));
engine.addSystem(std::unique_ptr<EntitySystemMockB>(new EntitySystemMockB()));

sys = engine.getSystems();

ASSERT_EQ(sys.size(), 2);
ASSERT_EQ(sys.size(), 2u);
}
20 changes: 10 additions & 10 deletions test/core/EntityTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,26 +248,26 @@ TEST_F(EntityTest, ComponentListener) {
e.componentAdded.add(dynAdd);
e.componentRemoved.add(dynRem);

ASSERT_EQ(0, dynAdd->counter);
ASSERT_EQ(0, dynRem->counter);
ASSERT_EQ(0u, dynAdd->counter);
ASSERT_EQ(0u, dynRem->counter);

e.add<ashley::test::PositionComponent>(5, 2);

ASSERT_EQ(1, dynAdd->counter);
ASSERT_EQ(0, dynRem->counter);
ASSERT_EQ(1u, dynAdd->counter);
ASSERT_EQ(0u, dynRem->counter);

e.remove<ashley::test::PositionComponent>();

ASSERT_EQ(1, dynAdd->counter);
ASSERT_EQ(1, dynRem->counter);
ASSERT_EQ(1u, dynAdd->counter);
ASSERT_EQ(1u, dynRem->counter);

e.add<ashley::test::VelocityComponent>(5, 222);

ASSERT_EQ(2, dynAdd->counter);
ASSERT_EQ(1, dynRem->counter);
ASSERT_EQ(2u, dynAdd->counter);
ASSERT_EQ(1u, dynRem->counter);

e.remove<ashley::test::VelocityComponent>();

ASSERT_EQ(2, dynAdd->counter);
ASSERT_EQ(2, dynRem->counter);
ASSERT_EQ(2u, dynAdd->counter);
ASSERT_EQ(2u, dynRem->counter);
}
2 changes: 2 additions & 0 deletions test/googletest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# python
*.pyc
Loading

0 comments on commit 2847fa6

Please sign in to comment.