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

bug: printed shrink example is not actual input #24

Open
peter-jerry-ye opened this issue Jan 2, 2025 · 4 comments
Open

bug: printed shrink example is not actual input #24

peter-jerry-ye opened this issue Jan 2, 2025 · 4 comments
Labels
enhancement New feature or request

Comments

@peter-jerry-ye
Copy link
Contributor

peter-jerry-ye commented Jan 2, 2025

Example

///|
fn prop_deque(deque : (Deque, Int)) -> Bool {
  println("input: \{deque}")
  let (deque, n) = deque
  deque._.truncate(n)
  guard n < 0 || deque._.length() <= n else { return false }
  guard n > 0 || deque._.is_empty() else { return false }
  true
}

///|
type Deque @deque.T[Int] derive(Show)

///|
impl @quickcheck.Arbitrary for Deque with arbitrary(i, randomstate) {
  @deque.from_array(Array::arbitrary(i, randomstate))
}

test {
  @lib.quick_check_fn!(prop_deque)
}

Test with Core 532f2e8e4c5e041ba695c24ca69a50cc0005d7bd (where truncate has a bug)

Expected Output

*** [5/0/100] Failed! Falsified.
(Deque(@deque.of([])), -2)

Actual Output

*** [5/0/100] Failed! Falsified.
(Deque(@deque.of([])), 0)
@CAIMEOX
Copy link
Collaborator

CAIMEOX commented Jan 2, 2025

Thanks for reporting.
The function prop_deque here is not pure because truncate will change the state of parameter deque. Impure function can not be tested properly by QuickCheck. There are two solutions can be adapted:

  • implement a copy function for Deque to eliminate the state effect inside queue: deque.copy()._.truncate(n)
  • (QuickCheck): Use the same RandomState to generate two deques, one for reporting and one for testing. However, this solution may have a 2x performance loss, which is obvious when generating large test samples, so I haven't decided whether to do this yet.
    Maybe we should have a Copy trait in core and support derive(Copy)?

@peter-jerry-ye
Copy link
Contributor Author

peter-jerry-ye commented Jan 2, 2025

Still one other possibility is to keep the output in some global ref, which also may be used in #23 . All of these solutions have some kind of performance penalty for non pure data structures.

Another possibility may be adding a verbose and seed option, so that the user can jump to the seed when the failure started and then print every shrinked test case before testing each of them

@peter-jerry-ye
Copy link
Contributor Author

peter-jerry-ye commented Jan 2, 2025

It seems that the issue is caused by this line:

counterexample(f(x), s)

which should've been f(a).

@CAIMEOX CAIMEOX added the enhancement New feature or request label Jan 3, 2025
@CAIMEOX
Copy link
Collaborator

CAIMEOX commented Jan 3, 2025

It seems that the issue is caused by this line:

counterexample(f(x), s)

which should've been f(a).

Fixed in this commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants