Skip to content

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

Merged
merged 5 commits into from
Nov 16, 2020
Merged

Fix 'fromIndex' internal helper #23

merged 5 commits into from
Nov 16, 2020

Conversation

garyb
Copy link
Member

@garyb garyb commented Jun 3, 2020

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 the Foldable1 since it uses Last 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.

@garyb garyb requested a review from hdgarrood June 3, 2020 15:25
where
go j (Cons a _) | j <= 0 = a
go j (Cons _ as) = go (j - 1) as
go _ Nil = un Last (foldMap1 Last xs)

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.

@garyb
Copy link
Member Author

garyb commented Jul 22, 2020

Finally added that case 🤦

@JordanMartinez
Copy link
Contributor

Perhaps this should be merged before #26 because its psa version is lower than v0.8.0?

@@ -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")
Copy link
Contributor

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.

  

Copy link
Member Author

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.

Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So.... 1,000, right? 😆

Copy link
Contributor

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.

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

Successfully merging this pull request may close these issues.

Gen.elements only returns first or last element.
5 participants