1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include "cata_catch.h"
#include <memory>
#include "safe_reference.h"
struct example {
safe_reference_anchor anchor;
safe_reference<example> get_ref() {
return anchor.reference_to( this );
}
};
TEST_CASE( "safe_reference_returns_correct_object", "[safe_reference]" )
{
example e;
safe_reference<example> ref = e.get_ref();
CHECK( ref );
CHECK( ref.get() == &e );
}
TEST_CASE( "safe_reference_invalidated_by_destructor", "[safe_reference]" )
{
std::unique_ptr<example> e = std::make_unique<example>();
safe_reference<example> ref = e->get_ref();
e.reset();
CHECK( !ref );
CHECK( ref.get() == nullptr );
}
TEST_CASE( "safe_reference_invalidated_by_assignment", "[safe_reference]" )
{
example e;
safe_reference<example> ref = e.get_ref();
e = example();
CHECK( !ref );
CHECK( ref.get() == nullptr );
}
TEST_CASE( "safe_reference_not_shared_by_copy", "[safe_reference]" )
{
std::unique_ptr<example> e0 = std::make_unique<example>();
safe_reference<example> ref0 = e0->get_ref();
std::unique_ptr<example> e1 = std::make_unique<example>( *e0 );
safe_reference<example> ref1 = e1->get_ref();
CHECK( ref0 );
CHECK( ref1 );
CHECK( ref0.get() != ref1.get() );
e0.reset();
CHECK( !ref0 );
CHECK( ref1 );
}
|