File: divide_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 (55 lines) | stat: -rw-r--r-- 1,755 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
55
require File.dirname(__FILE__) + '/../../spec_helper'
require 'complex'

describe "Complex#/ with Complex" do
  it "divides according to the usual rule for complex numbers" do
    a = Complex((1 * 10) - (2 * 20), (1 * 20) + (2 * 10))
    b = Complex(1, 2)
    (a / b).should == Complex(10, 20)

    c = Complex((1.5 * 100.2) - (2.1 * -30.3), (1.5 * -30.3) + (2.1 * 100.2))
    d = Complex(1.5, 2.1)
    # remember the floating-point arithmetic
    (c / d).should be_close(Complex(100.2, -30.3), TOLERANCE)
  end
end

describe "Complex#/ with Fixnum" do
  it "divides both parts of the Complex number" do
    (Complex(20, 40) / 2).should == Complex(10, 20)
    (Complex(30, 30) / 10).should == Complex(3, 3)
  end
  
  it "raises a ZeroDivisionError when given zero" do
    lambda { Complex(20, 40) / 0 }.should raise_error(ZeroDivisionError)
  end
end

describe "Complex#/ with Bignum" do
  it "divides both parts of the Complex number" do
    (Complex(20, 40) / 2).should == Complex(10, 20)
    (Complex(15, 16) / 2.0).should be_close(Complex(7.5, 8), TOLERANCE)
  end
end

describe "Complex#/ with Float" do
  it "divides both parts of the Complex number" do
    (Complex(3, 9) / 1.5).should == Complex(2, 6)
    (Complex(15, 16) / 2.0).should be_close(Complex(7.5, 8), TOLERANCE)
  end

  it "returns Complex(Infinity, Infinity) when given zero" do
    (Complex(20, 40) / 0.0).inspect.should == "Complex(Infinity, Infinity)"
    (Complex(-20, -40) / 0.0).inspect.should == "Complex(-Infinity, -Infinity)"
  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([4, 2])
    (value / obj).should == 2
  end
end