-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtest_unique_ptr.cpp
110 lines (88 loc) · 3.02 KB
/
test_unique_ptr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "catch2/catch_amalgamated.hpp"
#include "jumble/unique_ptr.hpp"
#include <string>
#include <functional>
class DeleterWrapper {
public:
using DeleterType = std::function<void(std::string*)>;
std::size_t delCnt;
DeleterType deleter;
DeleterWrapper() : delCnt(0) {
deleter = [&](std::string* p) {
++delCnt;
delete p;
};
}
};
TEST_CASE("Basic") {
jumble::UniquePtr<std::string> p1;
REQUIRE(!static_cast<bool>(p1));
REQUIRE(p1.get() == nullptr);
jumble::UniquePtr<std::string> p2(new std::string("hello world"));
REQUIRE(static_cast<bool>(p2));
REQUIRE(*p2 == *p2.get());
REQUIRE(p2->substr(6) == "world");
jumble::swap<std::string>(p1, p2);
REQUIRE(!static_cast<bool>(p2));
REQUIRE(p2.get() == nullptr);
REQUIRE(static_cast<bool>(p1));
REQUIRE(*p1 == *p1.get());
REQUIRE(p1->substr(6) == "world");
}
TEST_CASE("Reset") {
DeleterWrapper deleterWrapper;
auto s = new std::string("hello world");
jumble::UniquePtr<std::string, DeleterWrapper::DeleterType> p1(s, deleterWrapper.deleter);
REQUIRE(*p1.get() == "hello world");
p1.reset();
REQUIRE(p1.get() == nullptr);
REQUIRE(deleterWrapper.delCnt == 1);
p1.reset(new std::string("quick fox"));
REQUIRE(*p1.get() == "quick fox");
REQUIRE(deleterWrapper.delCnt == 1);
std::string *p2 = p1.release();
REQUIRE(p1.get() == nullptr);
REQUIRE(*p2 == "quick fox");
REQUIRE(deleterWrapper.delCnt == 1);
delete p2;
}
TEST_CASE("CopyControl1") {
jumble::UniquePtr<std::string> p1(new std::string("hello world"));
REQUIRE(*p1.get() == "hello world");
jumble::UniquePtr<std::string> p2(std::move(p1));
REQUIRE(p1.get() == nullptr);
REQUIRE(*p2.get() == "hello world");
jumble::UniquePtr<std::string> p3;
p3 = std::move(p2);
REQUIRE(p1.get() == nullptr);
REQUIRE(p2.get() == nullptr);
REQUIRE(*p3.get() == "hello world");
p3 = nullptr;
REQUIRE(p1.get() == nullptr);
REQUIRE(p2.get() == nullptr);
REQUIRE(p3.get() == nullptr);
}
TEST_CASE("CopyControl2") {
DeleterWrapper deleterWrapper;
jumble::UniquePtr<std::string, DeleterWrapper::DeleterType> p1(deleterWrapper.deleter);
REQUIRE(p1.get() == nullptr);
REQUIRE(deleterWrapper.delCnt == 0);
auto s = new std::string("quick fox");
jumble::UniquePtr<std::string, DeleterWrapper::DeleterType> p2(s, deleterWrapper.deleter);
REQUIRE(*p2.get() == "quick fox");
REQUIRE(deleterWrapper.delCnt == 0);
p1 = std::move(p2);
REQUIRE(*p1.get() == "quick fox");
REQUIRE(p2.get() == nullptr);
REQUIRE(deleterWrapper.delCnt == 0);
jumble::UniquePtr<std::string, DeleterWrapper::DeleterType> p3(std::move(p1));
REQUIRE(p1.get() == nullptr);
REQUIRE(p2.get() == nullptr);
REQUIRE(*p3.get() == "quick fox");
REQUIRE(deleterWrapper.delCnt == 0);
p3.reset();
REQUIRE(p1.get() == nullptr);
REQUIRE(p2.get() == nullptr);
REQUIRE(p3.get() == nullptr);
REQUIRE(deleterWrapper.delCnt == 1);
}