forked from ruby/spec
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complex#**: Share btw. 1.8 (lib) and 1.9 (core)
- Loading branch information
Showing
3 changed files
with
88 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |