TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
test_stack.cpp
1#include <cassert>
2#include <iostream>
3#include <stdexcept>
4#include <vector>
5
6#include "./stack.hpp"
7
8template <typename T>
9void testConstructedStackIsEmpty() {
10 const stack<T> curStack;
11 assert(curStack.isEmptyStack());
12}
13
14void testPush() {
15 using valueType = int;
16 stack<valueType> curStack;
17 curStack.push(10);
18 curStack.push(20);
19 curStack.push(30);
20 curStack.push(40);
21 const auto expectedData = std::vector<valueType>({40, 30, 20, 10});
22 assert(curStack.toVector() == expectedData);
23}
24
25void testTop() {
26 using valueType = unsigned;
27 stack<valueType> curStack;
28 curStack.push(1);
29 curStack.push(2);
30 curStack.push(3);
31 curStack.push(4);
32 assert(curStack.top() == static_cast<valueType>(4));
33}
34
35void testPop() {
36 using valueType = int;
37 stack<valueType> curStack;
38 curStack.push(100);
39 curStack.push(200);
40 curStack.push(300);
41
42 assert(curStack.top() == static_cast<valueType>(300));
43 curStack.pop();
44 assert(curStack.top() == static_cast<valueType>(200));
45 curStack.pop();
46 assert(curStack.top() == static_cast<valueType>(100));
47 curStack.pop();
48 assert(curStack.isEmptyStack());
49}
50
51void testClear() {
52 stack<int> curStack;
53 curStack.push(1000);
54 curStack.push(2000);
55 curStack.clear();
56 assert(curStack.isEmptyStack());
57}
58
59void testCopyOfStackHasSameData() {
60 stack<int> stackA;
61 stackA.push(10);
62 stackA.push(200);
63 stackA.push(3000);
64 const auto stackB(stackA);
65 assert(stackA.toVector() == stackB.toVector());
66}
67
68void testPushingToCopyDoesNotChangeOriginal() {
69 using valueType = int;
70 stack<valueType> stackA;
71 stackA.push(10);
72 stackA.push(20);
73 stackA.push(30);
74 auto stackB(stackA);
75 stackB.push(40);
76
77 const auto expectedDataA = std::vector<valueType>({30, 20, 10});
78 const auto expectedDataB = std::vector<valueType>({40, 30, 20, 10});
79
80 assert(stackA.toVector() == expectedDataA);
81 assert(stackB.toVector() == expectedDataB);
82}
83
84void testPoppingFromCopyDoesNotChangeOriginal() {
85 using valueType = int;
86 stack<valueType> stackA;
87 stackA.push(10);
88 stackA.push(20);
89 stackA.push(30);
90 auto stackB(stackA);
91 stackB.pop();
92
93 const auto expectedDataA = std::vector<valueType>({30, 20, 10});
94 const auto expectedDataB = std::vector<valueType>({20, 10});
95
96 assert(stackA.toVector() == expectedDataA);
97 assert(stackB.toVector() == expectedDataB);
98}
99
100void testPushingToOrginalDoesNotChangeCopy() {
101 using valueType = int;
102 stack<valueType> stackA;
103 stackA.push(10);
104 stackA.push(20);
105 stackA.push(30);
106 const auto stackB(stackA);
107 stackA.push(40);
108
109 const auto expectedDataA = std::vector<valueType>({40, 30, 20, 10});
110 const auto expectedDataB = std::vector<valueType>({30, 20, 10});
111
112 assert(stackA.toVector() == expectedDataA);
113 assert(stackB.toVector() == expectedDataB);
114}
115
116void testPoppingFromOrginalDoesNotChangeCopy() {
117 using valueType = int;
118 stack<valueType> stackA;
119 stackA.push(10);
120 stackA.push(20);
121 stackA.push(30);
122 const auto stackB(stackA);
123 stackA.pop();
124
125 const auto expectedDataA = std::vector<valueType>({20, 10});
126 const auto expectedDataB = std::vector<valueType>({30, 20, 10});
127
128 assert(stackA.toVector() == expectedDataA);
129 assert(stackB.toVector() == expectedDataB);
130}
131
132void testAssign() {
133 using valueType = int;
134 stack<valueType> stackA;
135 stackA.push(10);
136 stackA.push(20);
137 stackA.push(30);
138 stack<valueType> stackB = stackA;
139 stackA.pop();
140 stackB.push(40);
141
142 const auto expectedDataA = std::vector<valueType>({20, 10});
143 const auto expectedDataB = std::vector<valueType>({40, 30, 20, 10});
144
145 assert(stackA.toVector() == expectedDataA);
146 assert(stackB.toVector() == expectedDataB);
147
148 stackB = stackA;
149 stackA.pop();
150 stackB.push(5);
151 stackB.push(6);
152
153 const auto otherExpectedDataA = std::vector<valueType>({10});
154 const auto otherExpectedDataB = std::vector<valueType>({6, 5, 20, 10});
155
156 assert(stackA.toVector() == otherExpectedDataA);
157 assert(stackB.toVector() == otherExpectedDataB);
158}
159
160void testTopThrowsAnInvalidArgumentWhenStackEmpty() {
161 const stack<long double> curStack;
162 bool wasException = false;
163 try {
164 curStack.top();
165 } catch (const std::invalid_argument&) {
166 wasException = true;
167 }
168 assert(wasException);
169}
170
171void testPopThrowsAnInvalidArgumentWhenStackEmpty() {
172 stack<bool> curStack;
173 bool wasException = false;
174 try {
175 curStack.pop();
176 } catch (const std::invalid_argument&) {
177 wasException = true;
178 }
179 assert(wasException);
180}
181
182int main() {
183 testConstructedStackIsEmpty<int>();
184 testConstructedStackIsEmpty<char>();
185
186 testPush();
187 testPop();
188 testClear();
189
190 testCopyOfStackHasSameData();
191 testPushingToCopyDoesNotChangeOriginal();
192 testPoppingFromCopyDoesNotChangeOriginal();
193 testPushingToOrginalDoesNotChangeCopy();
194 testPoppingFromOrginalDoesNotChangeCopy();
195
196 testAssign();
197
198 testTopThrowsAnInvalidArgumentWhenStackEmpty();
199 testPopThrowsAnInvalidArgumentWhenStackEmpty();
200
201 std::cout << "All tests pass!\n";
202 return 0;
203}
for std::invalid_argument
Definition stack.hpp:19
bool isEmptyStack() const
Definition stack.hpp:44
void pop()
Definition stack.hpp:62
void clear()
Definition stack.hpp:69
void push(const value_type &item)
Definition stack.hpp:47
value_type top() const
Definition stack.hpp:56
int main()
Main function.
This class specifies the basic operation on a stack as a linked list.