Skip to content

Commit

Permalink
Tidied up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Porter committed Nov 9, 2011
1 parent 500fbae commit 838cbd8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ main = do
if length a > 0
then return $ read (head a)
else return 10000
putStrLn $ "Testing the word-count MapReduce algorithm with size bound " ++ show b
putStrLn $ "Testing the word-count MapReduce algorithm with size bound " ++ show (fromIntegral b)
putStrLn "WARNING: Can be very slow if bound > 10000"
putStrLn "Total count test (coarse-grained): " >> quickCheck (prop_Equal b)
putStrLn "Detail test (very strong): " >> quickCheck (prop_FullCheck b)
20 changes: 13 additions & 7 deletions src/Process/MapReduce/WordCount/Documents.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Process.MapReduce.WordCount.Documents (
makeDictionary,makeSequence,makeWords
) where

import Test.QuickCheck(Gen,choose)
import Test.QuickCheck(Gen,Positive,choose)
import Control.Monad (liftM2)


Expand Down Expand Up @@ -38,12 +38,18 @@ makeSequence m n = liftM2 (:) (choose (0,m-1)) $ makeSequence m (n-1)

-- | Make a list of a specified number of random words drawn from a dictionary of
-- at most a specified size.
makeWords :: Int -> Int -- ^ @(nWords,maxVocab)@ where @nWords@ is the number of words
-- required, and @maxVocab@ is the maximum vocabulary size
-> Gen [String] -- ^ The message wrapped in 'Gen'
makeWords n m = if (n<=0) || (m <=0)
makeWords :: Positive Int -- ^ The number of words to generate
-> Positive Int -- The maximum vocabulary size
-> Gen [String] -- ^ The word list wrapped in 'Gen'
makeWords n m = if
(n<=0) || (m <=0)
then return []
else do
dict <- makeDictionary m
seq <- makeSequence m n
-- This is important: 'makeDictionary' and 'makeSequence' must be
-- able to take 0 as an argument
let m' = fromIntegral m
let n' = fromIntegral n
dict <- makeDictionary m'
seq <- makeSequence m' n'
return $ map (\i -> dict!!i) seq

45 changes: 17 additions & 28 deletions src/Process/MapReduce/WordCount/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,16 @@ import Test.QuickCheck
import Process.MapReduce.WordCount(mapReduce)
import Process.MapReduce.WordCount.Documents(makeWords)

-- | Data type for specifying a test case; simply a pair of 'Int' values,
-- one for the dictionary size, the other for the document length. Have
-- chosen to do it this way, rather than using a @'Positive' 'Int'@ to
-- simplify application code.
data Pair = P Int Int
deriving (Show)

-- | Generator for a 'Pair', with the positivity constraint built in.
instance Arbitrary Pair where
arbitrary = do
x <- choose (1,maxBound::Int)
y <- choose (1,maxBound::Int)
return $ P x y

-- | Utility function to reduce a 'Pair' modulo some 'Int'
shrinkIt :: Int -- ^ the base to reduce modulo
-> Pair -- ^ the pair to reduce
-> Pair
shrinkIt b (P n m) = P (r n b) (r m b)
-- | Utility function to reduce a @('Positive' 'Int','Positive' 'Int')@ modulo
-- some @'Positive' 'Int'@
shrinkIt :: Positive Int -- ^ the base to reduce modulo
-> (Positive Int,Positive Int) -- ^ the pair to reduce
-> (Positive Int,Positive Int)
shrinkIt b (n,m) = (n `rem1` b, m `rem1` b)
where
r x b = 1 + (x `rem` b)
rem1 x b = 1 + (x `rem` b)


-- #####################################################################
-- THE TESTS: TEST 1
Expand All @@ -51,13 +40,13 @@ countWords = foldr ((+).snd) 0
-- | Simple test that 'mapReduce' returns the correct total number of words,
-- that is that the sum of all of its counts equals the total number of
-- words it was given.
prop_Equal :: Int -- ^ The upper bound on test size
-> Pair -- ^ The proposed test set
-> Property -- ^ Whether the test passed
prop_Equal :: Positive Int -- ^ The upper bound on test size
-> (Positive Int,Positive Int) -- ^ The proposed test set
-> Property -- ^ Whether the test passed
prop_Equal b p = forAll (makeWords n m) $ \words ->
countWords (mapReduce 16 words) == n
countWords (mapReduce 16 words) == fromIntegral n
where
P n m = shrinkIt b p
(n,m) = shrinkIt b p

-- #####################################################################
-- THE TESTS: TEST 2
Expand Down Expand Up @@ -98,10 +87,10 @@ testCount ss cs = compare (makeCounts ss) cs

-- | More complex test to verify the counts 'mapReduce' produces on a word-by-word
-- basis.
prop_FullCheck :: Int -- ^ The upper bound on test size
-> Pair -- ^ The proposed test set
-> Property -- ^ Whether the test passed
prop_FullCheck :: Positive Int -- ^ The upper bound on test size
-> (Positive Int,Positive Int) -- ^ The proposed test set
-> Property -- ^ Whether the test passed
prop_FullCheck b p = forAll (makeWords n m) $ \words ->
testCount words (mapReduce 16 words)
where
P n m = shrinkIt b p
(n,m) = shrinkIt b p

0 comments on commit 838cbd8

Please sign in to comment.