1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
require 'spec_helper'
describe Immutable::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
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(Immutable::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
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.set(index, index * index).should equal(vector)
end
end
end
end
end
end
end
|