on 3feeec2 help sign in
227 lines 7.3 kB history raw
1{-# LANGUAGE MonadComprehensions #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE OverloadedLists #-}
5module Main (main) where
7#if !(MIN_VERSION_base(4,11,0))
8import Data.Semigroup ((<>))
9#endif
10import Test.Hspec
11import qualified GHC.Exts as GHC
13#ifdef USE_STREAMLY_LIST
14import Data.Functor.Identity
15import Streamly.Internal.Data.List (List(..), pattern Cons, pattern Nil, ZipList(..),
16 fromZipList, toZipList)
17import Streamly.Prelude (SerialT)
18import qualified Streamly.Prelude as S
19#else
20import Prelude -- to suppress compiler warning
22type List = []
24pattern Nil :: [a]
25pattern Nil <- [] where Nil = []
27pattern Cons :: a -> [a] -> [a]
28pattern Cons x xs = x : xs
29infixr 5 `Cons`
31{-# COMPLETE Nil, Cons #-}
32#endif
34moduleName :: String
35#ifdef USE_STREAMLY_LIST
36moduleName = "Data.List+Data.List.Base"
37#else
38moduleName = "Data.List.Base"
39#endif
41main :: IO ()
42main = hspec $
43 describe moduleName $ do
44#ifdef USE_STREAMLY_LIST
45 describe "OverloadedLists for 'SerialT Identity' type" $ do
46 it "Overloaded lists" $ do
47 ([1..3] :: SerialT Identity Int) `shouldBe` S.fromList [1..3]
48 GHC.toList ([1..3] :: SerialT Identity Int) `shouldBe` [1..3]
50 it "Show instance" $ do
51 show (S.fromList [1..3] :: SerialT Identity Int)
52 `shouldBe` "fromList [1,2,3]"
53 it "Read instance" $ do
54 (read "fromList [1,2,3]" :: SerialT Identity Int) `shouldBe` [1..3]
56 it "Eq instance" $ do
57 ([1,2,3] :: SerialT Identity Int) == [1,2,3] `shouldBe` True
59 it "Ord instance" $ do
60 ([1,2,3] :: SerialT Identity Int) > [1,2,1] `shouldBe` True
62 it "Monad comprehension" $ do
63 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
64 ([(1,1), (1,2), (2,1), (2,2)] :: SerialT Identity (Int, Int))
66 it "Foldable (sum)" $ sum ([1..3] :: SerialT Identity Int)
67 `shouldBe` 6
69 it "Traversable (mapM)" $
70 mapM return ([1..10] :: SerialT Identity Int)
71 `shouldReturn` [1..10]
73 describe "OverloadedStrings for 'SerialT Identity' type" $ do
74 it "overloaded strings" $ do
75 ("hello" :: SerialT Identity Char) `shouldBe` S.fromList "hello"
76#endif
78 describe "OverloadedLists for List type" $ do
79 it "overloaded lists" $ do
80 ([1..3] :: List Int) `shouldBe` GHC.fromList [1..3]
81 GHC.toList ([1..3] :: List Int) `shouldBe` [1..3]
83 it "Construct empty list" $ (Nil :: List Int) `shouldBe` []
85 it "Construct a list" $ do
86 (Nil :: List Int) `shouldBe` []
87 ('x' `Cons` Nil) `shouldBe` ['x']
88 (1 `Cons` [2 :: Int]) `shouldBe` [1,2]
89 (1 `Cons` 2 `Cons` 3 `Cons` Nil :: List Int) `shouldBe` [1,2,3]
91 it "pattern matches" $ do
92 case [] of
93 Nil -> return ()
94 _ -> expectationFailure "not reached"
96 case ['x'] of
97 Cons 'x' Nil -> return ()
98 _ -> expectationFailure "not reached"
100 case [1..10 :: Int] of
101 Cons x xs -> do
102 x `shouldBe` 1
103 xs `shouldBe` [2..10]
104 _ -> expectationFailure "not reached"
106 case [1..10 :: Int] of
107 x `Cons` y `Cons` xs -> do
108 x `shouldBe` 1
109 y `shouldBe` 2
110 xs `shouldBe` [3..10]
111 _ -> expectationFailure "not reached"
113 it "Show instance" $ do
114 show ([1..3] :: List Int) `shouldBe`
115#ifdef USE_STREAMLY_LIST
116 "List {toSerial = fromList [1,2,3]}"
117#else
118 "[1,2,3]"
119#endif
121 it "Read instance" $ do
122 (read
123#ifdef USE_STREAMLY_LIST
124 "List {toSerial = fromList [1,2,3]}"
125#else
126 "[1,2,3]"
127#endif
128 :: List Int) `shouldBe` [1..3]
130 it "Eq instance" $ do
131 ([1,2,3] :: List Int) == [1,2,3] `shouldBe` True
133 it "Ord instance" $ do
134 ([1,2,3] :: List Int) > [1,2,1] `shouldBe` True
136 it "Monad comprehension" $ do
137 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
138 ([(1,1), (1,2), (2,1), (2,2)] :: List (Int, Int))
140 it "Foldable (sum)" $ sum ([1..3] :: List Int) `shouldBe` 6
142 it "Traversable (mapM)" $
143 mapM return ([1..10] :: List Int)
144 `shouldReturn` [1..10]
146 describe "OverloadedStrings for List type" $ do
147 it "overloaded strings" $ do
148 ("hello" :: List Char) `shouldBe` GHC.fromList "hello"
150 it "pattern matches" $ do
151#if __GLASGOW_HASKELL__ >= 802
152 case "" of
153#else
154 case "" :: List Char of
155#endif
156 Nil -> return ()
157 _ -> expectationFailure "not reached"
159 case "a" of
160 Cons x Nil -> x `shouldBe` 'a'
161 _ -> expectationFailure "not reached"
163 case "hello" <> "world" of
164 Cons x1 (Cons x2 xs) -> do
165 x1 `shouldBe` 'h'
166 x2 `shouldBe` 'e'
167 xs `shouldBe` "lloworld"
168 _ -> expectationFailure "not reached"
170#ifdef USE_STREAMLY_LIST
171 describe "OverloadedLists for ZipList type" $ do
172 it "overloaded lists" $ do
173 ([1..3] :: ZipList Int) `shouldBe` GHC.fromList [1..3]
174 GHC.toList ([1..3] :: ZipList Int) `shouldBe` [1..3]
176 it "toZipList" $ do
177 toZipList (Nil :: List Int) `shouldBe` []
178 toZipList ('x' `Cons` Nil) `shouldBe` ['x']
179 toZipList (1 `Cons` [2 :: Int]) `shouldBe` [1,2]
180 toZipList (1 `Cons` 2 `Cons` 3 `Cons` Nil :: List Int) `shouldBe` [1,2,3]
182 it "fromZipList" $ do
183 case fromZipList [] of
184 Nil -> return ()
185 _ -> expectationFailure "not reached"
187 case fromZipList ['x'] of
188 Cons 'x' Nil -> return ()
189 _ -> expectationFailure "not reached"
191 case fromZipList [1..10 :: Int] of
192 Cons x xs -> do
193 x `shouldBe` 1
194 xs `shouldBe` [2..10]
195 _ -> expectationFailure "not reached"
197 case fromZipList [1..10 :: Int] of
198 x `Cons` y `Cons` xs -> do
199 x `shouldBe` 1
200 y `shouldBe` 2
201 xs `shouldBe` [3..10]
202 _ -> expectationFailure "not reached"
204 it "Show instance" $ do
205 show ([1..3] :: ZipList Int) `shouldBe`
206 "ZipList {toZipSerial = fromList [1,2,3]}"
208 it "Read instance" $ do
209 (read "ZipList {toZipSerial = fromList [1,2,3]}" :: ZipList Int)
210 `shouldBe` [1..3]
212 it "Eq instance" $ do
213 ([1,2,3] :: ZipList Int) == [1,2,3] `shouldBe` True
215 it "Ord instance" $ do
216 ([1,2,3] :: ZipList Int) > [1,2,1] `shouldBe` True
218 it "Foldable (sum)" $ sum ([1..3] :: ZipList Int) `shouldBe` 6
220 it "Traversable (mapM)" $
221 mapM return ([1..10] :: ZipList Int)
222 `shouldReturn` [1..10]
224 it "Applicative Zip" $ do
225 (,) <$> "abc" <*> [1..3] `shouldBe`
226 ([('a',1),('b',2),('c',3)] :: ZipList (Char, Int))
227#endif