-
Notifications
You must be signed in to change notification settings - Fork 23
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
Construct a lazy list of shrink values to avoid eager running #67
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package hedgehog.predef | ||
|
||
/** | ||
* A _very_ naive lazy-list where the element evaluation is lazy but the spine itself is strict. | ||
* Unfortunately using Scala `Stream` results in the head being evaluated prematurely for shrinking. | ||
*/ | ||
sealed trait LazyList[A] { | ||
|
||
import LazyList._ | ||
|
||
def map[B](f: A => B): LazyList[B] = | ||
this match { | ||
case Nil() => | ||
nil | ||
case Cons(h, t) => | ||
Cons(() => f(h()), t.map(f)) | ||
} | ||
|
||
def ++(b: LazyList[A]): LazyList[A] = | ||
this match { | ||
case Nil() => | ||
b | ||
case Cons(h, t) => | ||
Cons(h, t ++ b) | ||
} | ||
} | ||
|
||
object LazyList { | ||
|
||
case class Cons[A](head: () => A, tail: LazyList[A]) extends LazyList[A] | ||
case class Nil[A]() extends LazyList[A] | ||
|
||
def nil[A]: LazyList[A] = | ||
Nil() | ||
|
||
def cons[A](h: => A, t: LazyList[A]): LazyList[A] = | ||
Cons(() => h, t) | ||
|
||
def apply[A](l: A*): LazyList[A] = | ||
fromList(l.toList) | ||
|
||
def fromList[A](l: List[A]): LazyList[A] = | ||
l.foldRight(nil[A])(cons(_, _)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package hedgehog | ||
|
||
import hedgehog.core._ | ||
import hedgehog.runner._ | ||
|
||
object ShrinkTest extends Properties { | ||
|
||
def tests: List[Test] = | ||
List( | ||
example("test that shrinking only 'runs' the test once per shrink", testLazy) | ||
) | ||
|
||
// https://github.com/hedgehogqa/scala-hedgehog/issues/66 | ||
def testLazy: Result = { | ||
// What's that, mutable state?!? | ||
// We really want to observe how many times our test is _actually_ run, not just what hedgehog thinks it ran. | ||
// In previous incarnations we were accidentally running the test for _each_ shrink, | ||
// but only taking the first failed result. For any non-trivial test (ie IO test) this would basically make | ||
// shrinking useless. | ||
var failed = 0 | ||
|
||
val r = Property.check(PropertyConfig.default, for { | ||
// NOTE: We're also generating lists-of-lists here at the same time | ||
// If implemented too strictly the shrinking _will_ run out of memory | ||
// https://github.com/hedgehogqa/scala-hedgehog/issues/62 | ||
x <- Gen.string(Gen.alpha, Range.linear(0, 100)).list(Range.linear(0, 100)).log("x") | ||
} yield { | ||
val b = x.length < 5 | ||
if (!b) { | ||
failed = failed + 1 | ||
} | ||
Result.assert(b) | ||
}, Seed.fromTime()).value | ||
|
||
r.status match { | ||
case Failed(s, _) => | ||
// This count also includes the first failure case | ||
ShrinkCount(failed - 1) ==== s | ||
case _ => | ||
Result.failure.log("Test failed incorrectly") | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're actually discussing about doing the opposite in the F# version: https://github.com/hedgehogqa/fsharp-hedgehog/pull/180/files#diff-440c70d7e172f54e93d63abc97ef0a55
Definitely a good reason (for me) to look at #66 and investigate further on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@moodmosaic Yeah Jacob mentioned your PR after I had merged all of mine. At the moment I'm just happy that my shrinking doesn't run the wrong tests and it runs in constant memory. I suspect I've made it more complicated than I need to though so I'm curious what you find on your side of the fence. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@charleso I'll keep you posted. 👍