Sha256: 6ad86b71e12f729bcf26e00beb89b1c9aa40659b2cbeb997931f8a3c8b2785da

Contents?: true

Size: 983 Bytes

Versions: 4

Compression:

Stored size: 983 Bytes

Contents

require 'spec_helper'

describe Array do
  describe '.weighted_sample' do
    before do
      Kernel.stub(:rand) { 0.5 }
    end

    context "Stub test" do
      subject { Kernel.rand }
      it { should eq 0.5 }
    end

    let(:array) { [1, 2, 3, 4] }
    subject { array.weighted_sample }

    it { should eq 3 }

    context "different random" do
      before do
        Kernel.stub(:rand) { 0 }
      end

      it { should eq 1 }
    end

    context "different random" do
      before do
        Kernel.stub(:rand) { 1 }
      end

      it { should eq 4 }
    end

    context "with weights" do
      subject { array.weighted_sample([1, 0, 0, 0]) }
      it { should eq 1 }
    end
    
    context "all the same weights" do
      before { Kernel.stub(:rand) { 1 } }
      subject { array.weighted_sample([0, 0, 0, 0]) }
      it { should eq 4 }
      context "random 0" do
        before { Kernel.stub(:rand) { 0 } }
        it { should eq 1 }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ab_panel-0.3.3 spec/array_spec.rb
ab_panel-0.3.2 spec/array_spec.rb
ab_panel-0.3.1 spec/array_spec.rb
ab_panel-0.3.0 spec/array_spec.rb