Skip to content

Commit

Permalink
Add Slice#fill (#10924)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Jul 26, 2021
1 parent 32f034f commit fc0617f
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 108 deletions.
92 changes: 66 additions & 26 deletions spec/std/array_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -696,53 +696,73 @@ describe "Array" do
a.equals?(d, &f).should be_false
end

describe "fill" do
describe "#fill" do
it "replaces all values" do
a = ['a', 'b', 'c']
expected = ['x', 'x', 'x']
a.fill('x').should eq(expected)
a.fill('x').should be(a)
a.should eq(expected)

a = [1, 2, 3]
expected = [0, 0, 0]
a.fill(0).should eq(expected)
a.fill(0).should be(a)
a.should eq(expected)

a = [1.0, 2.0, 3.0]
expected = [0, 0, 0]
a.fill(0).should eq(expected)
expected = [0.0, 0.0, 0.0]
a.fill(0.0).should be(a)
a.should eq(expected)
end

it "replaces only values between index and size" do
a = ['a', 'b', 'c']
expected = ['x', 'x', 'c']
a.fill('x', 0, 2).should eq(expected)
a.fill('x', 0, 2).should be(a)
a.should eq(expected)

a = [1, 2, 3]
expected = [0, 0, 3]
a.fill(0, 0, 2).should eq(expected)
a.fill(0, 0, 2).should be(a)
a.should eq(expected)

a = [1.0, 2.0, 3.0]
expected = [0, 0, 3]
a.fill(0, 0, 2).should eq(expected)
a.fill(0, 0, 2).should be(a)
a.should eq(expected)
end

it "replaces only values between index and size (2)" do
a = ['a', 'b', 'c']
expected = ['a', 'x', 'x']
a.fill('x', 1, 2).should eq(expected)
a.fill('x', 1, 2).should be(a)
a.should eq(expected)

a = [1, 2, 3]
expected = [1, 0, 0]
a.fill(0, 1, 2).should eq(expected)
a.fill(0, 1, 2).should be(a)
a.should eq(expected)

a = [1.0, 2.0, 3.0]
expected = [1, 0, 0]
a.fill(0, 1, 2).should eq(expected)
a.fill(0, 1, 2).should be(a)
a.should eq(expected)
end

it "replaces all values from index onwards" do
a = ['a', 'b', 'c']
expected = ['a', 'x', 'x']
a.fill('x', -2).should eq(expected)
a.fill('x', -2).should be(a)
a.should eq(expected)

a = [1, 2, 3]
expected = [1, 0, 0]
a.fill(0, -2).should eq(expected)
a.fill(0, -2).should be(a)
a.should eq(expected)

a = [1.0, 2.0, 3.0]
expected = [1, 0, 0]
a.fill(0, -2).should eq(expected)
a.fill(0, -2).should be(a)
a.should eq(expected)
end

it "raises when given big negative number (#4539)" do
Expand All @@ -760,13 +780,18 @@ describe "Array" do
it "replaces only values between negative index and size" do
a = ['a', 'b', 'c']
expected = ['a', 'b', 'x']
a.fill('x', -1, 1).should eq(expected)
a.fill('x', -1, 1).should be(a)
a.should eq(expected)

a = [1, 2, 3]
expected = [1, 2, 0]
a.fill(0, -1, 1).should eq(expected)
a.fill(0, -1, 1).should be(a)
a.should eq(expected)

a = [1.0, 2.0, 3.0]
expected = [1, 2, 0]
a.fill(0, -1, 1).should eq(expected)
a.fill(0, -1, 1).should be(a)
a.should eq(expected)
end

it "raises when given big negative number in from/count (#4539)" do
Expand All @@ -784,37 +809,52 @@ describe "Array" do
it "replaces only values in range" do
a = ['a', 'b', 'c']
expected = ['x', 'x', 'c']
a.fill('x', -3..1).should eq(expected)
a.fill('x', -3..1).should be(a)
a.should eq(expected)

a = [1, 2, 3]
expected = [0, 0, 3]
a.fill(0, -3..1).should eq(expected)
a.fill(0, -3..1).should be(a)
a.should eq(expected)

a = [1.0, 2.0, 3.0]
expected = [0, 0, 3]
a.fill(0, -3..1).should eq(expected)
a.fill(0, -3..1).should be(a)
a.should eq(expected)
end

it "replaces only values in range without end" do
a = ['a', 'b', 'c']
expected = ['a', 'x', 'x']
a.fill('x', 1..nil).should eq(expected)
a.fill('x', 1..nil).should be(a)
a.should eq(expected)

a = [1, 2, 3]
expected = [1, 0, 0]
a.fill(0, 1..nil).should eq(expected)
a.fill(0, 1..nil).should be(a)
a.should eq(expected)

a = [1.0, 2.0, 3.0]
expected = [1, 0, 0]
a.fill(0, 1..nil).should eq(expected)
a.fill(0, 1..nil).should be(a)
a.should eq(expected)
end

it "replaces only values in range begin" do
a = ['a', 'b', 'c']
expected = ['x', 'x', 'c']
a.fill('x', nil..1).should eq(expected)
a.fill('x', nil..1).should be(a)
a.should eq(expected)

a = [1, 2, 3]
expected = [0, 0, 3]
a.fill(0, nil..1).should eq(expected)
a.fill(0, nil..1).should be(a)
a.should eq(expected)

a = [1.0, 2.0, 3.0]
expected = [0, 0, 3]
a.fill(0, nil..1).should eq(expected)
a.fill(0, nil..1).should be(a)
a.should eq(expected)
end

it "works with a block" do
Expand Down
52 changes: 52 additions & 0 deletions spec/std/slice_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,57 @@ describe "Slice" do
slice.to_s.should eq("Bytes[1, 2, 3]")
end

describe "#fill" do
it "replaces all values, without block" do
slice = Slice.new(4) { |i| i + 1 }
expected = Slice.new(4, 7)
slice.fill(7).should eq(expected)
slice.should eq(expected)

expected = Slice.new(4, 5)
slice.fill(5).should eq(expected)
slice.should eq(expected)
end

it "works with primitive number types and 0" do
slice = Slice.new(4) { |i| i + 1 }
expected = Slice.new(4, 0)
slice.fill(0).should eq(expected)
slice.should eq(expected)

slice = Slice.new(4, &.to_f64)
expected = Slice.new(4, 0.0)
slice.fill(0.0).should eq(expected)
slice.should eq(expected)

slice = Slice.new(4, &.to_u8)
expected = Slice.new(4, 0_u8)
slice.fill(0).should eq(expected)
slice.should eq(expected)
end

it "works with Bytes" do
slice = Bytes[1, 2, 3]
expected = Slice.new(3, 7_u8)
slice.fill(7).should eq(expected)
slice.should eq(expected)
end

it "replaces all values, with block" do
slice = Slice.new(4) { |i| i + 1 }
expected = Slice[0, 1, 4, 9]
slice.fill { |i| i * i }.should eq(expected)
slice.should eq(expected)
end

it "replaces all values, with block and offset" do
slice = Slice.new(4) { |i| i + 1 }
expected = Slice[9, 16, 25, 36]
slice.fill(offset: 3) { |i| i * i }.should eq(expected)
slice.should eq(expected)
end
end

it "does copy_from pointer" do
pointer = Pointer.malloc(4) { |i| i + 1 }
slice = Slice.new(4, 0)
Expand Down Expand Up @@ -507,6 +558,7 @@ describe "Slice" do
it "creates read-only slice" do
slice = Slice.new(3, 0, read_only: true)
expect_raises(Exception, "Can't write to read-only Slice") { slice[0] = 1 }
expect_raises(Exception, "Can't write to read-only Slice") { slice.fill(0) }
expect_raises(Exception, "Can't write to read-only Slice") { slice.copy_from(slice) }

subslice = slice[0, 1]
Expand Down
28 changes: 17 additions & 11 deletions spec/std/static_array_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,24 @@ describe "StaticArray" do
a.to_s.should eq("StaticArray[1, 2, 3]")
end

it "does #fill, without block" do
a = StaticArray(Int32, 3).new { |i| i + 1 }
a.fill(0).should eq(StaticArray[0, 0, 0])
a.should eq(StaticArray[0, 0, 0])
a.fill(2).should eq(StaticArray[2, 2, 2])
a.should eq(StaticArray[2, 2, 2])
end
describe "#fill" do
it "replaces all values, without block" do
a = StaticArray(Int32, 3).new { |i| i + 1 }
expected = StaticArray[0, 0, 0]
a.fill(0).should eq(expected)
a.should eq(expected)

expected = StaticArray[2, 2, 2]
a.fill(2).should eq(expected)
a.should eq(expected)
end

it "does #fill, with block" do
a = StaticArray(Int32, 4).new { |i| i + 1 }
a.fill { |i| i * i }.should eq(StaticArray[0, 1, 4, 9])
a.should eq(StaticArray[0, 1, 4, 9])
it "replaces all values, with block" do
a = StaticArray(Int32, 4).new { |i| i + 1 }
expected = StaticArray[0, 1, 4, 9]
a.fill { |i| i * i }.should eq(expected)
a.should eq(expected)
end
end

it "shuffles" do
Expand Down
Loading

0 comments on commit fc0617f

Please sign in to comment.