Debian Codegraph @

tests/test_instance_tracker.h

raw
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 PHMAP_CONTAINER_INTERNAL_TEST_INSTANCE_TRACKER_H_
16 #define PHMAP_CONTAINER_INTERNAL_TEST_INSTANCE_TRACKER_H_
17
18 #include <cstdlib>
19 #include <ostream>
20
21 namespace phmap {
22 namespace test_internal {
23
24 // A type that counts number of occurences of the type, the live occurrences of
25 // the type, as well as the number of copies, moves, swaps, and comparisons that
26 // have occurred on the type. This is used as a base class for the copyable,
27 // copyable+movable, and movable types below that are used in actual tests. Use
28 // InstanceTracker in tests to track the number of instances.
29 class BaseCountedInstance {
30 public:
31 explicit BaseCountedInstance(int x) : value_(x) {
32 ++num_instances_;
33 ++num_live_instances_;
34 }
35 BaseCountedInstance(const BaseCountedInstance& x)
36 : value_(x.value_), is_live_(x.is_live_) {
37 ++num_instances_;
38 if (is_live_) ++num_live_instances_;
39 ++num_copies_;
40 }
41 BaseCountedInstance(BaseCountedInstance&& x)
42 : value_(x.value_), is_live_(x.is_live_) {
43 x.is_live_ = false;
44 ++num_instances_;
45 ++num_moves_;
46 }
47 ~BaseCountedInstance() {
48 --num_instances_;
49 if (is_live_) --num_live_instances_;
50 }
51
52 BaseCountedInstance& operator=(const BaseCountedInstance& x) {
53 value_ = x.value_;
54 if (is_live_) --num_live_instances_;
55 is_live_ = x.is_live_;
56 if (is_live_) ++num_live_instances_;
57 ++num_copies_;
58 return *this;
59 }
60 BaseCountedInstance& operator=(BaseCountedInstance&& x) {
61 value_ = x.value_;
62 if (is_live_) --num_live_instances_;
63 is_live_ = x.is_live_;
64 x.is_live_ = false;
65 ++num_moves_;
66 return *this;
67 }
68
69 bool operator==(const BaseCountedInstance& x) const {
70 ++num_comparisons_;
71 return value_ == x.value_;
72 }
73
74 bool operator!=(const BaseCountedInstance& x) const {
75 ++num_comparisons_;
76 return value_ != x.value_;
77 }
78
79 bool operator<(const BaseCountedInstance& x) const {
80 ++num_comparisons_;
81 return value_ < x.value_;
82 }
83
84 bool operator>(const BaseCountedInstance& x) const {
85 ++num_comparisons_;
86 return value_ > x.value_;
87 }
88
89 bool operator<=(const BaseCountedInstance& x) const {
90 ++num_comparisons_;
91 return value_ <= x.value_;
92 }
93
94 bool operator>=(const BaseCountedInstance& x) const {
95 ++num_comparisons_;
96 return value_ >= x.value_;
97 }
98
99 int value() const {
100 if (!is_live_) std::abort();
101 return value_;
102 }
103
104 friend std::ostream& operator<<(std::ostream& o,
105 const BaseCountedInstance& v) {
106 return o << "[value:" << v.value() << "]";
107 }
108
109 // Implementation of efficient swap() that counts swaps.
110 static void SwapImpl(
111 BaseCountedInstance& lhs, // NOLINT(runtime/references)
112 BaseCountedInstance& rhs) { // NOLINT(runtime/references)
113 using std::swap;
114 swap(lhs.value_, rhs.value_);
115 swap(lhs.is_live_, rhs.is_live_);
116 ++BaseCountedInstance::num_swaps_;
117 }
118
119 private:
120 friend class InstanceTracker;
121
122 int value_;
123
124 // Indicates if the value is live, ie it hasn't been moved away from.
125 bool is_live_ = true;
126
127 // Number of instances.
128 static int num_instances_;
129
130 // Number of live instances (those that have not been moved away from.)
131 static int num_live_instances_;
132
133 // Number of times that BaseCountedInstance objects were moved.
134 static int num_moves_;
135
136 // Number of times that BaseCountedInstance objects were copied.
137 static int num_copies_;
138
139 // Number of times that BaseCountedInstance objects were swapped.
140 static int num_swaps_;
141
142 // Number of times that BaseCountedInstance objects were compared.
143 static int num_comparisons_;
144 };
145
146 // Helper to track the BaseCountedInstance instance counters. Expects that the
147 // number of instances and live_instances are the same when it is constructed
148 // and when it is destructed.
149 class InstanceTracker {
150 public:
151 InstanceTracker()
152 : start_instances_(BaseCountedInstance::num_instances_),
153 start_live_instances_(BaseCountedInstance::num_live_instances_) {
154 ResetCopiesMovesSwaps();
155 }
156 ~InstanceTracker() {
157 if (instances() != 0) std::abort();
158 if (live_instances() != 0) std::abort();
159 }
160
161 // Returns the number of BaseCountedInstance instances both containing valid
162 // values and those moved away from compared to when the InstanceTracker was
163 // constructed
164 int instances() const {
165 return BaseCountedInstance::num_instances_ - start_instances_;
166 }
167
168 // Returns the number of live BaseCountedInstance instances compared to when
169 // the InstanceTracker was constructed
170 int live_instances() const {
171 return BaseCountedInstance::num_live_instances_ - start_live_instances_;
172 }
173
174 // Returns the number of moves on BaseCountedInstance objects since
175 // construction or since the last call to ResetCopiesMovesSwaps().
176 int moves() const { return BaseCountedInstance::num_moves_ - start_moves_; }
177
178 // Returns the number of copies on BaseCountedInstance objects since
179 // construction or the last call to ResetCopiesMovesSwaps().
180 int copies() const {
181 return BaseCountedInstance::num_copies_ - start_copies_;
182 }
183
184 // Returns the number of swaps on BaseCountedInstance objects since
185 // construction or the last call to ResetCopiesMovesSwaps().
186 int swaps() const { return BaseCountedInstance::num_swaps_ - start_swaps_; }
187
188 // Returns the number of comparisons on BaseCountedInstance objects since
189 // construction or the last call to ResetCopiesMovesSwaps().
190 int comparisons() const {
191 return BaseCountedInstance::num_comparisons_ - start_comparisons_;
192 }
193
194 // Resets the base values for moves, copies, comparisons, and swaps to the
195 // current values, so that subsequent Get*() calls for moves, copies,
196 // comparisons, and swaps will compare to the situation at the point of this
197 // call.
198 void ResetCopiesMovesSwaps() {
199 start_moves_ = BaseCountedInstance::num_moves_;
200 start_copies_ = BaseCountedInstance::num_copies_;
201 start_swaps_ = BaseCountedInstance::num_swaps_;
202 start_comparisons_ = BaseCountedInstance::num_comparisons_;
203 }
204
205 private:
206 int start_instances_;
207 int start_live_instances_;
208 int start_moves_;
209 int start_copies_;
210 int start_swaps_;
211 int start_comparisons_;
212 };
213
214 // Copyable, not movable.
215 class CopyableOnlyInstance : public BaseCountedInstance {
216 public:
217 explicit CopyableOnlyInstance(int x) : BaseCountedInstance(x) {}
218 CopyableOnlyInstance(const CopyableOnlyInstance& rhs) = default;
219 CopyableOnlyInstance& operator=(const CopyableOnlyInstance& rhs) = default;
220
221 friend void swap(CopyableOnlyInstance& lhs, CopyableOnlyInstance& rhs) {
222 BaseCountedInstance::SwapImpl(lhs, rhs);
223 }
224
225 static bool supports_move() { return false; }
226 };
227
228 // Copyable and movable.
229 class CopyableMovableInstance : public BaseCountedInstance {
230 public:
231 explicit CopyableMovableInstance(int x) : BaseCountedInstance(x) {}
232 CopyableMovableInstance(const CopyableMovableInstance& rhs) = default;
233 CopyableMovableInstance(CopyableMovableInstance&& rhs) = default;
234 CopyableMovableInstance& operator=(const CopyableMovableInstance& rhs) =
235 default;
236 CopyableMovableInstance& operator=(CopyableMovableInstance&& rhs) = default;
237
238 friend void swap(CopyableMovableInstance& lhs, CopyableMovableInstance& rhs) {
239 BaseCountedInstance::SwapImpl(lhs, rhs);
240 }
241
242 static bool supports_move() { return true; }
243 };
244
245 // Only movable, not default-constructible.
246 class MovableOnlyInstance : public BaseCountedInstance {
247 public:
248 explicit MovableOnlyInstance(int x) : BaseCountedInstance(x) {}
249 MovableOnlyInstance(MovableOnlyInstance&& other) = default;
250 MovableOnlyInstance& operator=(MovableOnlyInstance&& other) = default;
251
252 friend void swap(MovableOnlyInstance& lhs, MovableOnlyInstance& rhs) {
253 BaseCountedInstance::SwapImpl(lhs, rhs);
254 }
255
256 static bool supports_move() { return true; }
257 };
258
259 } // namespace test_internal
260 } // namespace phmap
261
262 #endif // PHMAP_CONTAINER_INTERNAL_TEST_INSTANCE_TRACKER_H_