Skip to content

Commit

Permalink
Added a variant unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
kgorking committed Oct 4, 2023
1 parent 7e3df74 commit cf19054
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ make_test (test_interval "interval.cpp")
make_test (test_hierarchy "hierarchy.cpp")
make_test (test_tagged_pointer "tagged_pointer.cpp")
make_test (test_type_list "type_list_tests.cpp")
make_test (test_variant "variant.cpp")
69 changes: 69 additions & 0 deletions unittest/variant.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <ecs/ecs.h>
#define CATCH_CONFIG_MAIN
#include "catch.hpp"

// Create a variant tree
// A
// / \
// B C
// |
// D
//
struct A {};
struct B { using variant_of = A; };
struct C { using variant_of = A; };
struct D { using variant_of = B; };

TEST_CASE("Variant components", "[component][variant]") {
ecs::runtime rt;

// First, add 'A'
rt.add_component(0, A{});
rt.commit_changes();
REQUIRE(rt.get_component_count<A>() == 1);
REQUIRE(rt.get_component_count<B>() == 0);
REQUIRE(rt.get_component_count<C>() == 0);
REQUIRE(rt.get_component_count<D>() == 0);

// Add 'B' and 'C'. Both are variants of 'A', so 'A' will be removed
rt.add_component(0, B{}, C{});
rt.commit_changes();
REQUIRE(rt.get_component_count<A>() == 0);
REQUIRE(rt.get_component_count<B>() == 1);
REQUIRE(rt.get_component_count<C>() == 1);
REQUIRE(rt.get_component_count<D>() == 0);

// Add 'D', which is only a variant of 'B' and 'A', so 'B' will be removed.
// 'C' remains untouched
rt.add_component(0, D{});
rt.commit_changes();
REQUIRE(rt.get_component_count<A>() == 0);
REQUIRE(rt.get_component_count<B>() == 0);
REQUIRE(rt.get_component_count<C>() == 1);
REQUIRE(rt.get_component_count<D>() == 1);

// Add 'A', which a parent variant to all other components,
// so they will all be removed
rt.add_component(0, A{});
rt.commit_changes();
REQUIRE(rt.get_component_count<A>() == 1);
REQUIRE(rt.get_component_count<B>() == 0);
REQUIRE(rt.get_component_count<C>() == 0);
REQUIRE(rt.get_component_count<D>() == 0);

// Add 'D', which is a variant of 'A'.
rt.add_component(0, D{});
rt.commit_changes();
REQUIRE(rt.get_component_count<A>() == 0);
REQUIRE(rt.get_component_count<B>() == 0);
REQUIRE(rt.get_component_count<C>() == 0);
REQUIRE(rt.get_component_count<D>() == 1);

// Add 'A' again
rt.add_component(0, A{});
rt.commit_changes();
REQUIRE(rt.get_component_count<A>() == 1);
REQUIRE(rt.get_component_count<B>() == 0);
REQUIRE(rt.get_component_count<C>() == 0);
REQUIRE(rt.get_component_count<D>() == 0);
}

0 comments on commit cf19054

Please sign in to comment.