test/BigArray_spec.rb in perobs-4.1.0 vs test/BigArray_spec.rb in perobs-4.2.0

- old
+ new

@@ -30,11 +30,11 @@ require 'spec_helper' require 'perobs' require 'perobs/BigArray' -NODE_ENTRIES = 4 +NODE_ENTRIES = 6 describe PEROBS::BigArray do before(:all) do @db_name = generate_db_name(__FILE__) @@ -57,26 +57,32 @@ it 'should be empty on create' do expect(@a.empty?).to be true expect(@a.length).to eq(0) expect(@a.check).to be true expect(@a[0]).to be nil + expect(@a.first).to be nil + expect(@a.last).to be nil end it 'should append the first element' do @a << 0 expect(@a.empty?).to be false expect(@a[0]).to eq(0) + expect(@a.first).to eq(0) + expect(@a.last).to eq(0) expect(@a.length).to eq(1) expect(@a.check).to be true end it 'should fill 10 nodes with appends' do (10 * NODE_ENTRIES).times do |i| @a << i expect(@a.check).to be true expect(@a.length).to eq(i + 1) end + expect(@a.first).to eq(0) + expect(@a.last).to eq(10 * NODE_ENTRIES - 1) end it 'should insert at 0' do @a.insert(0, 0) expect(@a.empty?).to be false @@ -109,11 +115,11 @@ expect(@a.to_a).to eql((0..3 * NODE_ENTRIES - 1).to_a) end it 'should support the [] operator' do expect(@a[0]).to be nil - expect{@a[-1]}.to raise_error(IndexError) + expect(@a[-1]).to be nil @a[0] = 0 expect(@a[0]).to eq(0) 1.upto(3 * NODE_ENTRIES) do |i| @a.insert(i, i) end @@ -122,11 +128,11 @@ end expect(@a[3 * NODE_ENTRIES + 1]).to be nil 0.upto(3 * NODE_ENTRIES) do |i| expect(@a[-3 * NODE_ENTRIES + i - 1]).to eq(i) end - expect{@a[-3 * NODE_ENTRIES - 2]}.to raise_error(IndexError) + expect(@a[-3 * NODE_ENTRIES - 2]).to be nil (3 * NODE_ENTRIES + 1).upto(4 * NODE_ENTRIES) do |i| expect(@a[i]).to be nil end end @@ -140,11 +146,11 @@ @a << 0 expect(@a.delete_at(0)).to eql(0) expect(@a.length).to eq(0) expect(@a.check).to be true - n = 3 * NODE_ENTRIES + n = 5 * NODE_ENTRIES 0.upto(n) { |i| @a.insert(i, i) } 0.upto(n) do |i| expect(@a.delete_at(0)).to eql(i) expect(@a.check).to be true end @@ -153,24 +159,56 @@ n.downto(0) do |i| expect(@a.delete_at(-1)).to eql(i) expect(@a.check).to be true end - n = 11 * NODE_ENTRIES + n = 15 * NODE_ENTRIES 0.upto(n - 1) { |i| @a.insert(i, i) } expect(@a.delete_at(n + 2)).to be nil expect(@a.delete_at(-(n + 2))).to be nil expect(@a.size).to eql(n) n.times do |i| - @a.delete_at(rand(@a.size)) + idx = rand(@a.size) + @a.delete_at(idx) expect(@a.size).to be (n - 1 - i) expect(@a.check).to be true end expect(@a.size).to eql(0) end + it 'should fill the gaps' do + 1.upto(4) do |i| + idx = i * NODE_ENTRIES * NODE_ENTRIES + @a[idx] = idx + expect(@a[idx - 1]).to be nil + expect(@a[idx + 1]).to be nil + expect(@a.check).to be true + end + expect(@a[0]).to be nil + end + + it 'should insert after a gap' do + ref = [] + 10.times do |i| + idx = 10 + i * 3 + @a[idx] = idx + ref[idx] = idx + expect(@a[idx]).to eql(idx) + expect(@a.check).to be true + end + 10.times do |i| + idx = i * 5 + @a[idx] = idx + ref[idx] = idx + expect(@a[idx]).to eql(idx) + expect(@a.check).to be true + end + expect(@a.check).to be true + expect(@a.to_a).to eql(ref) + end + it 'should iterate over all values' do n = 3 * NODE_ENTRIES 0.upto(n) { |i| @a.insert(i, i) } i = 0 @@ -187,9 +225,18 @@ i = 0 @a.reverse_each do |v| expect(v).to eql(n - i) i += 1 end + end + + it 'should insert at the beginning' do + (5 * NODE_ENTRIES).downto(0) do |i| + @a.insert(0, i) + end + expect(@a.check).to be true + a = Array.new(5 * NODE_ENTRIES + 1) { |i| i } + expect(@a.to_a).to eq(a) end it 'should persist the data' do db_name = generate_db_name(__FILE__ + "_persist") store = PEROBS::Store.new(db_name)