summaryrefslogtreecommitdiff
path: root/spec/ruby/core/range/bsearch_spec.rb
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2020-12-27 17:35:32 +0100
committerBenoit Daloze <eregontp@gmail.com>2020-12-27 17:35:32 +0100
commit727c97da1977544c91b9b3677811da3a44af7d53 (patch)
tree4f027117edad10789db57ff4b83242753a89e39d /spec/ruby/core/range/bsearch_spec.rb
parent267bed0cd91711e2a8c79219e97431ba22137b01 (diff)
Update to ruby/spec@4ce9f41
Diffstat (limited to 'spec/ruby/core/range/bsearch_spec.rb')
-rw-r--r--spec/ruby/core/range/bsearch_spec.rb121
1 files changed, 114 insertions, 7 deletions
diff --git a/spec/ruby/core/range/bsearch_spec.rb b/spec/ruby/core/range/bsearch_spec.rb
index 38ccd35170..d31c5d5d51 100644
--- a/spec/ruby/core/range/bsearch_spec.rb
+++ b/spec/ruby/core/range/bsearch_spec.rb
@@ -126,8 +126,8 @@ describe "Range#bsearch" do
inf = Float::INFINITY
(0..inf).bsearch { |x| x == inf }.should == inf
(0...inf).bsearch { |x| x == inf }.should == nil
- (-inf..0).bsearch { |x| x == -inf }.should == nil
- (-inf...0).bsearch { |x| x == -inf }.should == nil
+ (-inf..0).bsearch { |x| x != -inf }.should == -Float::MAX
+ (-inf...0).bsearch { |x| x != -inf }.should == -Float::MAX
(inf..inf).bsearch { |x| true }.should == inf
(inf...inf).bsearch { |x| true }.should == nil
(-inf..-inf).bsearch { |x| true }.should == -inf
@@ -194,10 +194,10 @@ describe "Range#bsearch" do
it "works with infinity bounds" do
inf = Float::INFINITY
- (0..inf).bsearch { |x| x == inf ? 0 : -1 }.should == nil
- (0...inf).bsearch { |x| x == inf ? 0 : -1 }.should == nil
- (-inf...0).bsearch { |x| x == -inf ? 0 : 1 }.should == nil
- (-inf..0).bsearch { |x| x == -inf ? 0 : 1 }.should == nil
+ (0..inf).bsearch { |x| x == inf ? 0 : 1 }.should == inf
+ (0...inf).bsearch { |x| x == inf ? 0 : 1 }.should == nil
+ (-inf...0).bsearch { |x| x == -inf ? 0 : -1 }.should == -inf
+ (-inf..0).bsearch { |x| x == -inf ? 0 : -1 }.should == -inf
(inf..inf).bsearch { 0 }.should == inf
(inf...inf).bsearch { 0 }.should == nil
(-inf..-inf).bsearch { 0 }.should == -inf
@@ -248,7 +248,7 @@ describe "Range#bsearch" do
it "returns an element at an index for which block returns 0" do
result = eval("(0..)").bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 }
- [1, 2].should include(result)
+ [1, 2, 3].should include(result)
end
end
end
@@ -330,4 +330,111 @@ describe "Range#bsearch" do
end
end
end
+
+
+ ruby_version_is "2.7" do
+ context "with beginless ranges and Integer values" do
+ context "with a block returning true or false" do
+ it "returns the smallest element for which block returns true" do
+ eval("(..10)").bsearch { |x| x >= 2 }.should == 2
+ eval("(...-1)").bsearch { |x| x >= -10 }.should == -10
+ end
+ end
+
+ context "with a block returning negative, zero, positive numbers" do
+ it "returns nil if the block returns greater than zero for every element" do
+ eval("(..0)").bsearch { |x| 1 }.should be_nil
+ end
+
+ it "returns nil if the block never returns zero" do
+ eval("(..0)").bsearch { |x| x > 5 ? -1 : 1 }.should be_nil
+ end
+
+ it "accepts Float::INFINITY from the block" do
+ eval("(..0)").bsearch { |x| Float::INFINITY }.should be_nil
+ end
+
+ it "returns an element at an index for which block returns 0.0" do
+ result = eval("(..10)").bsearch { |x| x < 2 ? 1.0 : x > 2 ? -1.0 : 0.0 }
+ result.should == 2
+ end
+
+ it "returns an element at an index for which block returns 0" do
+ result = eval("(...10)").bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 }
+ [1, 2, 3].should include(result)
+ end
+ end
+ end
+
+ context "with beginless ranges and Float values" do
+ context "with a block returning true or false" do
+ it "returns nil if the block returns true for every element" do
+ eval("(..-0.1)").bsearch { |x| x > 0.0 }.should be_nil
+ eval("(...-0.1)").bsearch { |x| x > 0.0 }.should be_nil
+ end
+
+ it "returns nil if the block returns nil for every element" do
+ eval("(..-0.1)").bsearch { |x| nil }.should be_nil
+ eval("(...-0.1)").bsearch { |x| nil }.should be_nil
+ end
+
+ it "returns the smallest element for which block returns true" do
+ eval("(..10)").bsearch { |x| x >= 2 }.should == 2
+ eval("(..10)").bsearch { |x| x >= 1 }.should == 1
+ end
+
+ it "works with infinity bounds" do
+ inf = Float::INFINITY
+ eval("(..inf)").bsearch { |x| true }.should == -inf
+ eval("(...inf)").bsearch { |x| true }.should == -inf
+ eval("(..-inf)").bsearch { |x| true }.should == -inf
+ eval("(...-inf)").bsearch { |x| true }.should == nil
+ end
+ end
+
+ context "with a block returning negative, zero, positive numbers" do
+ it "returns nil if the block returns less than zero for every element" do
+ eval("(..5.0)").bsearch { |x| -1 }.should be_nil
+ eval("(...5.0)").bsearch { |x| -1 }.should be_nil
+ end
+
+ it "returns nil if the block returns greater than zero for every element" do
+ eval("(..1.1)").bsearch { |x| 1 }.should be_nil
+ eval("(...1.1)").bsearch { |x| 1 }.should be_nil
+ end
+
+ it "returns nil if the block never returns zero" do
+ eval("(..6.3)").bsearch { |x| x < 2 ? 1 : -1 }.should be_nil
+ end
+
+ it "accepts (+/-)Float::INFINITY from the block" do
+ eval("(..5.0)").bsearch { |x| Float::INFINITY }.should be_nil
+ eval("(..7.0)").bsearch { |x| -Float::INFINITY }.should be_nil
+ end
+
+ it "returns an element at an index for which block returns 0.0" do
+ result = eval("(..8.0)").bsearch { |x| x < 2 ? 1.0 : x > 2 ? -1.0 : 0.0 }
+ result.should == 2
+ end
+
+ it "returns an element at an index for which block returns 0" do
+ result = eval("(..8.0)").bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 }
+ result.should >= 1
+ result.should <= 3
+ end
+
+ it "works with infinity bounds" do
+ inf = Float::INFINITY
+ eval("(..-inf)").bsearch { |x| 1 }.should == nil
+ eval("(...-inf)").bsearch { |x| 1 }.should == nil
+ eval("(..inf)").bsearch { |x| x == inf ? 0 : 1 }.should == inf
+ eval("(...inf)").bsearch { |x| x == inf ? 0 : 1 }.should == nil
+ eval("(..-inf)").bsearch { |x| x == -inf ? 0 : -1 }.should == -inf
+ eval("(...-inf)").bsearch { |x| x == -inf ? 0 : -1 }.should == nil
+ eval("(..inf)").bsearch { |x| 3 - x }.should == 3
+ eval("(...inf)").bsearch { |x| 3 - x }.should == 3
+ end
+ end
+ end
+ end
end