1module Test.Data.List.Reverse.StrictSpine where
2
3import qualified Data.List.Reverse.StrictSpine as Rev
4import qualified Data.List.Match as Match
5import qualified Data.List as List
6import Data.Tuple.HT (mapFst, mapPair, swap, )
7
8import Test.QuickCheck (Testable, quickCheck, )
9
10import Prelude hiding (takeWhile, dropWhile, span, )
11
12
13takeWhile :: (Ord a) => (a -> Bool) -> [a] -> Bool
14takeWhile p xs =
15   Rev.takeWhile p xs == reverse (List.takeWhile p (reverse xs))
16
17dropWhile :: (Ord a) => (a -> Bool) -> [a] -> Bool
18dropWhile p xs =
19   Rev.dropWhile p xs == reverse (List.dropWhile p (reverse xs))
20
21span :: (Ord a) => (a -> Bool) -> [a] -> Bool
22span p xs =
23   Rev.span p xs == swap (mapPair (reverse, reverse) (List.span p (reverse xs)))
24
25spanTakeDrop :: (Ord a) => (a -> Bool) -> [a] -> Bool
26spanTakeDrop p xs =
27   Rev.span p xs == (Rev.dropWhile p xs, Rev.takeWhile p xs)
28
29takeWhileBottom :: (Ord a) => a -> [a] -> [a] -> Bool
30takeWhileBottom x xs pad =
31   let ys = Rev.takeWhile (x/=) $ Match.replicate pad undefined ++ x:xs
32   in  ys==ys
33
34dropWhileBottom :: (Ord a) => a -> [a] -> [a] -> Bool
35dropWhileBottom x xs pad =
36   let n = length $ Rev.dropWhile (x/=) $ Match.replicate pad undefined ++ x:xs
37   in  n==n
38
39spanBottom :: (Ord a) => a -> [a] -> [a] -> Bool
40spanBottom x xs pad =
41   let (n,ys) =
42          mapFst length $ Rev.span (x/=) $
43          Match.replicate pad undefined ++ x:xs
44   in  n==n && ys==ys
45
46
47simple ::
48   (Testable test) =>
49   (Float -> [Float] -> test) -> IO ()
50simple = quickCheck
51
52
53tests :: [(String, IO ())]
54tests =
55   ("takeWhile",     simple (\a -> takeWhile (a>=))) :
56   ("dropWhile",     simple (\a -> dropWhile (a>=))) :
57   ("span",          simple (\a -> span (a>=))) :
58   ("spanTakeDrop",  simple (\a -> spanTakeDrop (a>=))) :
59   ("takeWhileBottom", simple takeWhileBottom) :
60   ("dropWhileBottom", simple dropWhileBottom) :
61   ("spanBottom", simple spanBottom) :
62   []
63