File: pow_spec.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (51 lines) | stat: -rw-r--r-- 1,796 bytes parent folder | download | duplicates (4)
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
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/exponent'

describe "Integer#pow" do
  context "one argument is passed" do
    it_behaves_like :integer_exponent, :pow
  end

  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

    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

    it "handles sign like #divmod does" do
       2.pow(5,  12).should ==  8
       2.pow(5, -12).should == -4
      -2.pow(5,  12).should ==  4
      -2.pow(5, -12).should == -8
    end

    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

    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

    it "raises a ZeroDivisionError when the given argument is 0" do
      -> { 2.pow(5, 0) }.should raise_error(ZeroDivisionError)
    end

    it "raises a RangeError when the first argument is negative and the second argument is present" do
      -> { 2.pow(-5, 1) }.should raise_error(RangeError)
    end
  end
end