Skip to content

Commit

Permalink
wip, split specs into multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
lxxxvi committed Oct 6, 2021
1 parent 8173484 commit 8b7fe61
Showing 1 changed file with 286 additions and 16 deletions.
302 changes: 286 additions & 16 deletions core/array/shared/slice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -534,22 +534,292 @@ def to.to_int() -2 end
end

ruby_version_is "3.0" do
it "can be sliced with Enumerator::ArithmeticSequence" do
a = [0, 1, 2, 3, 4, 5]
a.send(@method, eval("(0..).step(1)")).should == [0, 1, 2, 3, 4, 5]
a.send(@method, eval("(0..).step(2)")).should == [0, 2, 4]
a.send(@method, eval("(0..).step(3)")).should == [0, 3]
a.send(@method, eval("(0..).step(10)")).should == [0]
a.send(@method, eval("(0..1).step(2)")).should == [0]
a.send(@method, eval("(..0).step(1)")).should == [0]
a.send(@method, eval("(...0).step(1)")).should == []
a.send(@method, eval("(2..).step(2)")).should == [2, 4]
a.send(@method, eval("(2..-2).step(2)")).should == [2, 4]
a.send(@method, eval("(2...-2).step(2)")).should == [2]
a.send(@method, eval("(2..).step(-2)")).should == [2, 0]
a.send(@method, eval("(2..).step(-3)")).should == [2]
a.send(@method, eval("(..-2).step(2)")).should == [0, 2, 4]
a.send(@method, eval("(...-2).step(2)")).should == [0, 2]
describe "can be sliced with Enumerator::ArithmeticSequence" do
before :each do
@array = [0, 1, 2, 3, 4, 5]
end

it "has endless range and positive steps" do
@array.send(@method, eval("(0..).step(1)")).should == [0, 1, 2, 3, 4, 5]
@array.send(@method, eval("(0..).step(2)")).should == [0, 2, 4]
@array.send(@method, eval("(0..).step(10)")).should == [0]

@array.send(@method, eval("(2..).step(1)")).should == [2, 3, 4, 5]
@array.send(@method, eval("(2..).step(2)")).should == [2, 4]
@array.send(@method, eval("(2..).step(10)")).should == [2]

@array.send(@method, eval("(-3..).step(1)")).should == [3, 4, 5]
@array.send(@method, eval("(-3..).step(2)")).should == [3, 5]
@array.send(@method, eval("(-3..).step(10)")).should == [3]
end

it "has beginless range and positive steps" do
# end with zero index
@array.send(@method, eval("(..0).step(1)")).should == [0]
@array.send(@method, eval("(...0).step(1)")).should == []

@array.send(@method, eval("(..0).step(2)")).should == [0]
@array.send(@method, eval("(...0).step(2)")).should == []

@array.send(@method, eval("(..0).step(10)")).should == [0]
@array.send(@method, eval("(...0).step(10)")).should == []

# end with positive index
@array.send(@method, eval("(..3).step(1)")).should == [0, 1, 2, 3]
@array.send(@method, eval("(...3).step(1)")).should == [0, 1, 2]

@array.send(@method, eval("(..3).step(2)")).should == [0, 2]
@array.send(@method, eval("(...3).step(2)")).should == [0, 2]

@array.send(@method, eval("(..3).step(10)")).should == [0]
@array.send(@method, eval("(...3).step(10)")).should == [0]

# end with negative index
@array.send(@method, eval("(..-2).step(1)")).should == [0, 1, 2, 3, 4,]
@array.send(@method, eval("(...-2).step(1)")).should == [0, 1, 2, 3]

@array.send(@method, eval("(..-2).step(2)")).should == [0, 2, 4]
@array.send(@method, eval("(...-2).step(2)")).should == [0, 2]

@array.send(@method, eval("(..-2).step(10)")).should == [0]
@array.send(@method, eval("(...-2).step(10)")).should == [0]
end

it "has endless range and negative steps" do
@array.send(@method, eval("(0..).step(-1)")).should == [0]
@array.send(@method, eval("(0..).step(-2)")).should == [0]
@array.send(@method, eval("(0..).step(-10)")).should == [0]

@array.send(@method, eval("(2..).step(-1)")).should == [2, 1, 0]
@array.send(@method, eval("(2..).step(-2)")).should == [2, 0]
@array.send(@method, eval("(2..).step(-10)")).should == [0] # ???

@array.send(@method, eval("(-3..).step(-1)")).should == [3, 2, 1, 0]
@array.send(@method, eval("(-3..).step(-2)")).should == [3, 1]
@array.send(@method, eval("(-3..).step(-10)")).should == [0] # ???
end

# ????????
it "has beginless range and negative steps" do
# end with zero index
@array.send(@method, eval("(..0).step(-1)")).should == [5, 4, 3, 2, 1, 0]
@array.send(@method, eval("(...0).step(-1)")).should == [5, 4, 3, 2, 1, 0]

@array.send(@method, eval("(..0).step(-2)")).should == [5, 3, 1]
@array.send(@method, eval("(...0).step(-2)")).should == [5, 3, 1]

@array.send(@method, eval("(..0).step(-10)")).should == [0]
@array.send(@method, eval("(...0).step(-10)")).should == [0]

# end with positive index
@array.send(@method, eval("(..3).step(-1)")).should == [5, 4, 3]
@array.send(@method, eval("(...3).step(-1)")).should == [5, 4, 3]

@array.send(@method, eval("(..3).step(-2)")).should == [5, 3]
@array.send(@method, eval("(...3).step(-2)")).should == [5, 3]

@array.send(@method, eval("(..3).step(-10)")).should == [3]
@array.send(@method, eval("(...3).step(-10)")).should == [3]

# end with negative index
@array.send(@method, eval("(..-2).step(-1)")).should == [5, 4]
@array.send(@method, eval("(...-2).step(-1)")).should ==[5, 4]

@array.send(@method, eval("(..-2).step(-2)")).should == [5]
@array.send(@method, eval("(...-2).step(-2)")).should == [5]

@array.send(@method, eval("(..-2).step(-10)")).should == [4]
@array.send(@method, eval("(...-2).step(-10)")).should == [4]
end

it "has closed range and positive steps" do
# start and end with 0
@array.send(@method, eval("(0..0).step(1)")).should == [0]
@array.send(@method, eval("(0...0).step(1)")).should == []

@array.send(@method, eval("(0..0).step(2)")).should == [0]
@array.send(@method, eval("(0...0).step(2)")).should == []

@array.send(@method, eval("(0..0).step(10)")).should == [0]
@array.send(@method, eval("(0...0).step(10)")).should == []

# start and end with positive index
@array.send(@method, eval("(1..3).step(1)")).should == [1, 2, 3]
@array.send(@method, eval("(1...3).step(1)")).should == [1, 2]

@array.send(@method, eval("(1..3).step(2)")).should == [1, 3]
@array.send(@method, eval("(1...3).step(2)")).should == [1]

@array.send(@method, eval("(1..3).step(10)")).should == [1]
@array.send(@method, eval("(1...3).step(10)")).should == [1]

# start with positive index, end with negative index
@array.send(@method, eval("(1..-2).step(1)")).should == [1, 2, 3, 4]
@array.send(@method, eval("(1...-2).step(1)")).should == [1, 2, 3]

@array.send(@method, eval("(1..-2).step(2)")).should == [1, 3]
@array.send(@method, eval("(1...-2).step(2)")).should == [1, 3]

@array.send(@method, eval("(1..-2).step(10)")).should == [1]
@array.send(@method, eval("(1...-2).step(10)")).should == [1]

# start with negative index, end with positive index
@array.send(@method, eval("(-4..4).step(1)")).should == [2, 3, 4]
@array.send(@method, eval("(-4...4).step(1)")).should == [2, 3]

@array.send(@method, eval("(-4..4).step(2)")).should == [2, 4]
@array.send(@method, eval("(-4...4).step(2)")).should == [2]

@array.send(@method, eval("(-4..4).step(10)")).should == [2]
@array.send(@method, eval("(-4...4).step(10)")).should == [2]

# start with negative index, end with negative index
@array.send(@method, eval("(-4..-2).step(1)")).should == [2, 3, 4]
@array.send(@method, eval("(-4...-2).step(1)")).should == [2, 3]

@array.send(@method, eval("(-4..-2).step(2)")).should == [2, 4]
@array.send(@method, eval("(-4...-2).step(2)")).should == [2]

@array.send(@method, eval("(-4..-2).step(10)")).should == [2]
@array.send(@method, eval("(-4...-2).step(10)")).should == [2]
end

it "has closed range and negative steps" do
# start and end with 0
@array.send(@method, eval("(0..0).step(-1)")).should == [0]
@array.send(@method, eval("(0...0).step(-1)")).should == []

@array.send(@method, eval("(0..0).step(-2)")).should == [0]
@array.send(@method, eval("(0...0).step(-2)")).should == []

@array.send(@method, eval("(0..0).step(-10)")).should == [0]
@array.send(@method, eval("(0...0).step(-10)")).should == []

# start and end with positive index
@array.send(@method, eval("(1..3).step(-1)")).should == []
@array.send(@method, eval("(1...3).step(-1)")).should == []

@array.send(@method, eval("(1..3).step(-2)")).should == []
@array.send(@method, eval("(1...3).step(-2)")).should == []

@array.send(@method, eval("(1..3).step(-10)")).should == []
@array.send(@method, eval("(1...3).step(-10)")).should == []

# start with positive index, end with negative index
@array.send(@method, eval("(1..-2).step(-1)")).should == []
@array.send(@method, eval("(1...-2).step(-1)")).should == []

@array.send(@method, eval("(1..-2).step(-2)")).should == []
@array.send(@method, eval("(1...-2).step(-2)")).should == []

@array.send(@method, eval("(1..-2).step(-10)")).should == []
@array.send(@method, eval("(1...-2).step(-10)")).should == []

# start with negative index, end with positive index
@array.send(@method, eval("(-4..4).step(-1)")).should == []
@array.send(@method, eval("(-4...4).step(-1)")).should == []

@array.send(@method, eval("(-4..4).step(-2)")).should == []
@array.send(@method, eval("(-4...4).step(-2)")).should == []

@array.send(@method, eval("(-4..4).step(-10)")).should == []
@array.send(@method, eval("(-4...4).step(-10)")).should == []

# start with negative index, end with negative index
@array.send(@method, eval("(-4..-2).step(-1)")).should == []
@array.send(@method, eval("(-4...-2).step(-1)")).should == []

@array.send(@method, eval("(-4..-2).step(-2)")).should == []
@array.send(@method, eval("(-4...-2).step(-2)")).should == []

@array.send(@method, eval("(-4..-2).step(-10)")).should == []
@array.send(@method, eval("(-4...-2).step(-10)")).should == []
end

it "has inverted closed range and positive steps" do
# start and end with positive index
@array.send(@method, eval("(3..1).step(1)")).should == []
@array.send(@method, eval("(3...1).step(1)")).should == []

@array.send(@method, eval("(3..1).step(2)")).should == []
@array.send(@method, eval("(3...1).step(2)")).should == []

@array.send(@method, eval("(3..1).step(10)")).should == []
@array.send(@method, eval("(3...1).step(10)")).should == []

# start with negative index, end with positive index
@array.send(@method, eval("(-2..1).step(1)")).should == []
@array.send(@method, eval("(-2...1).step(1)")).should == []

@array.send(@method, eval("(-2..1).step(2)")).should == []
@array.send(@method, eval("(-2...1).step(2)")).should == []

@array.send(@method, eval("(-2..1).step(10)")).should == []
@array.send(@method, eval("(-2...1).step(10)")).should == []

# start with positive index, end with negative index
@array.send(@method, eval("(4..-4).step(1)")).should == []
@array.send(@method, eval("(4...-4).step(1)")).should == []

@array.send(@method, eval("(4..-4).step(2)")).should == []
@array.send(@method, eval("(4...-4).step(2)")).should == []

@array.send(@method, eval("(4..-4).step(10)")).should == []
@array.send(@method, eval("(4...-4).step(10)")).should == []

# start with negative index, end with negative index
@array.send(@method, eval("(-2..-4).step(1)")).should == []
@array.send(@method, eval("(-2...-4).step(1)")).should == []

@array.send(@method, eval("(-2..-4).step(2)")).should == []
@array.send(@method, eval("(-2...-4).step(2)")).should == []

@array.send(@method, eval("(-2..-4).step(10)")).should == []
@array.send(@method, eval("(-2...-4).step(10)")).should == []
end

# ????????
it "has inverted closed range and negative steps" do
# start and end with positive index
@array.send(@method, eval("(3..1).step(-1)")).should == [3, 2, 1]
@array.send(@method, eval("(3...1).step(-1)")).should == [2, 1]

@array.send(@method, eval("(3..1).step(-2)")).should == [3, 1]
@array.send(@method, eval("(3...1).step(-2)")).should == [2]

@array.send(@method, eval("(3..1).step(-10)")).should == [1]
@array.send(@method, eval("(3...1).step(-10)")).should == [1]

# start with negative index, end with positive index
@array.send(@method, eval("(-2..1).step(-1)")).should == [4, 3, 2, 1]
@array.send(@method, eval("(-2...1).step(-1)")).should == [3, 2, 1]

@array.send(@method, eval("(-2..1).step(-2)")).should == [4, 2]
@array.send(@method, eval("(-2...1).step(-2)")).should == [3, 1]

@array.send(@method, eval("(-2..1).step(-10)")).should == [1]
@array.send(@method, eval("(-2...1).step(-10)")).should == [1]

# start with positive index, end with negative index
@array.send(@method, eval("(4..-4).step(-1)")).should == [4, 3, 2]
@array.send(@method, eval("(4...-4).step(-1)")).should == [3, 2]

@array.send(@method, eval("(4..-4).step(-2)")).should == [4, 2]
@array.send(@method, eval("(4...-4).step(-2)")).should == [3]

@array.send(@method, eval("(4..-4).step(-10)")).should == [2]
@array.send(@method, eval("(4...-4).step(-10)")).should == [2]

# start with negative index, end with negative index
@array.send(@method, eval("(-2..-4).step(-1)")).should == [4, 3, 2]
@array.send(@method, eval("(-2...-4).step(-1)")).should == [3, 2]

@array.send(@method, eval("(-2..-4).step(-2)")).should == [4, 2]
@array.send(@method, eval("(-2...-4).step(-2)")).should == [3]

@array.send(@method, eval("(-2..-4).step(-10)")).should == [2]
@array.send(@method, eval("(-2...-4).step(-10)")).should == [2]
end
end
end

Expand Down

0 comments on commit 8b7fe61

Please sign in to comment.