File: exponent_spec.rb

package info (click to toggle)
dlr-languages 20090805%2Bgit.e6b28d27%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 51,484 kB
  • ctags: 59,257
  • sloc: cs: 298,829; ruby: 159,643; xml: 19,872; python: 2,820; yacc: 1,960; makefile: 96; sh: 65
file content (54 lines) | stat: -rw-r--r-- 2,114 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require File.dirname(__FILE__) + '/../../spec_helper'
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))
  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)
  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)
  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)
  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
  end
end