on 6726613 help sign in
170 lines 5.8 kB history raw
1-- |
2-- Module : Streamly.Test.Data.Array.Foreign
3-- Copyright : (c) 2019 Composewell technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
7-- Portability : GHC
9module Streamly.Test.Data.Array.Foreign (main) where
11#include "Streamly/Test/Data/Array/CommonImports.hs"
13import Data.Char (isLower)
14import Data.Word(Word8)
16import qualified Streamly.Internal.Data.Fold as Fold
17import qualified Streamly.Internal.Data.Array.Foreign as A
18import qualified Streamly.Internal.Data.Array.Foreign.Type as A
19import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
20import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
21type Array = A.Array
23moduleName :: String
24moduleName = "Data.Array.Foreign"
26#include "Streamly/Test/Data/Array/Common.hs"
28testFromStreamToStream :: Property
29testFromStreamToStream = genericTestFromTo (const A.fromStream) A.toStream (==)
31testFoldUnfold :: Property
32testFoldUnfold = genericTestFromTo (const (S.fold A.write)) (S.unfold A.read) (==)
34testFromList :: Property
35testFromList =
36 forAll (choose (0, maxArrLen)) $ \len ->
37 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
38 monadicIO $ do
39 let arr = A.fromList list
40 xs <- run $ S.toList $ (S.unfold A.read) arr
41 assert (xs == list)
43testLengthFromStream :: Property
44testLengthFromStream = genericTestFrom (const A.fromStream)
47unsafeWriteIndex :: [Int] -> Int -> Int -> IO Bool
48unsafeWriteIndex xs i x = do
49 arr <- MA.fromList xs
50 MA.putIndexUnsafe i x arr
51 x1 <- MA.getIndexUnsafe i arr
52 return $ x1 == x
54lastN :: Int -> [a] -> [a]
55lastN n l = drop (length l - n) l
57testLastN :: Property
58testLastN =
59 forAll (choose (0, maxArrLen)) $ \len ->
60 forAll (choose (0, len)) $ \n ->
61 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
62 monadicIO $ do
63 xs <- run
64 $ fmap A.toList
65 $ S.fold (A.writeLastN n)
66 $ S.fromList list
67 assert (xs == lastN n list)
69testLastN_LN :: Int -> Int -> IO Bool
70testLastN_LN len n = do
71 let list = [1..len]
72 l1 <- fmap A.toList $ S.fold (A.writeLastN n) $ S.fromList list
73 let l2 = lastN n list
74 return $ l1 == l2
76testStrip :: IO Bool
77testStrip = do
78 dt <- MA.fromList "abcDEFgeh"
79 dt' <- MA.strip isLower dt
80 x <- MA.toList dt'
81 return $ x == "DEF"
83testStripLeft :: IO Bool
84testStripLeft = do
85 dt <- MA.fromList "abcDEF"
86 dt' <- MA.strip isLower dt
87 x <- MA.toList dt'
88 return $ x == "DEF"
90testStripRight :: IO Bool
91testStripRight = do
92 dt <- MA.fromList "DEFgeh"
93 dt' <- MA.strip isLower dt
94 x <- MA.toList dt'
95 return $ x == "DEF"
97testStripZero :: IO Bool
98testStripZero = do
99 dt <- MA.fromList "DEF"
100 dt' <- MA.strip isLower dt
101 x <- MA.toList dt'
102 return $ x == "DEF"
104testStripEmpty :: IO Bool
105testStripEmpty = do
106 dt <- MA.fromList "abc"
107 dt' <- MA.strip isLower dt
108 x <- MA.toList dt'
109 return $ x == ""
111testStripNull :: IO Bool
112testStripNull = do
113 dt <- MA.fromList ""
114 dt' <- MA.strip isLower dt
115 x <- MA.toList dt'
116 return $ x == ""
118-- Instead of hard coding 10000 here we can have maxStreamLength for operations
119-- that use stream of arrays.
120concatArrayW8 :: Property
121concatArrayW8 =
122 forAll (vectorOf 10000 (arbitrary :: Gen Word8))
123 $ \w8List -> do
124 let w8ArrList = A.fromList . (: []) <$> w8List
125 f2 <- S.toList $ AS.concat $ S.fromList w8ArrList
126 w8List `shouldBe` f2
128unsafeSlice :: Int -> Int -> [Int] -> Bool
129unsafeSlice i n list =
130 let lst = take n $ drop i $ list
131 arr = A.toList $ A.getSliceUnsafe i n $ A.fromList list
132 in arr == lst
134main :: IO ()
135main =
136 hspec $
137 H.parallel $
138 modifyMaxSuccess (const maxTestCount) $ do
139 describe moduleName $ do
140 commonMain
141 describe "Construction" $ do
142 prop "length . fromStream === n" testLengthFromStream
143 prop "toStream . fromStream === id" testFromStreamToStream
144 prop "read . write === id" testFoldUnfold
145 prop "fromList" testFromList
146 prop "foldMany with writeNUnsafe concats to original"
147 (foldManyWith (\n -> Fold.take n (A.writeNUnsafe n)))
148 prop "AS.concat . (A.fromList . (:[]) <$>) === id" $ concatArrayW8
149 describe "unsafeSlice" $ do
150 it "partial" $ unsafeSlice 2 4 [1..10]
151 it "none" $ unsafeSlice 10 0 [1..10]
152 it "full" $ unsafeSlice 0 10 [1..10]
153 describe "Mut.unsafeWriteIndex" $ do
154 it "first" (unsafeWriteIndex [1..10] 0 0 `shouldReturn` True)
155 it "middle" (unsafeWriteIndex [1..10] 5 0 `shouldReturn` True)
156 it "last" (unsafeWriteIndex [1..10] 9 0 `shouldReturn` True)
157 describe "Fold" $ do
158 prop "writeLastN : 0 <= n <= len" $ testLastN
159 describe "writeLastN boundary conditions" $ do
160 it "writeLastN -1" (testLastN_LN 10 (-1) `shouldReturn` True)
161 it "writeLastN 0" (testLastN_LN 10 0 `shouldReturn` True)
162 it "writeLastN length" (testLastN_LN 10 10 `shouldReturn` True)
163 it "writeLastN (length + 1)" (testLastN_LN 10 11 `shouldReturn` True)
164 describe "Strip" $ do
165 it "strip" (testStrip `shouldReturn` True)
166 it "stripLeft" (testStripLeft `shouldReturn` True)
167 it "stripRight" (testStripRight `shouldReturn` True)
168 it "stripZero" (testStripZero `shouldReturn` True)
169 it "stripEmpty" (testStripEmpty `shouldReturn` True)
170 it "stripNull" (testStripNull `shouldReturn` True)