Skip to content

Commit

Permalink
Refactor ID tests and improve coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadden committed Jul 11, 2018
1 parent 5b5bb2c commit 24b2726
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/alia/id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void
clone_into(id_interface*& storage, id_interface const* id);

// captured_id is used to capture an ID for long-term storage (beyond the point
// where the id_interface
// where the id_interface reference will be valid).
struct captured_id
{
captured_id()
Expand Down
125 changes: 76 additions & 49 deletions unit_tests/id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,87 @@

#include <catch.hpp>

using namespace alia;

// Test all the relevant ID operations on a pair of equal IDs.
static void
test_ids(alia::id_interface const& a, alia::id_interface const& b)
test_equal_ids(id_interface const& a, id_interface const& b)
{
REQUIRE(a == b);
REQUIRE(b == a);
REQUIRE(!(a < b));
REQUIRE(!(b < a));
REQUIRE(
boost::lexical_cast<std::string>(a)
== boost::lexical_cast<std::string>(b));
REQUIRE(a.hash() == b.hash());
}

TEST_CASE("IDs", "[id]")
// Test all the ID operations on a single ID.
template<class Id>
void
test_single_id(Id const& id)
{
using namespace alia;
test_equal_ids(id, id);

std::shared_ptr<id_interface> clone(id.clone());
test_equal_ids(id, *clone);

// Test simple IDs and the basic ID interface operators.
simple_id<int> a = make_id(1);
REQUIRE(boost::lexical_cast<std::string>(a) == "1");
REQUIRE(boost::lexical_cast<std::string>(ref(a)) == "1");
REQUIRE(a == a);
simple_id<int> c = make_id(1);
REQUIRE(ref(a) == ref(a));
REQUIRE(a == c);
REQUIRE(ref(a) == ref(c));
simple_id<int> b = make_id(2);
Id copy;
id.deep_copy(&copy);
test_equal_ids(id, copy);
}

// Test all the ID operations on a pair of different IDs.
template<class A, class B>
void
test_different_ids(A const& a, B const& b)
{
test_single_id(a);
test_single_id(b);
REQUIRE(a != b);
REQUIRE(ref(a) != ref(b));
REQUIRE(a < b);
REQUIRE(ref(a) < ref(b));
REQUIRE(!(b < a));
REQUIRE(!(ref(b) < ref(a)));
int x;
simple_id<int*> d = make_id(&x);
REQUIRE(a != d);
REQUIRE(ref(a) != ref(d));
REQUIRE((a < d && !(d < a) || d < a && !(a < d)));

// Test captured_id.
captured_id o;
o.capture(a);
REQUIRE(o.get() == a);
REQUIRE(o.get() != b);
captured_id p;
REQUIRE(o != p);
p.capture(a);
REQUIRE(o == p);
p.capture(c);
REQUIRE(o == p);
p.capture(b);
REQUIRE(o != p);
REQUIRE(o < p);
REQUIRE(boost::lexical_cast<std::string>(o) == "1");

// Test id_pair.
o.capture(combine_ids(a, b));
REQUIRE(boost::lexical_cast<std::string>(o) == "(1,2)");
REQUIRE(o.get() == combine_ids(a, b));
REQUIRE(combine_ids(a, c) < combine_ids(a, b));
REQUIRE(combine_ids(a, b) != combine_ids(b, a));
REQUIRE(combine_ids(a, b) < combine_ids(b, a));
o.capture(combine_ids(a, ref(b)));
REQUIRE(o.get() == combine_ids(a, ref(b)));
REQUIRE((a < b && !(b < a) || b < a && !(a < b)));
REQUIRE(a.hash() != b.hash());
}

TEST_CASE("simple_id", "[id]")
{
test_different_ids(make_id(0), make_id(1));
}

TEST_CASE("simple_id_by_reference", "[id]")
{
int x = 0, y = 1;
test_different_ids(make_id_by_reference(x), make_id_by_reference(y));
}

TEST_CASE("id_ref", "[id]")
{
test_different_ids(ref(make_id(0)), ref(make_id(1)));
}

TEST_CASE("captured_id", "[id]")
{
captured_id c;
c.capture(make_id(0));
REQUIRE(c.matches(make_id(0)));
REQUIRE(!c.matches(make_id(1)));
REQUIRE(c.get() == make_id(0));
captured_id d;
REQUIRE(c != d);
d.capture(make_id(0));
REQUIRE(c == d);
d.capture(make_id(1));
REQUIRE(c != d);
REQUIRE(c < d);
REQUIRE(boost::lexical_cast<std::string>(c) == "0");
}

TEST_CASE("id_pair", "[id]")
{
auto a = combine_ids(make_id(0), make_id(1));
auto b = combine_ids(make_id(1), make_id(2));
REQUIRE(boost::lexical_cast<std::string>(a) == "(0,1)");
REQUIRE(boost::lexical_cast<std::string>(b) == "(1,2)");
test_different_ids(a, b);
}

0 comments on commit 24b2726

Please sign in to comment.