spec/array_spec.rb in darkhelmet-darkext-0.11.2 vs spec/array_spec.rb in darkhelmet-darkext-0.12.0

- old
+ new

@@ -1,10 +1,10 @@ require File.dirname(__FILE__) + '/spec_helper' describe Array do before(:each) do - @a = [1,2,3,4,5,6,7,8,9,10] + @a = (1..5).to_a end it 'should respond to all the new methods' do %w(rotate rotate_reverse sum product squares squares! random pick randomize randomize!).each { |method| [1].methods.include?(method).should == true } end @@ -40,24 +40,73 @@ b = @a.clone b.squares!.should == @a.squares end it 'should pick randomly' do - counts = [0,0,0,0,0,0,0,0,0,0] + counts = [0,0,0,0,0] count = 1000000 count.times do r = @a.random counts[r - 1] += 1 end - counts.each { |v| (v/count.to_f).should be_close(1/counts.size.to_f,0.001) } + counts.each do |v| + (v/count.to_f).should be_close(1/counts.size.to_f,0.001) + end + + @a.include?(@a.random).should be_true end it 'should return an Array from randomize' do @a.randomize.should be_a_kind_of(Array) end it 'should return nil from sum, product and random if size == 0' do %w(sum product random).each do |method| Array.new.send(method.intern).should be_nil end + end + + it 'should rotate properly' do + @a.rotate + @a.should == [2,3,4,5,1] + @a.rotate + @a.should == [3,4,5,1,2] + @a.rotate(@a.size) + @a.should == [3,4,5,1,2] + end + + it 'should rotate reverse properly' do + @a.rotate_reverse + @a.should == [5,1,2,3,4] + @a.rotate_reverse + @a.should == [4,5,1,2,3] + @a.rotate_reverse(@a.size) + @a.should == [4,5,1,2,3] + end + + it 'should sum properly' do + @a.sum.should == 15 + [1].sum.should == 1 + %w(a b c).sum.should == 'abc' + [1.0,0.1,0.01].sum.should == 1.11 + end + + it 'should do product properly' do + @a.product.should == 120 + [1].product.should == 1 + ['a',3].product.should == 'aaa' + end + + it 'should do squares properly' do + @a.squares.should == [1,4,9,16,25] + [1].squares.should == [1] + @a.squares! + @a.should == [1,4,9,16,25] + end + + it 'should randomize' do + @a.randomize.should_not == @a + a = @a.clone + @a.randomize! + @a.should_not == a end end