Sha256: d026ae382e6f9843973b3b6cdf65af68d38e272ef4701f8a236c8e3b8b25ac19

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

#!/usr/bin/env ruby

require 'rubygems'
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../lib')
require 'zipfian'
require 'test-unit'

class TestZipfian < Test::Unit::TestCase
  def test_init
    assert_raise(ArgumentError) { z = Zipfian.new }

    [0.9, 0, -1].each do |n|
      assert_raise(ArgumentError) { z = Zipfian.new n, 1 }
    end

    assert_raise(ArgumentError) { z = Zipfian.new 100, 0 }
    assert_raise(ArgumentError) { z = Zipfian.new 100, -1 }
  end

  def test_s_n
    z = Zipfian.new 1000, 0.1
    assert_equal 1000, z.n
    assert_equal 0.1, z.s
  end

  def test_invalid_rank
    z = Zipfian.new 1000, 0.1

    [0.1, 0, 2000].each do |k|
      assert_raise(ArgumentError) { z.pmf k }
      assert_raise(ArgumentError) { z.cdf k }
    end
  end

  def test_pmf_cdf
    z = Zipfian.new 1000, 1

    assert_equal z.pmf(1), z.cdf(1)
    (2..1000).each do |i|
      assert z.pmf(i) < z.cdf(i)
      assert z.pmf(i) < z.pmf(i - 1)
    end
    assert_equal 1, z.cdf(1000)
  end

  def test_sample
    z = Zipfian.new 100, 1

    10000.times do |i|
      assert (1..100).include?(z.sample)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zipfian-0.0.2 test/test_zipfian.rb