| 1 | module SequenceCheck where |
|---|
| 2 | |
|---|
| 3 | import qualified Data.Sequence as Seq |
|---|
| 4 | import qualified Data.List as List |
|---|
| 5 | import qualified Data.Foldable as Fold |
|---|
| 6 | import Data.List |
|---|
| 7 | import Data.Sequence |
|---|
| 8 | import Test.QuickCheck |
|---|
| 9 | import Data.Foldable |
|---|
| 10 | |
|---|
| 11 | prop_replicate :: Int -> Int -> Property |
|---|
| 12 | prop_replicate n x = let repSeq = Seq.replicate n x in |
|---|
| 13 | n >= 0 ==> (valid repSeq && toList repSeq == List.replicate n x) |
|---|
| 14 | |
|---|
| 15 | prop_zip :: [Int] -> [Int] -> Bool |
|---|
| 16 | prop_zip xs ys = let xys = Seq.zip (fromList xs) (fromList ys) |
|---|
| 17 | in valid xys && List.zip xs ys == toList xys |
|---|
| 18 | |
|---|
| 19 | prop_scanl :: [Int] -> Bool |
|---|
| 20 | prop_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 | |
|---|
| 24 | prop_scanr :: [Int] -> Bool |
|---|
| 25 | prop_scanr xs = let ys = Seq.scanr (:) [] (fromList xs) in |
|---|
| 26 | valid ys && toList ys == List.scanr (:) [] xs |
|---|
| 27 | |
|---|
| 28 | prop_tails :: [Int] -> Bool |
|---|
| 29 | prop_tails xs = let xss = Seq.tails (fromList xs) in |
|---|
| 30 | valid xss && Fold.all valid xss && map toList (toList xss) == List.tails xs |
|---|
| 31 | |
|---|
| 32 | prop_inits :: [Int] -> Bool |
|---|
| 33 | prop_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 | |
|---|
| 37 | prop_span :: [Int] -> Bool |
|---|
| 38 | prop_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 | |
|---|
| 41 | prop_unfoldr :: [Int] -> Bool |
|---|
| 42 | prop_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 | |
|---|
| 46 | prop_iterate :: Int -> Property |
|---|
| 47 | prop_iterate n = n >= 0 ==> let ys= Seq.iterate n (+1) 0 in valid ys && toList ys == List.take n (List.iterate (+1) 0) |
|---|
| 48 | |
|---|
| 49 | prop_partition :: [Int] -> Bool |
|---|
| 50 | prop_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 | |
|---|
| 53 | prop_append :: [Int] -> [Int] -> Bool |
|---|
| 54 | prop_append xs ys = let xys = Seq.fromList xs >< Seq.fromList ys in |
|---|
| 55 | valid xys && toList xys == xs ++ ys |
|---|
| 56 | |
|---|
| 57 | main = 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) |
|---|