spec/unit/test_aggregator.rb in alf-0.10.0 vs spec/unit/test_aggregator.rb in alf-0.10.1

- old
+ new

@@ -34,10 +34,25 @@ it "should behave correctly on max" do Aggregator.max{a}.aggregate(input).should == 3 end + it "should behave correctly on variance" do + vals = input.collect{|t| t[:a]} + mean = vals.inject(:+) / vals.size.to_f + exp = vals.collect{|v| (v - mean)**2 }.inject(:+) / vals.size.to_f + Aggregator.variance{a}.aggregate(input).should == exp + end + + it "should behave correctly on stddev" do + vals = input.collect{|t| t[:a]} + mean = vals.inject(:+) / vals.size.to_f + exp = vals.collect{|v| (v - mean)**2 }.inject(:+) / vals.size.to_f + exp = Math.sqrt(exp) + Aggregator.stddev{a}.aggregate(input).should == exp + end + it "should behave correctly on concat" do Aggregator.concat{a}.aggregate(input).should == "1231" Aggregator.concat(:between => " "){ a }.aggregate(input).should == "1 2 3 1" Aggregator.concat(:before => "[", :after => "]"){ a }.aggregate(input).should == "[1231]" end @@ -48,10 +63,32 @@ end it "should allow specific tuple computations" do Aggregator.sum{ 1.0 * a * sign }.aggregate(input).should == -3.0 end - + + describe Aggregator::Variance do + let(:values){ [1, 2, 3, 4, 5, 6] } + let(:stdev){ Aggregator::Variance.new } + specify{ + memo = values.inject(stdev.least){|memo,val| + stdev._happens(memo, val) + } + stdev.finalize(memo).should eq(17.5/6.0) + } + end + + describe Aggregator::Stddev do + let(:values){ [2, 4, 4, 4, 5, 5, 7, 9] } + let(:stdev){ Aggregator::Stddev.new } + specify{ + memo = values.inject(stdev.least){|memo,val| + stdev._happens(memo, val) + } + stdev.finalize(memo).should eq(2.0) + } + end + describe "coerce" do subject{ Aggregator.coerce(arg) } describe "from an Aggregator" do