aboutsummaryrefslogtreecommitdiffstats
path: root/spec/rubyspec/core/bignum/bit_length_spec.rb
blob: 1c4c518345ff8fe5e758c7a99363b2632ee6e37e (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
require File.expand_path('../../../spec_helper', __FILE__)

describe "Bignum#bit_length" do
  it "returns the position of the leftmost bit of a positive number" do
    (2**1000-1).bit_length.should == 1000
    (2**1000).bit_length.should == 1001
    (2**1000+1).bit_length.should == 1001

    (2**10000-1).bit_length.should == 10000
    (2**10000).bit_length.should == 10001
    (2**10000+1).bit_length.should == 10001

    (1 << 100).bit_length.should == 101
    (1 << 100).succ.bit_length.should == 101
    (1 << 100).pred.bit_length.should == 100
    (1 << 10000).bit_length.should == 10001
  end

  it "returns the position of the leftmost 0 bit of a negative number" do
    (-2**10000-1).bit_length.should == 10001
    (-2**10000).bit_length.should == 10000
    (-2**10000+1).bit_length.should == 10000

    (-2**1000-1).bit_length.should == 1001
    (-2**1000).bit_length.should == 1000
    (-2**1000+1).bit_length.should == 1000

    ((-1 << 100)-1).bit_length.should == 101
    ((-1 << 100)-1).succ.bit_length.should == 100
    ((-1 << 100)-1).pred.bit_length.should == 101
    ((-1 << 10000)-1).bit_length.should == 10001
  end
end