Skip to content

Make elements test no longer hang at cost of possible false negative #29

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 3 commits into from
Dec 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import Prelude
import Control.Monad.Gen as Gen
import Control.Monad.Gen.Common as GenC
import Control.Monad.Rec.Class (class MonadRec, tailRecM)
import Data.Array as Array
import Data.Bifunctor (bimap)
import Data.Int as Int
import Data.Foldable (foldl)
import Data.NonEmpty (NonEmpty, (:|))
import Data.Tuple (Tuple(..), snd)
import Effect (Effect)
Expand All @@ -32,12 +34,21 @@ 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")
_ ← Gen.suchThat (Gen.elements ("A" :| ["B", "C", "D"])) (_ == "B")
_ ← Gen.suchThat (Gen.elements ("A" :| ["B", "C", "D"])) (_ == "C")
_ ← Gen.suchThat (Gen.elements ("A" :| ["B", "C", "D"])) (_ == "D")

log "Ensure that `elements` will produce all possible values (low chance of false negative)"
let testElems = "A" :| ["B", "C", "D"]

-- use `resize` to generate an Array of 1,000 elements
-- to reduce the chance of a false positive.
elems :: Array String <- Gen.resize (\_ -> 1000) (Gen.unfoldable (Gen.elements testElems))

let
-- implement a Set-like value without depending on ordered-collections
-- because that library depends on this one.
actualElems = Array.sort $ Array.nub elems
expectedElems = foldl Array.snoc [] testElems

liftEffect $ assertEqual { actual: actualElems, expected: expectedElems }
pure unit

log "check frequency"
Expand Down