Skip to content

Commit 1841b52

Browse files
committed
start refactor
1 parent b16911c commit 1841b52

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

src/Kudzu.hs

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,72 @@ import qualified Test.QuickCheck.Random as QC
1010
import Trace.Hpc.Reflect (examineTix)
1111
import Trace.Hpc.Tix (Tix (..), TixModule (..))
1212

13-
testUntilSameQCMany :: (Traversable t, QC.Testable a) => Int -> t a -> IO (t (Int, Maybe Integer))
13+
testUntilSameQCMany :: (Traversable t, QC.Testable a) => Int -> t a -> IO (t (KudzuResult Integer))
1414
testUntilSameQCMany howMany ts = do
15-
mapM (testUntilSameQC howMany) ts
15+
mapM (testUntilSameQC howMany) ts
1616

1717
-- | QuickCheck
18-
testUntilSameQC :: QC.Testable a => Int -> a -> IO (Int, Maybe Integer)
18+
testUntilSameQC :: (QC.Testable a) => Int -> a -> IO (KudzuResult Integer)
1919
testUntilSameQC n testable = do
20-
let rs = map (examineAndCount' testable) [0..n]
21-
r1 <- head rs
22-
grabUntilNSame 0 n n (tail rs) r1
20+
let rs = map (examineAndCount' testable) [0 .. n]
21+
r1 <- head rs
22+
grabUntilNSame 0 n n (tail rs) r1
2323

24-
examineAndCount' :: QC.Testable prop => prop -> Int -> IO Integer
24+
examineAndCount' :: (QC.Testable prop) => prop -> Int -> IO Integer
2525
examineAndCount' v size = do
26-
qcg <- QC.newQCGen
27-
QC.quickCheckWith (QC.stdArgs { QC.replay = Just (qcg, size)}) (QC.withMaxSuccess 1 v)
28-
tixModuleCount <$> examineTix
26+
qcg <- QC.newQCGen
27+
QC.quickCheckWith (QC.stdArgs{QC.replay = Just (qcg, size)}) (QC.withMaxSuccess 1 v)
28+
tixModuleCount <$> examineTix
2929

3030
-- | Hedgehog
31-
testUntilSameHHMany :: Traversable t => Int -> t HH.Property -> IO (t (Int, Maybe Integer))
31+
testUntilSameHHMany :: (Traversable t) => Int -> t HH.Property -> IO (t (KudzuResult Integer))
3232
testUntilSameHHMany howMany ps = do
33-
mapM (testUntilSameHH howMany) ps
33+
mapM (testUntilSameHH howMany) ps
3434

35-
testUntilSameHH :: Int -> HH.Property -> IO (Int, Maybe Integer)
35+
testUntilSameHH :: Int -> HH.Property -> IO (KudzuResult Integer)
3636
testUntilSameHH n prop = do
37-
let rs = examineAndCountHH <$> repeat prop
38-
r1 <- head rs
39-
grabUntilNSame 0 n n (tail rs) r1
37+
let rs = examineAndCountHH <$> repeat prop
38+
r1 <- head rs
39+
grabUntilNSame 0 n n (tail rs) r1
4040

4141
examineAndCountHH :: HH.Property -> IO Integer
4242
examineAndCountHH prop = do
43-
passed <- HH.check prop
44-
unless passed $ error "property failed"
45-
tixModuleCount <$> examineTix
43+
passed <- HH.check prop
44+
unless passed $ error "property failed"
45+
tixModuleCount <$> examineTix
4646

4747
-- | LeanCheck
48-
testUntilSameLCMany :: (Traversable t, LC.Testable a) => Int -> t a -> IO (t (Int, Maybe Integer))
48+
testUntilSameLCMany :: (Traversable t, LC.Testable a) => Int -> t a -> IO (t (KudzuResult Integer))
4949
testUntilSameLCMany howMany ts = do
50-
mapM (testUntilSameLC howMany) ts
50+
mapM (testUntilSameLC howMany) ts
5151

52-
testUntilSameLC :: LC.Testable a => Int -> a -> IO (Int, Maybe Integer)
52+
testUntilSameLC :: (LC.Testable a) => Int -> a -> IO (KudzuResult Integer)
5353
testUntilSameLC n testable = do
54-
let rs = examineAndCount <$> LC.results testable
55-
r1 <- head rs
56-
grabUntilNSame 0 n n (tail rs) r1
54+
let rs = examineAndCount <$> LC.results testable
55+
r1 <- head rs
56+
grabUntilNSame 0 n n (tail rs) r1
5757

5858
examineAndCount :: ([String], Bool) -> IO Integer
59-
examineAndCount v = unless (snd v) (error $ unwords ("test failed with:":fst v)) >> tixModuleCount <$> examineTix
59+
examineAndCount v = unless (snd v) (error $ unwords ("test failed with:" : fst v)) >> tixModuleCount <$> examineTix
6060

61-
grabUntilNSame :: (Monad m, Eq a) => Int -> Int -> Int -> [m a] -> a -> m (Int, Maybe a)
62-
grabUntilNSame c _ 0 _ z = pure (c, Just z)
63-
grabUntilNSame c _ _ [] _ = pure (c, Nothing)
61+
data KudzuResult a = KFail Int | KSuccess Int a
62+
63+
{-
64+
Keep running property tests until the "amount" of code coverage is the same for N iterations of one test.
65+
orig: the number of iterations of a test that must have the same amount of code coverage before you give up and stop.
66+
c: the number of iterations checked
67+
n: countdown to the success case
68+
-}
69+
grabUntilNSame :: (Monad m, Eq a) => Int -> Int -> Int -> [m a] -> a -> m (KudzuResult a)
70+
grabUntilNSame c _ 0 _ z = pure $ KSuccess c z -- we reached the desired window size
71+
grabUntilNSame c _ _ [] _ = pure $ KFail c -- if we run out of list elements for test results, we're done
6472
grabUntilNSame c orig n (a : as) z = do
65-
a' <- a
66-
if a' == z
67-
then grabUntilNSame (c + 1) orig (n - 1) as z
68-
else grabUntilNSame (c + 1) orig orig as a'
73+
a' <- a
74+
if a' == z -- is the count of regions covered from this run the same as last run?
75+
then grabUntilNSame (c + 1) orig (n - 1) as z
76+
else grabUntilNSame (c + 1) orig orig as a'
77+
78+
-- where go c n a z =
6979

7080
-- | How many regions were executed at least once for this module?
7181
tixCount :: TixModule -> Integer

0 commit comments

Comments
 (0)