on e9828b3 help sign in
streamly/test/PureStreams.hs
217 lines 7.1 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
10import Streamly
12#ifdef USE_STREAMLY_LIST
13import Data.Functor.Identity
14import Streamly.Internal.Data.List (List(..), pattern Cons, pattern Nil, ZipList(..),
15 fromZipList, toZipList)
16import qualified Streamly.Prelude as S
17#else
18import Prelude -- to suppress compiler warning
20type List = []
22pattern Nil :: [a]
23pattern Nil <- [] where Nil = []
25pattern Cons :: a -> [a] -> [a]
26pattern Cons x xs = x : xs
27infixr 5 `Cons`
29{-# COMPLETE Nil, Cons #-}
30#endif
32main :: IO ()
33main = hspec $ do
34#ifdef USE_STREAMLY_LIST
35 describe "OverloadedLists for 'SerialT Identity' type" $ do
36 it "Overloaded lists" $ do
37 ([1..3] :: SerialT Identity Int) `shouldBe` S.fromList [1..3]
38 GHC.toList ([1..3] :: SerialT Identity Int) `shouldBe` [1..3]
40 it "Show instance" $ do
41 show (S.fromList [1..3] :: SerialT Identity Int)
42 `shouldBe` "fromList [1,2,3]"
43 it "Read instance" $ do
44 (read "fromList [1,2,3]" :: SerialT Identity Int) `shouldBe` [1..3]
46 it "Eq instance" $ do
47 ([1,2,3] :: SerialT Identity Int) == [1,2,3] `shouldBe` True
49 it "Ord instance" $ do
50 ([1,2,3] :: SerialT Identity Int) > [1,2,1] `shouldBe` True
52 it "Monad comprehension" $ do
53 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
54 ([(1,1), (1,2), (2,1), (2,2)] :: SerialT Identity (Int, Int))
56 it "Foldable (sum)" $ sum ([1..3] :: SerialT Identity Int)
57 `shouldBe` 6
59 it "Traversable (mapM)" $
60 mapM return ([1..10] :: SerialT Identity Int)
61 `shouldReturn` [1..10]
63 describe "OverloadedStrings for 'SerialT Identity' type" $ do
64 it "overloaded strings" $ do
65 ("hello" :: SerialT Identity Char) `shouldBe` S.fromList "hello"
66#endif
68 describe "OverloadedLists for List type" $ do
69 it "overloaded lists" $ do
70 ([1..3] :: List Int) `shouldBe` GHC.fromList [1..3]
71 GHC.toList ([1..3] :: List Int) `shouldBe` [1..3]
73 it "Construct empty list" $ (Nil :: List Int) `shouldBe` []
75 it "Construct a list" $ do
76 (Nil :: List Int) `shouldBe` []
77 ('x' `Cons` Nil) `shouldBe` ['x']
78 (1 `Cons` [2 :: Int]) `shouldBe` [1,2]
79 (1 `Cons` 2 `Cons` 3 `Cons` Nil :: List Int) `shouldBe` [1,2,3]
81 it "pattern matches" $ do
82 case [] of
83 Nil -> return ()
84 _ -> expectationFailure "not reached"
86 case ['x'] of
87 Cons 'x' Nil -> return ()
88 _ -> expectationFailure "not reached"
90 case [1..10 :: Int] of
91 Cons x xs -> do
92 x `shouldBe` 1
93 xs `shouldBe` [2..10]
94 _ -> expectationFailure "not reached"
96 case [1..10 :: Int] of
97 x `Cons` y `Cons` xs -> do
98 x `shouldBe` 1
99 y `shouldBe` 2
100 xs `shouldBe` [3..10]
101 _ -> expectationFailure "not reached"
103 it "Show instance" $ do
104 show ([1..3] :: List Int) `shouldBe`
105#ifdef USE_STREAMLY_LIST
106 "List {toSerial = fromList [1,2,3]}"
107#else
108 "[1,2,3]"
109#endif
111 it "Read instance" $ do
112 (read
113#ifdef USE_STREAMLY_LIST
114 "List {toSerial = fromList [1,2,3]}"
115#else
116 "[1,2,3]"
117#endif
118 :: List Int) `shouldBe` [1..3]
120 it "Eq instance" $ do
121 ([1,2,3] :: List Int) == [1,2,3] `shouldBe` True
123 it "Ord instance" $ do
124 ([1,2,3] :: List Int) > [1,2,1] `shouldBe` True
126 it "Monad comprehension" $ do
127 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
128 ([(1,1), (1,2), (2,1), (2,2)] :: List (Int, Int))
130 it "Foldable (sum)" $ sum ([1..3] :: List Int) `shouldBe` 6
132 it "Traversable (mapM)" $
133 mapM return ([1..10] :: List Int)
134 `shouldReturn` [1..10]
136 describe "OverloadedStrings for List type" $ do
137 it "overloaded strings" $ do
138 ("hello" :: List Char) `shouldBe` GHC.fromList "hello"
140 it "pattern matches" $ do
141#if __GLASGOW_HASKELL__ >= 802
142 case "" of
143#else
144 case "" :: List Char of
145#endif
146 Nil -> return ()
147 _ -> expectationFailure "not reached"
149 case "a" of
150 Cons x Nil -> x `shouldBe` 'a'
151 _ -> expectationFailure "not reached"
153 case "hello" <> "world" of
154 Cons x1 (Cons x2 xs) -> do
155 x1 `shouldBe` 'h'
156 x2 `shouldBe` 'e'
157 xs `shouldBe` "lloworld"
158 _ -> expectationFailure "not reached"
160#ifdef USE_STREAMLY_LIST
161 describe "OverloadedLists for ZipList type" $ do
162 it "overloaded lists" $ do
163 ([1..3] :: ZipList Int) `shouldBe` GHC.fromList [1..3]
164 GHC.toList ([1..3] :: ZipList Int) `shouldBe` [1..3]
166 it "toZipList" $ do
167 toZipList (Nil :: List Int) `shouldBe` []
168 toZipList ('x' `Cons` Nil) `shouldBe` ['x']
169 toZipList (1 `Cons` [2 :: Int]) `shouldBe` [1,2]
170 toZipList (1 `Cons` 2 `Cons` 3 `Cons` Nil :: List Int) `shouldBe` [1,2,3]
172 it "fromZipList" $ do
173 case fromZipList [] of
174 Nil -> return ()
175 _ -> expectationFailure "not reached"
177 case fromZipList ['x'] of
178 Cons 'x' Nil -> return ()
179 _ -> expectationFailure "not reached"
181 case fromZipList [1..10 :: Int] of
182 Cons x xs -> do
183 x `shouldBe` 1
184 xs `shouldBe` [2..10]
185 _ -> expectationFailure "not reached"
187 case fromZipList [1..10 :: Int] of
188 x `Cons` y `Cons` xs -> do
189 x `shouldBe` 1
190 y `shouldBe` 2
191 xs `shouldBe` [3..10]
192 _ -> expectationFailure "not reached"
194 it "Show instance" $ do
195 show ([1..3] :: ZipList Int) `shouldBe`
196 "ZipList {toZipSerial = fromList [1,2,3]}"
198 it "Read instance" $ do
199 (read "ZipList {toZipSerial = fromList [1,2,3]}" :: ZipList Int)
200 `shouldBe` [1..3]
202 it "Eq instance" $ do
203 ([1,2,3] :: ZipList Int) == [1,2,3] `shouldBe` True
205 it "Ord instance" $ do
206 ([1,2,3] :: ZipList Int) > [1,2,1] `shouldBe` True
208 it "Foldable (sum)" $ sum ([1..3] :: ZipList Int) `shouldBe` 6
210 it "Traversable (mapM)" $
211 mapM return ([1..10] :: ZipList Int)
212 `shouldReturn` [1..10]
214 it "Applicative Zip" $ do
215 (,) <$> "abc" <*> [1..3] `shouldBe`
216 ([('a',1),('b',2),('c',3)] :: ZipList (Char, Int))
217#endif