Skip to content

Commit 7ce5c4e

Browse files
committed
Count copies too
1 parent f6b792e commit 7ce5c4e

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

test/regressions.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <memory>
88
#include <string>
9+
#include <utility>
910
#include <vector>
1011

1112
#include "function2-test.hpp"
@@ -262,31 +263,41 @@ TYPED_TEST(AllReferenceRetConstructTests, reference_returns_not_buildable) {
262263
ASSERT_EQ(ref.data(), 8373827);
263264
}
264265

265-
/// Functor which counts the number of times it has been moved
266-
class MoveCountFunctor {
266+
/// Functor which counts the number of times it has been copied or moved
267+
class CopyAndMoveCountFunctor {
268+
int copy_count = 0;
267269
int move_count = 0;
268270

269271
public:
270-
MoveCountFunctor() {
272+
CopyAndMoveCountFunctor() = default;
273+
274+
CopyAndMoveCountFunctor(const CopyAndMoveCountFunctor& that) {
275+
*this = that;
271276
}
272277

273-
MoveCountFunctor(const MoveCountFunctor&) = delete;
274-
MoveCountFunctor& operator=(const MoveCountFunctor&) = delete;
275-
~MoveCountFunctor() = default;
278+
CopyAndMoveCountFunctor& operator=(const CopyAndMoveCountFunctor& that) {
279+
copy_count = that.copy_count + 1;
280+
move_count = that.move_count;
281+
}
276282

277-
MoveCountFunctor(MoveCountFunctor&& that): move_count(that.move_count + 1) {
283+
CopyAndMoveCountFunctor(CopyAndMoveCountFunctor&& that) {
284+
*this = std::move(that);
278285
}
279-
MoveCountFunctor& operator=(MoveCountFunctor&& that) {
286+
287+
CopyAndMoveCountFunctor& operator=(CopyAndMoveCountFunctor&& that) {
288+
copy_count = that.copy_count;
280289
move_count = that.move_count + 1;
281290
return *this;
282291
}
283292

284-
int operator()() const {
285-
return move_count;
293+
~CopyAndMoveCountFunctor() = default;
294+
295+
std::pair<int, int> operator()() const {
296+
return std::make_pair(copy_count, move_count);
286297
}
287298
};
288299

289300
TEST(regression_tests, no_extra_move_during_construction) {
290-
fu2::function<int()> f(MoveCountFunctor{});
291-
ASSERT_EQ(f(), 1);
301+
fu2::function<int()> f(CopyAndMoveCountFunctor{});
302+
ASSERT_EQ(f(), std::make_pair(0, 1));
292303
}

0 commit comments

Comments
 (0)