summaryrefslogtreecommitdiff
path: root/spec/ruby/core/bignum/comparison_spec.rb
blob: 435cc9aea2e07503a34b1aaccd9149b76f491d9d (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require File.expand_path('../../../spec_helper', __FILE__)

describe "Bignum#<=>" do
  describe "with a Fixnum" do
    it "returns -1 when other is larger" do
      (-bignum_value <=> 2).should == -1
    end

    it "returns 1 when other is smaller" do
      (bignum_value <=> 2).should == 1
    end
  end

  describe "with a Bignum" do
    describe "when other is negative" do
      it "returns -1 when self is negative and other is larger" do
        (-bignum_value(42) <=> -bignum_value).should == -1
      end

      it "returns 0 when other is equal" do
        (-bignum_value <=> -bignum_value).should == 0
      end

      it "returns 1 when self is negative and other is smaller" do
        (-bignum_value <=> -bignum_value(94)).should == 1
      end

      it "returns 1 when self is positive" do
        (bignum_value <=> -bignum_value).should == 1
      end
    end

    describe "when other is positive" do
      it "returns -1 when self is negative" do
        (-bignum_value <=> bignum_value).should == -1
      end

      it "returns -1 when self is positive and other is larger" do
        (bignum_value <=> bignum_value(38)).should == -1
      end

      it "returns 0 when other is equal" do
        (bignum_value <=> bignum_value).should == 0
      end

      it "returns 1 when other is smaller" do
        (bignum_value(56) <=> bignum_value).should == 1
      end
    end
  end

  describe "with a Float" do
    describe "when other is negative" do
      it "returns -1 when self is negative and other is larger" do
        (-bignum_value(0xffff) <=> -bignum_value.to_f).should == -1
      end

      it "returns 0 when other is equal" do
        (-bignum_value <=> -bignum_value.to_f).should == 0
      end

      it "returns 1 when self is negative and other is smaller" do
        (-bignum_value <=> -bignum_value(0xffef).to_f).should == 1
      end

      it "returns 1 when self is positive" do
        (bignum_value <=> -bignum_value.to_f).should == 1
      end
    end

    describe "when other is positive" do
      it "returns -1 when self is negative" do
        (-bignum_value <=> bignum_value.to_f).should == -1
      end

      it "returns -1 when self is positive and other is larger" do
        (bignum_value <=> bignum_value(0xfffe).to_f).should == -1
      end

      it "returns 0 when other is equal" do
        (bignum_value <=> bignum_value.to_f).should == 0
      end

      it "returns 1 when other is smaller" do
        (bignum_value(0xfeff) <=> bignum_value.to_f).should == 1
      end
    end
  end

  describe "with an Object" do
    before :each do
      @big = bignum_value
      @num = mock("value for Bignum#<=>")
    end

    it "calls #coerce on other" do
      @num.should_receive(:coerce).with(@big).and_return([@big.to_f, 2.5])
      @big <=> @num
    end

    ruby_version_is ""..."2.5" do
      it "returns nil if #coerce raises an exception" do
        @num.should_receive(:coerce).with(@big).and_raise(RuntimeError)
        lambda {
          @result = (@big <=> @num)
        }.should complain(/Numerical comparison operators will no more rescue exceptions/)
        @result.should be_nil
      end
    end

    ruby_version_is "2.5" do
      it "lets the exception go through if #coerce raises an exception" do
        @num.should_receive(:coerce).with(@big).and_raise(RuntimeError.new("my error"))
        lambda {
          @big <=> @num
        }.should raise_error(RuntimeError, "my error")
      end
    end

    it "raises an exception if #coerce raises a non-StandardError exception" do
      @num.should_receive(:coerce).with(@big).and_raise(Exception)
      lambda { @big <=> @num }.should raise_error(Exception)
    end

    it "returns nil if #coerce does not return an Array" do
      @num.should_receive(:coerce).with(@big).and_return(nil)
      (@big <=> @num).should be_nil
    end

    it "returns -1 if the coerced value is larger" do
      @num.should_receive(:coerce).with(@big).and_return([@big, bignum_value(10)])
      (@big <=> @num).should == -1
    end

    it "returns 0 if the coerced value is equal" do
      @num.should_receive(:coerce).with(@big).and_return([@big, bignum_value])
      (@big <=> @num).should == 0
    end

    it "returns 1 if the coerced value is smaller" do
      @num.should_receive(:coerce).with(@big).and_return([@big, 22])
      (@big <=> @num).should == 1
    end
  end

  # The tests below are taken from matz's revision 23730 for Ruby trunk
  it "returns 1 when self is Infinity and other is a Bignum" do
    (infinity_value <=> Float::MAX.to_i*2).should == 1
  end

  it "returns -1 when self is negative and other is Infinty" do
    (-Float::MAX.to_i*2 <=> infinity_value).should == -1
  end

  it "returns 1 when self is negative and other is -Infinity" do
    (-Float::MAX.to_i*2 <=> -infinity_value).should == 1
  end

  it "returns -1 when self is -Infinity and other is negative" do
    (-infinity_value <=> -Float::MAX.to_i*2).should == -1
  end
end