dodb.cr/spec/test-lists.cr

58 lines
1.3 KiB
Crystal
Raw Normal View History

2024-05-26 00:49:57 +02:00
require "spec"
require "../src/list.cr"
describe "DoubleLinkedList" do
it "add and remove values" do
list = DoubleLinkedList(Int32).new
list.push 1
list.to_s.should eq "[ 1 ]"
list.push 2
list.to_s.should eq "[ 1, 2 ]"
list << 3 << 4
list.to_s.should eq "[ 1, 2, 3, 4 ]"
list.insert_at(8, 1)
list.to_s.should eq "[ 1, 8, 2, 3, 4 ]"
list.insert_at(0, 0)
list.to_s.should eq "[ 0, 1, 8, 2, 3, 4 ]"
list.insert_at(5, 6)
list.to_s.should eq "[ 0, 1, 8, 2, 3, 4, 5 ]"
v = list.pop
v.value.should eq 5
list.to_s.should eq "[ 0, 1, 8, 2, 3, 4 ]"
v = list.pop
v.value.should eq 4
list.to_s.should eq "[ 0, 1, 8, 2, 3 ]"
v = list.pop
v.value.should eq 3
list.to_s.should eq "[ 0, 1, 8, 2 ]"
v = list.pop
v.value.should eq 2
list.to_s.should eq "[ 0, 1, 8 ]"
v = list.pop
v.value.should eq 8
list.to_s.should eq "[ 0, 1 ]"
v = list.pop
v.value.should eq 1
list.to_s.should eq "[ 0 ]"
v = list.pop
v.value.should eq 0
list.to_s.should eq "[ ]"
expect_raises DoubleLinkedList::OutOfBounds do
list.pop
end
2024-05-26 01:30:46 +02:00
expect_raises DoubleLinkedList::OutOfBounds do
list[0]
end
2024-05-26 00:49:57 +02:00
list.size.should eq 0
end
2024-05-26 01:41:49 +02:00
it "reverse" do
list = DoubleLinkedList(Int32).new
list << 1 << 2 << 3 << 4
list.reverse.to_s.should eq "[ 4, 3, 2, 1 ]"
end
2024-05-26 00:49:57 +02:00
end