-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhw5.cpp
More file actions
188 lines (142 loc) · 2.92 KB
/
hw5.cpp
File metadata and controls
188 lines (142 loc) · 2.92 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Christopher C. McCann, U05484743
* testflyjets@gmail.com
* C/C++ Programming 4, Section ID 106647, Ray Mitchell III
* May 14, 2015
* hw5.cpp
* Windows 8.1 Enterprise 64-bit
* Microsoft Visual Studio 2013 Express for Desktop
*
* A test program for an implementation of the auto_ptr
* class.
*
*/
#include <iostream>
using std::cout;
#include <string>
#include "auto_ptr.h"
using namespace ChrisMcCann::hw5;
#include "UnitTest++.h"
class TestClass
{
public:
TestClass(int i = 0) : testInt(i)
{
}
~TestClass()
{
}
void setInt(int i)
{
testInt = i;
}
int getInt()
{
return testInt;
}
private:
int testInt;
};
class DerivedTestClass : public TestClass
{
};
TEST(DefaultConstructor)
{
auto_ptr<TestClass> ap;
CHECK(ap.get() == nullptr);
}
TEST(Constructor)
{
auto_ptr<TestClass> ap(new TestClass);
CHECK(ap.get() != nullptr);
}
TEST(CopyConstructor)
{
TestClass *ptr = new TestClass(42);
auto_ptr<TestClass> ap(ptr);
auto_ptr<TestClass> apCopy(ap);
CHECK(ap.get() == nullptr);
CHECK(apCopy.get() == ptr);
}
TEST(CopyConstructorDerivedClass)
{
auto_ptr<DerivedTestClass> rhs(new DerivedTestClass);
auto_ptr<TestClass> apBase(rhs);
CHECK(apBase->getInt() == 0);
}
TEST(Destructor)
{
TestClass *ptr = new TestClass;
auto_ptr<TestClass> *ap = new auto_ptr<TestClass>(ptr);
try
{
delete ap;
}
catch (...)
{
CHECK(false);
}
}
TEST(GetAutoPtr)
{
TestClass *ptr = new TestClass(42);
auto_ptr<TestClass> ap(ptr);
CHECK(ap.get() == ptr);
}
TEST(DereferenceAutoPtr)
{
auto_ptr<TestClass> ap(new TestClass(42));
CHECK((*ap).getInt() == 42);
}
TEST(AutoPtrArrowOperator)
{
auto_ptr<TestClass> ap(new TestClass(42));
CHECK(ap->getInt() == 42);
}
TEST(AssignmentOperator)
{
auto_ptr<TestClass> ap(new TestClass(1));
auto_ptr<TestClass> rhs(new TestClass(2));
CHECK(ap->getInt() == 1);
ap = rhs;
CHECK(ap->getInt() == 2);
}
TEST(DerivedClassAssignmentOperator)
{
auto_ptr<TestClass> ap(new TestClass(42));
auto_ptr<DerivedTestClass> rhs(new DerivedTestClass);
CHECK(ap->getInt() == 42);
ap = rhs;
CHECK(ap->getInt() == 0);
}
TEST(ReleaseAutoPtr)
{
auto_ptr<TestClass> ap(new TestClass(42));
TestClass *newPtr = ap.release();
CHECK(ap.get() == nullptr);
CHECK(newPtr->getInt() == 42);
}
TEST(DefaultResetAutoPtr)
{
auto_ptr<TestClass> ap(new TestClass(1));
ap.reset();
CHECK(ap.get() == nullptr);
}
TEST(ResetAutoPtr)
{
TestClass *ptr1 = new TestClass(1);
auto_ptr<TestClass> ap(ptr1);
TestClass *ptr2 = new TestClass(2);
ap.reset(ptr2);
CHECK(ap->getInt() == 2);
}
TEST(ResetPreventSelfAssignment)
{
TestClass *ptr1 = new TestClass(1);
auto_ptr<TestClass> ap(ptr1);
ap.reset(ptr1);
CHECK(ap.get() == ptr1);
}
int main() {
cout << "Running tests for C4 HW5..." << "\n\n";
return UnitTest::RunAllTests();
}