Skip to content

Commit 2cbd90a

Browse files
committed
UnitTest/DummyObject: Copy & Move counters
1 parent e4851f4 commit 2cbd90a

2 files changed

Lines changed: 117 additions & 27 deletions

File tree

modules/UnitTest/DummyObject.mpp

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,74 @@ export namespace CppUtils::UnitTest
1010
using Logger = CppUtils::Logger<"CppUtils">;
1111

1212
public:
13+
inline DummyObject() = default;
14+
1315
explicit inline DummyObject(std::string name, std::size_t indentationLevel = 0):
1416
m_name{std::move(name)},
1517
m_indentationLevel{indentationLevel}
1618
{
17-
Logger::print("{}{}()\n", std::string(m_indentationLevel, '\t'), m_name);
18-
}
19+
Logger::print("{}{}()\n", std::string(m_indentationLevel, '\t'), m_name);
20+
}
1921

20-
inline virtual ~DummyObject()
22+
[[nodiscard]] inline auto getCopyCount() const -> std::size_t
2123
{
22-
Logger::print("{}~{}()\n", std::string(m_indentationLevel, '\t'), m_name);
24+
return m_copyCount;
2325
}
2426

25-
inline DummyObject(const DummyObject& other)
27+
[[nodiscard]] inline auto getMoveCount() const -> std::size_t
2628
{
27-
m_name = other.m_name;
28-
m_indentationLevel = other.m_indentationLevel;
29-
Logger::print("{}{}(const {}&)\n", std::string(m_indentationLevel, '\t'), m_name, m_name);
29+
return m_moveCount;
3030
}
3131

32-
inline DummyObject(DummyObject&& other)
32+
inline virtual ~DummyObject()
3333
{
34-
m_name = other.m_name;
35-
m_indentationLevel = other.m_indentationLevel;
36-
other.m_name += " [invalid object]";
37-
Logger::print("{}{}({}&&)\n", std::string(m_indentationLevel, '\t'), m_name, m_name);
34+
Logger::print("{}~{}()\n", std::string(m_indentationLevel, '\t'), m_name);
3835
}
3936

37+
inline DummyObject(const DummyObject& other):
38+
m_name{other.m_name},
39+
m_indentationLevel{other.m_indentationLevel},
40+
m_copyCount{other.m_copyCount.load() + 1},
41+
m_moveCount{other.m_moveCount.load()}
42+
{
43+
Logger::print("{}{}(const {}&)\n", std::string(m_indentationLevel, '\t'), m_name, m_name);
44+
}
45+
46+
inline DummyObject(DummyObject&& other) noexcept:
47+
m_name{std::move(other.m_name)},
48+
m_indentationLevel{other.m_indentationLevel},
49+
m_copyCount{other.m_copyCount.load()},
50+
m_moveCount{other.m_moveCount.load() + 1}
51+
{
52+
other.m_name += " [invalid object]";
53+
Logger::print("{}{}({}&&)\n", std::string(m_indentationLevel, '\t'), m_name, m_name);
54+
}
55+
4056
inline auto operator=(const DummyObject& rhs) -> DummyObject&
41-
{
42-
m_name = rhs.m_name;
43-
m_indentationLevel = rhs.m_indentationLevel;
44-
Logger::print("{}{}::operator=(const {}&)\n", std::string(m_indentationLevel, '\t'), m_name, m_name);
45-
return *this;
46-
}
57+
{
58+
m_name = rhs.m_name;
59+
m_indentationLevel = rhs.m_indentationLevel;
60+
m_copyCount.store(rhs.m_copyCount.load() + 1);
61+
m_moveCount.store(rhs.m_moveCount.load());
62+
Logger::print("{}{}::operator=(const {}&)\n", std::string(m_indentationLevel, '\t'), m_name, m_name);
63+
return *this;
64+
}
4765

48-
inline auto operator=(DummyObject&& rhs) -> DummyObject&
49-
{
50-
m_name = rhs.m_name;
51-
m_indentationLevel = rhs.m_indentationLevel;
52-
rhs.m_name += " [invalid object]";
53-
Logger::print("{}{}::operator=({}&&)\n", std::string(m_indentationLevel, '\t'), m_name, m_name);
54-
return *this;
55-
}
66+
inline auto operator=(DummyObject&& rhs) noexcept -> DummyObject&
67+
{
68+
m_name = std::move(rhs.m_name);
69+
m_indentationLevel = rhs.m_indentationLevel;
70+
m_copyCount.store(rhs.m_copyCount.load());
71+
m_moveCount.store(rhs.m_moveCount.load() + 1);
72+
rhs.m_name += " [invalid object]";
73+
Logger::print("{}{}::operator=({}&&)\n", std::string(m_indentationLevel, '\t'), m_name, m_name);
74+
return *this;
75+
}
5676

5777
private:
5878
std::string m_name;
5979
std::size_t m_indentationLevel;
80+
std::atomic_size_t m_copyCount = 0;
81+
std::atomic_size_t m_moveCount = 0;
6082
};
6183
}

tests/UnitTest/UnitTest.mpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,73 @@ export namespace CppUtils::UnitTest
2727
function();
2828
suite.expectCall(expectCall);
2929
});
30+
31+
suite.addTest("DummyObject - No Copy No Move", [&] {
32+
auto object = DummyObject{"DummyObject"};
33+
suite.expectEqual(object.getCopyCount(), 0uz);
34+
suite.expectEqual(object.getMoveCount(), 0uz);
35+
});
36+
37+
suite.addTest("DummyObject - Copy Construction", [&] {
38+
auto object = DummyObject{"DummyObject"};
39+
auto copied = object;
40+
suite.expectEqual(copied.getCopyCount(), 1uz);
41+
suite.expectEqual(copied.getMoveCount(), 0uz);
42+
});
43+
44+
suite.addTest("DummyObject - Move Construction", [&] {
45+
auto object = DummyObject{"DummyObject"};
46+
auto moved = std::move(object);
47+
suite.expectEqual(moved.getCopyCount(), 0uz);
48+
suite.expectEqual(moved.getMoveCount(), 1uz);
49+
});
50+
51+
suite.addTest("DummyObject - Copy Assignment", [&] {
52+
auto object = DummyObject{"DummyObject"};
53+
auto target = DummyObject{"Target"};
54+
target = object;
55+
suite.expectEqual(target.getCopyCount(), 1uz);
56+
suite.expectEqual(target.getMoveCount(), 0uz);
57+
});
58+
59+
suite.addTest("DummyObject - Move Assignment", [&] {
60+
auto object = DummyObject{"DummyObject"};
61+
auto target = DummyObject{"Target"};
62+
target = std::move(object);
63+
suite.expectEqual(target.getCopyCount(), 0uz);
64+
suite.expectEqual(target.getMoveCount(), 1uz);
65+
});
66+
67+
suite.addTest("DummyObject - Two Copies", [&] {
68+
auto object = DummyObject{"DummyObject"};
69+
auto copy1 = object;
70+
auto copy2 = copy1;
71+
suite.expectEqual(copy2.getCopyCount(), 2uz);
72+
suite.expectEqual(copy2.getMoveCount(), 0uz);
73+
});
74+
75+
suite.addTest("DummyObject - Two Moves", [&] {
76+
auto object = DummyObject{"DummyObject"};
77+
auto moved1 = std::move(object);
78+
auto moved2 = std::move(moved1);
79+
suite.expectEqual(moved2.getCopyCount(), 0uz);
80+
suite.expectEqual(moved2.getMoveCount(), 2uz);
81+
});
82+
83+
suite.addTest("DummyObject - Copy then Move", [&] {
84+
auto object = DummyObject{"DummyObject"};
85+
auto copy = object;
86+
auto moved = std::move(copy);
87+
suite.expectEqual(moved.getCopyCount(), 1uz);
88+
suite.expectEqual(moved.getMoveCount(), 1uz);
89+
});
90+
91+
suite.addTest("DummyObject - Move then Copy", [&] {
92+
auto object = DummyObject{"DummyObject"};
93+
auto moved = std::move(object);
94+
auto copy = moved;
95+
suite.expectEqual(copy.getCopyCount(), 1uz);
96+
suite.expectEqual(copy.getMoveCount(), 1uz);
97+
});
3098
}};
3199
}

0 commit comments

Comments
 (0)