Skip to content

Commit

Permalink
update rspec to use expect syntax exclusively
Browse files Browse the repository at this point in the history
  • Loading branch information
sujithvm committed Jun 24, 2016
1 parent 804b843 commit 2b84dd7
Show file tree
Hide file tree
Showing 18 changed files with 250 additions and 252 deletions.
26 changes: 13 additions & 13 deletions spec/beta_spec.rb
Expand Up @@ -8,7 +8,7 @@
a = rand * x
b = 1 + rand * 5
g = GSL::Ran.beta_pdf(x, a, b)
@engine.pdf(x, a, b).should be_within(1e-09).of(g)
expect(@engine.pdf(x, a, b)).to be_within(1e-09).of(g)
end
else
skip("No #{@engine}.pdf")
Expand All @@ -19,17 +19,17 @@
if @engine.respond_to? :cdf
# From GSL-1.9.
tol = 1_048_576.0 * Float::EPSILON
@engine.cdf(0.0, 1.2, 1.3).should eq(0.0)
@engine.cdf(1e-100, 1.2, 1.3).should be_within(tol).of(1.34434944656489596e-120)
@engine.cdf(0.001, 1.2, 1.3).should be_within(tol).of(3.37630042504535813e-4)
@engine.cdf(0.01, 1.2, 1.3).should be_within(tol).of(5.34317264038929473e-3)
@engine.cdf(0.1, 1.2, 1.3).should be_within(tol).of(8.33997828306748346e-2)
@engine.cdf(0.325, 1.2, 1.3).should be_within(tol).of(3.28698654180583916e-1)
@engine.cdf(0.5, 1.2, 1.3).should be_within(tol).of(5.29781429451299081e-1)
@engine.cdf(0.9, 1.2, 1.3).should be_within(tol).of(9.38529397224430659e-1)
@engine.cdf(0.99, 1.2, 1.3).should be_within(tol).of(9.96886438341254380e-1)
@engine.cdf(0.999, 1.2, 1.3).should be_within(tol).of(9.99843792833067634e-1)
@engine.cdf(1.0, 1.2, 1.3).should be_within(tol).of(1.0)
expect(@engine.cdf(0.0, 1.2, 1.3)).to eq(0.0)
expect(@engine.cdf(1e-100, 1.2, 1.3)).to be_within(tol).of(1.34434944656489596e-120)
expect(@engine.cdf(0.001, 1.2, 1.3)).to be_within(tol).of(3.37630042504535813e-4)
expect(@engine.cdf(0.01, 1.2, 1.3)).to be_within(tol).of(5.34317264038929473e-3)
expect(@engine.cdf(0.1, 1.2, 1.3)).to be_within(tol).of(8.33997828306748346e-2)
expect(@engine.cdf(0.325, 1.2, 1.3)).to be_within(tol).of(3.28698654180583916e-1)
expect(@engine.cdf(0.5, 1.2, 1.3)).to be_within(tol).of(5.29781429451299081e-1)
expect(@engine.cdf(0.9, 1.2, 1.3)).to be_within(tol).of(9.38529397224430659e-1)
expect(@engine.cdf(0.99, 1.2, 1.3)).to be_within(tol).of(9.96886438341254380e-1)
expect(@engine.cdf(0.999, 1.2, 1.3)).to be_within(tol).of(9.99843792833067634e-1)
expect(@engine.cdf(1.0, 1.2, 1.3)).to be_within(tol).of(1.0)
else
skip("No #{@engine}.cdf")
end
Expand All @@ -40,7 +40,7 @@
a = rand * x
b = 1 + rand * 5
pr = @engine.cdf(x / 100.0, a, b)
@engine.p_value(pr, a, b).should be_within(1e-09).of(x / 100.0)
expect(@engine.p_value(pr, a, b)).to be_within(1e-09).of(x / 100.0)
end
else
skip("No #{@engine}.p_value")
Expand Down
18 changes: 9 additions & 9 deletions spec/binomial_spec.rb
Expand Up @@ -9,7 +9,7 @@
[0, 1, n / 2, n - 1].each do |x|
exp = Math.binomial_coefficient(n, x) * pr**x * (1 - pr)**(n - x)
obs = @engine.pdf(x, n, pr)
obs.should be_within(1e-5).of(exp), "For pdf(#{x},#{n},#{pr}) expected #{exp}, obtained #{obs}"
expect(obs).to be_within(1e-5).of(exp), "For pdf(#{x},#{n},#{pr}) expected #{exp}, obtained #{obs}"
end
end
end
Expand All @@ -25,7 +25,7 @@
[1, n / 2, n - 1].each do |x|
exp = GSL::Cdf.binomial_P(x, pr, n)
obs = @engine.cdf(x, n, pr)
exp.should be_within(1e-5).of(obs), "For cdf(#{x},#{n},#{pr}) expected #{exp}, obtained #{obs}"
expect(exp).to be_within(1e-5).of(obs), "For cdf(#{x},#{n},#{pr}) expected #{exp}, obtained #{obs}"
end
end
end
Expand All @@ -42,24 +42,24 @@

it_should_behave_like 'binomial engine'

it { @engine.should respond_to(:exact_pdf) }
it { expect(@engine).to respond_to(:exact_pdf) }

it {
pending('No exact_p_value')
@engine.should respond_to(:exact_p_value)
expect(@engine).to respond_to(:exact_p_value)
}

it 'exact_cdf should return same values as cdf for n=50' do
pr = rand * 0.8 + 0.1
n = rand(10) + 10
[1, (n / 2).to_i, n - 1].each do |k|
@engine.exact_cdf(k, n, pr).should be_within(1e-10).of(@engine.cdf(k, n, pr))
expect(@engine.exact_cdf(k, n, pr)).to be_within(1e-10).of(@engine.cdf(k, n, pr))
end
end

it 'exact_pdf should not return a Float if not float is used as parameter' do
@engine.exact_pdf(1, 1, 1).should_not be_a(Float)
@engine.exact_pdf(16, 80, 1.quo(2)).should_not be_a(Float)
expect(@engine.exact_pdf(1, 1, 1)).to_not be_a(Float)
expect(@engine.exact_pdf(16, 80, 1.quo(2))).to_not be_a(Float)
end
end

Expand All @@ -77,7 +77,7 @@
p_value = @engine.p_value(cdf, n, pr)
msg = "For p_value(#{cdf},#{n},#{pr}) expected #{x}, obtained #{p_value}"

p_value.should eq(x), msg
expect(p_value).to eq(x), msg
end
end
end
Expand All @@ -93,7 +93,7 @@
p_value = @engine.p_value(cdf, n, pr)

msg = "For p_value(#{cdf},#{n},#{pr}) expected #{x}, obtained #{p_value}"
p_value.should eq(x), msg
expect(p_value).to eq(x), msg
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/bivariatenormal_spec.rb
Expand Up @@ -5,7 +5,7 @@
it_only_with_gsl 'should return correct pdf' do
if @engine.respond_to? :pdf
[0.2, 0.4, 0.6, 0.8, 0.9, 0.99, 0.999, 0.999999].each {|rho|
@engine.pdf(0, 0, rho, 1, 1).should be_within(1e-8).of(GSL::Ran.bivariate_gaussian_pdf(0, 0, 1, 1, rho))
expect(@engine.pdf(0, 0, rho, 1, 1)).to be_within(1e-8).of(GSL::Ran.bivariate_gaussian_pdf(0, 0, 1, 1, rho))
}
else
pending("No #{@engine}.pdf")
Expand All @@ -16,11 +16,11 @@
shared_examples_for 'all cdf normal capables engines' do
it 'should return correct cdf' do
if @engine.respond_to? :cdf
@engine.cdf(2, 0.5, 0.5).should be_within(1e-3).of(0.686)
@engine.cdf(2, 0.0, 0.5).should be_within(1e-3).of(0.498)
@engine.cdf(1.5, 0.5, 0.5).should be_within(1e-3).of(0.671)
expect(@engine.cdf(2, 0.5, 0.5)).to be_within(1e-3).of(0.686)
expect(@engine.cdf(2, 0.0, 0.5)).to be_within(1e-3).of(0.498)
expect(@engine.cdf(1.5, 0.5, 0.5)).to be_within(1e-3).of(0.671)
v = rand
@engine.cdf(10, 0, v).should be_within(1e-3).of(Distribution::Normal.cdf(0))
expect(@engine.cdf(10, 0, v)).to be_within(1e-3).of(Distribution::Normal.cdf(0))
else
pending("No #{@engine}.cdf")

Expand All @@ -43,7 +43,7 @@
it_should_behave_like 'all cdf normal capables engines'
it 'Ganz method should return similar method to Hull one' do
[-3, -2, -1, 0, 1, 1.5].each {|x|
@engine.cdf_hull(x, x, 0.5).should be_within(0.001).of(@engine.cdf_genz(x, x, 0.5))
expect(@engine.cdf_hull(x, x, 0.5)).to be_within(0.001).of(@engine.cdf_genz(x, x, 0.5))
}
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/chisquare_spec.rb
Expand Up @@ -7,7 +7,7 @@
1.upto(10) do |k|
v = 1 + rand(5)
chi = GSL::Ran.chisq_pdf(v, k)
@engine.pdf(v, k).should be_within(10e-10).of(chi)
expect(@engine.pdf(v, k)).to be_within(10e-10).of(chi)
end
else
skip("No #{@engine}.pdf")
Expand All @@ -21,7 +21,7 @@
1.upto(10) do |k|
v = 1 + rand(5)
chi = GSL::Cdf.chisq_P(v, k)
@engine.cdf(v, k).should be_within(10e-10).of(chi)
expect(@engine.cdf(v, k)).to be_within(10e-10).of(chi)
end
else
skip("No #{@engine}.cdf")
Expand All @@ -33,7 +33,7 @@
1.upto(10) do |k|
v = 1 + rand(5)
pr = @engine.cdf(v, k)
@engine.p_value(pr, k).should be_within(10e-4).of(v)
expect(@engine.p_value(pr, k)).to be_within(10e-4).of(v)
end
else
skip("No #{@engine}.p_value")
Expand Down
6 changes: 3 additions & 3 deletions spec/distribution_spec.rb
Expand Up @@ -5,15 +5,15 @@
if Distribution.has_gsl?
expect(defined?(GSL)).to be
else
defined?(GSL).should be_nil
expect(defined?(GSL)).to be_nil
end
end
it 'should respond to has_statistics2?' do
lambda { Distribution.has_statistics2? }.should_not raise_exception
if Distribution.has_statistics2?
defined?(Statistics2).should be
expect(defined?(Statistics2)).to be
else
defined?(Statistics2).should be_nil
expect(defined?(Statistics2)).to be_nil
end
end
end
11 changes: 5 additions & 6 deletions spec/exponential_spec.rb
Expand Up @@ -6,7 +6,7 @@
if @engine.respond_to? :pdf
[0.5, 1, 1.5].each {|l|
1.upto(5) {|x|
@engine.pdf(x, l).should be_within(1e-10).of(l * Math.exp(-l * x))
expect(@engine.pdf(x, l)).to be_within(1e-10).of(l * Math.exp(-l * x))
}
}
else
Expand All @@ -18,7 +18,7 @@
if @engine.respond_to? :cdf
[0.5, 1, 1.5].each {|l|
1.upto(5) {|x|
@engine.cdf(x, l).should be_within(1e-10).of(1 - Math.exp(-l * x))
expect(@engine.cdf(x, l)).to be_within(1e-10).of(1 - Math.exp(-l * x))
}
}
else
Expand All @@ -31,7 +31,7 @@
[0.5, 1, 1.5].each {|l|
1.upto(5) {|x|
pr = @engine.cdf(x, l)
@engine.p_value(pr, l).should be_within(1e-10).of(x)
expect(@engine.p_value(pr, l)).to be_within(1e-10).of(x)
}
}
else
Expand Down Expand Up @@ -74,15 +74,14 @@
# end
describe 'rng' do
it 'should default to Kernel#rand if no :random is given' do
Random.stub(:rand)
Random.should_receive(:rand).and_return(0.5)
allow(Random).to receive(:rand).and_return(0.5)
rng = Distribution::Exponential.rng 1.0
rng.call
end

it 'should use a given rng if one is passed during construction' do
random = double('random')
random.should_receive(:rand).and_return(0.5)
allow(random).to receive(:rand).and_return(0.5)
rng = Distribution::Exponential.rng 1.0, random: random
rng.call
end
Expand Down
8 changes: 4 additions & 4 deletions spec/f_spec.rb
Expand Up @@ -15,7 +15,7 @@
[0.1, 0.5, 1, 2, 10, 20, 30].each do |x|
[2, 5, 10].product([2, 5, 10]).each do |n, m|
expected_value = GSL::Ran.fdist_pdf(x, n, m)
@engine.pdf(x, n, m).should be_within(1e-4).of(expected_value)
expect(@engine.pdf(x, n, m)).to be_within(1e-4).of(expected_value)
end
end
else
Expand All @@ -30,7 +30,7 @@
[0.1, 0.5, 1, 2, 10, 20, 30].each do |f|
[2, 5, 10].each do |n2|
[2, 5, 10].each do |n1|
@engine.cdf(f, n1, n2).should be_within(1e-4).of(GSL::Cdf.fdist_P(f, n1, n2))
expect(@engine.cdf(f, n1, n2)).to be_within(1e-4).of(GSL::Cdf.fdist_P(f, n1, n2))
end
end
end
Expand All @@ -43,15 +43,15 @@
if @engine.respond_to? :p_value

expected_value = GSL::Cdf.fdist_Pinv(0.975, 5, 4.189092917592713)
@engine.p_value(0.975, 5, 4.189092917592713).should be_within(1e-4).of(expected_value)
expect(@engine.p_value(0.975, 5, 4.189092917592713)).to be_within(1e-4).of(expected_value)

[0.1, 0.5, 1, 2, 10, 20, 30].each do |f|
[2, 5, 10].each do |n2|
[2, 5, 10].each do |n1|
area = @engine.cdf(f, n1, n2)
expected_value = GSL::Cdf.fdist_Pinv(area, n1, n2)

@engine.p_value(area, n1, n2).should be_within(1e-4).of(expected_value)
expect(@engine.p_value(area, n1, n2)).to be_within(1e-4).of(expected_value)
end
end
end
Expand Down
32 changes: 16 additions & 16 deletions spec/gamma_spec.rb
Expand Up @@ -8,7 +8,7 @@
a = rand * x
b = 1 + rand * 5
g = GSL::Ran.gamma_pdf(x, a, b)
@engine.pdf(x, a, b).should be_within(1e-10).of(g)
expect(@engine.pdf(x, a, b)).to be_within(1e-10).of(g)
end
else
pending("No #{@engine}.pdf")
Expand All @@ -18,20 +18,20 @@
if @engine.respond_to? :cdf
# From GSL-1.9.
tol = 1_048_576.0 * Float::EPSILON
@engine.cdf(0.0, 1.0, 1.0).should eq(0.0)
@engine.cdf(1e-100, 1.0, 1.0).should be_within(tol).of(1e-100)
@engine.cdf(0.001, 1.0, 1.0).should be_within(tol).of(9.99500166625008332e-4)
@engine.cdf(0.01, 1.0, 1.0).should be_within(tol).of(9.95016625083194643e-3)
@engine.cdf(0.1, 1.0, 1.0).should be_within(tol).of(9.51625819640404268e-2)
@engine.cdf(0.325, 1.0, 1.0).should be_within(tol).of(2.77472646357927811e-1)
@engine.cdf(1.0, 1.0, 1.0).should be_within(tol).of(6.32120558828557678e-1)
@engine.cdf(1.5, 1.0, 1.0).should be_within(tol).of(7.76869839851570171e-1)
@engine.cdf(2.0, 1.0, 1.0).should be_within(tol).of(8.64664716763387308e-1)
@engine.cdf(10.0, 1.0, 1.0).should be_within(tol).of(9.99954600070237515e-1)
@engine.cdf(20.0, 1.0, 1.0).should be_within(tol).of(9.99999997938846378e-1)
@engine.cdf(100.0, 1.0, 1.0).should be_within(tol).of(1e0)
@engine.cdf(1000.0, 1.0, 1.0).should be_within(tol).of(1e0)
@engine.cdf(10_000.0, 1.0, 1.0).should be_within(tol).of(1e0)
expect(@engine.cdf(0.0, 1.0, 1.0)).to eq(0.0)
expect(@engine.cdf(1e-100, 1.0, 1.0)).to be_within(tol).of(1e-100)
expect(@engine.cdf(0.001, 1.0, 1.0)).to be_within(tol).of(9.99500166625008332e-4)
expect(@engine.cdf(0.01, 1.0, 1.0)).to be_within(tol).of(9.95016625083194643e-3)
expect(@engine.cdf(0.1, 1.0, 1.0)).to be_within(tol).of(9.51625819640404268e-2)
expect(@engine.cdf(0.325, 1.0, 1.0)).to be_within(tol).of(2.77472646357927811e-1)
expect(@engine.cdf(1.0, 1.0, 1.0)).to be_within(tol).of(6.32120558828557678e-1)
expect(@engine.cdf(1.5, 1.0, 1.0)).to be_within(tol).of(7.76869839851570171e-1)
expect(@engine.cdf(2.0, 1.0, 1.0)).to be_within(tol).of(8.64664716763387308e-1)
expect(@engine.cdf(10.0, 1.0, 1.0)).to be_within(tol).of(9.99954600070237515e-1)
expect(@engine.cdf(20.0, 1.0, 1.0)).to be_within(tol).of(9.99999997938846378e-1)
expect(@engine.cdf(100.0, 1.0, 1.0)).to be_within(tol).of(1e0)
expect(@engine.cdf(1000.0, 1.0, 1.0)).to be_within(tol).of(1e0)
expect(@engine.cdf(10_000.0, 1.0, 1.0)).to be_within(tol).of(1e0)
else
pending("No #{@engine}.cdf")
end
Expand All @@ -42,7 +42,7 @@
a = rand * 0.5
b = 1 + rand * 5
pr = @engine.cdf(x, a, b)
@engine.p_value(pr, a, b).should be_within(1e-3).of(x)
expect(@engine.p_value(pr, a, b)).to be_within(1e-3).of(x)
end
else
skip("No #{@engine}.p_value")
Expand Down
20 changes: 10 additions & 10 deletions spec/hypergeometric_spec.rb
Expand Up @@ -19,7 +19,7 @@
if @engine.respond_to? :cdf
# [2].each do |k|
[0, 1, 2, 4, 8, 16].each do |k|
@engine.cdf(k, 80, 100, 10_000).should be_within(1e-10).of(@ruby.cdf(k, 80, 100, 10_000))
expect(@engine.cdf(k, 80, 100, 10_000)).to be_within(1e-10).of(@ruby.cdf(k, 80, 100, 10_000))
end
# result = RubyProf.stop

Expand All @@ -40,7 +40,7 @@
pending("Aprox. factorial doesn't work right")
if @engine.respond_to? :pdf
[0, 1, 2, 4, 8, 16].each do |k|
@engine.pdf_aprox(k, 80, 100, 1000).should be_within(1e-8).of(GSL::Ran.hypergeometric_pdf(k, 80, 920, 100))
expect(@engine.pdf_aprox(k, 80, 100, 1000)).to be_within(1e-8).of(GSL::Ran.hypergeometric_pdf(k, 80, 920, 100))
end
else
pending("No #{@engine}.pdf")
Expand All @@ -54,7 +54,7 @@
# [2].each do |k|
[0, 1, 2, 4, 8, 16].each do |k|
# puts "k:#{k}->#{@engine.pdf(k, 80, 100, 10000).to_f}"
@engine.pdf(k, 80, 100, 10_000).to_f.should be_within(1e-8).of(GSL::Ran.hypergeometric_pdf(k, 80, 9920, 100))
expect(@engine.pdf(k, 80, 100, 10_000).to_f).to be_within(1e-8).of(GSL::Ran.hypergeometric_pdf(k, 80, 9920, 100))
end
# result = RubyProf.stop

Expand All @@ -74,7 +74,7 @@
ac = 0
0.upto(m) do |i|
ac += @engine.pdf(i, m, n, total)
@engine.cdf(i, m, n, total).should eq(ac)
expect(@engine.cdf(i, m, n, total)).to eq(ac)
end
end

Expand All @@ -88,20 +88,20 @@
ac = 0
0.upto(m) do |k|
ac += @engine.pdf(k, m, n, total)
@engine.p_value(ac, m, n, total).should eq(k)
expect(@engine.p_value(ac, m, n, total)).to eq(k)
end
end
end
describe Distribution::Hypergeometric do
before do
@engine = Distribution::Hypergeometric
end
it { @engine.should respond_to(:exact_pdf) }
it { @engine.should respond_to(:exact_cdf) }
it { @engine.should respond_to(:exact_p_value) }
it { expect(@engine).to respond_to(:exact_pdf) }
it { expect(@engine).to respond_to(:exact_cdf) }
it { expect(@engine).to respond_to(:exact_p_value) }
it 'exact pdf should return a Rational' do
@engine.exact_pdf(1, 1, 1, 1).should_not be_a(Float)
@engine.exact_pdf(16, 80, 100, 10_000).should_not be_a(Float)
expect(@engine.exact_pdf(1, 1, 1, 1)).not_to be_a(Float)
expect(@engine.exact_pdf(16, 80, 100, 10_000)).not_to be_a(Float)
end
end
end

0 comments on commit 2b84dd7

Please sign in to comment.