9void testConstructedStackIsEmpty() {
15 using valueType = int;
21 const auto expectedData = std::vector<valueType>({40, 30, 20, 10});
22 assert(curStack.toVector() == expectedData);
26 using valueType = unsigned;
32 assert(curStack.
top() ==
static_cast<valueType
>(4));
36 using valueType = int;
42 assert(curStack.
top() ==
static_cast<valueType
>(300));
44 assert(curStack.
top() ==
static_cast<valueType
>(200));
46 assert(curStack.
top() ==
static_cast<valueType
>(100));
59void testCopyOfStackHasSameData() {
64 const auto stackB(stackA);
65 assert(stackA.toVector() == stackB.toVector());
68void testPushingToCopyDoesNotChangeOriginal() {
69 using valueType = int;
77 const auto expectedDataA = std::vector<valueType>({30, 20, 10});
78 const auto expectedDataB = std::vector<valueType>({40, 30, 20, 10});
80 assert(stackA.toVector() == expectedDataA);
81 assert(stackB.toVector() == expectedDataB);
84void testPoppingFromCopyDoesNotChangeOriginal() {
85 using valueType = int;
93 const auto expectedDataA = std::vector<valueType>({30, 20, 10});
94 const auto expectedDataB = std::vector<valueType>({20, 10});
96 assert(stackA.toVector() == expectedDataA);
97 assert(stackB.toVector() == expectedDataB);
100void testPushingToOrginalDoesNotChangeCopy() {
101 using valueType = int;
106 const auto stackB(stackA);
109 const auto expectedDataA = std::vector<valueType>({40, 30, 20, 10});
110 const auto expectedDataB = std::vector<valueType>({30, 20, 10});
112 assert(stackA.toVector() == expectedDataA);
113 assert(stackB.toVector() == expectedDataB);
116void testPoppingFromOrginalDoesNotChangeCopy() {
117 using valueType = int;
122 const auto stackB(stackA);
125 const auto expectedDataA = std::vector<valueType>({20, 10});
126 const auto expectedDataB = std::vector<valueType>({30, 20, 10});
128 assert(stackA.toVector() == expectedDataA);
129 assert(stackB.toVector() == expectedDataB);
133 using valueType = int;
142 const auto expectedDataA = std::vector<valueType>({20, 10});
143 const auto expectedDataB = std::vector<valueType>({40, 30, 20, 10});
145 assert(stackA.toVector() == expectedDataA);
146 assert(stackB.toVector() == expectedDataB);
153 const auto otherExpectedDataA = std::vector<valueType>({10});
154 const auto otherExpectedDataB = std::vector<valueType>({6, 5, 20, 10});
156 assert(stackA.toVector() == otherExpectedDataA);
157 assert(stackB.toVector() == otherExpectedDataB);
160void testTopThrowsAnInvalidArgumentWhenStackEmpty() {
162 bool wasException =
false;
165 }
catch (
const std::invalid_argument&) {
168 assert(wasException);
171void testPopThrowsAnInvalidArgumentWhenStackEmpty() {
173 bool wasException =
false;
176 }
catch (
const std::invalid_argument&) {
179 assert(wasException);
183 testConstructedStackIsEmpty<int>();
184 testConstructedStackIsEmpty<char>();
190 testCopyOfStackHasSameData();
191 testPushingToCopyDoesNotChangeOriginal();
192 testPoppingFromCopyDoesNotChangeOriginal();
193 testPushingToOrginalDoesNotChangeCopy();
194 testPoppingFromOrginalDoesNotChangeCopy();
198 testTopThrowsAnInvalidArgumentWhenStackEmpty();
199 testPopThrowsAnInvalidArgumentWhenStackEmpty();
201 std::cout <<
"All tests pass!\n";
for std::invalid_argument
bool isEmptyStack() const
void push(const value_type &item)
This class specifies the basic operation on a stack as a linked list.