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
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
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
36 forAll (choose (0, maxArrLen)) $ \len ->
37 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
39 let arr = A.fromList list
40 xs <- run $ S.toList $ (S.unfold A.read) arr
43testLengthFromStream :: Property
44testLengthFromStream = genericTestFrom (const A.fromStream)
47unsafeWriteIndex :: [Int] -> Int -> Int -> IO Bool
48unsafeWriteIndex xs i x = do
50 MA.putIndexUnsafe i x arr
51 x1 <- MA.getIndexUnsafe i arr
54lastN :: Int -> [a] -> [a]
55lastN n l = drop (length l - n) l
59 forAll (choose (0, maxArrLen)) $ \len ->
60 forAll (choose (0, len)) $ \n ->
61 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
65 $ S.fold (A.writeLastN n)
67 assert (xs == lastN n list)
69testLastN_LN :: Int -> Int -> IO Bool
70testLastN_LN len n = do
72 l1 <- fmap A.toList $ S.fold (A.writeLastN n) $ S.fromList list
78 dt <- MA.fromList "abcDEFgeh"
79 dt' <- MA.strip isLower dt
83testStripLeft :: IO Bool
85 dt <- MA.fromList "abcDEF"
86 dt' <- MA.strip isLower dt
90testStripRight :: IO Bool
92 dt <- MA.fromList "DEFgeh"
93 dt' <- MA.strip isLower dt
97testStripZero :: IO Bool
99 dt <- MA.fromList "DEF"
100 dt' <- MA.strip isLower dt
104testStripEmpty :: IO Bool
106 dt <- MA.fromList "abc"
107 dt' <- MA.strip isLower dt
111testStripNull :: IO Bool
114 dt' <- MA.strip isLower dt
118-- Instead of hard coding 10000 here we can have maxStreamLength for operations
119-- that use stream of arrays.
120concatArrayW8 :: Property
122 forAll (vectorOf 10000 (arbitrary :: Gen Word8))
124 let w8ArrList = A.fromList . (: []) <$> w8List
125 f2 <- S.toList $ AS.concat $ S.fromList w8ArrList
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
138 modifyMaxSuccess (const maxTestCount) $ do
139 describe moduleName $ do
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)
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)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list