-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
Copy pathtest_stack.cpp
203 lines (169 loc) · 5.17 KB
/
test_stack.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <cassert> /// for assert
#include <iostream> /// for std::cout
#include <stdexcept> /// std::invalid_argument
#include <vector> /// for std::vector
#include "./stack.hpp"
template <typename T>
void testConstructedStackIsEmpty() {
const stack<T> curStack;
assert(curStack.isEmptyStack());
}
void testPush() {
using valueType = int;
stack<valueType> curStack;
curStack.push(10);
curStack.push(20);
curStack.push(30);
curStack.push(40);
const auto expectedData = std::vector<valueType>({40, 30, 20, 10});
assert(curStack.toVector() == expectedData);
}
void testTop() {
using valueType = unsigned;
stack<valueType> curStack;
curStack.push(1);
curStack.push(2);
curStack.push(3);
curStack.push(4);
assert(curStack.top() == static_cast<valueType>(4));
}
void testPop() {
using valueType = int;
stack<valueType> curStack;
curStack.push(100);
curStack.push(200);
curStack.push(300);
assert(curStack.top() == static_cast<valueType>(300));
curStack.pop();
assert(curStack.top() == static_cast<valueType>(200));
curStack.pop();
assert(curStack.top() == static_cast<valueType>(100));
curStack.pop();
assert(curStack.isEmptyStack());
}
void testClear() {
stack<int> curStack;
curStack.push(1000);
curStack.push(2000);
curStack.clear();
assert(curStack.isEmptyStack());
}
void testCopyOfStackHasSameData() {
stack<int> stackA;
stackA.push(10);
stackA.push(200);
stackA.push(3000);
const auto stackB(stackA);
assert(stackA.toVector() == stackB.toVector());
}
void testPushingToCopyDoesNotChangeOriginal() {
using valueType = int;
stack<valueType> stackA;
stackA.push(10);
stackA.push(20);
stackA.push(30);
auto stackB(stackA);
stackB.push(40);
const auto expectedDataA = std::vector<valueType>({30, 20, 10});
const auto expectedDataB = std::vector<valueType>({40, 30, 20, 10});
assert(stackA.toVector() == expectedDataA);
assert(stackB.toVector() == expectedDataB);
}
void testPoppingFromCopyDoesNotChangeOriginal() {
using valueType = int;
stack<valueType> stackA;
stackA.push(10);
stackA.push(20);
stackA.push(30);
auto stackB(stackA);
stackB.pop();
const auto expectedDataA = std::vector<valueType>({30, 20, 10});
const auto expectedDataB = std::vector<valueType>({20, 10});
assert(stackA.toVector() == expectedDataA);
assert(stackB.toVector() == expectedDataB);
}
void testPushingToOrginalDoesNotChangeCopy() {
using valueType = int;
stack<valueType> stackA;
stackA.push(10);
stackA.push(20);
stackA.push(30);
const auto stackB(stackA);
stackA.push(40);
const auto expectedDataA = std::vector<valueType>({40, 30, 20, 10});
const auto expectedDataB = std::vector<valueType>({30, 20, 10});
assert(stackA.toVector() == expectedDataA);
assert(stackB.toVector() == expectedDataB);
}
void testPoppingFromOrginalDoesNotChangeCopy() {
using valueType = int;
stack<valueType> stackA;
stackA.push(10);
stackA.push(20);
stackA.push(30);
const auto stackB(stackA);
stackA.pop();
const auto expectedDataA = std::vector<valueType>({20, 10});
const auto expectedDataB = std::vector<valueType>({30, 20, 10});
assert(stackA.toVector() == expectedDataA);
assert(stackB.toVector() == expectedDataB);
}
void testAssign() {
using valueType = int;
stack<valueType> stackA;
stackA.push(10);
stackA.push(20);
stackA.push(30);
stack<valueType> stackB = stackA;
stackA.pop();
stackB.push(40);
const auto expectedDataA = std::vector<valueType>({20, 10});
const auto expectedDataB = std::vector<valueType>({40, 30, 20, 10});
assert(stackA.toVector() == expectedDataA);
assert(stackB.toVector() == expectedDataB);
stackB = stackA;
stackA.pop();
stackB.push(5);
stackB.push(6);
const auto otherExpectedDataA = std::vector<valueType>({10});
const auto otherExpectedDataB = std::vector<valueType>({6, 5, 20, 10});
assert(stackA.toVector() == otherExpectedDataA);
assert(stackB.toVector() == otherExpectedDataB);
}
void testTopThrowsAnInvalidArgumentWhenStackEmpty() {
const stack<long double> curStack;
bool wasException = false;
try {
curStack.top();
} catch (const std::invalid_argument&) {
wasException = true;
}
assert(wasException);
}
void testPopThrowsAnInvalidArgumentWhenStackEmpty() {
stack<bool> curStack;
bool wasException = false;
try {
curStack.pop();
} catch (const std::invalid_argument&) {
wasException = true;
}
assert(wasException);
}
int main() {
testConstructedStackIsEmpty<int>();
testConstructedStackIsEmpty<char>();
testPush();
testPop();
testClear();
testCopyOfStackHasSameData();
testPushingToCopyDoesNotChangeOriginal();
testPoppingFromCopyDoesNotChangeOriginal();
testPushingToOrginalDoesNotChangeCopy();
testPoppingFromOrginalDoesNotChangeCopy();
testAssign();
testTopThrowsAnInvalidArgumentWhenStackEmpty();
testPopThrowsAnInvalidArgumentWhenStackEmpty();
std::cout << "All tests pass!\n";
return 0;
}