on 4e252a8 help sign in
220 lines 7.2 kB history raw
1{-# LANGUAGE MonadComprehensions #-}
2{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE OverloadedLists #-}
5module Main (main) where
7import Test.Hspec
8import qualified GHC.Exts as GHC
10#ifdef USE_STREAMLY_LIST
11import Data.Functor.Identity
12import Streamly.Internal.Data.List (List(..), pattern Cons, pattern Nil, ZipList(..),
13 fromZipList, toZipList)
14import Streamly.Prelude (SerialT)
15import qualified Streamly.Prelude as S
16#else
17import Prelude -- to suppress compiler warning
19type List = []
21pattern Nil :: [a]
22pattern Nil <- [] where Nil = []
24pattern Cons :: a -> [a] -> [a]
25pattern Cons x xs = x : xs
26infixr 5 `Cons`
28{-# COMPLETE Nil, Cons #-}
29#endif
31moduleName :: String
32#ifdef USE_STREAMLY_LIST
33moduleName = "Data.List+Data.List.Base"
34#else
35moduleName = "Data.List.Base"
36#endif
38main :: IO ()
39main = hspec $
40 describe moduleName $ do
41#ifdef USE_STREAMLY_LIST
42 describe "OverloadedLists for 'SerialT Identity' type" $ do
43 it "Overloaded lists" $ do
44 ([1..3] :: SerialT Identity Int) `shouldBe` S.fromList [1..3]
45 GHC.toList ([1..3] :: SerialT Identity Int) `shouldBe` [1..3]
47 it "Show instance" $ do
48 show (S.fromList [1..3] :: SerialT Identity Int)
49 `shouldBe` "fromList [1,2,3]"
50 it "Read instance" $ do
51 (read "fromList [1,2,3]" :: SerialT Identity Int) `shouldBe` [1..3]
53 it "Eq instance" $ do
54 ([1,2,3] :: SerialT Identity Int) == [1,2,3] `shouldBe` True
56 it "Ord instance" $ do
57 ([1,2,3] :: SerialT Identity Int) > [1,2,1] `shouldBe` True
59 it "Monad comprehension" $ do
60 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
61 ([(1,1), (1,2), (2,1), (2,2)] :: SerialT Identity (Int, Int))
63 it "Foldable (sum)" $ sum ([1..3] :: SerialT Identity Int)
64 `shouldBe` 6
66 it "Traversable (mapM)" $
67 mapM return ([1..10] :: SerialT Identity Int)
68 `shouldReturn` [1..10]
70 describe "OverloadedStrings for 'SerialT Identity' type" $ do
71 it "overloaded strings" $ do
72 ("hello" :: SerialT Identity Char) `shouldBe` S.fromList "hello"
73#endif
75 describe "OverloadedLists for List type" $ do
76 it "overloaded lists" $ do
77 ([1..3] :: List Int) `shouldBe` GHC.fromList [1..3]
78 GHC.toList ([1..3] :: List Int) `shouldBe` [1..3]
80 it "Construct empty list" $ (Nil :: List Int) `shouldBe` []
82 it "Construct a list" $ do
83 (Nil :: List Int) `shouldBe` []
84 ('x' `Cons` Nil) `shouldBe` ['x']
85 (1 `Cons` [2 :: Int]) `shouldBe` [1,2]
86 (1 `Cons` 2 `Cons` 3 `Cons` Nil :: List Int) `shouldBe` [1,2,3]
88 it "pattern matches" $ do
89 case [] of
90 Nil -> return ()
91 _ -> expectationFailure "not reached"
93 case ['x'] of
94 Cons 'x' Nil -> return ()
95 _ -> expectationFailure "not reached"
97 case [1..10 :: Int] of
98 Cons x xs -> do
99 x `shouldBe` 1
100 xs `shouldBe` [2..10]
101 _ -> expectationFailure "not reached"
103 case [1..10 :: Int] of
104 x `Cons` y `Cons` xs -> do
105 x `shouldBe` 1
106 y `shouldBe` 2
107 xs `shouldBe` [3..10]
108 _ -> expectationFailure "not reached"
110 it "Show instance" $ do
111 show ([1..3] :: List Int) `shouldBe`
112#ifdef USE_STREAMLY_LIST
113 "List {toSerial = fromList [1,2,3]}"
114#else
115 "[1,2,3]"
116#endif
118 it "Read instance" $ do
119 (read
120#ifdef USE_STREAMLY_LIST
121 "List {toSerial = fromList [1,2,3]}"
122#else
123 "[1,2,3]"
124#endif
125 :: List Int) `shouldBe` [1..3]
127 it "Eq instance" $ do
128 ([1,2,3] :: List Int) == [1,2,3] `shouldBe` True
130 it "Ord instance" $ do
131 ([1,2,3] :: List Int) > [1,2,1] `shouldBe` True
133 it "Monad comprehension" $ do
134 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
135 ([(1,1), (1,2), (2,1), (2,2)] :: List (Int, Int))
137 it "Foldable (sum)" $ sum ([1..3] :: List Int) `shouldBe` 6
139 it "Traversable (mapM)" $
140 mapM return ([1..10] :: List Int)
141 `shouldReturn` [1..10]
143 describe "OverloadedStrings for List type" $ do
144 it "overloaded strings" $ do
145 ("hello" :: List Char) `shouldBe` GHC.fromList "hello"
147 it "pattern matches" $ do
148 case "" of
149 Nil -> return ()
150 _ -> expectationFailure "not reached"
152 case "a" of
153 Cons x Nil -> x `shouldBe` 'a'
154 _ -> expectationFailure "not reached"
156 case "hello" <> "world" of
157 Cons x1 (Cons x2 xs) -> do
158 x1 `shouldBe` 'h'
159 x2 `shouldBe` 'e'
160 xs `shouldBe` "lloworld"
161 _ -> expectationFailure "not reached"
163#ifdef USE_STREAMLY_LIST
164 describe "OverloadedLists for ZipList type" $ do
165 it "overloaded lists" $ do
166 ([1..3] :: ZipList Int) `shouldBe` GHC.fromList [1..3]
167 GHC.toList ([1..3] :: ZipList Int) `shouldBe` [1..3]
169 it "toZipList" $ do
170 toZipList (Nil :: List Int) `shouldBe` []
171 toZipList ('x' `Cons` Nil) `shouldBe` ['x']
172 toZipList (1 `Cons` [2 :: Int]) `shouldBe` [1,2]
173 toZipList (1 `Cons` 2 `Cons` 3 `Cons` Nil :: List Int) `shouldBe` [1,2,3]
175 it "fromZipList" $ do
176 case fromZipList [] of
177 Nil -> return ()
178 _ -> expectationFailure "not reached"
180 case fromZipList ['x'] of
181 Cons 'x' Nil -> return ()
182 _ -> expectationFailure "not reached"
184 case fromZipList [1..10 :: Int] of
185 Cons x xs -> do
186 x `shouldBe` 1
187 xs `shouldBe` [2..10]
188 _ -> expectationFailure "not reached"
190 case fromZipList [1..10 :: Int] of
191 x `Cons` y `Cons` xs -> do
192 x `shouldBe` 1
193 y `shouldBe` 2
194 xs `shouldBe` [3..10]
195 _ -> expectationFailure "not reached"
197 it "Show instance" $ do
198 show ([1..3] :: ZipList Int) `shouldBe`
199 "ZipList {toZipSerial = fromList [1,2,3]}"
201 it "Read instance" $ do
202 (read "ZipList {toZipSerial = fromList [1,2,3]}" :: ZipList Int)
203 `shouldBe` [1..3]
205 it "Eq instance" $ do
206 ([1,2,3] :: ZipList Int) == [1,2,3] `shouldBe` True
208 it "Ord instance" $ do
209 ([1,2,3] :: ZipList Int) > [1,2,1] `shouldBe` True
211 it "Foldable (sum)" $ sum ([1..3] :: ZipList Int) `shouldBe` 6
213 it "Traversable (mapM)" $
214 mapM return ([1..10] :: ZipList Int)
215 `shouldReturn` [1..10]
217 it "Applicative Zip" $ do
218 (,) <$> "abc" <*> [1..3] `shouldBe`
219 ([('a',1),('b',2),('c',3)] :: ZipList (Char, Int))
220#endif