summaryrefslogtreecommitdiff
path: root/spec/ruby/core/integer/shared/integer_floor_precision.rb
blob: 6247907d4cd0b85f78039e42c233ab4e9969ab0b (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
describe :integer_floor_precision, shared: true do
  context "precision is zero" do
    it "returns integer self" do
      send(@method, 0).floor(0).should.eql?(0)
      send(@method, 123).floor(0).should.eql?(123)
      send(@method, -123).floor(0).should.eql?(-123)
    end
  end

  context "precision is positive" do
    it "returns self" do
      send(@method, 0).floor(1).should.eql?(send(@method, 0))
      send(@method, 0).floor(10).should.eql?(send(@method, 0))

      send(@method, 123).floor(10).should.eql?(send(@method, 123))
      send(@method, -123).floor(10).should.eql?(send(@method, -123))
    end
  end

  context "precision is negative" do
    it "always returns 0 when self is 0" do
      send(@method, 0).floor(-1).should.eql?(0)
      send(@method, 0).floor(-10).should.eql?(0)
    end

    it "returns largest integer less than self with at least precision.abs trailing zeros" do
      send(@method, 123).floor(-1).should.eql?(120)
      send(@method, 123).floor(-2).should.eql?(100)
      send(@method, 123).floor(-3).should.eql?(0)

      send(@method, -123).floor(-1).should.eql?(-130)
      send(@method, -123).floor(-2).should.eql?(-200)
      send(@method, -123).floor(-3).should.eql?(-1000)
    end

    # Bug #20654
    it "returns -(10**precision.abs) when self is negative and precision.abs is larger than the number digits of self" do
      send(@method, -123).floor(-20).should.eql?(-100000000000000000000)
      send(@method, -123).floor(-50).should.eql?(-100000000000000000000000000000000000000000000000000)
    end
  end
end