summaryrefslogtreecommitdiff
path: root/spec/ruby/core/numeric/quo_spec.rb
blob: b4a23fd47662543e492c1f2bbe42d43c565e1ef3 (plain)
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
56
57
58
59
60
61
62
63
64
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/quo'

describe "Numeric#quo" do
  it "returns the result of self divided by the given Integer as a Rational" do
    5.quo(2).should eql(Rational(5,2))
  end

  it "returns the result of self divided by the given Float as a Float" do
    2.quo(2.5).should eql(0.8)
  end

  it "returns the result of self divided by the given Bignum as a Float" do
    45.quo(bignum_value).should be_close(1.04773789668636e-08, TOLERANCE)
  end

  it "raises a ZeroDivisionError when the given Integer is 0" do
    -> { 0.quo(0) }.should raise_error(ZeroDivisionError)
    -> { 10.quo(0) }.should raise_error(ZeroDivisionError)
    -> { -10.quo(0) }.should raise_error(ZeroDivisionError)
    -> { bignum_value.quo(0) }.should raise_error(ZeroDivisionError)
    -> { -bignum_value.quo(0) }.should raise_error(ZeroDivisionError)
  end

  it "calls #to_r to convert the object to a Rational" do
    obj = NumericSpecs::Subclass.new
    obj.should_receive(:to_r).and_return(Rational(1))

    obj.quo(19).should == Rational(1, 19)
  end

  it "raises a TypeError of #to_r does not return a Rational" do
    obj = NumericSpecs::Subclass.new
    obj.should_receive(:to_r).and_return(1)

    -> { obj.quo(19) }.should raise_error(TypeError)
  end

  it "raises a TypeError when given a non-Integer" do
    -> {
      (obj = mock('x')).should_not_receive(:to_int)
      13.quo(obj)
    }.should raise_error(TypeError)
    -> { 13.quo("10")    }.should raise_error(TypeError)
    -> { 13.quo(:symbol) }.should raise_error(TypeError)
  end

  it "returns the result of calling self#/ with other" do
    obj = NumericSpecs::Subclass.new
    obj.should_receive(:to_r).and_return(19.quo(20))

    obj.quo(19).should == 1.quo(20)
  end

  it "raises a ZeroDivisionError if the given argument is zero and not a Float" do
    -> { 1.quo(0) }.should raise_error(ZeroDivisionError)
  end

  it "returns infinity if the given argument is zero and is a Float" do
    (1.quo(0.0)).to_s.should == 'Infinity'
    (-1.quo(0.0)).to_s.should == '-Infinity'
  end
end