Skip to content

Commit

Permalink
adds large union list test
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderDennis committed Jan 29, 2024
1 parent 143c8d9 commit 2bae205
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
6 changes: 4 additions & 2 deletions NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Use Telemetry for metrics and stats?

- [x] Change `History.shrink_length/1` to remove one item at a time and fix the resulting error and occasional timeout.

- [x] Keep track of seen histories to avoid trying them again. (No longer needed after refactoring into `Shrinker` module.)
- [x] Keep track of seen histories to avoid trying them again. (No longer needed after refactoring into `Shrinker` module?)

- [x] Try a new implementation of shrinking. Create multiple histories from a given history. Test all of them against test_fn.
Keep best (shortlex smallest) that still fails the test and re-start the shrinking process with that one as the input.
Expand All @@ -76,6 +76,8 @@ Copy more of the elm-test implementation. Create a `Shrinker` module.

- [x] Implement binary search for finding smaller interesting values within the PRNG history.

- [ ] Change `list_of` to have some maximum list size. Adjust probability as it gets closer to the max? See https://github.com/elm-explorations/test/blob/9669a27d84fc29175364c7a60d5d700771a2801e/src/Fuzz.elm#L678

- [ ] Store `length` in `History` struct? This would make some operations more efficient.

- [ ] Add support for generating integers larger than the internal representation of the PRNG history which is currently a 32-bit integer. This requires consuming more than one value. The `next` funciton probably needs a byte_count parameter.
Expand Down Expand Up @@ -108,7 +110,7 @@ Maybe flatten the structure while keeping `random/0` and `hardcoded/1` construct

- [ ] Publish to Hex.pm

- [ ] add `mix dialyzer` to GitHub action
- [ ] add `mix dialyzer` to GitHub action see https://github.com/jeremyjh/dialyxir/blob/master/docs/github_actions.md

### How do we make generators composible?

Expand Down
2 changes: 1 addition & 1 deletion lib/decorum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ defmodule Decorum do
|> Enum.reduce_while({[], prng}, fn _, {list, prng} ->
{flip, prng} = Prng.next!(prng)

if rem(flip, 10) > 0 do
if rem(flip, 8) > 0 do
{value, prng} = generator.(prng)
{:cont, {[value | list], prng}}
else
Expand Down
56 changes: 38 additions & 18 deletions test/shrinking_challenge_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,47 @@ defmodule ShrinkingChallengeTest do
"""

test "bound5" do
# runs for about 4 seconds with mix test --seed 558117
# takes about 21 seconds with seed 631266
%Decorum.PropertyError{value: value} = assert_raise Decorum.PropertyError,
fn ->
Decorum.integer(-32768..32767)
|> Decorum.list_of()
|> Decorum.filter(fn lst ->
sum(lst) < 256
end)
|> Decorum.list_of_length(5)
|> Decorum.check_all(fn lists ->
assert lists
|> Enum.concat()
|> sum() <
5 * 256
end)
end
#assert that 3 of the lists are empty
%Decorum.PropertyError{value: value} =
assert_raise Decorum.PropertyError,
fn ->
Decorum.integer(-32768..32767)
|> Decorum.list_of()
|> Decorum.filter(fn lst ->
sum(lst) < 256
end)
|> Decorum.list_of_length(5)
|> Decorum.check_all(fn lists ->
assert lists
|> Enum.concat()
|> sum() <
5 * 256
end)
end

# assert that 3 of the lists are empty
# IO.inspect(value)
assert Enum.count(value, &(&1 == [])) == 3
end

test "large union list" do
# takes over 34 seconds with seed 590589
%Decorum.PropertyError{value: value} =
assert_raise Decorum.PropertyError,
fn ->
Decorum.integer(-4294967296..4294967295)
|> Decorum.list_of()
|> Decorum.list_of()
|> Decorum.check_all(fn lists ->
assert lists
|> Enum.concat()
|> Enum.uniq()
|> Enum.count() < 5
end)
end

assert value == [[1, 2, 3, 4, 0]]
end

defp normalize(n) do
n
|> underflow
Expand Down

0 comments on commit 2bae205

Please sign in to comment.