-
Notifications
You must be signed in to change notification settings - Fork 8
Fix 'fromIndex' internal helper #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
Conversation
src/Control/Monad/Gen.purs
Outdated
where | ||
go j (Cons a _) | j <= 0 = a | ||
go j (Cons _ as) = go (j - 1) as | ||
go _ Nil = un Last (foldMap1 Last xs) |
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.
If you added a case for Cons a Nil
you could avoid another traversal, and instead this would just serve as proof of non-emptyness, but never actually be invoked.
Finally added that case 🤦 |
Perhaps this should be merged before #26 because its |
@@ -31,6 +31,13 @@ main = do | |||
log "`genNonEmpty` should not reduce the remainder size below zero" | |||
one :: NonEmpty Array Int ← Gen.resize (const 0) $ GenC.genNonEmpty (Gen.sized pure) | |||
liftEffect $ assertEqual { actual: one, expected: 0 :| [] } | |||
|
|||
log "Ensure that `elements` will produce all possible values (tests will hang if this fails)" | |||
_ ← Gen.suchThat (Gen.elements ("A" :| ["B", "C", "D"])) (_ == "A") |
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.
Couldn't we do something that will fail (rather than hang CI for who knows how long) like this?
test = do
let testElems = "A" :| ["B", "C", "D"]
elems <- randomSample' 100 (Gen.elements testElems)
let generatedElems = foldl (\mymap e -> Map.insertWith (_ + 1) e 0 mymap) Map.empty elems
keys generatedElems `shouldEqual` (toArray testElems)
-- some other frequency test here, too.
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.
Only if we don't mind there occasionally being random failures. There's no limited number of samples you can generate that won't result in a false negative from time to time.
I suppose that's still technically true with the current test, just the number of things it will generate is bound by the CI timeout or node crashing rather than there being an explicit limit.
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.
Only if we don't mind there occasionally being random failures. There's no limited number of samples you can generate that won't result in a false negative from time to time.
True, but at least CI fails quicker. Also, if we increase the number of elements generated, wouldn't that decrease the possibility of a random failure occurring?
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.
Yeah, the larger the number the less likely it will fail.
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.
So.... 1,000, right? 😆
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.
I'll merge this PR and then submit a PR that uses the above approach.
Resolves #22
The test isn't great, since it'll just never complete if it fails, but that's better than nothing for now. Testing the distribution properly would be good at some point, seems like we did have more tests at one point: #16.
fromIndex
could be expensive if it used with an index greater than the size of theFoldable1
since it usesLast
after already having converted to a linked list and running through it... but I'm pretty sure it won't hit that case the way it's used anyway.