summaryrefslogtreecommitdiff
path: root/spec/ruby/library/bigdecimal/shared/quo.rb
blob: 46e6d62bf4bffdab2ab36c68c63dc07d6d994a00 (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
65
66
67
require 'bigdecimal'

describe :bigdecimal_quo, shared: true do
  before :each do
    @one = BigDecimal("1")
    @zero = BigDecimal("0")
    @zero_plus = BigDecimal("+0")
    @zero_minus = BigDecimal("-0")
    @two = BigDecimal("2")
    @three = BigDecimal("3")
    @eleven = BigDecimal("11")
    @nan = BigDecimal("NaN")
    @infinity = BigDecimal("Infinity")
    @infinity_minus = BigDecimal("-Infinity")
    @one_minus = BigDecimal("-1")
    @frac_1 = BigDecimal("1E-99999")
    @frac_2 = BigDecimal("0.9E-99999")
  end

  it "returns a / b" do
    @two.send(@method, @one, *@object).should == @two
    @one.send(@method, @two, *@object).should == BigDecimal("0.5")
    @eleven.send(@method, @three, *@object).should be_close(@three + (@two / @three), TOLERANCE)
    @one.send(@method, @one_minus, *@object).should == @one_minus
    @one_minus.send(@method, @one_minus, *@object).should == @one
    @frac_2.send(@method, @frac_1, *@object).should == BigDecimal("0.9")
    @frac_1.send(@method, @frac_1, *@object).should == @one
    @one.send(@method, BigDecimal('-2E5555'), *@object).should == BigDecimal('-0.5E-5555')
    @one.send(@method, BigDecimal('2E-5555'), *@object).should == BigDecimal('0.5E5555')
  end

  describe "with Object" do
    it "tries to coerce the other operand to self" do
      object = mock("Object")
      object.should_receive(:coerce).with(@one).and_return([@one, @two])
      @one.send(@method, object, *@object).should == BigDecimal("0.5")
    end
  end

  it "returns 0 if divided by Infinity" do
    @zero.send(@method, @infinity, *@object).should == 0
    @frac_2.send(@method, @infinity, *@object).should == 0
  end

  it "returns (+|-) Infinity if (+|-) Infinity divided by one" do
    @infinity_minus.send(@method, @one, *@object).should == @infinity_minus
    @infinity.send(@method, @one, *@object).should == @infinity
    @infinity_minus.send(@method, @one_minus, *@object).should == @infinity
  end

  it "returns NaN if Infinity / ((+|-) Infinity)" do
    @infinity.send(@method, @infinity_minus, *@object).should.nan?
    @infinity_minus.send(@method, @infinity, *@object).should.nan?
  end

  it "returns (+|-) Infinity if divided by zero" do
    @one.send(@method, @zero, *@object).should == @infinity
    @one.send(@method, @zero_plus, *@object).should == @infinity
    @one.send(@method, @zero_minus, *@object).should == @infinity_minus
  end

  it "returns NaN if zero is divided by zero" do
    @zero.send(@method, @zero, *@object).should.nan?
    @zero_minus.send(@method, @zero_plus, *@object).should.nan?
    @zero_plus.send(@method, @zero_minus, *@object).should.nan?
  end
end