Skip to content

Commit

Permalink
Counterexample -> Witness
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximilianAlgehed committed Apr 18, 2024
1 parent ab28a17 commit fdfa60c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
12 changes: 6 additions & 6 deletions src/Test/QuickCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ module Test.QuickCheck
, quickCheckWithResult
, quickCheckResult
#ifndef NO_TYPEABLE
, quickCheckCounterexample
, quickCheckWithCounterexample
, quickCheckWitness
, quickCheckWithWitness
#endif
, recheck
, isSuccess
Expand Down Expand Up @@ -323,10 +323,10 @@ module Test.QuickCheck
, disjoin
-- ** What to do on failure
#ifndef NO_TYPEABLE
, Counterexample(..)
, withCounterexample
, coerceCounterexample
, castCounterexample
, Witness(..)
, withWitness
, coerceWitness
, castWitness
#endif
, counterexample
, printTestCase
Expand Down
40 changes: 20 additions & 20 deletions src/Test/QuickCheck/Property.hs
Original file line number Diff line number Diff line change
Expand Up @@ -256,29 +256,29 @@ data CallbackKind = Counterexample -- ^ Affected by the 'verbose' combinator
| NotCounterexample -- ^ Not affected by the 'verbose' combinator

#ifndef NO_TYPEABLE
data Counterexample = forall a. (Typeable a, Show a) => Cex a
data Witness = forall a. (Typeable a, Show a) => Cex a

instance Show Counterexample where
instance Show Witness where
show (Cex a) = show a

coerceCounterexample :: Typeable a => Counterexample -> a
coerceCounterexample (Cex a) = case cast a of
Nothing -> error $ "Can't coerceCounterexample " ++ show a
coerceWitness :: Typeable a => Witness -> a
coerceWitness (Cex a) = case cast a of
Nothing -> error $ "Can't coerceWitness " ++ show a
Just a -> a

castCounterexample :: Typeable a => Counterexample -> Maybe a
castCounterexample (Cex a) = cast a
castWitness :: Typeable a => Witness -> Maybe a
castWitness (Cex a) = cast a

data Counterexamples = NoCounterexamples
| forall a. (Typeable a, Show a) => a :! Counterexamples
data Witnesses = NoWitnesses
| forall a. (Typeable a, Show a) => a :! Witnesses

toCounterexamples :: [Counterexample] -> Counterexamples
toCounterexamples [] = NoCounterexamples
toCounterexamples (Cex a : ces) = a :! toCounterexamples ces
toWitnesses :: [Witness] -> Witnesses
toWitnesses [] = NoWitnesses
toWitnesses (Cex a : ces) = a :! toWitnesses ces

#define COUNTEREXAMPLES(a) , theCounterexamples a
#define WITNESSES(a) , theWitnesses a
#else
#define COUNTEREXAMPLES(a)
#define WITNESSES(a)
#endif

-- | The result of a single test.
Expand Down Expand Up @@ -316,7 +316,7 @@ data Result
-- ^ the callbacks for this test case
, testCase :: [String]
-- ^ the generated test case
COUNTEREXAMPLES(:: [Counterexample])
WITNESSES(:: [Witness])
}

exception :: String -> AnException -> Result
Expand Down Expand Up @@ -357,7 +357,7 @@ succeeded, failed, rejected :: Result
, requiredCoverage = []
, callbacks = []
, testCase = []
COUNTEREXAMPLES(= [])
WITNESSES(= [])
}

--------------------------------------------------------------------------
Expand Down Expand Up @@ -532,10 +532,10 @@ withMaxSize :: Testable prop => Int -> prop -> Property
withMaxSize n = n `seq` mapTotalResult (\res -> res{ maybeMaxTestSize = Just n })

#ifndef NO_TYPEABLE
-- | Return a value in the 'counterexamples' field of the 'Result' returned by 'quickCheckResult'. Counterexamples
-- | Return a value in the 'counterexamples' field of the 'Result' returned by 'quickCheckResult'. Witnesses
-- are returned outer-most first.
withCounterexample :: (Typeable a, Show a, Testable prop) => a -> prop -> Property
withCounterexample a = a `seq` mapTotalResult (\res -> res{ theCounterexamples = Cex a : theCounterexamples res })
withWitness :: (Typeable a, Show a, Testable prop) => a -> prop -> Property
withWitness a = a `seq` mapTotalResult (\res -> res{ theWitnesses = Cex a : theWitnesses res })
#endif

-- | Check that all coverage requirements defined by 'cover' and 'coverTable'
Expand Down Expand Up @@ -1001,7 +1001,7 @@ disjoin ps =
testCase =
testCase result1 ++
testCase result2
COUNTEREXAMPLES(= theCounterexamples result1 ++ theCounterexamples result2)
WITNESSES(= theWitnesses result1 ++ theWitnesses result2)
}
Nothing -> result2
-- The "obvious" semantics of .||. has:
Expand Down
16 changes: 8 additions & 8 deletions src/Test/QuickCheck/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ data Result
, failingClasses :: Set String
-- ^ The test case's classes (see 'classify')
#ifndef NO_TYPEABLE
, counterexamples :: [Counterexample]
-- ^ The existentially quantified counterexamples provided by 'withCounterexample'
, counterexamples :: [Witness]
-- ^ The existentially quantified counterexamples provided by 'withWitness'
#endif
}
-- | A property that should have failed did not
Expand Down Expand Up @@ -207,17 +207,17 @@ quickCheckWithResult a p =
-- | Test a property and get counterexamples as a result. Can be used like:
--
-- @
-- $> x :! _ <- quickCheckCounterexample $ \ x -> withCounterexample (x :: Int) (x > 0)
-- $> x :! _ <- quickCheckWitness $ \ x -> withWitness (x :: Int) (x > 0)
-- *** Failed! Falsified (after 1 test):
-- 0
-- $> x
-- 0
quickCheckCounterexample :: Testable prop => prop -> IO Counterexamples
quickCheckCounterexample = quickCheckWithCounterexample stdArgs
quickCheckWitness :: Testable prop => prop -> IO Witnesses
quickCheckWitness = quickCheckWithWitness stdArgs

-- | Test a property, using test arguments, and get counterexamples as a result.
quickCheckWithCounterexample :: Testable prop => Args -> prop -> IO Counterexamples
quickCheckWithCounterexample args p = toCounterexamples . counterexamples <$> quickCheckWithResult args p
quickCheckWithWitness :: Testable prop => Args -> prop -> IO Witnesses
quickCheckWithWitness args p = toWitnesses . counterexamples <$> quickCheckWithResult args p
#endif

-- | Re-run a property with the seed and size that failed in a run of 'quickCheckResult'.
Expand Down Expand Up @@ -508,7 +508,7 @@ runATest st prop =
, failingLabels = P.labels res
, failingClasses = Set.fromList (map fst $ filter snd $ P.classes res)
#ifndef NO_TYPEABLE
, counterexamples = theCounterexamples res
, counterexamples = theWitnesses res
#endif
}
where
Expand Down

0 comments on commit fdfa60c

Please sign in to comment.