-
Notifications
You must be signed in to change notification settings - Fork 30
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
Incorrect shrinking (mutable objects in generator) #192
Comments
Might be related to (and possible duplicate of) #157. |
Yep, seems like a dupe of #157 which we've never get into it. I think what's happening is that everything above I'd be tempted to compare these results with the haskell version, because we had a similar issue reported there in hedgehogqa/haskell-hedgehog#122. |
I see. The Hedgehog.Experimental library contains more generators that use mutable state. Do you think you'd be able to have a look at this in the foreseeable future, or should I prioritize implementing them without immutability? (That might impact performance, so I'd rather not.) Or as a temporary workaround, I can generate a throwaway value using |
I'm planning to take a look this the soonest possible. It'd be interesting to see if your idea will work, using a |
Unfortunately, using |
|
As I'm looking at this, here's a minimal working example I've been using based on the above gen(s) open System
open System.Text
open Hedgehog
let shuffleCase_mut (s : string) : Gen<string> =
let sb = StringBuilder ()
gen {
for i = 0 to s.Length - 1 do
let! b = Gen.bool
let fn =
if b then
Char.ToUpperInvariant
else
Char.ToLowerInvariant
sb.Append (fn s.[i]) |> ignore
return sb.ToString ()
}
let shuffleCase_rec (s : string) : Gen<string> =
let rec go i c =
gen {
if i <> s.Length then
let! b = Gen.bool
let fn =
if b then
Char.ToUpperInvariant
else
Char.ToLowerInvariant
return! go (i + 1) (c + string (fn s.[i]))
else
return c
}
go 0 ""
[<EntryPoint>]
let main _ =
printfn
"shuffleCase_mut"
for __ = 0 to 3 do
shuffleCase_mut "ab" |> Gen.generateTree |> printfn "%A"
printfn
"shuffleCase_rec"
for __ = 0 to 3 do
shuffleCase_rec "ab" |> Gen.generateTree |> printfn "%A"
0
These are the generated rose trees, with the outcome and all the ways in which they can be made smaller. |
I looked into this a bit, but it is complicated. Hopefully things will be simpler when PR #238 is completed. I will investigate more after the PR is complete. |
If Tyson's comment here is correct, one way to avoid overflow in |
PR hedgehogqa/fsharp-hedgehog-experimental#53 implements a workaround in "client code" for the motivating example in the OP of this issue. I think this issue can be closed. The issue about mutation in generators can continue to be tracked via #157. |
Actually, I'll leave it to the maintainers of this repo to decide whether to close this. |
Closing in favor of #157. |
In Hedgehog.Experimental I have this:
Let's try to sample this:
Yields:
As you can see, there's something weird going on with shrinking. Additional characters are added.
If I instead implement it in an immutable way, it works. For example:
Why does the first one fail? Is it a general rule that using (locally defined) mutable objects in a generator will cause issues for shrinking?
The text was updated successfully, but these errors were encountered: