diff --git a/spec/test-lists.cr b/spec/test-lists.cr index 55ee408..34c20a3 100644 --- a/spec/test-lists.cr +++ b/spec/test-lists.cr @@ -42,6 +42,10 @@ describe "DoubleLinkedList" do list.pop end + expect_raises DoubleLinkedList::OutOfBounds do + list[0] + end + list.size.should eq 0 end end diff --git a/src/list.cr b/src/list.cr index 11bd1a6..fb3b92a 100644 --- a/src/list.cr +++ b/src/list.cr @@ -299,16 +299,18 @@ class DoubleLinkedList(V) # # TODO: either start with the first entry or the last depending on the index. def [](index : Int32) : Node(V) - raise OutOfBounds.new if index > @size + raise OutOfBounds.new if index >= @size - return @first if index == 0 - return @last if index == @size - 1 + return @first.not_nil! if index == 0 + return @last.not_nil! if index == @size - 1 i = 0 each_node do |node| return node if i == index i += 1 end + + raise BrokenList.new "couldn't find the node, smth must be brkn" end # Concats two lists.