@@ -1,3 +1,4 @@
1{-# LANGUAGE FlexibleInstances #-}
2{-# LANGUAGE FlexibleInstances #-}
2{-# LANGUAGE MonadComprehensions #-}
3{-# LANGUAGE MonadComprehensions #-}
3{-# LANGUAGE TypeFamilies #-}
4{-# LANGUAGE TypeFamilies #-}
@@ -12,16 +13,33 @@ module Main (main) where
12import Test.Hspec
13import Test.Hspec
13import qualified GHC.Exts as GHC
14import qualified GHC.Exts as GHC
15−import Data.Functor.Identity
16import Streamly
16import Streamly
18+#ifdef USE_STREAMLY_LIST
19+import Data.Functor.Identity
17−import Streamly.List (pattern Cons, pattern Nil)
20+import Streamly.List (List(..), pattern Cons, pattern Nil, ZipList(..),
21+ fromZipList, toZipList)
18import qualified Streamly.Prelude as S
22import qualified Streamly.Prelude as S
19−import qualified Streamly.List as S
23+#else
24+import Prelude -- to suppress compiler warning
29+pattern Nil <- [] where Nil = []
31+pattern Cons :: a -> [a] -> [a]
32+pattern Cons x xs = x : xs
35+{-# COMPLETE Nil, Cons #-}
21main :: IO ()
38main :: IO ()
22main = hspec $ do
39main = hspec $ do
40+#ifdef USE_STREAMLY_LIST
23 describe "OverloadedLists for 'SerialT Identity' type" $ do
41 describe "OverloadedLists for 'SerialT Identity' type" $ do
24− it "overloaded lists" $ do
42+ it "Overloaded lists" $ do
25 ([1..3] :: SerialT Identity Int) `shouldBe` S.fromList [1..3]
43 ([1..3] :: SerialT Identity Int) `shouldBe` S.fromList [1..3]
26 GHC.toList ([1..3] :: SerialT Identity Int) `shouldBe` [1..3]
44 GHC.toList ([1..3] :: SerialT Identity Int) `shouldBe` [1..3]
@@ -41,60 +59,149 @@ main = hspec $ do
41 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
59 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
42 ([(1,1), (1,2), (2,1), (2,2)] :: SerialT Identity (Int, Int))
60 ([(1,1), (1,2), (2,1), (2,2)] :: SerialT Identity (Int, Int))
62+ it "Foldable (sum)" $ sum (S.fromList [1..3] :: SerialT Identity Int)
65+ describe "OverloadedStrings for 'SerialT Identity' type" $ do
66+ it "overloaded strings" $ do
67+ ("hello" :: SerialT Identity Char) `shouldBe` S.fromList "hello"
44 describe "OverloadedLists for List type" $ do
70 describe "OverloadedLists for List type" $ do
45 it "overloaded lists" $ do
71 it "overloaded lists" $ do
46− [1..3 :: Int] `shouldBe` S.fromSerial (S.fromList [1..3])
72+ ([1..3] :: List Int) `shouldBe` GHC.fromList [1..3]
47− GHC.toList ([1..3] :: S.List Int) `shouldBe` [1..3]
73+ GHC.toList ([1..3] :: List Int) `shouldBe` [1..3]
49− it "pattern constructor constructs a list" $ do
75+ it "Construct empty list" $ (Nil :: List Int) `shouldBe` []
77+ it "Construct a list" $ do
78+ (Nil :: List Int) `shouldBe` []
50 ('x' `Cons` Nil) `shouldBe` ['x']
79 ('x' `Cons` Nil) `shouldBe` ['x']
51 (1 `Cons` [2 :: Int]) `shouldBe` [1,2]
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
86+ _ -> expectationFailure "not reached"
89+ Cons 'x' Nil -> return ()
90+ _ -> expectationFailure "not reached"
53− it "pattern match on non-empty list" $
54 case [1..10 :: Int] of
92 case [1..10 :: Int] of
55 Cons x xs -> do
93 Cons x xs -> do
56 x `shouldBe` 1
94 x `shouldBe` 1
57 xs `shouldBe` [2..10]
95 xs `shouldBe` [2..10]
58 _ -> expectationFailure "not reached"
96 _ -> expectationFailure "not reached"
98+ case [1..10 :: Int] of
99+ x `Cons` y `Cons` xs -> do
102+ xs `shouldBe` [3..10]
103+ _ -> expectationFailure "not reached"
60 it "Show instance" $ do
105 it "Show instance" $ do
61− show ([1..3] :: S.List Int) `shouldBe` "fromList [1,2,3]"
106+ show ([1..3] :: List Int) `shouldBe`
107+#ifdef USE_STREAMLY_LIST
108+ "List {toSerial = fromList [1,2,3]}"
63 it "Read instance" $ do
113 it "Read instance" $ do
115+#ifdef USE_STREAMLY_LIST
116+ "List {toSerial = fromList [1,2,3]}"
64− (read "fromList [1,2,3]" :: S.List Int) `shouldBe` [1..3]
120+ :: List Int) `shouldBe` [1..3]
66 it "Eq instance" $ do
122 it "Eq instance" $ do
67− ([1,2,3] :: S.List Int) == [1,2,3] `shouldBe` True
123+ ([1,2,3] :: List Int) == [1,2,3] `shouldBe` True
69 it "Ord instance" $ do
125 it "Ord instance" $ do
70− ([1,2,3] :: S.List Int) > [1,2,1] `shouldBe` True
126+ ([1,2,3] :: List Int) > [1,2,1] `shouldBe` True
72 it "Monad comprehension" $ do
128 it "Monad comprehension" $ do
73 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
129 [(x,y) | x <- [1..2], y <- [1..2]] `shouldBe`
74− ([(1,1), (1,2), (2,1), (2,2)] :: S.List (Int, Int))
130+ ([(1,1), (1,2), (2,1), (2,2)] :: List (Int, Int))
76− describe "OverloadedStrings for 'SerialT Identity' type" $ do
132+ it "Foldable (sum)" $ sum ([1..3] :: List Int) `shouldBe` 6
77− it "overloaded strings" $ do
78− ("hello" :: SerialT Identity Char) `shouldBe` S.fromList "hello"
80 describe "OverloadedStrings for List type" $ do
134 describe "OverloadedStrings for List type" $ do
81 it "overloaded strings" $ do
135 it "overloaded strings" $ do
82− "hello" `shouldBe` S.fromSerial (S.fromList "hello")
136+ ("hello" :: List Char) `shouldBe` GHC.fromList "hello"
84− it "pattern match on empty string" $
138+ it "pattern matches" $ do
85 case "" of
139 case "" of
86 Nil -> return ()
140 Nil -> return ()
87 _ -> expectationFailure "not reached"
141 _ -> expectationFailure "not reached"
89− it "pattern matches on singleton string" $
90 case "a" of
143 case "a" of
91 Cons x Nil -> x `shouldBe` 'a'
144 Cons x Nil -> x `shouldBe` 'a'
92 _ -> expectationFailure "not reached"
145 _ -> expectationFailure "not reached"
94− it "pattern matches on non-empty string" $
95 case "hello" <> "world" of
147 case "hello" <> "world" of
96 Cons x1 (Cons x2 xs) -> do
148 Cons x1 (Cons x2 xs) -> do
97 x1 `shouldBe` 'h'
149 x1 `shouldBe` 'h'
98 x2 `shouldBe` 'e'
150 x2 `shouldBe` 'e'
99 xs `shouldBe` "lloworld"
151 xs `shouldBe` "lloworld"
100 _ -> expectationFailure "not reached"
152 _ -> expectationFailure "not reached"
154+#ifdef USE_STREAMLY_LIST
155+ describe "OverloadedLists for ZipList type" $ do
156+ it "overloaded lists" $ do
157+ ([1..3] :: ZipList Int) `shouldBe` GHC.fromList [1..3]
158+ GHC.toList ([1..3] :: ZipList Int) `shouldBe` [1..3]
161+ toZipList (Nil :: List Int) `shouldBe` []
162+ toZipList ('x' `Cons` Nil) `shouldBe` ['x']
163+ toZipList (1 `Cons` [2 :: Int]) `shouldBe` [1,2]
164+ toZipList (1 `Cons` 2 `Cons` 3 `Cons` Nil :: List Int) `shouldBe` [1,2,3]
166+ it "fromZipList" $ do
167+ case fromZipList [] of
169+ _ -> expectationFailure "not reached"
171+ case fromZipList ['x'] of
172+ Cons 'x' Nil -> return ()
173+ _ -> expectationFailure "not reached"
175+ case fromZipList [1..10 :: Int] of
178+ xs `shouldBe` [2..10]
179+ _ -> expectationFailure "not reached"
181+ case fromZipList [1..10 :: Int] of
182+ x `Cons` y `Cons` xs -> do
185+ xs `shouldBe` [3..10]
186+ _ -> expectationFailure "not reached"
188+ it "Show instance" $ do
189+ show ([1..3] :: ZipList Int) `shouldBe`
190+ "ZipList {toZipSerial = fromList [1,2,3]}"
192+ it "Read instance" $ do
193+ (read "ZipList {toZipSerial = fromList [1,2,3]}" :: ZipList Int)
196+ it "Eq instance" $ do
197+ ([1,2,3] :: ZipList Int) == [1,2,3] `shouldBe` True
199+ it "Ord instance" $ do
200+ ([1,2,3] :: ZipList Int) > [1,2,1] `shouldBe` True
202+ it "Foldable (sum)" $ sum ([1..3] :: ZipList Int) `shouldBe` 6
204+ it "Applicative Zip" $ do
205+ (,) <$> "abc" <*> [1..3] `shouldBe`
206+ ([('a',1),('b',2),('c',3)] :: ZipList (Char, Int))