diff options
author | Benoit Daloze <eregontp@gmail.com> | 2022-04-25 14:53:54 +0200 |
---|---|---|
committer | Benoit Daloze <eregontp@gmail.com> | 2022-04-25 14:53:54 +0200 |
commit | 45cf4f218728a15eb36d14a6c9912086525f5e3f (patch) | |
tree | 2aa93fadcb904c226f722dde47827098b87a9846 /spec/ruby/core/integer/element_reference_spec.rb | |
parent | 6ae81d49b52563a6720d666a6118ffa6e484f398 (diff) | |
download | ruby-45cf4f218728a15eb36d14a6c9912086525f5e3f.tar.gz |
Update to ruby/spec@3affe1e
Diffstat (limited to 'spec/ruby/core/integer/element_reference_spec.rb')
-rw-r--r-- | spec/ruby/core/integer/element_reference_spec.rb | 122 |
1 files changed, 60 insertions, 62 deletions
diff --git a/spec/ruby/core/integer/element_reference_spec.rb b/spec/ruby/core/integer/element_reference_spec.rb index 7197ecdc03..cb7e0dc9b0 100644 --- a/spec/ruby/core/integer/element_reference_spec.rb +++ b/spec/ruby/core/integer/element_reference_spec.rb @@ -79,81 +79,79 @@ describe "Integer#[]" do 3[bignum_value.to_f].should == 0 end - ruby_version_is "2.7" do - context "when index and length passed" do - it "returns specified number of bits from specified position" do - 0b101001101[2, 4].should == 0b0011 - 0b101001101[2, 5].should == 0b10011 - 0b101001101[2, 7].should == 0b1010011 - end + context "when index and length passed" do + it "returns specified number of bits from specified position" do + 0b101001101[2, 4].should == 0b0011 + 0b101001101[2, 5].should == 0b10011 + 0b101001101[2, 7].should == 0b1010011 + end - it "ensures n[i, len] equals to (n >> i) & ((1 << len) - 1)" do - n = 0b101001101; i = 2; len = 4 - n[i, len].should == (n >> i) & ((1 << len) - 1) - end + it "ensures n[i, len] equals to (n >> i) & ((1 << len) - 1)" do + n = 0b101001101; i = 2; len = 4 + n[i, len].should == (n >> i) & ((1 << len) - 1) + end - it "moves start position to the most significant bits when negative index passed" do - 0b000001[-1, 4].should == 0b10 - 0b000001[-2, 4].should == 0b100 - 0b000001[-3, 4].should == 0b1000 - end + it "moves start position to the most significant bits when negative index passed" do + 0b000001[-1, 4].should == 0b10 + 0b000001[-2, 4].should == 0b100 + 0b000001[-3, 4].should == 0b1000 + end - it "ignores negative length" do - 0b101001101[1, -1].should == 0b10100110 - 0b101001101[2, -1].should == 0b1010011 - 0b101001101[3, -1].should == 0b101001 + it "ignores negative length" do + 0b101001101[1, -1].should == 0b10100110 + 0b101001101[2, -1].should == 0b1010011 + 0b101001101[3, -1].should == 0b101001 - 0b101001101[3, -5].should == 0b101001 - 0b101001101[3, -15].should == 0b101001 - 0b101001101[3, -125].should == 0b101001 - end + 0b101001101[3, -5].should == 0b101001 + 0b101001101[3, -15].should == 0b101001 + 0b101001101[3, -125].should == 0b101001 end + end - context "when range passed" do - it "returns bits specified by range" do - 0b101001101[2..5].should == 0b0011 - 0b101001101[2..6].should == 0b10011 - 0b101001101[2..8].should == 0b1010011 - end + context "when range passed" do + it "returns bits specified by range" do + 0b101001101[2..5].should == 0b0011 + 0b101001101[2..6].should == 0b10011 + 0b101001101[2..8].should == 0b1010011 + end - it "ensures n[i..j] equals to (n >> i) & ((1 << (j - i + 1)) - 1)" do - n = 0b101001101; i = 2; j = 5 - n[i..j].should == (n >> i) & ((1 << (j - i + 1)) - 1) - end + it "ensures n[i..j] equals to (n >> i) & ((1 << (j - i + 1)) - 1)" do + n = 0b101001101; i = 2; j = 5 + n[i..j].should == (n >> i) & ((1 << (j - i + 1)) - 1) + end - it "ensures n[i..] equals to (n >> i)" do - eval("0b101001101[3..]").should == 0b101001101 >> 3 - end + it "ensures n[i..] equals to (n >> i)" do + eval("0b101001101[3..]").should == 0b101001101 >> 3 + end - it "moves lower boundary to the most significant bits when negative value passed" do - 0b000001[-1, 4].should == 0b10 - 0b000001[-2, 4].should == 0b100 - 0b000001[-3, 4].should == 0b1000 - end + it "moves lower boundary to the most significant bits when negative value passed" do + 0b000001[-1, 4].should == 0b10 + 0b000001[-2, 4].should == 0b100 + 0b000001[-3, 4].should == 0b1000 + end - it "ignores upper boundary smaller than lower boundary" do - 0b101001101[4..1].should == 0b10100 - 0b101001101[4..2].should == 0b10100 - 0b101001101[-4..-5].should == 0b1010011010000 - end + it "ignores upper boundary smaller than lower boundary" do + 0b101001101[4..1].should == 0b10100 + 0b101001101[4..2].should == 0b10100 + 0b101001101[-4..-5].should == 0b1010011010000 + end + + it "raises FloatDomainError if any boundary is infinity" do + -> { 0x0001[3..Float::INFINITY] }.should raise_error(FloatDomainError, /Infinity/) + -> { 0x0001[-Float::INFINITY..3] }.should raise_error(FloatDomainError, /-Infinity/) + end - it "raises FloatDomainError if any boundary is infinity" do - -> { 0x0001[3..Float::INFINITY] }.should raise_error(FloatDomainError, /Infinity/) - -> { 0x0001[-Float::INFINITY..3] }.should raise_error(FloatDomainError, /-Infinity/) + context "when passed (..i)" do + it "returns 0 if all i bits equal 0" do + eval("0b10000[..1]").should == 0 + eval("0b10000[..2]").should == 0 + eval("0b10000[..3]").should == 0 end - context "when passed (..i)" do - it "returns 0 if all i bits equal 0" do - eval("0b10000[..1]").should == 0 - eval("0b10000[..2]").should == 0 - eval("0b10000[..3]").should == 0 - end - - it "raises ArgumentError if any of i bit equals 1" do - -> { - eval("0b111110[..3]") - }.should raise_error(ArgumentError, /The beginless range for Integer#\[\] results in infinity/) - end + it "raises ArgumentError if any of i bit equals 1" do + -> { + eval("0b111110[..3]") + }.should raise_error(ArgumentError, /The beginless range for Integer#\[\] results in infinity/) end end end |