diff --git a/Rakefile b/Rakefile index bf5e06c..38d739f 100644 --- a/Rakefile +++ b/Rakefile @@ -1,10 +1,13 @@ require 'bundler/setup' require 'bundler/gem_tasks' require 'rake/testtask' +require 'rspec/core/rake_task' Rake::TestTask.new do |t| - t.test_files = FileList['spec/*.rb','test/*.rb'] + t.test_files = FileList['test/*.rb'] t.verbose = true end -task :default => :test +RSpec::Core::RakeTask.new(:spec) + +task :default => [ :test, :spec ] diff --git a/descriptive_statistics.gemspec b/descriptive_statistics.gemspec index 12d51d1..660948d 100644 --- a/descriptive_statistics.gemspec +++ b/descriptive_statistics.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'descriptive_statistics' - s.version = '2.0.0' + s.version = '2.0.1' s.homepage = 'https://github.com/thirtysixthspan/descriptive_statistics' s.summary = 'Descriptive Statistics' s.description = 'Adds descriptive statistics methods to Enumerable module for use on collections or Numeric data' diff --git a/lib/descriptive_statistics/percentile.rb b/lib/descriptive_statistics/percentile.rb index a69e01f..59e2fb4 100644 --- a/lib/descriptive_statistics/percentile.rb +++ b/lib/descriptive_statistics/percentile.rb @@ -3,6 +3,8 @@ def percentile(p) values = Support::convert(self) return unless values.size > 0 + return values.first unless values.size > 1 + sorted = values.sort return sorted[-1] if p == 100 rank = p / 100.0 * (values.number - 1) diff --git a/pkg/descriptive_statistics-2.0.0.gem b/pkg/descriptive_statistics-2.0.0.gem deleted file mode 100644 index 94c16ff..0000000 Binary files a/pkg/descriptive_statistics-2.0.0.gem and /dev/null differ diff --git a/pkg/descriptive_statistics-2.0.1.gem b/pkg/descriptive_statistics-2.0.1.gem new file mode 100644 index 0000000..6f7d305 Binary files /dev/null and b/pkg/descriptive_statistics-2.0.1.gem differ diff --git a/spec/single_value_spec.rb b/spec/single_value_spec.rb new file mode 100644 index 0000000..90bffdf --- /dev/null +++ b/spec/single_value_spec.rb @@ -0,0 +1,58 @@ +require 'rspec' + +describe "DescriptiveStatistics" do + require 'descriptive_statistics' + + subject { [2] } + + context "with a single value array" do + + it "calculates the number" do + expect(subject.number).to eql(1.0) + end + + it "calculates the sum" do + expect(subject.sum).to eql(2.0) + end + + it "calculates the mean" do + expect(subject.mean).to eql(2.0) + end + + it "calculates the median" do + expect(subject.median).to eql(2.0) + end + + it "calculates the variance" do + expect(subject.variance).to eql(0.0) + end + + it "calculates the standard_deviation" do + expect(subject.standard_deviation).to eql(0.0) + end + + it "calculates the percentile" do + expect(subject.percentile(30)).to eql(2.0) + expect(subject.percentile(50)).to eql(2.0) + expect(subject.percentile(70)).to eql(2.0) + end + + it "calculates the same value for the 50th percentile and median" do + expect(subject.percentile(50)).to eql(subject.median) + end + + it "calculates the mode" do + expect(subject.mode).to eql(2) + end + + it "calculates the range" do + expect(subject.range).to eql(0.0) + end + + it "calculates the percentile rank" do + expect(subject.percentile_rank(2)).to eql(100.0) + end + + end + +end \ No newline at end of file