From c89c32eca08172785a76a6aa07f9f04c8905d52d Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sun, 26 May 2024 01:30:46 +0200 Subject: [PATCH] DoubleLinkedList: [] --- spec/test-lists.cr | 4 ++++ src/list.cr | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) 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.