Files
Cataclysm-DDA/tests/safe_reference_test.cpp
dumb-kevin 05ab42807e tests/: wrap TU-local test fixture types in anon namespaces
- addiction_test: wrap addict_effect_totals
- battery_density_test: wrap battery_chemistry_family
- comestible_test: wrap all_stats
- encumbrance_test: wrap add_trait
- invlet_test: fuse inventory_location + invlet_state + test_action enums
- throwing_test: fuse throw_test_data + throw_test_pstats, move operator<< inside anon-ns
- vehicle_test: wrap vehicle_preset, damage_preset, folded_item_damage_preset, fuse rack_activation + rack_preset
- vision_test: wrap vision_test_case
- water_movement_test: fuse 6 swimmer/swim structs into one block
- weakpoint_test: wrap weakpoint_test_data, weakpoint_expectation, weakpoint_report
2026-05-12 10:53:28 +04:00

59 lines
1.3 KiB
C++

#include "cata_catch.h"
#include <memory>
#include "safe_reference.h"
namespace
{
struct example {
safe_reference_anchor anchor;
safe_reference<example> get_ref() {
return anchor.reference_to( this );
}
};
} // namespace
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 );
}