fortheck/test/stack_test.gleam

92 lines
1.5 KiB
Gleam

import fortheck/error
import fortheck/stack
import gleam/iterator
import gleam/pair
import gleam/result
import gleeunit
import gleeunit/should
pub fn main() {
gleeunit.main()
}
pub fn new_test() {
stack.new()
|> stack.to_list
|> should.equal([])
}
pub fn push_test() {
stack.new()
|> stack.push(123)
|> should.be_ok
|> stack.push(456)
|> should.be_ok
|> stack.to_list
|> should.equal([123, 456])
}
pub fn push_stack_overflow_test() {
iterator.range(1, 131_072)
|> iterator.try_fold(stack.new(), stack.push)
|> should.be_ok
|> stack.push(1)
|> should.be_error
|> should.equal(error.StackOverflow)
}
pub fn from_list_test() {
[123, 456]
|> stack.from_list
|> should.be_ok
|> stack.to_list
|> should.equal([123, 456])
}
pub fn pop_stack_underflow_test() {
stack.new()
|> stack.pop
|> should.be_error
|> should.equal(error.StackUnderflow)
}
pub fn pop_test() {
let assert Ok(stack) = stack.from_list([1, 2, 3])
stack
|> stack.pop
|> should.be_ok
|> pair.first
|> should.equal(3)
{
use a, _ <- stack.try_pop(stack)
Ok(a)
}
|> should.be_ok
|> should.equal(3)
}
pub fn pop_2_test() {
let assert Ok(stack) = stack.from_list([1, 2, 3])
stack
|> stack.pop_2
|> should.be_ok
|> pair.first
|> should.equal(#(2, 3))
{
use a, b, _ <- stack.try_pop_2(stack)
Ok(#(a, b))
}
|> should.be_ok
|> should.equal(#(2, 3))
}
pub fn pop_2_stack_underflow_test() {
stack.from_list([1])
|> result.try(stack.pop_2)
|> should.be_error
|> should.equal(error.StackUnderflow)
}