abseil-cpp/absl/container/internal/test_instance_tracker.h
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ABSL_CONTAINER_INTERNAL_TEST_INSTANCE_TRACKER_H_
16 #define ABSL_CONTAINER_INTERNAL_TEST_INSTANCE_TRACKER_H_
17 
18 #include <cstdlib>
19 #include <ostream>
20 
21 #include "absl/types/compare.h"
22 
23 namespace absl {
25 namespace test_internal {
26 
27 // A type that counts number of occurrences of the type, the live occurrences of
28 // the type, as well as the number of copies, moves, swaps, and comparisons that
29 // have occurred on the type. This is used as a base class for the copyable,
30 // copyable+movable, and movable types below that are used in actual tests. Use
31 // InstanceTracker in tests to track the number of instances.
33  public:
34  explicit BaseCountedInstance(int x) : value_(x) {
37  }
42  ++num_copies_;
43  }
46  x.is_live_ = false;
48  ++num_moves_;
49  }
53  }
54 
56  value_ = x.value_;
58  is_live_ = x.is_live_;
60  ++num_copies_;
61  return *this;
62  }
64  value_ = x.value_;
66  is_live_ = x.is_live_;
67  x.is_live_ = false;
68  ++num_moves_;
69  return *this;
70  }
71 
72  bool operator==(const BaseCountedInstance& x) const {
74  return value_ == x.value_;
75  }
76 
77  bool operator!=(const BaseCountedInstance& x) const {
79  return value_ != x.value_;
80  }
81 
82  bool operator<(const BaseCountedInstance& x) const {
84  return value_ < x.value_;
85  }
86 
87  bool operator>(const BaseCountedInstance& x) const {
89  return value_ > x.value_;
90  }
91 
92  bool operator<=(const BaseCountedInstance& x) const {
94  return value_ <= x.value_;
95  }
96 
97  bool operator>=(const BaseCountedInstance& x) const {
99  return value_ >= x.value_;
100  }
101 
104  return value_ < x.value_
105  ? absl::weak_ordering::less
106  : value_ == x.value_ ? absl::weak_ordering::equivalent
107  : absl::weak_ordering::greater;
108  }
109 
110  int value() const {
111  if (!is_live_) std::abort();
112  return value_;
113  }
114 
115  friend std::ostream& operator<<(std::ostream& o,
116  const BaseCountedInstance& v) {
117  return o << "[value:" << v.value() << "]";
118  }
119 
120  // Implementation of efficient swap() that counts swaps.
121  static void SwapImpl(
122  BaseCountedInstance& lhs, // NOLINT(runtime/references)
123  BaseCountedInstance& rhs) { // NOLINT(runtime/references)
124  using std::swap;
125  swap(lhs.value_, rhs.value_);
126  swap(lhs.is_live_, rhs.is_live_);
128  }
129 
130  private:
131  friend class InstanceTracker;
132 
133  int value_;
134 
135  // Indicates if the value is live, ie it hasn't been moved away from.
136  bool is_live_ = true;
137 
138  // Number of instances.
139  static int num_instances_;
140 
141  // Number of live instances (those that have not been moved away from.)
143 
144  // Number of times that BaseCountedInstance objects were moved.
145  static int num_moves_;
146 
147  // Number of times that BaseCountedInstance objects were copied.
148  static int num_copies_;
149 
150  // Number of times that BaseCountedInstance objects were swapped.
151  static int num_swaps_;
152 
153  // Number of times that BaseCountedInstance objects were compared.
154  static int num_comparisons_;
155 };
156 
157 // Helper to track the BaseCountedInstance instance counters. Expects that the
158 // number of instances and live_instances are the same when it is constructed
159 // and when it is destructed.
161  public:
163  : start_instances_(BaseCountedInstance::num_instances_),
164  start_live_instances_(BaseCountedInstance::num_live_instances_) {
166  }
168  if (instances() != 0) std::abort();
169  if (live_instances() != 0) std::abort();
170  }
171 
172  // Returns the number of BaseCountedInstance instances both containing valid
173  // values and those moved away from compared to when the InstanceTracker was
174  // constructed
175  int instances() const {
177  }
178 
179  // Returns the number of live BaseCountedInstance instances compared to when
180  // the InstanceTracker was constructed
181  int live_instances() const {
183  }
184 
185  // Returns the number of moves on BaseCountedInstance objects since
186  // construction or since the last call to ResetCopiesMovesSwaps().
188 
189  // Returns the number of copies on BaseCountedInstance objects since
190  // construction or the last call to ResetCopiesMovesSwaps().
191  int copies() const {
193  }
194 
195  // Returns the number of swaps on BaseCountedInstance objects since
196  // construction or the last call to ResetCopiesMovesSwaps().
198 
199  // Returns the number of comparisons on BaseCountedInstance objects since
200  // construction or the last call to ResetCopiesMovesSwaps().
201  int comparisons() const {
203  }
204 
205  // Resets the base values for moves, copies, comparisons, and swaps to the
206  // current values, so that subsequent Get*() calls for moves, copies,
207  // comparisons, and swaps will compare to the situation at the point of this
208  // call.
214  }
215 
216  private:
223 };
224 
225 // Copyable, not movable.
227  public:
229  CopyableOnlyInstance(const CopyableOnlyInstance& rhs) = default;
230  CopyableOnlyInstance& operator=(const CopyableOnlyInstance& rhs) = default;
231 
234  }
235 
236  static bool supports_move() { return false; }
237 };
238 
239 // Copyable and movable.
241  public:
243  CopyableMovableInstance(const CopyableMovableInstance& rhs) = default;
246  default;
248 
251  }
252 
253  static bool supports_move() { return true; }
254 };
255 
256 // Only movable, not default-constructible.
258  public:
260  MovableOnlyInstance(MovableOnlyInstance&& other) = default;
262 
263  friend void swap(MovableOnlyInstance& lhs, MovableOnlyInstance& rhs) {
265  }
266 
267  static bool supports_move() { return true; }
268 };
269 
270 } // namespace test_internal
272 } // namespace absl
273 
274 #endif // ABSL_CONTAINER_INTERNAL_TEST_INSTANCE_TRACKER_H_
absl::test_internal::InstanceTracker::InstanceTracker
InstanceTracker()
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:162
absl::test_internal::InstanceTracker::moves
int moves() const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:187
absl::test_internal::BaseCountedInstance::value
int value() const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:110
absl::test_internal::CopyableOnlyInstance::CopyableOnlyInstance
CopyableOnlyInstance(int x)
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:228
absl::test_internal::MovableOnlyInstance::operator=
MovableOnlyInstance & operator=(MovableOnlyInstance &&other)=default
absl::test_internal::InstanceTracker::~InstanceTracker
~InstanceTracker()
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:167
absl::test_internal::InstanceTracker::instances
int instances() const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:175
absl::test_internal::BaseCountedInstance::is_live_
bool is_live_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:136
absl::test_internal::BaseCountedInstance::SwapImpl
static void SwapImpl(BaseCountedInstance &lhs, BaseCountedInstance &rhs)
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:121
absl::test_internal::BaseCountedInstance::num_swaps_
static int num_swaps_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:151
absl::test_internal::InstanceTracker::ResetCopiesMovesSwaps
void ResetCopiesMovesSwaps()
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:209
absl::test_internal::CopyableMovableInstance::operator=
CopyableMovableInstance & operator=(const CopyableMovableInstance &rhs)=default
absl::test_internal::CopyableOnlyInstance::supports_move
static bool supports_move()
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:236
absl::test_internal::BaseCountedInstance::num_instances_
static int num_instances_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:139
absl::test_internal::InstanceTracker::swaps
int swaps() const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:197
absl::test_internal::CopyableOnlyInstance
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:226
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::test_internal::InstanceTracker::start_copies_
int start_copies_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:220
absl::test_internal::BaseCountedInstance::BaseCountedInstance
BaseCountedInstance(const BaseCountedInstance &x)
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:38
absl::test_internal::BaseCountedInstance::BaseCountedInstance
BaseCountedInstance(int x)
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:34
o
UnboundConversion o
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:97
absl::test_internal::BaseCountedInstance::operator=
BaseCountedInstance & operator=(BaseCountedInstance &&x)
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:63
absl::test_internal::BaseCountedInstance::BaseCountedInstance
BaseCountedInstance(BaseCountedInstance &&x)
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:44
absl::test_internal::InstanceTracker::comparisons
int comparisons() const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:201
absl::test_internal::BaseCountedInstance::~BaseCountedInstance
~BaseCountedInstance()
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:50
absl::test_internal::BaseCountedInstance::operator>=
bool operator>=(const BaseCountedInstance &x) const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:97
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::test_internal::CopyableMovableInstance::swap
friend void swap(CopyableMovableInstance &lhs, CopyableMovableInstance &rhs)
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:249
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
absl::test_internal::InstanceTracker::copies
int copies() const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:191
absl::swap
void swap(btree_map< K, V, C, A > &x, btree_map< K, V, C, A > &y)
Definition: abseil-cpp/absl/container/btree_map.h:474
absl::test_internal::BaseCountedInstance
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:32
absl::test_internal::InstanceTracker::start_swaps_
int start_swaps_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:221
absl::test_internal::CopyableMovableInstance::supports_move
static bool supports_move()
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:253
std::swap
void swap(Json::Value &a, Json::Value &b)
Specialize std::swap() for Json::Value.
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:1226
absl::test_internal::MovableOnlyInstance::swap
friend void swap(MovableOnlyInstance &lhs, MovableOnlyInstance &rhs)
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:263
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
absl::test_internal::InstanceTracker::start_moves_
int start_moves_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:219
absl::test_internal::BaseCountedInstance::operator>
bool operator>(const BaseCountedInstance &x) const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:87
absl::test_internal::BaseCountedInstance::num_moves_
static int num_moves_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:145
absl::test_internal::BaseCountedInstance::operator<
bool operator<(const BaseCountedInstance &x) const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:82
absl::test_internal::BaseCountedInstance::value_
int value_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:133
absl::test_internal::MovableOnlyInstance
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:257
absl::test_internal::BaseCountedInstance::num_comparisons_
static int num_comparisons_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:154
absl::test_internal::BaseCountedInstance::operator<=
bool operator<=(const BaseCountedInstance &x) const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:92
absl::test_internal::CopyableMovableInstance::CopyableMovableInstance
CopyableMovableInstance(int x)
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:242
absl::test_internal::BaseCountedInstance::operator<<
friend std::ostream & operator<<(std::ostream &o, const BaseCountedInstance &v)
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:115
absl::test_internal::BaseCountedInstance::num_live_instances_
static int num_live_instances_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:142
absl::test_internal::InstanceTracker::live_instances
int live_instances() const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:181
absl::test_internal::InstanceTracker::start_live_instances_
int start_live_instances_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:218
absl::test_internal::MovableOnlyInstance::MovableOnlyInstance
MovableOnlyInstance(int x)
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:259
absl::test_internal::BaseCountedInstance::compare
absl::weak_ordering compare(const BaseCountedInstance &x) const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:102
absl::test_internal::CopyableOnlyInstance::swap
friend void swap(CopyableOnlyInstance &lhs, CopyableOnlyInstance &rhs)
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:232
absl::test_internal::InstanceTracker::start_comparisons_
int start_comparisons_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:222
absl::weak_ordering
Definition: abseil-cpp/absl/types/compare.h:347
absl::test_internal::InstanceTracker::start_instances_
int start_instances_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:217
absl::test_internal::CopyableMovableInstance
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:240
absl::test_internal::InstanceTracker
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:160
absl::test_internal::BaseCountedInstance::operator!=
bool operator!=(const BaseCountedInstance &x) const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:77
absl::test_internal::BaseCountedInstance::operator=
BaseCountedInstance & operator=(const BaseCountedInstance &x)
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:55
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::test_internal::BaseCountedInstance::operator==
bool operator==(const BaseCountedInstance &x) const
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:72
absl::test_internal::CopyableOnlyInstance::operator=
CopyableOnlyInstance & operator=(const CopyableOnlyInstance &rhs)=default
absl::test_internal::BaseCountedInstance::num_copies_
static int num_copies_
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:148
absl::test_internal::MovableOnlyInstance::supports_move
static bool supports_move()
Definition: abseil-cpp/absl/container/internal/test_instance_tracker.h:267


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:28