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.List (sort)
15import Data.Word(Word8)
16import Test.QuickCheck (chooseInt, listOf)
18import qualified Streamly.Internal.Data.Fold as Fold
19import qualified Streamly.Internal.Data.Array.Foreign as A
20import qualified Streamly.Internal.Data.Array.Foreign.Type as A
21import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
22import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
27moduleName = "Data.Array.Foreign"
29#include "Streamly/Test/Data/Array/Common.hs"
31testFromStreamToStream :: Property
32testFromStreamToStream = genericTestFromTo (const A.fromStream) A.toStream (==)
34testFoldUnfold :: Property
35testFoldUnfold = genericTestFromTo (const (S.fold A.write)) (S.unfold A.read) (==)
37testFromList :: Property
39 forAll (choose (0, maxArrLen)) $ \len ->
40 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
42 let arr = A.fromList list
43 xs <- run $ S.toList $ (S.unfold A.read) arr
46testLengthFromStream :: Property
47testLengthFromStream = genericTestFrom (const A.fromStream)
50unsafeWriteIndex :: [Int] -> Int -> Int -> IO Bool
51unsafeWriteIndex xs i x = do
53 MA.putIndexUnsafe i x arr
54 x1 <- MA.getIndexUnsafe i arr
57lastN :: Int -> [a] -> [a]
58lastN n l = drop (length l - n) l
62 forAll (choose (0, maxArrLen)) $ \len ->
63 forAll (choose (0, len)) $ \n ->
64 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
68 $ S.fold (A.writeLastN n)
70 assert (xs == lastN n list)
72testLastN_LN :: Int -> Int -> IO Bool
73testLastN_LN len n = do
75 l1 <- fmap A.toList $ S.fold (A.writeLastN n) $ S.fromList list
81 dt <- MA.fromList "abcDEFgeh"
82 dt' <- MA.strip isLower dt
86testStripLeft :: IO Bool
88 dt <- MA.fromList "abcDEF"
89 dt' <- MA.strip isLower dt
93testStripRight :: IO Bool
95 dt <- MA.fromList "DEFgeh"
96 dt' <- MA.strip isLower dt
100testStripZero :: IO Bool
102 dt <- MA.fromList "DEF"
103 dt' <- MA.strip isLower dt
107testStripEmpty :: IO Bool
109 dt <- MA.fromList "abc"
110 dt' <- MA.strip isLower dt
114testStripNull :: IO Bool
117 dt' <- MA.strip isLower dt
121-- Instead of hard coding 10000 here we can have maxStreamLength for operations
122-- that use stream of arrays.
123concatArrayW8 :: Property
125 forAll (vectorOf 10000 (arbitrary :: Gen Word8))
127 let w8ArrList = A.fromList . (: []) <$> w8List
128 f2 <- S.toList $ AS.concat $ S.fromList w8ArrList
131unsafeSlice :: Int -> Int -> [Int] -> Bool
132unsafeSlice i n list =
133 let lst = take n $ drop i $ list
134 arr = A.toList $ A.getSliceUnsafe i n $ A.fromList list
138testAppend :: Property
140 forAll (listOf (chooseInt (-50, 100))) $ \ls0 ->
141 monadicIO $ action ls0
147 (MA.append (MA.newArray 0))
148 (S.fromList (ls::[Int]))
153testBubbleWith :: Bool -> Property
155 forAll (listOf (chooseInt (-50, 100))) $ \ls0 ->
156 monadicIO $ action ls0
161 x <- S.fold (fldm ls) $ S.fromList ls
164 then assert (sort ls == lst)
165 else assert (sort ls == reverse lst)
172 then MA.bubble compare arr
173 else MA.bubble (flip compare) arr
176 (MA.newArray $ length ls)
178testBubbleAsc :: Property
179testBubbleAsc = testBubbleWith True
181testBubbleDesc :: Property
182testBubbleDesc = testBubbleWith False
188 modifyMaxSuccess (const maxTestCount) $ do
189 describe moduleName $ do
191 describe "Construction" $ do
192 -- XXX There is an issue https://github.com/composewell/streamly/issues/1577
193 --prop "testAppend" testAppend
194 prop "testBubbleAsc" testBubbleAsc
195 prop "testBubbleDesc" testBubbleDesc
196 prop "length . fromStream === n" testLengthFromStream
197 prop "toStream . fromStream === id" testFromStreamToStream
198 prop "read . write === id" testFoldUnfold
199 prop "fromList" testFromList
200 prop "foldMany with writeNUnsafe concats to original"
201 (foldManyWith (\n -> Fold.take n (A.writeNUnsafe n)))
202 prop "AS.concat . (A.fromList . (:[]) <$>) === id" $ concatArrayW8
203 describe "unsafeSlice" $ do
204 it "partial" $ unsafeSlice 2 4 [1..10]
205 it "none" $ unsafeSlice 10 0 [1..10]
206 it "full" $ unsafeSlice 0 10 [1..10]
207 describe "Mut.unsafeWriteIndex" $ do
208 it "first" (unsafeWriteIndex [1..10] 0 0 `shouldReturn` True)
209 it "middle" (unsafeWriteIndex [1..10] 5 0 `shouldReturn` True)
210 it "last" (unsafeWriteIndex [1..10] 9 0 `shouldReturn` True)
212 prop "writeLastN : 0 <= n <= len" $ testLastN
213 describe "writeLastN boundary conditions" $ do
214 it "writeLastN -1" (testLastN_LN 10 (-1) `shouldReturn` True)
215 it "writeLastN 0" (testLastN_LN 10 0 `shouldReturn` True)
216 it "writeLastN length" (testLastN_LN 10 10 `shouldReturn` True)
217 it "writeLastN (length + 1)" (testLastN_LN 10 11 `shouldReturn` True)
218 describe "Strip" $ do
219 it "strip" (testStrip `shouldReturn` True)
220 it "stripLeft" (testStripLeft `shouldReturn` True)
221 it "stripRight" (testStripRight `shouldReturn` True)
222 it "stripZero" (testStripZero `shouldReturn` True)
223 it "stripEmpty" (testStripEmpty `shouldReturn` True)
224 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