summaryrefslogtreecommitdiff
path: root/spec/ruby/core/integer
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2022-04-25 14:53:54 +0200
committerBenoit Daloze <eregontp@gmail.com>2022-04-25 14:53:54 +0200
commit45cf4f218728a15eb36d14a6c9912086525f5e3f (patch)
tree2aa93fadcb904c226f722dde47827098b87a9846 /spec/ruby/core/integer
parent6ae81d49b52563a6720d666a6118ffa6e484f398 (diff)
Update to ruby/spec@3affe1e
Diffstat (limited to 'spec/ruby/core/integer')
-rw-r--r--spec/ruby/core/integer/chr_spec.rb20
-rw-r--r--spec/ruby/core/integer/element_reference_spec.rb122
-rw-r--r--spec/ruby/core/integer/fdiv_spec.rb46
3 files changed, 115 insertions, 73 deletions
diff --git a/spec/ruby/core/integer/chr_spec.rb b/spec/ruby/core/integer/chr_spec.rb
index a919d29f12..3e13f45480 100644
--- a/spec/ruby/core/integer/chr_spec.rb
+++ b/spec/ruby/core/integer/chr_spec.rb
@@ -245,16 +245,14 @@ describe "Integer#chr with an encoding argument" do
end
end
- ruby_version_is "2.7" do
- it 'returns a String encoding self interpreted as a codepoint in the CESU-8 encoding' do
- # see more details here https://en.wikipedia.org/wiki/CESU-8
- # code points from U+0000 to U+FFFF is encoded in the same way as in UTF-8
- 0x0045.chr(Encoding::CESU_8).bytes.should == 0x0045.chr(Encoding::UTF_8).bytes
-
- # code points in range from U+10000 to U+10FFFF is CESU-8 data containing a 6-byte surrogate pair,
- # which decodes to a 4-byte UTF-8 string
- 0x10400.chr(Encoding::CESU_8).bytes.should != 0x10400.chr(Encoding::UTF_8).bytes
- 0x10400.chr(Encoding::CESU_8).bytes.to_a.should == [0xED, 0xA0, 0x81, 0xED, 0xB0, 0x80]
- end
+ it 'returns a String encoding self interpreted as a codepoint in the CESU-8 encoding' do
+ # see more details here https://en.wikipedia.org/wiki/CESU-8
+ # code points from U+0000 to U+FFFF is encoded in the same way as in UTF-8
+ 0x0045.chr(Encoding::CESU_8).bytes.should == 0x0045.chr(Encoding::UTF_8).bytes
+
+ # code points in range from U+10000 to U+10FFFF is CESU-8 data containing a 6-byte surrogate pair,
+ # which decodes to a 4-byte UTF-8 string
+ 0x10400.chr(Encoding::CESU_8).bytes.should != 0x10400.chr(Encoding::UTF_8).bytes
+ 0x10400.chr(Encoding::CESU_8).bytes.to_a.should == [0xED, 0xA0, 0x81, 0xED, 0xB0, 0x80]
end
end
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
diff --git a/spec/ruby/core/integer/fdiv_spec.rb b/spec/ruby/core/integer/fdiv_spec.rb
index 6de170278f..d99a19eb0f 100644
--- a/spec/ruby/core/integer/fdiv_spec.rb
+++ b/spec/ruby/core/integer/fdiv_spec.rb
@@ -9,6 +9,52 @@ describe "Integer#fdiv" do
8.fdiv(bignum_value).should be_close(8.673617379884035e-19, TOLERANCE)
end
+ it "performs floating-point division between self bignum and a bignum" do
+ num = 1000000000000000000000000000000000048148248609680896326399448564623182963452541226153892315137780403285956264146010000000000000000000000000000000000048148248609680896326399448564623182963452541226153892315137780403285956264146010000000000000000000000000000000000048148248609680896326399448564623182963452541226153892315137780403285956264146009
+ den = 2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ num.fdiv(den).should == 500.0
+ end
+
+ it "rounds to the correct value for bignums" do
+ den = 9 * 10**342
+
+ num = 1 * 10**344
+ num.fdiv(den).should == 11.11111111111111
+
+ num = 1 * 10**343
+ num.fdiv(den).should == 1.1111111111111112
+
+ num = 1 * 10**342
+ num.fdiv(den).should == 0.1111111111111111
+
+ num = 2 * 10**342
+ num.fdiv(den).should == 0.2222222222222222
+
+ num = 3 * 10**342
+ num.fdiv(den).should == 0.3333333333333333
+
+ num = 4 * 10**342
+ num.fdiv(den).should == 0.4444444444444444
+
+ num = 5 * 10**342
+ num.fdiv(den).should == 0.5555555555555556
+
+ num = 6 * 10**342
+ num.fdiv(den).should == 0.6666666666666666
+
+ num = 7 * 10**342
+ num.fdiv(den).should == 0.7777777777777778
+
+ num = 8 * 10**342
+ num.fdiv(den).should == 0.8888888888888888
+
+ num = 9 * 10**342
+ num.fdiv(den).should == 1.0
+
+ num = -5 * 10**342
+ num.fdiv(den).should == -0.5555555555555556
+ end
+
it "performs floating-point division between self and a Float" do
8.fdiv(9.0).should be_close(0.888888888888889, TOLERANCE)
end