2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'fixtures/classes'
|
|
|
|
require_relative 'shared/exponent'
|
2018-01-29 11:08:16 -05:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
describe "Integer#pow" do
|
|
|
|
context "one argument is passed" do
|
|
|
|
it_behaves_like :integer_exponent, :pow
|
|
|
|
end
|
2018-01-29 11:08:16 -05:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
context "two arguments are passed" do
|
|
|
|
it "returns modulo of self raised to the given power" do
|
|
|
|
2.pow(5, 12).should == 8
|
|
|
|
2.pow(6, 13).should == 12
|
|
|
|
2.pow(7, 14).should == 2
|
|
|
|
2.pow(8, 15).should == 1
|
|
|
|
end
|
2018-01-29 11:08:16 -05:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "works well with bignums" do
|
|
|
|
2.pow(61, 5843009213693951).should eql 3697379018277258
|
|
|
|
2.pow(62, 5843009213693952).should eql 1551748822859776
|
|
|
|
2.pow(63, 5843009213693953).should eql 3103497645717974
|
|
|
|
2.pow(64, 5843009213693954).should eql 363986077738838
|
|
|
|
end
|
2018-01-29 11:08:16 -05:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "handles sign like #divmod does" do
|
2020-04-02 20:44:40 -04:00
|
|
|
2.pow(5, 12).should == 8
|
|
|
|
2.pow(5, -12).should == -4
|
2020-02-08 05:43:27 -05:00
|
|
|
-2.pow(5, 12).should == 4
|
|
|
|
-2.pow(5, -12).should == -8
|
|
|
|
end
|
2018-01-29 11:08:16 -05:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "ensures all arguments are integers" do
|
|
|
|
-> { 2.pow(5, 12.0) }.should raise_error(TypeError, /2nd argument not allowed unless all arguments are integers/)
|
|
|
|
-> { 2.pow(5, Rational(12, 1)) }.should raise_error(TypeError, /2nd argument not allowed unless all arguments are integers/)
|
|
|
|
end
|
2018-01-29 11:08:16 -05:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "raises TypeError for non-numeric value" do
|
|
|
|
-> { 2.pow(5, "12") }.should raise_error(TypeError)
|
|
|
|
-> { 2.pow(5, []) }.should raise_error(TypeError)
|
|
|
|
-> { 2.pow(5, nil) }.should raise_error(TypeError)
|
|
|
|
end
|
2018-01-29 11:08:16 -05:00
|
|
|
|
2020-02-08 05:43:27 -05:00
|
|
|
it "raises a ZeroDivisionError when the given argument is 0" do
|
|
|
|
-> { 2.pow(5, 0) }.should raise_error(ZeroDivisionError)
|
2018-01-29 11:08:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|