Skip to content

Commit

Permalink
Make captured_id more efficient and improve ID test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadden committed Jul 13, 2018
1 parent 677640f commit 87f8df5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/alia/id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ clone_into(id_interface*& storage, id_interface const* id)
}
}

void
clone_into(std::shared_ptr<id_interface>& storage, id_interface const* id)
{
if (!id)
{
storage.reset();
}
else if (storage && types_match(*storage, *id))
{
id->deep_copy(&*storage);
}
else
{
storage.reset(id->clone());
}
}

bool
operator==(captured_id const& a, captured_id const& b)
{
Expand Down
5 changes: 4 additions & 1 deletion src/alia/id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ struct id_interface_pointer_hash
// (if any) and create a new clone to store there.
void
clone_into(id_interface*& storage, id_interface const* id);
// Same, but where the storage is a shared_ptr.
void
clone_into(std::shared_ptr<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 reference will be valid).
Expand All @@ -132,7 +135,7 @@ struct captured_id
void
capture(id_interface const& new_id)
{
id_.reset(new_id.clone());
clone_into(id_, &new_id);
}
bool
is_initialized() const
Expand Down
67 changes: 65 additions & 2 deletions unit_tests/id.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#include <alia/id.hpp>
#include <boost/lexical_cast.hpp>

#include <map>
#include <unordered_map>
#include <utility>

#include <boost/lexical_cast.hpp>

#include <catch.hpp>


using namespace alia;

// Test all the relevant ID operations on a pair of equal IDs.
Expand Down Expand Up @@ -72,10 +77,16 @@ TEST_CASE("id_ref", "[id]")
TEST_CASE("captured_id", "[id]")
{
captured_id c;
REQUIRE(!c.is_initialized());
REQUIRE(!c.matches(make_id(0)));
c.capture(make_id(0));
REQUIRE(c.is_initialized());
REQUIRE(c.matches(make_id(0)));
REQUIRE(!c.matches(make_id(1)));
REQUIRE(c.get() == make_id(0));
c.clear();
REQUIRE(!c.is_initialized());
c.capture(make_id(0));
captured_id d;
REQUIRE(c != d);
d.capture(make_id(0));
Expand Down Expand Up @@ -118,7 +129,7 @@ TEST_CASE("combine_ids x4", "[id]")
test_different_ids(a, b);
}

TEST_CASE("clone_into", "[id]")
TEST_CASE("clone_into/pointer", "[id]")
{
id_interface* storage = 0;
auto zero = make_id(0);
Expand All @@ -133,3 +144,55 @@ TEST_CASE("clone_into", "[id]")
clone_into(storage, nullptr);
REQUIRE(!storage);
}

TEST_CASE("clone_into/shared_ptr", "[id]")
{
std::shared_ptr<id_interface> storage;
auto zero = make_id(0);
auto abc = make_id(string("abc"));
auto one = make_id(1);
clone_into(storage, &zero);
REQUIRE(*storage == zero);
clone_into(storage, &one);
REQUIRE(*storage == one);
clone_into(storage, &abc);
REQUIRE(*storage == abc);
clone_into(storage, nullptr);
REQUIRE(!storage);
}

TEST_CASE("map of IDs", "[id]")
{
auto zero = make_id(0);
auto abc = make_id(string("abc"));
auto one = make_id(1);
auto another_one = make_id(1);

std::map<id_interface const*,int,id_interface_pointer_less_than_test> m;
m[&zero] = 0;
m[&abc] = 123;
REQUIRE(m.at(&zero) == 0);
REQUIRE(m.at(&abc) == 123);
REQUIRE(m.find(&one) == m.end());
m[&one] = 1;
REQUIRE(m.at(&one) == 1);
REQUIRE(m.at(&another_one) == 1);
}

TEST_CASE("unordered_map of IDs", "[id]")
{
auto zero = make_id(0);
auto abc = make_id(string("abc"));
auto one = make_id(1);
auto another_one = make_id(1);

std::unordered_map<id_interface const*,int,id_interface_pointer_hash,id_interface_pointer_equality_test> m;
m[&zero] = 0;
m[&abc] = 123;
REQUIRE(m.at(&zero) == 0);
REQUIRE(m.at(&abc) == 123);
REQUIRE(m.find(&one) == m.end());
m[&one] = 1;
REQUIRE(m.at(&one) == 1);
REQUIRE(m.at(&another_one) == 1);
}

0 comments on commit 87f8df5

Please sign in to comment.