Skip to content

Commit

Permalink
Add shared spec integer_ceil_precision
Browse files Browse the repository at this point in the history
This spec can be shared between Integer#ceil and Float#ceil and
improves coverage of cases where precision is passed in.
  • Loading branch information
peterzhu2118 committed Jul 26, 2024
1 parent 59d3c7e commit f78487f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/float/ceil_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
require_relative '../../spec_helper'
require_relative '../integer/shared/integer_ceil_precision'

describe "Float#ceil" do
context "with precision" do
it_behaves_like :integer_ceil_precision, :Float
end

it "returns the smallest Integer greater than or equal to self" do
-1.2.ceil.should eql( -1)
-1.0.ceil.should eql( -1)
Expand Down
5 changes: 5 additions & 0 deletions core/integer/ceil_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
require_relative '../../spec_helper'
require_relative 'shared/to_i'
require_relative 'shared/integer_rounding'
require_relative 'shared/integer_ceil_precision'

describe "Integer#ceil" do
it_behaves_like :integer_to_i, :ceil
it_behaves_like :integer_rounding_positive_precision, :ceil

context "with precision" do
it_behaves_like :integer_ceil_precision, :Integer
end

context "precision argument specified as part of the ceil method is negative" do
it "returns the smallest integer greater than self with at least precision.abs trailing zeros" do
18.ceil(-1).should eql(20)
Expand Down
36 changes: 36 additions & 0 deletions core/integer/shared/integer_ceil_precision.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
describe :integer_ceil_precision, shared: true do
context "precision is zero" do
it "returns integer 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 largest integer less 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)
end
end
end

0 comments on commit f78487f

Please sign in to comment.