spec/die_spec.rb in games_dice-0.0.3 vs spec/die_spec.rb in games_dice-0.0.5

- old
+ new

@@ -22,12 +22,10 @@ it "should return an object that represents e.g. a six-sided die" do die = GamesDice::Die.new(6) die.min.should == 1 die.max.should == 6 die.sides.should == 6 - die.probabilities.should == { 1 => 1.0/6, 2 => 1.0/6, 3 => 1.0/6, 4 => 1.0/6, 5 => 1.0/6, 6 => 1.0/6 } - die.expected_result.should == 3.5 end it "should accept any object with a rand(Integer) method as the second param" do prng = TestPRNG.new() die = GamesDice::Die.new(20,prng) @@ -54,42 +52,40 @@ die.min.should == 1 die.max.should == 20 end end - describe "#expected_result" do - it "should calculate correct weighted mean" do - die = GamesDice::Die.new(20) - die.expected_result.should be_within(1e-10).of 10.5 - end - end - describe "#probabilities" do - it "should calculate probabilities accurately" do + it "should return the die's probability distribution as a GamesDice::Probabilities object" do die = GamesDice::Die.new(6) - die.probabilities[1].should be_within(1e-10).of 1/6.0 - die.probabilities[2].should be_within(1e-10).of 1/6.0 - die.probabilities[3].should be_within(1e-10).of 1/6.0 - die.probabilities[4].should be_within(1e-10).of 1/6.0 - die.probabilities[5].should be_within(1e-10).of 1/6.0 - die.probabilities[6].should be_within(1e-10).of 1/6.0 - die.probabilities.values.inject(:+).should be_within(1e-9).of 1.0 + die.probabilities.is_a?( GamesDice::Probabilities ).should be_true + probs = die.probabilities - die.probability_gt(6).should == 0.0 - die.probability_gt(-20).should == 1.0 - die.probability_gt(4).should be_within(1e-10).of 2/6.0 + probs.p_eql(1).should be_within(1e-10).of 1/6.0 + probs.p_eql(2).should be_within(1e-10).of 1/6.0 + probs.p_eql(3).should be_within(1e-10).of 1/6.0 + probs.p_eql(4).should be_within(1e-10).of 1/6.0 + probs.p_eql(5).should be_within(1e-10).of 1/6.0 + probs.p_eql(6).should be_within(1e-10).of 1/6.0 + probs.to_h.values.inject(:+).should be_within(1e-9).of 1.0 - die.probability_ge(20).should == 0.0 - die.probability_ge(1).should == 1.0 - die.probability_ge(4).should be_within(1e-10).of 0.5 + probs.p_gt(6).should == 0.0 + probs.p_gt(-20).should == 1.0 + probs.p_gt(4).should be_within(1e-10).of 2/6.0 - die.probability_le(6).should == 1.0 - die.probability_le(-3).should == 0.0 - die.probability_le(5).should be_within(1e-10).of 5/6.0 + probs.p_ge(20).should == 0.0 + probs.p_ge(1).should == 1.0 + probs.p_ge(4).should be_within(1e-10).of 0.5 - die.probability_lt(7).should == 1.0 - die.probability_lt(1).should == 0.0 - die.probability_lt(3).should be_within(1e-10).of 2/6.0 + probs.p_le(6).should == 1.0 + probs.p_le(-3).should == 0.0 + probs.p_le(5).should be_within(1e-10).of 5/6.0 + + probs.p_lt(7).should == 1.0 + probs.p_lt(1).should == 0.0 + probs.p_lt(3).should be_within(1e-10).of 2/6.0 + + probs.expected.should be_within(1e-10).of 3.5 end end end \ No newline at end of file