From 47aa9c301a44eef0d46778d9d22ad7fdf8611234 Mon Sep 17 00:00:00 2001 From: Stormwind Date: Tue, 20 Oct 2015 12:55:52 +0200 Subject: [PATCH] Add tests for Rubella::Weighting::PerCount --- spec/spec_helper.rb | 1 + .../weighting/per_count/buckets_eql_spec.rb | 22 ++++++++++ .../rubella/weighting/per_count/new_spec.rb | 22 ++++++++++ .../rubella/weighting/per_count/parse_spec.rb | 43 +++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 spec/unit/rubella/weighting/per_count/buckets_eql_spec.rb create mode 100644 spec/unit/rubella/weighting/per_count/new_spec.rb create mode 100644 spec/unit/rubella/weighting/per_count/parse_spec.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3dcef4d..b580b19 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,3 +11,4 @@ require 'rubella/storage' require 'rubella/input/base' require 'rubella/input/json' require 'rubella/weighting/base' +require 'rubella/weighting/per_count' diff --git a/spec/unit/rubella/weighting/per_count/buckets_eql_spec.rb b/spec/unit/rubella/weighting/per_count/buckets_eql_spec.rb new file mode 100644 index 0000000..a030813 --- /dev/null +++ b/spec/unit/rubella/weighting/per_count/buckets_eql_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe Rubella::Weighting::PerCount, '.buckets=' do + + it "uses the given value" do + weighting = Rubella::Weighting::PerCount.new + + weighting.buckets = 5 + + expect(weighting.buckets).to eq(5) + end + + it "accepts every given value" do + weighting = Rubella::Weighting::PerCount.new + + amount = rand(100) + weighting.buckets = amount + + expect(weighting.buckets).to eq(amount) + end + +end diff --git a/spec/unit/rubella/weighting/per_count/new_spec.rb b/spec/unit/rubella/weighting/per_count/new_spec.rb new file mode 100644 index 0000000..b4ef32a --- /dev/null +++ b/spec/unit/rubella/weighting/per_count/new_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe Rubella::Weighting::PerCount, '.new' do + + it "creates a new Weighting instance" do + weighting = Rubella::Weighting::PerCount.new + + expect(weighting).to be_instance_of(Rubella::Weighting::PerCount) + end + + it "uses 7 buckets by default" do + weighting = Rubella::Weighting::PerCount.new + + expect(weighting.buckets).to eq(7) + end + + it "uses the given number of buckets" do + weighting = Rubella::Weighting::PerCount.new 5 + + expect(weighting.buckets).to eq(5) + end +end diff --git a/spec/unit/rubella/weighting/per_count/parse_spec.rb b/spec/unit/rubella/weighting/per_count/parse_spec.rb new file mode 100644 index 0000000..50be0ee --- /dev/null +++ b/spec/unit/rubella/weighting/per_count/parse_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' + +describe Rubella::Weighting::PerCount, '.parse' do + + it "returns a Rubella::Storage object" do + input = [] + # input = Rubella::Input::Base.new [] # Does not work atm FIX IT! + weighting = Rubella::Weighting::PerCount.new + + expect(weighting.parse(input)).to be_instance_of(Rubella::Storage) + end + + it "splits data in given amount of buckets" do + input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] + weighting = Rubella::Weighting::PerCount.new 7 + + storage = weighting.parse(input) + + expect(storage.length).to eq(2) + expect(storage.dataset_length).to eq(7) + end + + it "gives maximum intensity (1) to the maximum value" do + input = [1, 2, 1, 3, 1, 4, 10, 2, 1, 2, 3, 4, 2, 1] + weighting = Rubella::Weighting::PerCount.new 7 + + storage = weighting.parse(input) + + expect(storage.data[0][6]).to eq(1.0) + end + + it "computes the intensity on the maximum value" do + input = [1, 2, 1, 3, 1, 4, 5, 2, 1, 2, 3, 4, 2, 1] + weighting = Rubella::Weighting::PerCount.new 7 + + storage = weighting.parse(input) + + expect(storage.data).to eq( + [[0.2, 0.4, 0.2, 0.6, 0.2, 0.8, 1.0], [0.4, 0.2, 0.4, 0.6, 0.8, 0.4, 0.2]] + ) + end + +end \ No newline at end of file