diff options
Diffstat (limited to 'spec/ruby/core/range/bsearch_spec.rb')
| -rw-r--r-- | spec/ruby/core/range/bsearch_spec.rb | 136 |
1 files changed, 83 insertions, 53 deletions
diff --git a/spec/ruby/core/range/bsearch_spec.rb b/spec/ruby/core/range/bsearch_spec.rb index 9c93671d85..151a1798cf 100644 --- a/spec/ruby/core/range/bsearch_spec.rb +++ b/spec/ruby/core/range/bsearch_spec.rb @@ -3,38 +3,46 @@ require_relative '../enumerable/shared/enumeratorized' describe "Range#bsearch" do it "returns an Enumerator when not passed a block" do - (0..1).bsearch.should be_an_instance_of(Enumerator) + (0..1).bsearch.should.instance_of?(Enumerator) end it_behaves_like :enumeratorized_with_unknown_size, :bsearch, (1..3) it "raises a TypeError if the block returns an Object" do - -> { (0..1).bsearch { Object.new } }.should raise_error(TypeError) + -> { (0..1).bsearch { Object.new } }.should.raise(TypeError, "wrong argument type Object (must be numeric, true, false or nil)") end - it "raises a TypeError if the block returns a String" do - -> { (0..1).bsearch { "1" } }.should raise_error(TypeError) + it "raises a TypeError if the block returns a String and boundaries are Integer values" do + -> { (0..1).bsearch { "1" } }.should.raise(TypeError, "wrong argument type String (must be numeric, true, false or nil)") + end + + it "raises a TypeError if the block returns a String and boundaries are Float values" do + -> { (0.0..1.0).bsearch { "1" } }.should.raise(TypeError, "wrong argument type String (must be numeric, true, false or nil)") end it "raises a TypeError if the Range has Object values" do value = mock("range bsearch") r = Range.new value, value - -> { r.bsearch { true } }.should raise_error(TypeError) + -> { r.bsearch { true } }.should.raise(TypeError, "can't do binary search for MockObject") end it "raises a TypeError if the Range has String values" do - -> { ("a".."e").bsearch { true } }.should raise_error(TypeError) + -> { ("a".."e").bsearch { true } }.should.raise(TypeError, "can't do binary search for String") + end + + it "raises TypeError when non-Numeric begin/end and block not passed" do + -> { ("a".."e").bsearch }.should.raise(TypeError, "can't do binary search for String") end context "with Integer values" do context "with a block returning true or false" do it "returns nil if the block returns false for every element" do - (0...3).bsearch { |x| x > 3 }.should be_nil + (0...3).bsearch { |x| x > 3 }.should == nil end it "returns nil if the block returns nil for every element" do - (0..3).bsearch { |x| nil }.should be_nil + (0..3).bsearch { |x| nil }.should == nil end it "returns minimum element if the block returns true for every element" do @@ -54,21 +62,21 @@ describe "Range#bsearch" do context "with a block returning negative, zero, positive numbers" do it "returns nil if the block returns less than zero for every element" do - (0..3).bsearch { |x| x <=> 5 }.should be_nil + (0..3).bsearch { |x| x <=> 5 }.should == nil end it "returns nil if the block returns greater than zero for every element" do - (0..3).bsearch { |x| x <=> -1 }.should be_nil + (0..3).bsearch { |x| x <=> -1 }.should == nil end it "returns nil if the block never returns zero" do - (0..3).bsearch { |x| x < 2 ? 1 : -1 }.should be_nil + (0..3).bsearch { |x| x < 2 ? 1 : -1 }.should == nil end it "accepts (+/-)Float::INFINITY from the block" do - (0..4).bsearch { |x| Float::INFINITY }.should be_nil - (0..4).bsearch { |x| -Float::INFINITY }.should be_nil + (0..4).bsearch { |x| Float::INFINITY }.should == nil + (0..4).bsearch { |x| -Float::INFINITY }.should == nil end it "returns an element at an index for which block returns 0.0" do @@ -78,7 +86,7 @@ describe "Range#bsearch" do it "returns an element at an index for which block returns 0" do result = (0..4).bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 } - [1, 2].should include(result) + [1, 2].should.include?(result) end end @@ -94,16 +102,20 @@ describe "Range#bsearch" do (4..2).bsearch { 0 }.should == nil (4..2).bsearch { -1 }.should == nil end + + it "returns enumerator when block not passed" do + (0...3).bsearch.kind_of?(Enumerator).should == true + end end context "with Float values" do context "with a block returning true or false" do it "returns nil if the block returns false for every element" do - (0.1...2.3).bsearch { |x| x > 3 }.should be_nil + (0.1...2.3).bsearch { |x| x > 3 }.should == nil end it "returns nil if the block returns nil for every element" do - (-0.0..2.3).bsearch { |x| nil }.should be_nil + (-0.0..2.3).bsearch { |x| nil }.should == nil end it "returns minimum element if the block returns true for every element" do @@ -151,21 +163,20 @@ describe "Range#bsearch" do context "with a block returning negative, zero, positive numbers" do it "returns nil if the block returns less than zero for every element" do - (-2.0..3.2).bsearch { |x| x <=> 5 }.should be_nil + (-2.0..3.2).bsearch { |x| x <=> 5 }.should == nil end it "returns nil if the block returns greater than zero for every element" do - (0.3..3.0).bsearch { |x| x <=> -1 }.should be_nil - + (0.3..3.0).bsearch { |x| x <=> -1 }.should == nil end it "returns nil if the block never returns zero" do - (0.2..2.3).bsearch { |x| x < 2 ? 1 : -1 }.should be_nil + (0.2..2.3).bsearch { |x| x < 2 ? 1 : -1 }.should == nil end it "accepts (+/-)Float::INFINITY from the block" do - (0.1..4.5).bsearch { |x| Float::INFINITY }.should be_nil - (-5.0..4.0).bsearch { |x| -Float::INFINITY }.should be_nil + (0.1..4.5).bsearch { |x| Float::INFINITY }.should == nil + (-5.0..4.0).bsearch { |x| -Float::INFINITY }.should == nil end it "returns an element at an index for which block returns 0.0" do @@ -213,6 +224,10 @@ describe "Range#bsearch" do (0...inf).bsearch { |x| x >= Float::MAX ? 0 : 1 }.should == Float::MAX end end + + it "returns enumerator when block not passed" do + (0.1...2.3).bsearch.kind_of?(Enumerator).should == true + end end context "with endless ranges and Integer values" do @@ -229,15 +244,15 @@ describe "Range#bsearch" do 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("(0..)").bsearch { |x| -1 }.should be_nil + eval("(0..)").bsearch { |x| -1 }.should == nil end it "returns nil if the block never returns zero" do - eval("(0..)").bsearch { |x| x > 5 ? -1 : 1 }.should be_nil + eval("(0..)").bsearch { |x| x > 5 ? -1 : 1 }.should == nil end it "accepts -Float::INFINITY from the block" do - eval("(0..)").bsearch { |x| -Float::INFINITY }.should be_nil + eval("(0..)").bsearch { |x| -Float::INFINITY }.should == nil end it "returns an element at an index for which block returns 0.0" do @@ -247,21 +262,25 @@ 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, 3].should include(result) + [1, 2, 3].should.include?(result) end end + + it "returns enumerator when block not passed" do + eval("(-2..)").bsearch.kind_of?(Enumerator).should == true + end end context "with endless ranges and Float values" do context "with a block returning true or false" do it "returns nil if the block returns false 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 + eval("(0.1..)").bsearch { |x| x < 0.0 }.should == nil + eval("(0.1...)").bsearch { |x| x < 0.0 }.should == nil end it "returns nil if the block returns nil for every element" do - eval("(-0.0..)").bsearch { |x| nil }.should be_nil - eval("(-0.0...)").bsearch { |x| nil }.should be_nil + eval("(-0.0..)").bsearch { |x| nil }.should == nil + eval("(-0.0...)").bsearch { |x| nil }.should == nil end it "returns minimum element if the block returns true for every element" do @@ -285,22 +304,22 @@ describe "Range#bsearch" do 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("(-2.0..)").bsearch { |x| -1 }.should be_nil - eval("(-2.0...)").bsearch { |x| -1 }.should be_nil + eval("(-2.0..)").bsearch { |x| -1 }.should == nil + eval("(-2.0...)").bsearch { |x| -1 }.should == nil end it "returns nil if the block returns greater than zero for every element" do - eval("(0.3..)").bsearch { |x| 1 }.should be_nil - eval("(0.3...)").bsearch { |x| 1 }.should be_nil + eval("(0.3..)").bsearch { |x| 1 }.should == nil + eval("(0.3...)").bsearch { |x| 1 }.should == nil end it "returns nil if the block never returns zero" do - eval("(0.2..)").bsearch { |x| x < 2 ? 1 : -1 }.should be_nil + eval("(0.2..)").bsearch { |x| x < 2 ? 1 : -1 }.should == nil end it "accepts (+/-)Float::INFINITY from the block" do - eval("(0.1..)").bsearch { |x| Float::INFINITY }.should be_nil - eval("(-5.0..)").bsearch { |x| -Float::INFINITY }.should be_nil + eval("(0.1..)").bsearch { |x| Float::INFINITY }.should == nil + eval("(-5.0..)").bsearch { |x| -Float::INFINITY }.should == nil end it "returns an element at an index for which block returns 0.0" do @@ -327,8 +346,11 @@ describe "Range#bsearch" do eval("(0.0...)").bsearch { 0 }.should != inf end end - end + it "returns enumerator when block not passed" do + eval("(0.1..)").bsearch.kind_of?(Enumerator).should == true + end + end context "with beginless ranges and Integer values" do context "with a block returning true or false" do @@ -340,15 +362,15 @@ describe "Range#bsearch" do context "with a block returning negative, zero, positive numbers" do it "returns nil if the block returns greater than zero for every element" do - (..0).bsearch { |x| 1 }.should be_nil + (..0).bsearch { |x| 1 }.should == nil end it "returns nil if the block never returns zero" do - (..0).bsearch { |x| x > 5 ? -1 : 1 }.should be_nil + (..0).bsearch { |x| x > 5 ? -1 : 1 }.should == nil end it "accepts Float::INFINITY from the block" do - (..0).bsearch { |x| Float::INFINITY }.should be_nil + (..0).bsearch { |x| Float::INFINITY }.should == nil end it "returns an element at an index for which block returns 0.0" do @@ -358,21 +380,25 @@ describe "Range#bsearch" do it "returns an element at an index for which block returns 0" do result = (...10).bsearch { |x| x < 1 ? 1 : x > 3 ? -1 : 0 } - [1, 2, 3].should include(result) + [1, 2, 3].should.include?(result) end end + + it "returns enumerator when block not passed" do + (..10).bsearch.kind_of?(Enumerator).should == true + 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 - (..-0.1).bsearch { |x| x > 0.0 }.should be_nil - (...-0.1).bsearch { |x| x > 0.0 }.should be_nil + (..-0.1).bsearch { |x| x > 0.0 }.should == nil + (...-0.1).bsearch { |x| x > 0.0 }.should == nil end it "returns nil if the block returns nil for every element" do - (..-0.1).bsearch { |x| nil }.should be_nil - (...-0.1).bsearch { |x| nil }.should be_nil + (..-0.1).bsearch { |x| nil }.should == nil + (...-0.1).bsearch { |x| nil }.should == nil end it "returns the smallest element for which block returns true" do @@ -391,22 +417,22 @@ describe "Range#bsearch" do context "with a block returning negative, zero, positive numbers" do it "returns nil if the block returns less than zero for every element" do - (..5.0).bsearch { |x| -1 }.should be_nil - (...5.0).bsearch { |x| -1 }.should be_nil + (..5.0).bsearch { |x| -1 }.should == nil + (...5.0).bsearch { |x| -1 }.should == nil end it "returns nil if the block returns greater than zero for every element" do - (..1.1).bsearch { |x| 1 }.should be_nil - (...1.1).bsearch { |x| 1 }.should be_nil + (..1.1).bsearch { |x| 1 }.should == nil + (...1.1).bsearch { |x| 1 }.should == nil end it "returns nil if the block never returns zero" do - (..6.3).bsearch { |x| x < 2 ? 1 : -1 }.should be_nil + (..6.3).bsearch { |x| x < 2 ? 1 : -1 }.should == nil end it "accepts (+/-)Float::INFINITY from the block" do - (..5.0).bsearch { |x| Float::INFINITY }.should be_nil - (..7.0).bsearch { |x| -Float::INFINITY }.should be_nil + (..5.0).bsearch { |x| Float::INFINITY }.should == nil + (..7.0).bsearch { |x| -Float::INFINITY }.should == nil end it "returns an element at an index for which block returns 0.0" do @@ -432,5 +458,9 @@ describe "Range#bsearch" do (...inf).bsearch { |x| 3 - x }.should == 3 end end + + it "returns enumerator when block not passed" do + (..-0.1).bsearch.kind_of?(Enumerator).should == true + end end end |
