diff --git a/include/stack.h b/include/stack.h index e1e3ecc..b0b2e51 100644 --- a/include/stack.h +++ b/include/stack.h @@ -33,9 +33,17 @@ public: StackErr push(T val); + uint64_t size(); + void print(); }; +template +requires std::formattable +uint64_t Stack::size() { + return this->len; +} + template requires std::formattable Stack::Stack() { @@ -87,6 +95,7 @@ std::expected Stack::pop() { } T return_val = this->data[this->len - 1]; + this->len--; if (this->cap / 4 > this->len) { uint64_t new_capacity = this->cap / 2; @@ -106,7 +115,7 @@ std::expected Stack::pop() { this->data = tmp; this->cap = new_capacity; } - + return return_val; } diff --git a/test/test_stack.cpp b/test/test_stack.cpp index e69de29..9d41f98 100644 --- a/test/test_stack.cpp +++ b/test/test_stack.cpp @@ -0,0 +1,52 @@ +#include "stack.h" +#include + +TEST_CASE("Test push 3 items into stack.", "[stack]") { + Stack stack = {}; + + REQUIRE(stack.size() == 0); + + stack.push(80); + REQUIRE(stack.size() == 1); + REQUIRE(stack.peek() == 80); + + stack.push(90); + REQUIRE(stack.size() == 2); + REQUIRE(stack.peek() == 90); + + stack.push(60); + REQUIRE(stack.size() == 3); + REQUIRE(stack.peek() == 60); +} + +TEST_CASE("Test push 3 items and poping them.", "[stack]") { + Stack stack = {}; + + REQUIRE(stack.size() == 0); + + stack.push(80); + REQUIRE(stack.size() == 1); + REQUIRE(stack.peek() == 80); + + stack.push(90); + REQUIRE(stack.size() == 2); + REQUIRE(stack.peek() == 90); + + stack.push(60); + REQUIRE(stack.size() == 3); + REQUIRE(stack.peek() == 60); + + int return_val; + return_val = stack.pop().value(); + REQUIRE(stack.size() == 2); + REQUIRE(return_val == 60); + + return_val = stack.pop().value(); + REQUIRE(stack.size() == 1); + REQUIRE(return_val == 90); + + return_val = stack.pop().value(); + REQUIRE(stack.size() == 0); + REQUIRE(return_val == 80); + +}