Skip to content

Commit

Permalink
Out of range operations should return nil
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrisonJ committed Apr 24, 2024
1 parent efcc20a commit 1149d93
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 14 deletions.
7 changes: 4 additions & 3 deletions lib/sorted_containers/sorted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def slice!(*args)
#
# @return [Object] The last value in the array.
def last
raise "Array is empty" if @size.zero?
return nil if @size.zero?

@lists.last.last
end
Expand All @@ -224,7 +224,7 @@ def last
#
# @return [Object] The first value in the array.
def first
raise "Array is empty" if @size.zero?
return nil if @size.zero?

@lists.first.first
end
Expand All @@ -233,6 +233,7 @@ def first
#
# @param index [Integer] The index of the value to delete.
def delete_at(index)
return nil if index.abs >= @size
pos, idx = pos(index)
internal_delete(pos, idx)
end
Expand All @@ -241,7 +242,7 @@ def delete_at(index)
#
# @return [Object] The last value in the array.
def pop
raise "Array is empty" if @size.zero?
return nil if @size.zero?

value = @lists.last.pop
if @lists.last.empty?
Expand Down
81 changes: 70 additions & 11 deletions spec/sorted_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,21 @@
expect(array.first).to eq(1)
end

it "should return nil if the array is empty" do
array = SortedContainers::SortedArray.new
expect(array.first).to be_nil
end

it "should return the last value in the array" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array.last).to eq(5)
end

it "should return nil if the array is empty" do
array = SortedContainers::SortedArray.new
expect(array.last).to be_nil
end

it "should return true if all elements meet a given criterion" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array.all? { |i| i > 0 }).to be true
Expand Down Expand Up @@ -121,17 +131,31 @@
expect(array[2]).to eq(3)
end

it "should raise an error if the index is out of range" do
it "should handle negative indices" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array[-1]).to eq(5)
end

it "should return nil if negative index is out of range" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect { array[5] }.to raise_error("Index out of range")
expect(array[-6]).to be_nil
end

it "should return nil if the index is out of range" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array[5]).to be_nil
end

# tests array[range] syntax
it "should return the values in the given range" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array[1..3]).to eq([2, 3, 4])
end

it "should return empty array if the range is out of range" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array[5..6]).to eq([])
end

it "should return the values in the given range excluding the last value" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array[1...3]).to eq([2, 3])
Expand All @@ -142,6 +166,11 @@
expect(array[1, 3]).to eq([2, 3, 4])
end

it "should return nil if the index is out of range" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array[5, 1]).to be_nil
end

it "should return values in the arithmetic sequence two dots" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array[(1..).step(2)].to_a).to eq([2, 4])
Expand Down Expand Up @@ -222,15 +251,40 @@
expect(array.to_a).to eq([1, 2])
end

it "should delete the value at the given index" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
array.delete_at(2)
expect(array.to_a).to eq([1, 2, 4, 5])
end
describe "delete_at" do
it "should delete the value at the given index" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
array.delete_at(2)
expect(array.to_a).to eq([1, 2, 4, 5])
end

it "should raise an error if the index is out of range" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect { array.delete_at(5) }.to raise_error(IndexError)
it "should return the value at the given index" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array.delete_at(2)).to eq(3)
end

it "should return nil if the index is out of range" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array.delete_at(5)).to be_nil
end

it "should work when array size is larger than load factor" do
array = SortedContainers::SortedArray.new((1..1000).to_a, load_factor: 3)
array.delete_at(500)
expect(array.size).to eq(999)
end

it "should update the index after deleting an element" do
array = SortedContainers::SortedArray.new((1..1000).to_a, load_factor: 3)
array.delete_at(50)
expect(array[50]).to eq(52)
end

it "should update the index after deleting an element from small array" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5], load_factor: 1)
array.delete_at(2)
expect(array[2]).to eq(4)
end
end

it "bisect_left should return the index where the value should be inserted" do
Expand Down Expand Up @@ -301,6 +355,11 @@
expect(array.to_a).to eq([1, 2, 3, 4])
end

it "should return nil if the array is empty" do
array = SortedContainers::SortedArray.new
expect(array.pop).to be_nil
end

it "should sort hundreds of values" do
array = SortedContainers::SortedArray.new
(1..1000).to_a.shuffle.each do |i|
Expand Down

0 comments on commit 1149d93

Please sign in to comment.