on ea2d85b help sign in
120 lines 4.5 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.Word(Word8)
15import qualified Streamly.Internal.Data.Fold as Fold
16import qualified Streamly.Internal.Data.Array.Foreign as A
17import qualified Streamly.Internal.Data.Array.Foreign.Type as A
18import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
19import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
20type Array = A.Array
22moduleName :: String
23moduleName = "Data.Array.Foreign"
25#include "Streamly/Test/Data/Array/Common.hs"
27testFromStreamToStream :: Property
28testFromStreamToStream = genericTestFromTo (const A.fromStream) A.toStream (==)
30testFoldUnfold :: Property
31testFoldUnfold = genericTestFromTo (const (S.fold A.write)) (S.unfold A.read) (==)
33testFromList :: Property
34testFromList =
35 forAll (choose (0, maxArrLen)) $ \len ->
36 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
37 monadicIO $ do
38 let arr = A.fromList list
39 xs <- run $ S.toList $ (S.unfold A.read) arr
40 assert (xs == list)
42testLengthFromStream :: Property
43testLengthFromStream = genericTestFrom (const A.fromStream)
46unsafeWriteIndex :: [Int] -> Int -> Int -> IO Bool
47unsafeWriteIndex xs i x = do
48 arr <- MA.fromList xs
49 MA.putIndexUnsafe i x arr
50 x1 <- MA.getIndexUnsafe i arr
51 return $ x1 == x
53lastN :: Int -> [a] -> [a]
54lastN n l = drop (length l - n) l
56testLastN :: Property
57testLastN =
58 forAll (choose (0, maxArrLen)) $ \len ->
59 forAll (choose (0, len)) $ \n ->
60 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
61 monadicIO $ do
62 xs <- run
63 $ fmap A.toList
64 $ S.fold (A.writeLastN n)
65 $ S.fromList list
66 assert (xs == lastN n list)
68testLastN_LN :: Int -> Int -> IO Bool
69testLastN_LN len n = do
70 let list = [1..len]
71 l1 <- fmap A.toList $ S.fold (A.writeLastN n) $ S.fromList list
72 let l2 = lastN n list
73 return $ l1 == l2
75-- Instead of hard coding 10000 here we can have maxStreamLength for operations
76-- that use stream of arrays.
77concatArrayW8 :: Property
78concatArrayW8 =
79 forAll (vectorOf 10000 (arbitrary :: Gen Word8))
80 $ \w8List -> do
81 let w8ArrList = A.fromList . (: []) <$> w8List
82 f2 <- S.toList $ AS.concat $ S.fromList w8ArrList
83 w8List `shouldBe` f2
85unsafeSlice :: Int -> Int -> [Int] -> Bool
86unsafeSlice i n list =
87 let lst = take n $ drop i $ list
88 arr = A.toList $ A.getSliceUnsafe i n $ A.fromList list
89 in arr == lst
91main :: IO ()
92main =
93 hspec $
94 H.parallel $
95 modifyMaxSuccess (const maxTestCount) $ do
96 describe moduleName $ do
97 commonMain
98 describe "Construction" $ do
99 prop "length . fromStream === n" testLengthFromStream
100 prop "toStream . fromStream === id" testFromStreamToStream
101 prop "read . write === id" testFoldUnfold
102 prop "fromList" testFromList
103 prop "foldMany with writeNUnsafe concats to original"
104 (foldManyWith (\n -> Fold.take n (A.writeNUnsafe n)))
105 prop "AS.concat . (A.fromList . (:[]) <$>) === id" $ concatArrayW8
106 describe "unsafeSlice" $ do
107 it "partial" $ unsafeSlice 2 4 [1..10]
108 it "none" $ unsafeSlice 10 0 [1..10]
109 it "full" $ unsafeSlice 0 10 [1..10]
110 describe "Mut.unsafeWriteIndex" $ do
111 it "first" (unsafeWriteIndex [1..10] 0 0 `shouldReturn` True)
112 it "middle" (unsafeWriteIndex [1..10] 5 0 `shouldReturn` True)
113 it "last" (unsafeWriteIndex [1..10] 9 0 `shouldReturn` True)
114 describe "Fold" $ do
115 prop "writeLastN : 0 <= n <= len" $ testLastN
116 describe "writeLastN boundary conditions" $ do
117 it "writeLastN -1" (testLastN_LN 10 (-1) `shouldReturn` True)
118 it "writeLastN 0" (testLastN_LN 10 0 `shouldReturn` True)
119 it "writeLastN length" (testLastN_LN 10 10 `shouldReturn` True)
120 it "writeLastN (length + 1)" (testLastN_LN 10 11 `shouldReturn` True)