Skip to content

Commit

Permalink
Merge branch 'elm-explorations:master' into fuzzer-filterMap
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaechler authored Jan 12, 2024
2 parents 82d06f8 + 2900b08 commit 6545e6d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 85 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

| Version | Notes |
| ------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| [**2.1.2**](https://github.com/elm-explorations/test/tree/2.1.2) | `Fuzz`: Remove arbitrary limit on amount of randomness drawn
| [**2.1.1**](https://github.com/elm-explorations/test/tree/2.1.1) | `Test.Html.Query`: Change how boolean attributes are rendered
| [**2.1.0**](https://github.com/elm-explorations/test/tree/2.1.0) | Add `Test.Html.Selector.exactText` |
| [**2.0.1**](https://github.com/elm-explorations/test/tree/2.0.1) | Documentation fixes |
Expand Down
2 changes: 1 addition & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "elm-explorations/test",
"summary": "Write unit and fuzz tests for Elm code.",
"license": "BSD-3-Clause",
"version": "2.1.1",
"version": "2.1.2",
"exposed-modules": [
"Test",
"Test.Runner",
Expand Down
105 changes: 46 additions & 59 deletions src/Fuzz.elm
Original file line number Diff line number Diff line change
Expand Up @@ -1557,70 +1557,63 @@ rollDice : Int -> Random.Generator Int -> Fuzzer Int
rollDice maxValue diceGenerator =
Fuzzer <|
\prng ->
if RandomRun.isFull (PRNG.getRun prng) then
Rejected
{ reason = "Fuzz.rollDice: Your fuzzers have hit the max size of RandomRun (generating too much data)."
, prng = prng
}
case prng of
Random r ->
let
( diceRoll, newSeed ) =
Random.step diceGenerator r.seed
in
if diceRoll < 0 then
Rejected
{ reason = "elm-test bug: generated a choice < 0"
, prng = prng
}

else
case prng of
Random r ->
let
( diceRoll, newSeed ) =
Random.step diceGenerator r.seed
in
if diceRoll < 0 then
Rejected
{ reason = "elm-test bug: generated a choice < 0"
, prng = prng
}
else if diceRoll > maxValue then
Rejected
{ reason = "elm-test bug: generated a choice > maxChoice"
, prng = prng
}

else if diceRoll > maxValue then
else
Generated
{ value = diceRoll
, prng =
Random
{ seed = newSeed
, run = RandomRun.append diceRoll r.run
}
}

Hardcoded h ->
case RandomRun.nextChoice h.unusedPart of
Nothing ->
-- This happens if we simplified too much / in an incompatible way
Rejected
{ reason = "elm-test bug: generated a choice > maxChoice"
{ reason = "elm-test internals: hardcoded PRNG run out of numbers"
, prng = prng
}

else
Generated
{ value = diceRoll
, prng =
Random
{ seed = newSeed
, run = RandomRun.append diceRoll r.run
}
}

Hardcoded h ->
case RandomRun.nextChoice h.unusedPart of
Nothing ->
-- This happens if we simplified too much / in an incompatible way
Just ( hardcodedChoice, restOfChoices ) ->
if hardcodedChoice < 0 then
-- This happens eg. when decrementing after delete shrink
Rejected
{ reason = "elm-test internals: hardcoded PRNG run out of numbers"
{ reason = "elm-test internals: generated a choice < 0"
, prng = prng
}

Just ( hardcodedChoice, restOfChoices ) ->
if hardcodedChoice < 0 then
-- This happens eg. when decrementing after delete shrink
Rejected
{ reason = "elm-test internals: generated a choice < 0"
, prng = prng
}

else if hardcodedChoice > maxValue then
-- This happens eg. when redistributing choices
Rejected
{ reason = "elm-test internals: generated a choice > maxChoice"
, prng = prng
}
else if hardcodedChoice > maxValue then
-- This happens eg. when redistributing choices
Rejected
{ reason = "elm-test internals: generated a choice > maxChoice"
, prng = prng
}

else
Generated
{ value = hardcodedChoice
, prng = Hardcoded { h | unusedPart = restOfChoices }
}
else
Generated
{ value = hardcodedChoice
, prng = Hardcoded { h | unusedPart = restOfChoices }
}


forcedChoice : Int -> Fuzzer Int
Expand All @@ -1633,12 +1626,6 @@ forcedChoice n =
, prng = prng
}

else if RandomRun.isFull (PRNG.getRun prng) then
Rejected
{ reason = "Fuzz.forcedChoice: Your fuzzers have hit the max size of RandomRun (generating too much data)."
, prng = prng
}

else
case prng of
Random r ->
Expand Down
25 changes: 0 additions & 25 deletions src/RandomRun.elm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module RandomRun exposing
, equal
, get
, isEmpty
, isFull
, length
, nextChoice
, replace
Expand All @@ -33,25 +32,6 @@ type alias RandomRun =
}


{-| A cap for the maximum amount of entropy a fuzzer can use.
This stops infinite recursion (in cases where each step of the recursion makes
a PRNG choice), like in:
infiniteList : Fuzzer a -> Fuzzer (List a)
infiniteList itemFuzzer =
let
go accList =
itemFuzzer
|> Fuzz.andThen (\item -> go (item :: accList))
in
go []
-}
maxLength : Int
maxLength =
64 * 1024


type alias Chunk =
{ size : Int
, startIndex : Int
Expand All @@ -70,11 +50,6 @@ isEmpty run =
run.length == 0


isFull : RandomRun -> Bool
isFull run =
run.length == maxLength


nextChoice : RandomRun -> Maybe ( Int, RandomRun )
nextChoice run =
case Queue.dequeue run.data of
Expand Down

0 comments on commit 6545e6d

Please sign in to comment.