Skip to content

Commit

Permalink
Complex#**: Share btw. 1.8 (lib) and 1.9 (core)
Browse files Browse the repository at this point in the history
  • Loading branch information
runpaint committed Jun 30, 2009
1 parent a719db4 commit 0317a7a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 42 deletions.
23 changes: 23 additions & 0 deletions core/complex/exponent_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require File.dirname(__FILE__) + '/../../shared/complex/exponent'

ruby_version_is "1.9" do
describe "Complex#** when given 0" do
it_behaves_like(:complex_exponent_zero, :**)
end

describe "Complex#** with Complex" do
it_behaves_like(:complex_exponent_complex, :**)
end

describe "Complex#** with Integer" do
it_behaves_like(:complex_exponent_integer, :**)
end

describe "Complex#** with Rational" do
it_behaves_like(:complex_exponent_rational, :**)
end

describe "Complex#** with Object" do
it_behaves_like(:complex_exponent_object, :**)
end
end
55 changes: 13 additions & 42 deletions library/complex/exponent_spec.rb
Original file line number Diff line number Diff line change
@@ -1,54 +1,25 @@
require File.dirname(__FILE__) + '/../../spec_helper'
require File.dirname(__FILE__) + '/../../shared/complex/exponent'
require "complex"
require "rational"

describe "Complex#** when given 0" do
it "returns Complex(1)" do
(Complex(3, 4) ** 0).should eql(Complex(1))
(Complex(10, 20) ** 0.0).should eql(Complex(1))
ruby_version_is ""..."1.9" do
describe "Complex#** when given 0" do
it_behaves_like(:complex_exponent_zero, :**)
end
end

describe "Complex#** with Complex" do
it "returns self raised to the given power" do
(Complex(2, 1) ** Complex(2, 1)).should be_close(Complex(-0.504824688978319, 3.10414407699553), TOLERANCE)
(Complex(2, 1) ** Complex(3, 4)).should be_close(Complex(-0.179174656916581, -1.74071656397662), TOLERANCE)

(Complex(2, 1) ** Complex(-2, -1)).should be_close(Complex(-0.051041070450869, -0.313849223270419), TOLERANCE)
(Complex(-2, -1) ** Complex(2, 1)).should be_close(Complex(-11.6819929610857, 71.8320439736158), TOLERANCE)
describe "Complex#** with Complex" do
it_behaves_like(:complex_exponent_complex, :**)
end
end

describe "Complex#** with Integer" do
it "returns self raised to the given power" do
(Complex(2, 1) ** 2).should == Complex(3, 4)
(Complex(3, 4) ** 2).should == Complex(-7, 24)
(Complex(3, 4) ** -2).should be_close(Complex(-0.0112, -0.0384), TOLERANCE)


(Complex(2, 1) ** 2.5).should be_close(Complex(2.99179707178602, 6.85206901006896), TOLERANCE)
(Complex(3, 4) ** 2.5).should be_close(Complex(-38.0, 41.0), TOLERANCE)
(Complex(3, 4) ** -2.5).should be_close(Complex(-0.01216, -0.01312), TOLERANCE)

# NOTE: Takes way too long...
#(Complex(2, 1) ** bignum_value)
describe "Complex#** with Integer" do
it_behaves_like(:complex_exponent_integer, :**)
end
end

describe "Complex#** with Rational" do
it "returns self raised to the given power" do
(Complex(2, 1) ** Rational(3, 4)).should be_close(Complex(1.71913265276568, 0.623124744394697), TOLERANCE)
(Complex(2, 1) ** Rational(4, 3)).should be_close(Complex(2.3828547125173, 1.69466313833091), TOLERANCE)
(Complex(2, 1) ** Rational(-4, 3)).should be_close(Complex(0.278700377879388, -0.198209003071003), TOLERANCE)
describe "Complex#** with Rational" do
it_behaves_like(:complex_exponent_rational, :**)
end
end

describe "Complex#** with Object" do
it "tries to coerce self into other" do
value = Complex(3, 9)

obj = mock("Object")
obj.should_receive(:coerce).with(value).and_return([2, 5])
(value ** obj).should == 2 ** 5
describe "Complex#** with Object" do
it_behaves_like(:complex_exponent_object, :**)
end
end
end
52 changes: 52 additions & 0 deletions shared/complex/exponent.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require File.dirname(__FILE__) + '/../../spec_helper'

describe :complex_exponent_zero, :shared => true do
it "returns Complex(1)" do
(Complex(3, 4) ** 0).should eql(Complex(1))
(Complex(10, 20) ** 0.0).should eql(Complex(1.0, 0.0))
end
end

describe :complex_exponent_complex, :shared => true do
it "returns self raised to the given power" do
(Complex(2, 1) ** Complex(2, 1)).should be_close(Complex(-0.504824688978319, 3.10414407699553), TOLERANCE)
(Complex(2, 1) ** Complex(3, 4)).should be_close(Complex(-0.179174656916581, -1.74071656397662), TOLERANCE)

(Complex(2, 1) ** Complex(-2, -1)).should be_close(Complex(-0.051041070450869, -0.313849223270419), TOLERANCE)
(Complex(-2, -1) ** Complex(2, 1)).should be_close(Complex(-11.6819929610857, 71.8320439736158), TOLERANCE)
end
end

describe :complex_exponent_integer, :shared => true do
it "returns self raised to the given power" do
(Complex(2, 1) ** 2).should == Complex(3, 4)
(Complex(3, 4) ** 2).should == Complex(-7, 24)
(Complex(3, 4) ** -2).should be_close(Complex(-0.0112, -0.0384), TOLERANCE)


(Complex(2, 1) ** 2.5).should be_close(Complex(2.99179707178602, 6.85206901006896), TOLERANCE)
(Complex(3, 4) ** 2.5).should be_close(Complex(-38.0, 41.0), TOLERANCE)
(Complex(3, 4) ** -2.5).should be_close(Complex(-0.01216, -0.01312), TOLERANCE)

# NOTE: Takes way too long...
#(Complex(2, 1) ** bignum_value)
end
end

describe :complex_exponent_rational, :shared => true do
it "returns self raised to the given power" do
(Complex(2, 1) ** Rational(3, 4)).should be_close(Complex(1.71913265276568, 0.623124744394697), TOLERANCE)
(Complex(2, 1) ** Rational(4, 3)).should be_close(Complex(2.3828547125173, 1.69466313833091), TOLERANCE)
(Complex(2, 1) ** Rational(-4, 3)).should be_close(Complex(0.278700377879388, -0.198209003071003), TOLERANCE)
end
end

describe :complex_exponent_object, :shared => true do
it "tries to coerce self into other" do
value = Complex(3, 9)

obj = mock("Object")
obj.should_receive(:coerce).with(value).and_return([2, 5])
(value ** obj).should == 2 ** 5
end
end

0 comments on commit 0317a7a

Please sign in to comment.