File: safe_reference_test.cpp

package info (click to toggle)
cataclysm-dda 0.H-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 710,808 kB
  • sloc: cpp: 524,019; python: 11,580; sh: 1,228; makefile: 1,169; xml: 507; javascript: 150; sql: 56; exp: 41; perl: 37
file content (53 lines) | stat: -rw-r--r-- 1,319 bytes parent folder | download
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 );
}