dodb.cr/spec/test-lists.cr

105 lines
2.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
2024-05-26 03:07:19 +02:00
it "insert_at" do
list = DoubleLinkedList(Int32).new
list.insert_at 2, 0
list.insert_at 1, 0
list.insert_at 3, 2
list.insert_at 0, 0
list.to_s.should eq "[ 0, 1, 2, 3 ]"
end
2024-05-26 01:41:49 +02:00
it "reverse" do
list = DoubleLinkedList(Int32).new
2024-05-26 03:07:19 +02:00
list.reverse.to_s.should eq "[ ]"
2024-05-26 01:41:49 +02:00
list << 1 << 2 << 3 << 4
list.reverse.to_s.should eq "[ 4, 3, 2, 1 ]"
end
2024-05-26 03:07:19 +02:00
it "concat" do
list1 = DoubleLinkedList(Int32).new
list2 = DoubleLinkedList(Int32).new
list1 << 1 << 2
list2 << 3 << 4
list1.concat list2
list1.to_s.should eq "[ 1, 2, 3, 4 ]"
end
it "+" do
list1 = DoubleLinkedList(Int32).new
list2 = DoubleLinkedList(Int32).new
list1 << 1 << 2
list2 << 3 << 4
list3 = list1 + list2
list1.to_s.should eq "[ 1, 2 ]"
list2.to_s.should eq "[ 3, 4 ]"
list3.to_s.should eq "[ 1, 2, 3, 4 ]"
end
it "shift" do
list = DoubleLinkedList(Int32).new
list << 1 << 2 << 3 << 4
list.shift.value.should eq 1
list.shift.value.should eq 2
list.shift.value.should eq 3
list.shift.value.should eq 4
end
it "peek" do
list = DoubleLinkedList(Int32).new
list << 1 << 2 << 3 << 4
list.peek.value.should eq 4
list.pop
list.peek.value.should eq 3
end
2024-05-26 00:49:57 +02:00
end