diff --git a/ex02/src/MutantStack.hpp b/ex02/src/MutantStack.hpp new file mode 100644 index 0000000..f8b2b22 --- /dev/null +++ b/ex02/src/MutantStack.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +template class MutantStack : public std::stack { + public: + MutantStack() {}; + MutantStack(const MutantStack& other) { *this = other; }; + + MutantStack& operator=(const MutantStack& other) { return std::stack::operator=(other); }; + + typedef typename std::stack::container_type::iterator iterator; + iterator begin() { return this->c.begin(); } + iterator end() { return this->c.end(); } + + ~MutantStack() {}; +}; diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp index 6dd80a5..c0ca703 100644 --- a/ex02/src/main.cpp +++ b/ex02/src/main.cpp @@ -1,7 +1,77 @@ +#include #include - -#define ASSERT_THROWS(expression) try { (expression); assert(false && "Expected exception did not throw: " #expression); } catch (...) {} +#include int main() { -} + { + MutantStack stack; + + assert(stack.begin() == stack.end()); + + stack.push(1); + + assert(*stack.begin() == 1); + assert(*(stack.end() - 1) == 1); + + stack.push(2); + stack.push(3); + + assert(*stack.begin() == 1); + assert(*(stack.end() - 1) == 3); + + stack.pop(); + + assert(*stack.begin() == 1); + assert(*(stack.end() - 1) == 2); + + MutantStack::iterator begin = stack.begin(); + MutantStack::iterator end = stack.end() - 1; + + assert(*begin == 1); + assert(*end == 2); + + begin++; + + assert(*begin == 2); + assert(begin == end); + + end--; + assert(*end == 1); + } + { + MutantStack stack; + + assert(stack.begin() == stack.end()); + + stack.push("Hello"); + + assert(*stack.begin() == "Hello"); + assert(*(stack.end() - 1) == "Hello"); + + stack.push("World"); + stack.push("!"); + + assert(*stack.begin() == "Hello"); + assert(*(stack.end() - 1) == "!"); + + stack.pop(); + + assert(*stack.begin() == "Hello"); + assert(*(stack.end() - 1) == "World"); + + MutantStack::iterator begin = stack.begin(); + MutantStack::iterator end = stack.end() - 1; + + assert(*begin == "Hello"); + assert(*end == "World"); + + begin++; + + assert(*begin == "World"); + assert(begin == end); + + end--; + assert(*end == "Hello"); + } +}