init
This commit is contained in:
commit
2c9de8cefd
16 changed files with 554 additions and 0 deletions
2
spec/spec_helper.cr
Normal file
2
spec/spec_helper.cr
Normal file
|
@ -0,0 +1,2 @@
|
|||
require "spec"
|
||||
require "../src/lifo"
|
104
spec/stack/queue.cr
Normal file
104
spec/stack/queue.cr
Normal file
|
@ -0,0 +1,104 @@
|
|||
describe Lifo::Queue do
|
||||
describe "#initialize" do
|
||||
it "sets capacity to -1 if none is given" do
|
||||
queue = Lifo::Queue(Int32).new
|
||||
queue.capacity.should eq -1
|
||||
end
|
||||
end
|
||||
|
||||
describe "#enqueue" do
|
||||
it "enqueues to the queue" do
|
||||
queue = Lifo::Queue(Int32).new
|
||||
queue.enqueue(1)
|
||||
queue.peek.should eq(1)
|
||||
queue.enqueue(3)
|
||||
queue.peek.should eq(1)
|
||||
queue.enqueue(5)
|
||||
queue.peek.should eq(1)
|
||||
end
|
||||
|
||||
it "returns queue size" do
|
||||
queue = Lifo::Queue(Int32).new
|
||||
queue.enqueue(1)
|
||||
queue.enqueue(3)
|
||||
queue.size.should eq(2)
|
||||
end
|
||||
|
||||
it "raises OverflowError if queue is full" do
|
||||
queue = Lifo::Queue(Int32).new(0)
|
||||
|
||||
expect_raises(Lifo::OverflowError) do
|
||||
queue.enqueue(1)
|
||||
end
|
||||
end
|
||||
|
||||
it "doesn't raise an error when queue is infinite" do
|
||||
queue = Lifo::Queue(Int32).new
|
||||
queue.enqueue(1)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#enqueue?" do
|
||||
it "enqueues to the queue" do
|
||||
queue = Lifo::Queue(Int32).new
|
||||
queue.enqueue?(1)
|
||||
queue.peek.should eq(1)
|
||||
queue.enqueue?(3)
|
||||
queue.peek.should eq(1)
|
||||
queue.enqueue?(5)
|
||||
queue.peek.should eq(1)
|
||||
end
|
||||
|
||||
it "returns queue size" do
|
||||
queue = Lifo::Queue(Int32).new
|
||||
queue.enqueue?(1)
|
||||
queue.enqueue?(3)
|
||||
queue.size.should eq(2)
|
||||
end
|
||||
|
||||
it "returns nil if error raised" do
|
||||
queue = Lifo::Queue(Int32).new(0)
|
||||
queue.enqueue?(1).should be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "#dequeue" do
|
||||
it "dequeues from the queue" do
|
||||
queue = Lifo::Queue(Int32).new
|
||||
queue.enqueue(1)
|
||||
queue.enqueue(3)
|
||||
queue.peek.should eq(1)
|
||||
queue.dequeue
|
||||
queue.peek.should eq(3)
|
||||
queue.dequeue
|
||||
end
|
||||
|
||||
it "returns dequeued element" do
|
||||
queue = Lifo::Queue(Int32).new
|
||||
queue.enqueue(1)
|
||||
queue.enqueue(3)
|
||||
queue.peek.should eq(1)
|
||||
queue.dequeue.should eq(1)
|
||||
queue.dequeue.should eq(3)
|
||||
end
|
||||
|
||||
it "raises UnderflowError if queue is empty" do
|
||||
queue = Lifo::Queue(Int32).new(0)
|
||||
expect_raises(Lifo::UnderflowError) do
|
||||
queue.dequeue
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#dequeue?" do
|
||||
it "dequeues from the queue" do
|
||||
queue = Lifo::Queue(Int32).new
|
||||
queue.enqueue(1)
|
||||
queue.enqueue(3)
|
||||
queue.peek.should eq(1)
|
||||
queue.dequeue?
|
||||
queue.peek.should eq(3)
|
||||
queue.dequeue?
|
||||
end
|
||||
end
|
||||
end
|
165
spec/stack/stack.cr
Normal file
165
spec/stack/stack.cr
Normal file
|
@ -0,0 +1,165 @@
|
|||
describe Lifo::Stack do
|
||||
describe "#initialize" do
|
||||
it "sets capacity to -1 if none is given" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.capacity.should eq -1
|
||||
end
|
||||
end
|
||||
|
||||
describe "#push" do
|
||||
it "pushes to the stack" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.push(1)
|
||||
stack.peek.should eq(1)
|
||||
stack.push(3)
|
||||
stack.peek.should eq(3)
|
||||
end
|
||||
|
||||
it "returns stack size" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.size.should eq(2)
|
||||
end
|
||||
|
||||
it "raises OverflowError if stack is full" do
|
||||
stack = Lifo::Stack(Int32).new(0)
|
||||
|
||||
expect_raises(Lifo::OverflowError) do
|
||||
stack.push(1)
|
||||
end
|
||||
end
|
||||
|
||||
it "doesn't raise an error when stack is infinite" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.push(1)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#push?" do
|
||||
it "pushes to the stack" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.push?(1)
|
||||
stack.peek.should eq(1)
|
||||
stack.push?(3)
|
||||
stack.peek.should eq(3)
|
||||
end
|
||||
|
||||
it "returns stack size" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.push?(1)
|
||||
stack.push?(3)
|
||||
stack.size.should eq(2)
|
||||
end
|
||||
|
||||
it "returns nil if error raised" do
|
||||
stack = Lifo::Stack(Int32).new(0)
|
||||
stack.push?(1).should be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "#pop" do
|
||||
it "pops from the stack" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.peek.should eq(3)
|
||||
stack.pop
|
||||
stack.peek.should eq(1)
|
||||
stack.pop
|
||||
end
|
||||
|
||||
it "returns popped element" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.pop.should eq(3)
|
||||
stack.pop.should eq(1)
|
||||
end
|
||||
|
||||
it "raises UnderflowError if stack is empty" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
expect_raises(Lifo::UnderflowError) do
|
||||
stack.pop
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#pop?" do
|
||||
it "pops from the stack" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.peek.should eq(3)
|
||||
stack.pop?
|
||||
stack.peek.should eq(1)
|
||||
stack.pop?
|
||||
end
|
||||
|
||||
it "returns popped element" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.peek.should eq(3)
|
||||
stack.pop?.should eq(3)
|
||||
stack.peek.should eq(1)
|
||||
stack.pop?.should eq(1)
|
||||
end
|
||||
|
||||
it "returns nil if stack is empty" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.pop?.should be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "#peek" do
|
||||
it "returns top element" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.push(2)
|
||||
stack.peek.should eq(2)
|
||||
end
|
||||
|
||||
it "raises UnderflowError if stack is empty" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
expect_raises(Lifo::UnderflowError) do
|
||||
stack.peek
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#peek?" do
|
||||
it "returns top element" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.peek?.should eq(3)
|
||||
end
|
||||
|
||||
it "returns nil if stack is empty" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.peek?.should be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "#size" do
|
||||
it "returns stack size" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.size.should eq(2)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#empty?" do
|
||||
it "returns true if stack is empty" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.empty?.should be_true
|
||||
end
|
||||
|
||||
it "returns false if stack is not empty" do
|
||||
stack = Lifo::Stack(Int32).new
|
||||
stack.push(1)
|
||||
stack.empty?.should be_false
|
||||
end
|
||||
end
|
||||
end
|
4
spec/stack_spec.cr
Normal file
4
spec/stack_spec.cr
Normal file
|
@ -0,0 +1,4 @@
|
|||
require "./spec_helper"
|
||||
|
||||
require "./stack/stack"
|
||||
require "./stack/queue"
|
Loading…
Add table
Add a link
Reference in a new issue