on 00a2f7b help sign in
219 lines 7.2 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
34main :: IO ()
35main = hspec $ do
36#ifdef USE_STREAMLY_LIST
37 describe "OverloadedLists for 'SerialT Identity' type" $ do
38 it "Overloaded lists" $ do
39 ([1..3] :: SerialT Identity Int) `shouldBe` S.fromList [1..3]
40 GHC.toList ([1..3] :: SerialT Identity Int) `shouldBe` [1..3]
42 it "Show instance" $ do
43 show (S.fromList [1..3] :: SerialT Identity Int)
44 `shouldBe` "fromList [1,2,3]"
45 it "Read instance" $ do
46 (read "fromList [1,2,3]" :: SerialT Identity Int) `shouldBe` [1..3]
48 it "Eq instance" $ do
49 ([1,2,3] :: SerialT Identity Int) == [1,2,3] `shouldBe` True
51 it "Ord instance" $ do
52 ([1,2,3] :: SerialT Identity Int) > [1,2,1] `shouldBe` True
54 it "Monad comprehension" $ do
55 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
56 ([(1,1), (1,2), (2,1), (2,2)] :: SerialT Identity (Int, Int))
58 it "Foldable (sum)" $ sum ([1..3] :: SerialT Identity Int)
59 `shouldBe` 6
61 it "Traversable (mapM)" $
62 mapM return ([1..10] :: SerialT Identity Int)
63 `shouldReturn` [1..10]
65 describe "OverloadedStrings for 'SerialT Identity' type" $ do
66 it "overloaded strings" $ do
67 ("hello" :: SerialT Identity Char) `shouldBe` S.fromList "hello"
68#endif
70 describe "OverloadedLists for List type" $ do
71 it "overloaded lists" $ do
72 ([1..3] :: List Int) `shouldBe` GHC.fromList [1..3]
73 GHC.toList ([1..3] :: List Int) `shouldBe` [1..3]
75 it "Construct empty list" $ (Nil :: List Int) `shouldBe` []
77 it "Construct a list" $ do
78 (Nil :: List Int) `shouldBe` []
79 ('x' `Cons` Nil) `shouldBe` ['x']
80 (1 `Cons` [2 :: Int]) `shouldBe` [1,2]
81 (1 `Cons` 2 `Cons` 3 `Cons` Nil :: List Int) `shouldBe` [1,2,3]
83 it "pattern matches" $ do
84 case [] of
85 Nil -> return ()
86 _ -> expectationFailure "not reached"
88 case ['x'] of
89 Cons 'x' Nil -> return ()
90 _ -> expectationFailure "not reached"
92 case [1..10 :: Int] of
93 Cons x xs -> do
94 x `shouldBe` 1
95 xs `shouldBe` [2..10]
96 _ -> expectationFailure "not reached"
98 case [1..10 :: Int] of
99 x `Cons` y `Cons` xs -> do
100 x `shouldBe` 1
101 y `shouldBe` 2
102 xs `shouldBe` [3..10]
103 _ -> expectationFailure "not reached"
105 it "Show instance" $ do
106 show ([1..3] :: List Int) `shouldBe`
107#ifdef USE_STREAMLY_LIST
108 "List {toSerial = fromList [1,2,3]}"
109#else
110 "[1,2,3]"
111#endif
113 it "Read instance" $ do
114 (read
115#ifdef USE_STREAMLY_LIST
116 "List {toSerial = fromList [1,2,3]}"
117#else
118 "[1,2,3]"
119#endif
120 :: List Int) `shouldBe` [1..3]
122 it "Eq instance" $ do
123 ([1,2,3] :: List Int) == [1,2,3] `shouldBe` True
125 it "Ord instance" $ do
126 ([1,2,3] :: List Int) > [1,2,1] `shouldBe` True
128 it "Monad comprehension" $ do
129 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
130 ([(1,1), (1,2), (2,1), (2,2)] :: List (Int, Int))
132 it "Foldable (sum)" $ sum ([1..3] :: List Int) `shouldBe` 6
134 it "Traversable (mapM)" $
135 mapM return ([1..10] :: List Int)
136 `shouldReturn` [1..10]
138 describe "OverloadedStrings for List type" $ do
139 it "overloaded strings" $ do
140 ("hello" :: List Char) `shouldBe` GHC.fromList "hello"
142 it "pattern matches" $ do
143#if __GLASGOW_HASKELL__ >= 802
144 case "" of
145#else
146 case "" :: List Char of
147#endif
148 Nil -> return ()
149 _ -> expectationFailure "not reached"
151 case "a" of
152 Cons x Nil -> x `shouldBe` 'a'
153 _ -> expectationFailure "not reached"
155 case "hello" <> "world" of
156 Cons x1 (Cons x2 xs) -> do
157 x1 `shouldBe` 'h'
158 x2 `shouldBe` 'e'
159 xs `shouldBe` "lloworld"
160 _ -> expectationFailure "not reached"
162#ifdef USE_STREAMLY_LIST
163 describe "OverloadedLists for ZipList type" $ do
164 it "overloaded lists" $ do
165 ([1..3] :: ZipList Int) `shouldBe` GHC.fromList [1..3]
166 GHC.toList ([1..3] :: ZipList Int) `shouldBe` [1..3]
168 it "toZipList" $ do
169 toZipList (Nil :: List Int) `shouldBe` []
170 toZipList ('x' `Cons` Nil) `shouldBe` ['x']
171 toZipList (1 `Cons` [2 :: Int]) `shouldBe` [1,2]
172 toZipList (1 `Cons` 2 `Cons` 3 `Cons` Nil :: List Int) `shouldBe` [1,2,3]
174 it "fromZipList" $ do
175 case fromZipList [] of
176 Nil -> return ()
177 _ -> expectationFailure "not reached"
179 case fromZipList ['x'] of
180 Cons 'x' Nil -> return ()
181 _ -> expectationFailure "not reached"
183 case fromZipList [1..10 :: Int] of
184 Cons x xs -> do
185 x `shouldBe` 1
186 xs `shouldBe` [2..10]
187 _ -> expectationFailure "not reached"
189 case fromZipList [1..10 :: Int] of
190 x `Cons` y `Cons` xs -> do
191 x `shouldBe` 1
192 y `shouldBe` 2
193 xs `shouldBe` [3..10]
194 _ -> expectationFailure "not reached"
196 it "Show instance" $ do
197 show ([1..3] :: ZipList Int) `shouldBe`
198 "ZipList {toZipSerial = fromList [1,2,3]}"
200 it "Read instance" $ do
201 (read "ZipList {toZipSerial = fromList [1,2,3]}" :: ZipList Int)
202 `shouldBe` [1..3]
204 it "Eq instance" $ do
205 ([1,2,3] :: ZipList Int) == [1,2,3] `shouldBe` True
207 it "Ord instance" $ do
208 ([1,2,3] :: ZipList Int) > [1,2,1] `shouldBe` True
210 it "Foldable (sum)" $ sum ([1..3] :: ZipList Int) `shouldBe` 6
212 it "Traversable (mapM)" $
213 mapM return ([1..10] :: ZipList Int)
214 `shouldReturn` [1..10]
216 it "Applicative Zip" $ do
217 (,) <$> "abc" <*> [1..3] `shouldBe`
218 ([('a',1),('b',2),('c',3)] :: ZipList (Char, Int))
219#endif