Sha256: 94aae261b9fd9ef595e6f7c9baf9b927dcb51a50594816afcadac54079b5079a

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

require File.dirname(__FILE__) + '/spec_helper'

describe Array do
  before(:each) do
    @a = [1,2,3,4,5,6,7,8,9,10]
  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

  it 'should not remove or add elements when randomizing' do
    @a.randomize.size.should == @a.size
  end

  it 'should rotate back to normal' do
    b = @a.clone
    b.rotate(b.size)
    b.should == @a
  end

  it 'should rotate reverse back to normal' do
    b = @a.clone
    b.rotate_reverse(b.size)
    b.should == @a
  end

  it 'should return a Numeric from sum and sum' do
    @a.sum.should be_a_kind_of(Numeric)
    @a.product.should be_a_kind_of(Numeric)
  end

  it 'should return an array of equal size from squares' do
    squares = @a.squares
    squares.should be_a_kind_of(Array)
    squares.size.should == @a.size
  end

  it 'should destructively collect squares' do
    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]
    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) }
  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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
darkhelmet-darkext-0.11.1 spec/array_spec.rb
darkhelmet-darkext-0.11.2 spec/array_spec.rb