Ticket #3271: SequenceCheck.hs

File SequenceCheck.hs, 2.4 KB (added by LouisWasserman, 6 years ago)

Quick Check properties testing that each function specified in the ticket behaves analogously to the Data.List equivalents.

Line 
1module SequenceCheck where
2
3import qualified Data.Sequence as Seq
4import qualified Data.List as List
5import qualified Data.Foldable as Fold
6import Data.List
7import Data.Sequence
8import Test.QuickCheck
9import Data.Foldable
10
11prop_replicate :: Int -> Int -> Property
12prop_replicate n x = let repSeq = Seq.replicate n x in
13        n >= 0 ==> (valid repSeq && toList repSeq == List.replicate n x)
14
15prop_zip :: [Int] -> [Int] -> Bool
16prop_zip xs ys = let xys = Seq.zip (fromList xs) (fromList ys)
17        in  valid xys && List.zip xs ys == toList xys
18
19prop_scanl :: [Int] -> Bool
20prop_scanl xs = let ys = Seq.scanl f [] (fromList xs) in 
21        valid ys && toList ys == List.scanl f [] xs
22        where   f xs x = x:xs
23
24prop_scanr :: [Int] -> Bool
25prop_scanr xs = let ys = Seq.scanr (:) [] (fromList xs) in 
26        valid ys && toList ys == List.scanr (:) [] xs
27
28prop_tails :: [Int] -> Bool
29prop_tails xs = let xss = Seq.tails (fromList xs) in
30        valid xss && Fold.all valid xss && map toList (toList xss) == List.tails xs
31
32prop_inits :: [Int] -> Bool
33prop_inits xs = let xss = Seq.inits (fromList xs) in
34        valid xss && Fold.all valid xss && map toList (toList xss) == List.inits xs
35
36
37prop_span :: [Int] -> Bool
38prop_span xs = let (ys1, ys2) = Seq.span even (fromList xs) in 
39        valid ys1 && valid ys2 && (toList ys1, toList ys2) == List.span even xs
40
41prop_unfoldr :: [Int] -> Bool
42prop_unfoldr xs = let ys = Seq.unfoldr unfold xs in valid ys && toList ys == xs
43        where   unfold (x:xs) = Just (x, xs)
44                unfold [] = Nothing
45
46prop_iterate :: Int -> Property
47prop_iterate n = n >= 0 ==> let ys= Seq.iterate n (+1) 0 in valid ys && toList ys == List.take n (List.iterate (+1) 0)
48
49prop_partition :: [Int] -> Bool
50prop_partition xs = let (ys1, ys2) = Seq.partition even (Seq.fromList xs) in
51        valid ys1 && valid ys2 && (toList ys1, toList ys2) == List.partition even xs
52
53prop_append :: [Int] -> [Int] -> Bool
54prop_append xs ys = let xys = Seq.fromList xs >< Seq.fromList ys in 
55        valid xys && toList xys == xs ++ ys
56
57main = do       quickCheck (label "replicate" prop_replicate)
58                quickCheck (label "zip" prop_zip)
59                quickCheck (label "scanl" prop_scanl)
60                quickCheck (label "scanr" prop_scanr)
61                quickCheck (label "tails" prop_tails)
62                quickCheck (label "inits" prop_inits)
63                quickCheck (label "span" prop_span)
64                quickCheck (label "unfoldr" prop_unfoldr)
65                quickCheck (label "iterate" prop_iterate)
66                quickCheck (label "partition" prop_partition)
67                quickCheck (label "append" prop_append)