Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More sophisticated array shrink algorithm #15

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/pbt/arbitrary/arbitrary_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def printable_string(**kwargs)
end

def symbol(**kwargs)
array(one_of(*SYMBOL_SAFE_CHARS), **kwargs).map(SYMBOL_MAPPER, SYMBOL_UNMAPPER)
array(one_of(*SYMBOL_SAFE_CHARS), empty: false, **kwargs).map(SYMBOL_MAPPER, SYMBOL_UNMAPPER)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symbol basically should not be empty.

end

def float
Expand Down
21 changes: 17 additions & 4 deletions lib/pbt/arbitrary/array_arbitrary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,28 @@ def generate(rng)
size.times.map { @arbitrary.generate(rng) }
end

# Returns shrunken arrays.
# This doesn't produce all possible patterns because it could be too large (+1000) even for small arrays.
# @param current [Array]
# @return [Enumerator]
def shrink(current)
return Enumerator.new { |_| } if current.size == 0
return Enumerator.new { |_| } if current.size == @min_length

# TODO: Implement more sophisticated shrinking. It should be shrink each item as well.
Enumerator.new do |y|
@length_arb.shrink(current.size).each do |length|
slice_start = current.size - length
y.yield current[slice_start..]
if length == 0
y.yield []
next
end
current.each_cons(length) do |con|
y.yield con
end
end

current.each_with_index do |item, i|
@arbitrary.shrink(item).each do |val|
y.yield [*current[...i], val, *current[i + 1..]]
end
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/pbt/arbitrary/constant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ module Arbitrary
*("\u{E000}".."\u{FFFD}"),
*("\u{10000}".."\u{10FFFF}")
].freeze

CHAR_MAPPER = ->(v) { [v].pack("U") }
CHAR_UNMAPPER = ->(v) { v.unpack1("U") }
STRING_MAPPER = ->(v) { v.join }
Expand Down
38 changes: 29 additions & 9 deletions spec/pbt/arbitrary/arbitrary_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,19 @@

it "returns an Enumerator that iterates set shrinking towards empty" do
arb = Pbt.set(Pbt.integer)
expect(arb.shrink(Set.new([10, 20, -44])).to_a).to eq [
Set.new([20, -44]),
Set.new([-44]),
Set.new([])
expect(arb.shrink(Set.new([1, 2, -4])).to_a).to eq [
Set.new([1, 2]),
Set.new([2, -4]),
Set.new([1]),
Set.new([2]),
Set.new([-4]),
Set.new([]),
Set.new([0, 2, -4]),
Set.new([1, -4]),
Set.new([1, 0, -4]),
Set.new([1, 2, -2]),
Set.new([1, 2, -1]),
Set.new([1, 2, 0])
]
end
end
Expand All @@ -278,12 +287,23 @@
expect(arb.shrink(val)).to be_a(Enumerator)
end

it "returns an Enumerator that iterates hash shrinking towards empty" do
it "returns an Enumerator that iterates hash with shrunken length or value" do
arb = Pbt.hash(Pbt.symbol, Pbt.integer)
expect(arb.shrink({a: 10, b: 20, c: -1}).to_a).to eq [
{b: 20, c: -1},
{c: -1},
{}
expect(arb.shrink({a: 10, b: 20}).to_a).to eq [
{a: 10},
{b: 20},
{},
{a: 5, b: 20},
{a: 3, b: 20},
{a: 2, b: 20},
{a: 1, b: 20},
{a: 0, b: 20},
{a: 10, b: 10},
{a: 10, b: 5},
{a: 10, b: 3},
{a: 10, b: 2},
{a: 10, b: 1},
{a: 10, b: 0}
]
end
end
Expand Down
30 changes: 24 additions & 6 deletions spec/pbt/arbitrary/array_arbitrary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,31 @@
expect(arb.shrink(val)).to be_a(Enumerator)
end

it "returns an Enumerator that iterates halved arrays" do
it "returns an Enumerator that iterates arrays with shrunken length or value" do
arb = Pbt::Arbitrary::ArrayArbitrary.new(Pbt.integer)
expect(arb.shrink([50, 25, 13, 7, 4, 2, 1]).to_a).to eq [
[7, 4, 2, 1],
[2, 1],
[1],
[]
expect(arb.shrink([3]).to_a).to eq [[], [2], [1], [0]]
expect(arb.shrink([2, 0]).to_a).to eq [[2], [0], [], [1, 0], [0, 0]]
expect(arb.shrink([13, 7, 9]).to_a).to eq [
[13, 7],
[7, 9],
[13],
[7],
[9],
[],
[7, 7, 9],
[4, 7, 9],
[2, 7, 9],
[1, 7, 9],
[0, 7, 9],
[13, 4, 9],
[13, 2, 9],
[13, 1, 9],
[13, 0, 9],
[13, 7, 5],
[13, 7, 3],
[13, 7, 2],
[13, 7, 1],
[13, 7, 0]
]
end

Expand Down