on 57fad21 help sign in
streamly/test/PureStreams.hs
223 lines 7.3 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE MonadComprehensions #-}
4{-# LANGUAGE TypeFamilies #-}
5{-# LANGUAGE OverloadedStrings #-}
6{-# LANGUAGE OverloadedLists #-}
7{-# LANGUAGE PatternSynonyms #-}
8{-# LANGUAGE ViewPatterns #-}
9{-# LANGUAGE ScopedTypeVariables #-}
11module Main (main) where
13import Test.Hspec
14import qualified GHC.Exts as GHC
16import Streamly
18#ifdef USE_STREAMLY_LIST
19import Data.Functor.Identity
20import Streamly.List (List(..), pattern Cons, pattern Nil, ZipList(..),
21 fromZipList, toZipList)
22import qualified Streamly.Prelude as S
23#else
24import Prelude -- to suppress compiler warning
26type List = []
28pattern Nil :: [a]
29pattern Nil <- [] where Nil = []
31pattern Cons :: a -> [a] -> [a]
32pattern Cons x xs = x : xs
33infixr 5 `Cons`
35{-# COMPLETE Nil, Cons #-}
36#endif
38main :: IO ()
39main = hspec $ do
40#ifdef USE_STREAMLY_LIST
41 describe "OverloadedLists for 'SerialT Identity' type" $ do
42 it "Overloaded lists" $ do
43 ([1..3] :: SerialT Identity Int) `shouldBe` S.fromList [1..3]
44 GHC.toList ([1..3] :: SerialT Identity Int) `shouldBe` [1..3]
46 it "Show instance" $ do
47 show (S.fromList [1..3] :: SerialT Identity Int)
48 `shouldBe` "fromList [1,2,3]"
49 it "Read instance" $ do
50 (read "fromList [1,2,3]" :: SerialT Identity Int) `shouldBe` [1..3]
52 it "Eq instance" $ do
53 ([1,2,3] :: SerialT Identity Int) == [1,2,3] `shouldBe` True
55 it "Ord instance" $ do
56 ([1,2,3] :: SerialT Identity Int) > [1,2,1] `shouldBe` True
58 it "Monad comprehension" $ do
59 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
60 ([(1,1), (1,2), (2,1), (2,2)] :: SerialT Identity (Int, Int))
62 it "Foldable (sum)" $ sum ([1..3] :: SerialT Identity Int)
63 `shouldBe` 6
65 it "Traversable (mapM)" $
66 mapM return ([1..10] :: SerialT Identity Int)
67 `shouldReturn` [1..10]
69 describe "OverloadedStrings for 'SerialT Identity' type" $ do
70 it "overloaded strings" $ do
71 ("hello" :: SerialT Identity Char) `shouldBe` S.fromList "hello"
72#endif
74 describe "OverloadedLists for List type" $ do
75 it "overloaded lists" $ do
76 ([1..3] :: List Int) `shouldBe` GHC.fromList [1..3]
77 GHC.toList ([1..3] :: List Int) `shouldBe` [1..3]
79 it "Construct empty list" $ (Nil :: List Int) `shouldBe` []
81 it "Construct a list" $ do
82 (Nil :: List Int) `shouldBe` []
83 ('x' `Cons` Nil) `shouldBe` ['x']
84 (1 `Cons` [2 :: Int]) `shouldBe` [1,2]
85 (1 `Cons` 2 `Cons` 3 `Cons` Nil :: List Int) `shouldBe` [1,2,3]
87 it "pattern matches" $ do
88 case [] of
89 Nil -> return ()
90 _ -> expectationFailure "not reached"
92 case ['x'] of
93 Cons 'x' Nil -> return ()
94 _ -> expectationFailure "not reached"
96 case [1..10 :: Int] of
97 Cons x xs -> do
98 x `shouldBe` 1
99 xs `shouldBe` [2..10]
100 _ -> expectationFailure "not reached"
102 case [1..10 :: Int] of
103 x `Cons` y `Cons` xs -> do
104 x `shouldBe` 1
105 y `shouldBe` 2
106 xs `shouldBe` [3..10]
107 _ -> expectationFailure "not reached"
109 it "Show instance" $ do
110 show ([1..3] :: List Int) `shouldBe`
111#ifdef USE_STREAMLY_LIST
112 "List {toSerial = fromList [1,2,3]}"
113#else
114 "[1,2,3]"
115#endif
117 it "Read instance" $ do
118 (read
119#ifdef USE_STREAMLY_LIST
120 "List {toSerial = fromList [1,2,3]}"
121#else
122 "[1,2,3]"
123#endif
124 :: List Int) `shouldBe` [1..3]
126 it "Eq instance" $ do
127 ([1,2,3] :: List Int) == [1,2,3] `shouldBe` True
129 it "Ord instance" $ do
130 ([1,2,3] :: List Int) > [1,2,1] `shouldBe` True
132 it "Monad comprehension" $ do
133 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
134 ([(1,1), (1,2), (2,1), (2,2)] :: List (Int, Int))
136 it "Foldable (sum)" $ sum ([1..3] :: List Int) `shouldBe` 6
138 it "Traversable (mapM)" $
139 mapM return ([1..10] :: List Int)
140 `shouldReturn` [1..10]
142 describe "OverloadedStrings for List type" $ do
143 it "overloaded strings" $ do
144 ("hello" :: List Char) `shouldBe` GHC.fromList "hello"
146 it "pattern matches" $ do
147#if __GLASGOW_HASKELL__ >= 802
148 case "" of
149#else
150 case "" :: List Char of
151#endif
152 Nil -> return ()
153 _ -> expectationFailure "not reached"
155 case "a" of
156 Cons x Nil -> x `shouldBe` 'a'
157 _ -> expectationFailure "not reached"
159 case "hello" <> "world" of
160 Cons x1 (Cons x2 xs) -> do
161 x1 `shouldBe` 'h'
162 x2 `shouldBe` 'e'
163 xs `shouldBe` "lloworld"
164 _ -> expectationFailure "not reached"
166#ifdef USE_STREAMLY_LIST
167 describe "OverloadedLists for ZipList type" $ do
168 it "overloaded lists" $ do
169 ([1..3] :: ZipList Int) `shouldBe` GHC.fromList [1..3]
170 GHC.toList ([1..3] :: ZipList Int) `shouldBe` [1..3]
172 it "toZipList" $ do
173 toZipList (Nil :: List Int) `shouldBe` []
174 toZipList ('x' `Cons` Nil) `shouldBe` ['x']
175 toZipList (1 `Cons` [2 :: Int]) `shouldBe` [1,2]
176 toZipList (1 `Cons` 2 `Cons` 3 `Cons` Nil :: List Int) `shouldBe` [1,2,3]
178 it "fromZipList" $ do
179 case fromZipList [] of
180 Nil -> return ()
181 _ -> expectationFailure "not reached"
183 case fromZipList ['x'] of
184 Cons 'x' Nil -> return ()
185 _ -> expectationFailure "not reached"
187 case fromZipList [1..10 :: Int] of
188 Cons x xs -> do
189 x `shouldBe` 1
190 xs `shouldBe` [2..10]
191 _ -> expectationFailure "not reached"
193 case fromZipList [1..10 :: Int] of
194 x `Cons` y `Cons` xs -> do
195 x `shouldBe` 1
196 y `shouldBe` 2
197 xs `shouldBe` [3..10]
198 _ -> expectationFailure "not reached"
200 it "Show instance" $ do
201 show ([1..3] :: ZipList Int) `shouldBe`
202 "ZipList {toZipSerial = fromList [1,2,3]}"
204 it "Read instance" $ do
205 (read "ZipList {toZipSerial = fromList [1,2,3]}" :: ZipList Int)
206 `shouldBe` [1..3]
208 it "Eq instance" $ do
209 ([1,2,3] :: ZipList Int) == [1,2,3] `shouldBe` True
211 it "Ord instance" $ do
212 ([1,2,3] :: ZipList Int) > [1,2,1] `shouldBe` True
214 it "Foldable (sum)" $ sum ([1..3] :: ZipList Int) `shouldBe` 6
216 it "Traversable (mapM)" $
217 mapM return ([1..10] :: ZipList Int)
218 `shouldReturn` [1..10]
220 it "Applicative Zip" $ do
221 (,) <$> "abc" <*> [1..3] `shouldBe`
222 ([('a',1),('b',2),('c',3)] :: ZipList (Char, Int))
223#endif