diff options
Diffstat (limited to 'spec/ruby/core/range')
-rw-r--r-- | spec/ruby/core/range/bsearch_spec.rb | 121 | ||||
-rw-r--r-- | spec/ruby/core/range/count_spec.rb | 14 | ||||
-rw-r--r-- | spec/ruby/core/range/each_spec.rb | 12 | ||||
-rw-r--r-- | spec/ruby/core/range/equal_value_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/range/first_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/range/inspect_spec.rb | 12 | ||||
-rw-r--r-- | spec/ruby/core/range/max_spec.rb | 19 | ||||
-rw-r--r-- | spec/ruby/core/range/min_spec.rb | 18 | ||||
-rw-r--r-- | spec/ruby/core/range/size_spec.rb | 16 | ||||
-rw-r--r-- | spec/ruby/core/range/to_a_spec.rb | 10 |
10 files changed, 223 insertions, 11 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 diff --git a/spec/ruby/core/range/count_spec.rb b/spec/ruby/core/range/count_spec.rb new file mode 100644 index 0000000000..f6f60fa054 --- /dev/null +++ b/spec/ruby/core/range/count_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../spec_helper' + +describe "Range#count" do + ruby_version_is "2.7" do + it "returns Infinity for beginless ranges without arguments or blocks" do + inf = Float::INFINITY + eval("('a'...)").count.should == inf + eval("(7..)").count.should == inf + eval("(...'a')").count.should == inf + eval("(...nil)").count.should == inf + eval("(..10.0)").count.should == inf + end + end +end diff --git a/spec/ruby/core/range/each_spec.rb b/spec/ruby/core/range/each_spec.rb index 8935d86829..1dce9c1f1e 100644 --- a/spec/ruby/core/range/each_spec.rb +++ b/spec/ruby/core/range/each_spec.rb @@ -32,6 +32,12 @@ describe "Range#each" do a.should == [x, y] end + it "works for non-ASCII ranges" do + a = [] + ('Σ'..'Ω').each { |i| a << i } + a.should == ["Σ", "Τ", "Υ", "Φ", "Χ", "Ψ", "Ω"] + end + ruby_version_is "2.6" do it "works with endless ranges" do a = [] @@ -54,6 +60,12 @@ describe "Range#each" do end end + ruby_version_is "2.7" do + it "raises a TypeError beginless ranges" do + -> { eval("(..2)").each { |x| x } }.should raise_error(TypeError) + end + end + it "raises a TypeError if the first element does not respond to #succ" do -> { (0.5..2.4).each { |i| i } }.should raise_error(TypeError) diff --git a/spec/ruby/core/range/equal_value_spec.rb b/spec/ruby/core/range/equal_value_spec.rb index 43d5e0e38a..f88d3029bb 100644 --- a/spec/ruby/core/range/equal_value_spec.rb +++ b/spec/ruby/core/range/equal_value_spec.rb @@ -13,4 +13,10 @@ describe "Range#==" do eval("(1.0..)").should == eval("(1.0..)") end end + + ruby_version_is "2.7" do + it "returns true if the endpoints are == for beginless ranges" do + eval("(...10)").should == eval("(...10)") + end + end end diff --git a/spec/ruby/core/range/first_spec.rb b/spec/ruby/core/range/first_spec.rb index 5f073b48ee..c5c90800ac 100644 --- a/spec/ruby/core/range/first_spec.rb +++ b/spec/ruby/core/range/first_spec.rb @@ -46,4 +46,10 @@ describe "Range#first" do it "raises a TypeError when passed a String" do -> { (2..3).first("1") }.should raise_error(TypeError) end + + ruby_version_is "2.7" do + it "raises a RangeError when called on an beginless range" do + -> { eval("(..1)").first }.should raise_error(RangeError) + end + end end diff --git a/spec/ruby/core/range/inspect_spec.rb b/spec/ruby/core/range/inspect_spec.rb index d1072531e9..6a206a0355 100644 --- a/spec/ruby/core/range/inspect_spec.rb +++ b/spec/ruby/core/range/inspect_spec.rb @@ -19,6 +19,18 @@ describe "Range#inspect" do end end + ruby_version_is '2.7' do + it "works for beginless ranges" do + eval("(..1)").inspect.should == "..1" + eval("(...0.1)").inspect.should == "...0.1" + end + + it "works for nil ... nil ranges" do + eval("(..nil)").inspect.should == "nil..nil" + eval("(nil...)").inspect.should == "nil...nil" + end + end + ruby_version_is ''...'2.7' do it "returns a tainted string if either end is tainted" do (("a".taint)..."c").inspect.tainted?.should be_true diff --git a/spec/ruby/core/range/max_spec.rb b/spec/ruby/core/range/max_spec.rb index 728053cefd..f2672170d9 100644 --- a/spec/ruby/core/range/max_spec.rb +++ b/spec/ruby/core/range/max_spec.rb @@ -51,6 +51,19 @@ describe "Range#max" do -> { eval("(1..)").max }.should raise_error(RangeError) end end + + ruby_version_is "3.0" do + it "returns the end point for beginless ranges" do + eval("(..1)").max.should == 1 + eval("(..1.0)").max.should == 1.0 + end + + it "raises for an exclusive beginless range" do + -> { + eval("(...1)").max + }.should raise_error(TypeError, 'cannot exclude end value with non Integer begin value') + end + end end describe "Range#max given a block" do @@ -85,4 +98,10 @@ describe "Range#max given a block" do ('z'..'l').max {|x,y| x <=> y}.should be_nil (5...5).max {|x,y| x <=> y}.should be_nil end + + ruby_version_is "2.7" do + it "raises RangeError when called with custom comparison method on an beginless range" do + -> { eval("(..1)").max {|a, b| a} }.should raise_error(RangeError) + end + end end diff --git a/spec/ruby/core/range/min_spec.rb b/spec/ruby/core/range/min_spec.rb index f1dff73e6d..ffd40f3a07 100644 --- a/spec/ruby/core/range/min_spec.rb +++ b/spec/ruby/core/range/min_spec.rb @@ -38,6 +38,19 @@ describe "Range#min" do time_end = Time.now + 1.0 (time_start...time_end).min.should equal(time_start) end + + ruby_version_is "2.6" do + it "returns the start point for endless ranges" do + eval("(1..)").min.should == 1 + eval("(1.0...)").min.should == 1.0 + end + end + + ruby_version_is "2.7" do + it "raises RangeError when called on an beginless range" do + -> { eval("(..1)").min }.should raise_error(RangeError) + end + end end describe "Range#min given a block" do @@ -74,9 +87,8 @@ describe "Range#min given a block" do end ruby_version_is "2.6" do - it "returns the start point for endless ranges" do - eval("(1..)").min.should == 1 - eval("(1.0...)").min.should == 1.0 + it "raises RangeError when called with custom comparison method on an endless range" do + -> { eval("(1..)").min {|a, b| a} }.should raise_error(RangeError) end end end diff --git a/spec/ruby/core/range/size_spec.rb b/spec/ruby/core/range/size_spec.rb index b687342b72..4bf525e541 100644 --- a/spec/ruby/core/range/size_spec.rb +++ b/spec/ruby/core/range/size_spec.rb @@ -25,10 +25,24 @@ describe "Range#size" do end ruby_version_is "2.6" do - it 'returns Float::INFINITY for endless ranges' do + it 'returns Float::INFINITY for endless ranges if the start is numeric' do eval("(1..)").size.should == Float::INFINITY eval("(0.5...)").size.should == Float::INFINITY end + + it 'returns nil for endless ranges if the start is not numeric' do + eval("('z'..)").size.should == nil + eval("([]...)").size.should == nil + end + end + + ruby_version_is "2.7" do + it 'returns Float::INFINITY for all beginless ranges' do + eval("(..1)").size.should == Float::INFINITY + eval("(...0.5)").size.should == Float::INFINITY + eval("(..nil)").size.should == Float::INFINITY + eval("(...'o')").size.should == Float::INFINITY + end end it "returns nil if first and last are not Numeric" do diff --git a/spec/ruby/core/range/to_a_spec.rb b/spec/ruby/core/range/to_a_spec.rb index b0067f0e07..08f50e4d9e 100644 --- a/spec/ruby/core/range/to_a_spec.rb +++ b/spec/ruby/core/range/to_a_spec.rb @@ -20,9 +20,19 @@ describe "Range#to_a" do (:A..:z).to_a.size.should == 58 end + it "works for non-ASCII ranges" do + ('Σ'..'Ω').to_a.should == ["Σ", "Τ", "Υ", "Φ", "Χ", "Ψ", "Ω"] + end + ruby_version_is "2.6" do it "throws an exception for endless ranges" do -> { eval("(1..)").to_a }.should raise_error(RangeError) end end + + ruby_version_is "2.7" do + it "throws an exception for beginless ranges" do + -> { eval("(..1)").to_a }.should raise_error(TypeError) + end + end end |