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

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

      send(@method, 123).ceil(10).should.eql?(send(@method, 123))
      send(@method, -123).ceil(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).ceil(-1).should.eql?(0)
      send(@method, 0).ceil(-10).should.eql?(0)
    end

    it "returns Integer equal to self if there are already at least precision.abs trailing zeros" do
      send(@method, 10).ceil(-1).should.eql?(10)
      send(@method, 100).ceil(-1).should.eql?(100)
      send(@method, 100).ceil(-2).should.eql?(100)
      send(@method, -10).ceil(-1).should.eql?(-10)
      send(@method, -100).ceil(-1).should.eql?(-100)
      send(@method, -100).ceil(-2).should.eql?(-100)
    end

    it "returns smallest Integer greater than self with at least precision.abs trailing zeros" do
      send(@method, 123).ceil(-1).should.eql?(130)
      send(@method, 123).ceil(-2).should.eql?(200)
      send(@method, 123).ceil(-3).should.eql?(1000)

      send(@method, -123).ceil(-1).should.eql?(-120)
      send(@method, -123).ceil(-2).should.eql?(-100)
      send(@method, -123).ceil(-3).should.eql?(0)

      send(@method, 100).ceil(-3).should.eql?(1000)
      send(@method, -100).ceil(-3).should.eql?(0)
    end

    # Bug #20654
    it "returns 10**precision.abs when precision.abs has more digits than self" do
      send(@method, 123).ceil(-20).should.eql?(100000000000000000000)
      send(@method, 123).ceil(-50).should.eql?(100000000000000000000000000000000000000000000000000)
    end
  end
end