Skip to content

Commit

Permalink
Removed deprecated Vector#set usage in docs and specs
Browse files Browse the repository at this point in the history
Following the changes in 5bc4737, Vector#put is now used in all the
examples and specs code (older set_spec.rb was renamed to put_spec.rb
and all .set calls were modified to .put calls).

A short spec for Vector#set is in place
(spec/lib/hamster/vector/set_spec.rb) until the method is completely
removed.
  • Loading branch information
dubek committed Oct 26, 2015
1 parent 5bc4737 commit e7ad277
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 164 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ A `Vector` is an integer-indexed collection much like an immutable `Array`. Exam
vector = Hamster::Vector[1, 2, 3, 4] # => Hamster::Vector[1, 2, 3, 4]
vector[0] # => 1
vector[-1] # => 4
vector.set(1, :a) # => Hamster::Vector[1, :a, 3, 4]
vector.put(1, :a) # => Hamster::Vector[1, :a, 3, 4]
vector.add(:b) # => Hamster::Vector[1, 2, 3, 4, :b]
vector.insert(2, :a, :b) # => Hamster::Vector[1, 2, :a, :b, 3, 4]
vector.delete_at(0) # => Hamster::Vector[2, 3, 4]
Expand Down
2 changes: 1 addition & 1 deletion YARD-README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ A `Vector` is an integer-indexed collection much like an immutable `Array`. Exam
vector = Hamster::Vector[1, 2, 3, 4] # => Hamster::Vector[1, 2, 3, 4]
vector[0] # => 1
vector[-1] # => 4
vector.set(1, :a) # => Hamster::Vector[1, :a, 3, 4]
vector.put(1, :a) # => Hamster::Vector[1, :a, 3, 4]
vector.add(:b) # => Hamster::Vector[1, 2, 3, 4, :b]
vector.insert(2, :a, :b) # => Hamster::Vector[1, 2, :a, :b, 3, 4]
vector.delete_at(0) # => Hamster::Vector[2, 3, 4]
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/hamster/vector/get_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
if rand(2) == 0
value, index = rand(10000), rand(size)
array[index] = value
vector = vector.set(index, value)
vector = vector.put(index, value)
else
index = rand(array.size)
array.delete_at(index)
Expand Down
175 changes: 175 additions & 0 deletions spec/lib/hamster/vector/put_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
require "spec_helper"
require "hamster/vector"

describe Hamster::Vector do
let(:vector) { V[*values] }

describe "#put" do
context "when empty" do
let(:vector) { V.empty }

it "raises an error for index -1" do
expect { vector.put(-1, :a) }.to raise_error
end

it "allows indexes 0 and 1 to be put" do
vector.put(0, :a).should eql(V[:a])
vector.put(1, :a).should eql(V[nil, :a])
end
end

context "when not empty" do
let(:vector) { V["A", "B", "C"] }

context "with a block" do
context "and a positive index" do
context "within the absolute bounds of the vector" do
it "passes the current value to the block" do
vector.put(1) { |value| value.should == "B" }
end

it "replaces the value with the result of the block" do
result = vector.put(1) { |value| "FLIBBLE" }
result.should eql(V["A", "FLIBBLE", "C"])
end

it "supports to_proc methods" do
result = vector.put(1, &:downcase)
result.should eql(V["A", "b", "C"])
end
end

context "just past the end of the vector" do
it "passes nil to the block and adds a new value" do
result = vector.put(3) { |value| value.should be_nil; "D" }
result.should eql(V["A", "B", "C", "D"])
end
end

context "further outside the bounds of the vector" do
it "passes nil to the block, fills up missing nils, and adds a new value" do
result = vector.put(5) { |value| value.should be_nil; "D" }
result.should eql(V["A", "B", "C", nil, nil, "D"])
end
end
end

context "and a negative index" do
context "within the absolute bounds of the vector" do
it "passes the current value to the block" do
vector.put(-2) { |value| value.should == "B" }
end

it "replaces the value with the result of the block" do
result = vector.put(-2) { |value| "FLIBBLE" }
result.should eql(V["A", "FLIBBLE", "C"])
end

it "supports to_proc methods" do
result = vector.put(-2, &:downcase)
result.should eql(V["A", "b", "C"])
end
end

context "outside the absolute bounds of the vector" do
it "raises an error" do
expect { vector.put(-vector.size.next) {} }.to raise_error
end
end
end
end

context "with a value" do
context "and a positive index" do
context "within the absolute bounds of the vector" do
let(:put) { vector.put(1, "FLIBBLE") }

it "preserves the original" do
vector.should eql(V["A", "B", "C"])
end

it "puts the new value at the specified index" do
put.should eql(V["A", "FLIBBLE", "C"])
end
end

context "just past the end of the vector" do
it "adds a new value" do
result = vector.put(3, "FLIBBLE")
result.should eql(V["A", "B", "C", "FLIBBLE"])
end
end

context "outside the absolute bounds of the vector" do
it "fills up with nils" do
result = vector.put(5, "FLIBBLE")
result.should eql(V["A", "B", "C", nil, nil, "FLIBBLE"])
end
end
end

context "with a negative index" do
let(:put) { vector.put(-2, "FLIBBLE") }

it "preserves the original" do
put
vector.should eql(V["A", "B", "C"])
end

it "puts the new value at the specified index" do
put.should eql(V["A", "FLIBBLE", "C"])
end
end

context "outside the absolute bounds of the vector" do
it "raises an error" do
expect { vector.put(-vector.size.next, "FLIBBLE") }.to raise_error
end
end
end
end

context "from a subclass" do
it "returns an instance of the subclass" do
subclass = Class.new(Hamster::Vector)
instance = subclass[1,2,3]
instance.put(1, 2.5).class.should be(subclass)
end
end

[10, 31, 32, 33, 1000, 1023, 1024, 1025, 2000].each do |size|
context "on a #{size}-item vector" do
it "works correctly" do
array = (1..size).to_a
vector = V.new(array)

[0, 1, 10, 31, 32, 33, 100, 500, 1000, 1023, 1024, 1025, 1998, 1999].select { |n| n < size }.each do |i|
value = rand(10000)
array[i] = value
vector = vector.put(i, value)
vector[i].should be(value)
end

0.upto(size-1) do |i|
vector.get(i).should == array[i]
end
end
end
end

context "with an identical value to an existing item" do
[1, 2, 5, 31,32, 33, 100, 200].each do |size|
context "on a #{size}-item vector" do
let(:array) { (0...size).map { |x| x * x} }
let(:vector) { V.new(array) }

it "returns self" do
(0...size).each do |index|
vector.put(index, index * index).should equal(vector)
end
end
end
end
end
end
end
173 changes: 12 additions & 161 deletions spec/lib/hamster/vector/set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,173 +2,24 @@
require "hamster/vector"

describe Hamster::Vector do
let(:vector) { V[*values] }

describe "#set" do
context "when empty" do
let(:vector) { V.empty }

it "raises an error for index -1" do
expect { vector.set(-1, :a) }.to raise_error
end

it "allows indexes 0 and 1 to be set" do
vector.set(0, :a).should eql(V[:a])
vector.set(1, :a).should eql(V[nil, :a])
end
end

context "when not empty" do
let(:vector) { V["A", "B", "C"] }

context "with a block" do
context "and a positive index" do
context "within the absolute bounds of the vector" do
it "passes the current value to the block" do
vector.set(1) { |value| value.should == "B" }
end

it "replaces the value with the result of the block" do
result = vector.set(1) { |value| "FLIBBLE" }
result.should eql(V["A", "FLIBBLE", "C"])
end

it "supports to_proc methods" do
result = vector.set(1, &:downcase)
result.should eql(V["A", "b", "C"])
end
end

context "just past the end of the vector" do
it "passes nil to the block and adds a new value" do
result = vector.set(3) { |value| value.should be_nil; "D" }
result.should eql(V["A", "B", "C", "D"])
end
end

context "further outside the bounds of the vector" do
it "passes nil to the block, fills up missing nils, and adds a new value" do
result = vector.set(5) { |value| value.should be_nil; "D" }
result.should eql(V["A", "B", "C", nil, nil, "D"])
end
end
end

context "and a negative index" do
context "within the absolute bounds of the vector" do
it "passes the current value to the block" do
vector.set(-2) { |value| value.should == "B" }
end

it "replaces the value with the result of the block" do
result = vector.set(-2) { |value| "FLIBBLE" }
result.should eql(V["A", "FLIBBLE", "C"])
end

it "supports to_proc methods" do
result = vector.set(-2, &:downcase)
result.should eql(V["A", "b", "C"])
end
end

context "outside the absolute bounds of the vector" do
it "raises an error" do
expect { vector.set(-vector.size.next) {} }.to raise_error
end
end
end
end
# Note: Vector#set will be deprecated; use Vector#put instead. See
# `spec/lib/hamster/vector/put_spec.rb` for the full specs of Vector#put.

context "with a value" do
context "and a positive index" do
context "within the absolute bounds of the vector" do
let(:set) { vector.set(1, "FLIBBLE") }

it "preserves the original" do
vector.should eql(V["A", "B", "C"])
end

it "sets the new value at the specified index" do
set.should eql(V["A", "FLIBBLE", "C"])
end
end

context "just past the end of the vector" do
it "adds a new value" do
result = vector.set(3, "FLIBBLE")
result.should eql(V["A", "B", "C", "FLIBBLE"])
end
end

context "outside the absolute bounds of the vector" do
it "fills up with nils" do
result = vector.set(5, "FLIBBLE")
result.should eql(V["A", "B", "C", nil, nil, "FLIBBLE"])
end
end
end

context "with a negative index" do
let(:set) { vector.set(-2, "FLIBBLE") }

it "preserves the original" do
set
vector.should eql(V["A", "B", "C"])
end

it "sets the new value at the specified index" do
set.should eql(V["A", "FLIBBLE", "C"])
end
end

context "outside the absolute bounds of the vector" do
it "raises an error" do
expect { vector.set(-vector.size.next, "FLIBBLE") }.to raise_error
end
end
end
end

context "from a subclass" do
it "returns an instance of the subclass" do
subclass = Class.new(Hamster::Vector)
instance = subclass[1,2,3]
instance.set(1, 2.5).class.should be(subclass)
end
end

[10, 31, 32, 33, 1000, 1023, 1024, 1025, 2000].each do |size|
context "on a #{size}-item vector" do
it "works correctly" do
array = (1..size).to_a
vector = V.new(array)

[0, 1, 10, 31, 32, 33, 100, 500, 1000, 1023, 1024, 1025, 1998, 1999].select { |n| n < size }.each do |i|
value = rand(10000)
array[i] = value
vector = vector.set(i, value)
vector[i].should be(value)
end
describe "#set" do
let(:vector) { V[5, 6, 7] }

0.upto(size-1) do |i|
vector.get(i).should == array[i]
end
end
context "without block" do
it "replaces the element" do
result = vector.set(1, 100)
result.should eql(V[5, 100, 7])
end
end

context "with an identical value to an existing item" do
[1, 2, 5, 31,32, 33, 100, 200].each do |size|
context "on a #{size}-item vector" do
let(:array) { (0...size).map { |x| x * x} }
let(:vector) { V.new(array) }

it "returns self" do
(0...size).each do |index|
vector.set(index, index * index).should equal(vector)
end
end
end
context "with block" do
it "passes the existing element to the block and replaces the result" do
result = vector.set(1) { |e| e + 100 }
result.should eql(V[5, 106, 7])
end
end
end
Expand Down

0 comments on commit e7ad277

Please sign in to comment.