Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack #158

Closed
wants to merge 6 commits into from
Closed

Stack #158

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"queue",
"reentrancy",
"signed_integers",
"stack",
"storagemapvec",
"strings/storage_string",
"strings/string",
Expand Down
2 changes: 2 additions & 0 deletions libs/stack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
7 changes: 7 additions & 0 deletions libs/stack/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "stack"

[dependencies]
143 changes: 143 additions & 0 deletions libs/stack/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
library;
//! The `Stack` type corresponds to the same called data structure.
//! A Stack is defined as a linear data structure that is open at the only ends and the operations are performed in Last In First Out order.

pub struct Stack<T> {
vec: Vec<T>,
}

impl<T> Stack<T> {
pub fn new() -> Self {
Self {
vec: Vec::new(),
}
}

pub fn is_empty(self) -> bool {
self.vec.is_empty()
}

pub fn len(self) -> u64 {
self.vec.len()
}

pub fn push(ref mut self, value: T) {
self.vec.push(value);
}

pub fn pop(ref mut self) -> Option<T> {
self.vec.pop()
}

pub fn peek(self) -> Option<T> {
if self.vec.is_empty() {
Option::None
} else {
self.vec.get(self.vec.len() - 1)
}
}
}

#[test]
fn test_new_stack_is_empty() {
let stack: Stack<u32> = Stack::new();
assert(stack.is_empty());
}

#[test]
fn test_push_single_element() {
let mut stack = Stack::new();
stack.push(42);

assert(stack.len() == 1);
std::logging::log(stack.len());
std::logging::log(stack.vec.get(0).unwrap());
assert(!stack.is_empty());
}

#[test]
fn test_push_multiple_elements() {
let mut stack = Stack::new();
stack.push(1);
stack.push(2);
stack.push(3);
assert(stack.len() == 3);
assert(!stack.is_empty());
}

#[test]
fn test_pop_empty_stack() {
let mut stack: Stack<u32> = Stack::new();
let popped = stack.pop();
assert(popped.is_none());
assert(stack.len() == 0);
assert(stack.is_empty());
}

#[test]
fn test_pop_single_element() {
let mut stack = Stack::new();
stack.push(42);
let popped = stack.pop();
assert(popped.unwrap() == 42);
assert(stack.len() == 0);
assert(stack.is_empty());
}

#[test]
fn test_pop_multiple_elements() {
let mut stack = Stack::new();
stack.push(1);
stack.push(2);
stack.push(3);
let popped1 = stack.pop();
assert(popped1.unwrap() == 3);
assert(stack.len() == 2);
assert(!stack.is_empty());

let popped2 = stack.pop();
assert(popped2.unwrap() == 2);
assert(stack.len() == 1);
assert(!stack.is_empty());

let popped3 = stack.pop();
assert(popped3.unwrap() == 1);
assert(stack.len() == 0);
assert(stack.is_empty());

let popped4 = stack.pop();
assert(popped4.is_none());
assert(stack.len() == 0);
assert(stack.is_empty());
}

#[test]
fn test_peek_empty_stack() {
let stack: Stack<u32> = Stack::new();
let peeked = stack.peek();
assert(peeked.is_none());
assert(stack.len() == 0);
assert(stack.is_empty());
}

#[test]
fn test_peek_single_element() {
let mut stack = Stack::new();
stack.push(42);
let peeked = stack.peek();
assert(peeked.unwrap() == 42);
assert(stack.len() == 1);
assert(!stack.is_empty());
}

#[test]
fn test_peek_multiple_elements() {
let mut stack = Stack::new();
stack.push(1);
stack.push(2);
stack.push(3);
let peeked = stack.peek();
assert(peeked.unwrap() == 3);
assert(stack.len() == 3);
assert(!stack.is_empty());
}