aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/common/probe.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/testing/common/probe.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/testing/common/probe.h')
-rw-r--r--library/cpp/testing/common/probe.h140
1 files changed, 140 insertions, 0 deletions
diff --git a/library/cpp/testing/common/probe.h b/library/cpp/testing/common/probe.h
new file mode 100644
index 00000000000..19910979b56
--- /dev/null
+++ b/library/cpp/testing/common/probe.h
@@ -0,0 +1,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