Skip to content

Commit

Permalink
fix: bound_mean_cumn returns correct value if only one present
Browse files Browse the repository at this point in the history
  • Loading branch information
wallin committed Nov 22, 2015
1 parent 396c2b9 commit 2986018
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
7 changes: 4 additions & 3 deletions lib/tdigest/tdigest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

module TDigest
class TDigest
attr_accessor :centroids
def initialize(delta = 0.01, k = 25, cx = 1.1)
@delta = delta
@k = k
Expand Down Expand Up @@ -33,10 +34,10 @@ def bound_mean_cumn(cumn)
last_c = v
end
end
lower = bounds[0]
upper = bounds[1]
# If still no results, pick lagging value if any
bounds << last_c if bounds.empty? && !last_c.nil?

[lower, upper]
bounds
end

def compress!
Expand Down
19 changes: 13 additions & 6 deletions test/tdigest_test.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
require 'test_helper'
require 'benchmark'

class TDigestTest < Minitest::Test
def test_that_it_has_a_version_number
refute_nil ::TDigest::VERSION
end

def test_percentile_edge_case
tdigest = ::TDigest::TDigest.new
tdigest.push(60, 100)
pct = tdigest.percentile(0.90) # This should not crash
assert_equal nil, pct
describe '#percentile' do
it 'returns nil if empty' do
tdigest = ::TDigest::TDigest.new
tdigest.percentile(0.90).must_be_nil # This should not crash
end

describe 'with only signle value' do
it 'returns the value' do
tdigest = ::TDigest::TDigest.new
tdigest.push(60, 100)
tdigest.percentile(0.90).must_equal 60
end
end
end
end

0 comments on commit 2986018

Please sign in to comment.