Skip to content

Move Fixnum and Bignum specs to Integer specs - #580

Merged
eregon merged 10 commits into
ruby:masterfrom
andrykonchin:merge-fixnum-and-bignum-specs-to-integer-specs
Jan 10, 2018
Merged

eregon merged 10 commits into
ruby:masterfrom
andrykonchin:merge-fixnum-and-bignum-specs-to-integer-specs

Conversation

@andrykonchin

@andrykonchin andrykonchin commented Jan 3, 2018 •

Copy link
Copy Markdown
Member

Issue #578

So now we have following methods implemented in Bignum (Ruby 2.3) but not implemented (and inherited from Numeric/Kernel) in Integer (Ruby 2.4): #hash, #eql?, #dup, zero?

By convention we should test them in Numeric/Kernel so I don't include them to Integer specs.

@@ -1,39 +0,0 @@
require File.expand_path('../../../spec_helper', __FILE__)

describe "Fixnum#coerce when given a Fixnum" do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is actually Integer#coerce though:

[1] pry(main)> 3.method :coerce
=> #<Method: Integer#coerce>

So I think this should be moved to core/integer/coerce_spec.rb

Comment thread core/integer/divide_spec.rb Outdated
it_behaves_like :integer_arithmetic_exception_in_coerce, :/
ruby_version_is '2.4' do
describe "Integer#/" do
it_behaves_like :integer_arithmetic_exception_in_coerce, :/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the guards not be part of the shared example?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By having describe "Integer#/" I think it's OK to mean it's Fixnum#/ + Bignum#/ behavior in Ruby 2.3 and before.
No need to separate those specs, as I meant in #578.

@andrykonchin andrykonchin Jan 4, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to separate those specs, as I meant in #578.

@eregon Do you mean we need to "adopt" Bignum specs as well?

Please correct me if I misunderstood you. I would like to copy/paste (and revise) all Fixnum/Bignum specs to Integer specs with ruby version guard "2.4" and add ruby version guard ""..."2.4" for existing Fixnum/Bignum specs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, really move (and merge) core/fixnum/* and core/bignum/* to core/integer and rename descriptions to Integer#some_method.
So no extra version guards.
The version guards are not needed and not worth the trouble just for classifying methods differently.

I think duplicating the specs would be bad.
And making them shared specs would make them needlessly complicated.

Please share your thoughts if you disagree.
Maybe it's better to wait 2.2 and 2.3 become unsupported, so then it's really clear there should no longer be core/{fixnum,bignum} specs.
I'm proposing to do that already now since anyway behaviorally it is compatible, and the new unified Integer has a nicer structure than separate "small/big integers" specs.

@andrykonchin andrykonchin Jan 6, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eregon FYI There are following minor differences:

  • Fixnum#hash (as well as Integer#hash) is implemented in Kernel
  • Fixnum#coerce is implemented in Numeric
  • Fixnum#eql? (as well as Integer#eql?) is implemented in Numeric

I am OK with moving specs and quite finished it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Fixnum#coerce is implemented in Numeric

It's in Integer in 2.5 I think:

> 3.method :coerce
=> #<Method: Integer#coerce>

Comment thread bit_length_spec.rb
@@ -0,0 +1,44 @@
require File.expand_path('../../../spec_helper', __FILE__)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems this file went to the wrong place 😉

@andrykonchin andrykonchin changed the title [WIP] Merge fixnum and bignum specs to integer specs Merge ixnum and bignum specs to integer specs Jan 6, 2018
@andrykonchin andrykonchin changed the title Merge ixnum and bignum specs to integer specs Move Fixnum and Bignum specs to Integer specs Jan 6, 2018
@andrykonchin

Copy link
Copy Markdown
Member Author

@eregon Done

Comment thread core/integer/div_spec.rb

@bignum.div(21 ).should == 439208192231179804
@bignum.div(21.0).should == 439208192231179776
end

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a bug

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, the Numeric#div rdoc says:

Implementation from Numeric
------------------------------------------------------------------------------
  num.div(numeric)  ->  integer

------------------------------------------------------------------------------

Uses / to perform division, then converts the result to an integer.
Numeric does not define the / operator; this is left to subclasses.

And if indeed it goes through Float then it loses precision:

> 9223372036854775896/21.0
=> 4.392081922311798e+17

And the Integer#div rdoc is fairly imprecise:

Implementation from Integer
------------------------------------------------------------------------------
  int.div(numeric)  ->  integer

------------------------------------------------------------------------------

Performs integer division: returns the integer result of dividing int
by numeric.

The implementation is pretty simple:
https://github.com/ruby/ruby/blob/3a887648fe75c022c3f445e5943525226f15e3d0/numeric.c#L571-L588
It calls / then floor.
That would be a good spec and clarify the behavior.

Might still be worth asking on MRI's tracker for clarification though.

Comment thread core/integer/divide_spec.rb Outdated
it "raises a TypeError if other is zero and not a Float" do
lambda { @bignum.send(@method, 0) }.should raise_error(TypeError, /nil is not a symbol nor a string/)
end
end

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again it's very strange behaviour

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is not in a shared spec, so @method is probably nil?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh

ruby_version_is "2.5" do
it_behaves_like :integer_arithmetic_coerce_not_rescue, :/
end
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you extract the version guards outside of the shared spec in this commit?
It seems to make usages of the shared spec more verbose and fragile.
Are they problematic or unclear somehow?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eregon looks like I misunderstood your previous comment:

Can the guards not be part of the shared example?

There are minor pros and cons so if you want I will revert this change

lambda { 1.coerce(1..4) }.should raise_error(TypeError)
lambda { 1.coerce(:test) }.should raise_error(TypeError)
end
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit seems to remove many specs.
Are they duplicate or so?

@andrykonchin andrykonchin Jan 8, 2018 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eregon
Fixnum#coerce in Ruby 2.3 is implemented in Numeric class

irb(main):002:0> 1.method(:coerce).owner
=> Numeric

So I just merged them with Numeric#coerce specs. And yes - there was some duplication.

@andrykonchin

Copy link
Copy Markdown
Member Author

@eregon done

@eregon eregon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thank you!

@eregon
eregon merged commit 98bbaf3 into ruby:master Jan 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants