All tests done, just need the documentation and report
This commit is contained in:
@@ -33,9 +33,17 @@ public:
|
|||||||
|
|
||||||
StackErr push(T val);
|
StackErr push(T val);
|
||||||
|
|
||||||
|
uint64_t size();
|
||||||
|
|
||||||
void print();
|
void print();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
requires std::formattable<T, char>
|
||||||
|
uint64_t Stack<T>::size() {
|
||||||
|
return this->len;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
requires std::formattable<T, char>
|
requires std::formattable<T, char>
|
||||||
Stack<T>::Stack() {
|
Stack<T>::Stack() {
|
||||||
@@ -87,6 +95,7 @@ std::expected<T, StackErr> Stack<T>::pop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
T return_val = this->data[this->len - 1];
|
T return_val = this->data[this->len - 1];
|
||||||
|
this->len--;
|
||||||
|
|
||||||
if (this->cap / 4 > this->len) {
|
if (this->cap / 4 > this->len) {
|
||||||
uint64_t new_capacity = this->cap / 2;
|
uint64_t new_capacity = this->cap / 2;
|
||||||
@@ -106,7 +115,7 @@ std::expected<T, StackErr> Stack<T>::pop() {
|
|||||||
this->data = tmp;
|
this->data = tmp;
|
||||||
this->cap = new_capacity;
|
this->cap = new_capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
return return_val;
|
return return_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#include "stack.h"
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
|
TEST_CASE("Test push 3 items into stack.", "[stack]") {
|
||||||
|
Stack<int> 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<int> 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user