aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/common/probe.h
blob: 84ea8a0678d4e9496a725883a5a29eeab9a3a346 (plain) (blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#pragma once

#include <util/system/yassert.h>
 
namespace NTesting {
    ////////////////////////////////////////////////////////////////////////////////

    // Below there is a serie of probe classes for testing construction/destruction copying/moving of class.
    // for examples see tests in probe_ut.cpp

    struct TProbeState {
        int Constructors = 0;
        int Destructors = 0;
        int ShadowDestructors = 0;
        int CopyConstructors = 0;
        int CopyAssignments = 0;
        int MoveConstructors = 0;
        int MoveAssignments = 0;
        int Touches = 0;

        TProbeState() = default;

        void Reset() {
            *this = TProbeState{};
        }
    };

    // Used for probing the number of copies that occur if a type must be coerced.
    class TCoercibleToProbe {
    public:
        TProbeState* State;
        TProbeState* ShadowState;

    public:
        explicit TCoercibleToProbe(TProbeState* state)
            : State(state)
            , ShadowState(state)
        {}

    private:
        TCoercibleToProbe(const TCoercibleToProbe&);
        TCoercibleToProbe(TCoercibleToProbe&&);
        TCoercibleToProbe& operator=(const TCoercibleToProbe&);
        TCoercibleToProbe& operator=(TCoercibleToProbe&&);
    };

    // Used for probing the number of copies in an argument.
    class TProbe {
    public:
        TProbeState* State;
        TProbeState* ShadowState;

    public:
        static TProbe ExplicitlyCreateInvalidProbe() {
            return TProbe();
        }

        explicit TProbe(TProbeState* state)
            : State(state)
            , ShadowState(state)
        {
            Y_ASSERT(State);
            ++State->Constructors;
        }

        ~TProbe() {
            if (State) {
                ++State->Destructors;
            }
            if (ShadowState) {
                ++ShadowState->ShadowDestructors;
            }
        }

        TProbe(const TProbe& other)
            : State(other.State)
            , ShadowState(other.ShadowState)
        {
            Y_ASSERT(State);
            ++State->CopyConstructors;
        }

        TProbe(TProbe&& other)
            : State(other.State)
            , ShadowState(other.ShadowState)
        {
            Y_ASSERT(State);
            other.State = nullptr;
            ++State->MoveConstructors;
        }

        TProbe(const TCoercibleToProbe& other)
            : State(other.State)
            , ShadowState(other.ShadowState)
        {
            Y_ASSERT(State);
            ++State->CopyConstructors;
        }

        TProbe(TCoercibleToProbe&& other)
            : State(other.State)
            , ShadowState(other.ShadowState)
        {
            Y_ASSERT(State);
            other.State = nullptr;
            ++State->MoveConstructors;
        }

        TProbe& operator=(const TProbe& other) {
            State = other.State;
            ShadowState = other.ShadowState;
            Y_ASSERT(State);
            ++State->CopyAssignments;
            return *this;
        }

        TProbe& operator=(TProbe&& other) {
            State = other.State;
            ShadowState = other.ShadowState;
            Y_ASSERT(State);
            other.State = nullptr;
            ++State->MoveAssignments;
            return *this;
        }

        void Touch() const {
            Y_ASSERT(State);
            ++State->Touches;
        }

        bool IsValid() const {
            return nullptr != State;
        }

    private:
        TProbe()
            : State(nullptr)
        {}
    };
} // namespace NTesting