2-- Module : Streamly.Test.Data.Array.Unboxed
3-- Copyright : (c) 2019 Composewell technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
9module Streamly.Test.Data.Array.Unboxed (main) where
11import Data.Char (isLower)
12import Data.List (sort)
13import Data.Word(Word8)
14import Foreign.Storable (peek)
15import GHC.Ptr (plusPtr)
16import Streamly.Data.Fold (Fold)
17import Streamly.Internal.Data.Stream (Stream)
18import Streamly.Internal.Data.Unboxed (Unboxed, sizeOf)
19import Streamly.Test.Common (listEquals)
21import qualified Streamly.Data.Fold as Fold
22import qualified Streamly.Internal.Data.Array.Unboxed as A
23import qualified Streamly.Internal.Data.Array.Unboxed.Type as A
24import qualified Streamly.Internal.Data.Array.Unboxed.Mut.Type as MA
25import qualified Streamly.Internal.Data.Stream as S
28import Test.Hspec.QuickCheck
29import Test.QuickCheck (Property, chooseInt, listOf
30 , forAll, Gen, vectorOf, arbitrary, choose)
31import Test.QuickCheck.Monadic (monadicIO, assert, run)
36moduleName = "Data.Array.Unboxed"
38#include "Streamly/Test/Data/Array/Common.hs"
40testFromStreamToStream :: Property
41testFromStreamToStream = genericTestFromTo (const A.fromStream) A.toStream (==)
43testFoldUnfold :: Property
44testFoldUnfold = genericTestFromTo (const (S.fold A.write)) (S.unfold A.read) (==)
46testFromList :: Property
48 forAll (choose (0, maxArrLen)) $ \len ->
49 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
51 let arr = A.fromList list
52 xs <- run $ S.fold Fold.toList $ S.unfold A.read arr
55testLengthFromStream :: Property
56testLengthFromStream = genericTestFrom (const A.fromStream)
58unsafeWriteIndex :: [Int] -> Int -> Int -> IO Bool
59unsafeWriteIndex xs i x = do
61 MA.putIndexUnsafe i x arr
62 x1 <- MA.getIndexUnsafe i arr
65lastN :: Int -> [a] -> [a]
66lastN n l = drop (length l - n) l
70 forAll (choose (0, maxArrLen)) $ \len ->
71 forAll (choose (0, len)) $ \n ->
72 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
76 $ S.fold (A.writeLastN n)
78 assert (xs == lastN n list)
80testLastN_LN :: Int -> Int -> IO Bool
81testLastN_LN len n = do
83 l1 <- fmap A.toList $ S.fold (A.writeLastN n) $ S.fromList list
89 dt <- MA.fromList "abcDEFgeh"
90 dt' <- MA.strip isLower dt
94testStripLeft :: IO Bool
96 dt <- MA.fromList "abcDEF"
97 dt' <- MA.strip isLower dt
101testStripRight :: IO Bool
103 dt <- MA.fromList "DEFgeh"
104 dt' <- MA.strip isLower dt
108testStripZero :: IO Bool
110 dt <- MA.fromList "DEF"
111 dt' <- MA.strip isLower dt
115testStripEmpty :: IO Bool
117 dt <- MA.fromList "abc"
118 dt' <- MA.strip isLower dt
122testStripNull :: IO Bool
125 dt' <- MA.strip isLower dt
129unsafeSlice :: Int -> Int -> [Int] -> Bool
130unsafeSlice i n list =
131 let lst = take n $ drop i list
132 arr = A.toList $ A.getSliceUnsafe i n $ A.fromList list
135testBubbleWith :: Bool -> Property
137 forAll (listOf (chooseInt (-50, 100))) $ \ls0 ->
138 monadicIO $ action ls0
143 x <- S.fold (fldm ls) $ S.fromList ls
146 then assert (sort ls == lst)
147 else assert (sort ls == reverse lst)
154 then MA.bubble compare arr
155 else MA.bubble (flip compare) arr
158 (MA.newArray $ length ls)
160testBubbleAsc :: Property
161testBubbleAsc = testBubbleWith True
163testBubbleDesc :: Property
164testBubbleDesc = testBubbleWith False
166testByteLengthWithMA :: forall a. Unboxed a => a -> IO ()
167testByteLengthWithMA _ = do
168 arrA <- MA.newArray 100 :: IO (MA.Array a)
169 let arrW8 = MA.castUnsafe arrA :: MA.Array Word8
170 MA.byteLength arrA `shouldBe` MA.length arrW8
172testBreakOn :: [Word8] -> Word8 -> [Word8] -> Maybe [Word8] -> IO ()
173testBreakOn inp sep bef aft = do
174 (bef_, aft_) <- A.breakOn sep (A.fromList inp)
175 bef_ `shouldBe` A.fromList bef
176 aft_ `shouldBe` fmap A.fromList aft
178testWrite :: [Char] -> IO ()
180 arr <- S.fold A.write (S.fromList inp)
181 A.toList arr `shouldBe` inp
183testFromToList :: [Char] -> IO ()
184testFromToList inp = A.toList (A.fromList inp) `shouldBe` inp
186testUnsafeIndxedFromList :: [Char] -> IO ()
187testUnsafeIndxedFromList inp =
188 let arr = A.fromList inp
189 in fmap (`A.unsafeIndex` arr) [0 .. (length inp - 1)] `shouldBe` inp
191testAsPtrUnsafeMA :: IO ()
192testAsPtrUnsafeMA = do
193 arr <- MA.fromList ([0 .. 99] :: [Int])
194 MA.asPtrUnsafe arr (getList (0 :: Int)) `shouldReturn` [0 .. 99]
198 sizeOfInt = sizeOf (undefined :: Int)
200 -- We need to be careful here. We assume Unboxed and Storable are compatible
201 -- with each other. For Int, they are compatible.
203 | i >= 100 = return []
206 rest <- getList (i + 1) (p `plusPtr` sizeOfInt)
212 bSize = len * sizeOf (undefined :: Char)
213 in forAll (vectorOf len (arbitrary :: Gen Char)) $ \vec ->
214 forAll (chooseInt (bSize - 2000, bSize + 2000)) $ \newBLen -> do
215 arr <- MA.fromList vec
216 arr1 <- MA.realloc newBLen arr
218 lst1 <- MA.toList arr1
225 modifyMaxSuccess (const maxTestCount) $ do
226 describe moduleName $ do
228 describe "Construction" $ do
229 -- XXX There is an issue https://github.com/composewell/streamly/issues/1577
230 --prop "testAppend" testAppend
231 prop "testBubbleAsc" testBubbleAsc
232 prop "testBubbleDesc" testBubbleDesc
233 prop "length . fromStream === n" testLengthFromStream
234 prop "toStream . fromStream === id" testFromStreamToStream
235 prop "read . write === id" testFoldUnfold
236 prop "fromList" testFromList
237 prop "foldMany with writeNUnsafe concats to original"
238 (foldManyWith (\n -> Fold.take n (A.writeNUnsafe n)))
239 describe "unsafeSlice" $ do
240 it "partial" $ unsafeSlice 2 4 [1..10]
241 it "none" $ unsafeSlice 10 0 [1..10]
242 it "full" $ unsafeSlice 0 10 [1..10]
243 describe "Mut.unsafeWriteIndex" $ do
244 it "first" (unsafeWriteIndex [1..10] 0 0 `shouldReturn` True)
245 it "middle" (unsafeWriteIndex [1..10] 5 0 `shouldReturn` True)
246 it "last" (unsafeWriteIndex [1..10] 9 0 `shouldReturn` True)
248 prop "writeLastN : 0 <= n <= len" testLastN
249 describe "writeLastN boundary conditions" $ do
250 it "writeLastN -1" (testLastN_LN 10 (-1) `shouldReturn` True)
251 it "writeLastN 0" (testLastN_LN 10 0 `shouldReturn` True)
252 it "writeLastN length" (testLastN_LN 10 10 `shouldReturn` True)
253 it "writeLastN (length + 1)" (testLastN_LN 10 11 `shouldReturn` True)
254 describe "Strip" $ do
255 it "strip" (testStrip `shouldReturn` True)
256 it "stripLeft" (testStripLeft `shouldReturn` True)
257 it "stripRight" (testStripRight `shouldReturn` True)
258 it "stripZero" (testStripZero `shouldReturn` True)
259 it "stripEmpty" (testStripEmpty `shouldReturn` True)
260 it "stripNull" (testStripNull `shouldReturn` True)
262 it "testByteLengthWithMA Int"
263 (testByteLengthWithMA (undefined :: Int))
264 it "testByteLengthWithMA Char"
265 (testByteLengthWithMA (undefined :: Char))
266 it "testAsPtrUnsafeMA" testAsPtrUnsafeMA
267 it "reallocMA" reallocMA
268 describe "breakOn" $ do
269 it "testBreakOn [1, 0, 2] 0"
270 (testBreakOn [1, 0, 2] 0 [1] (Just [2]))
271 it "testBreakOn [1, 0] 0" (testBreakOn [1, 0] 0 [1] (Just []))
272 it "testBreakOn [1] 0" (testBreakOn [1] 0 [1] Nothing)
273 describe "toList . fromList" $ do
274 it "testFromToList abc" (testFromToList "abc")
275 it "testFromToList \\22407" (testFromToList "\22407")
276 describe "unsafeIndex . fromList" $ do
277 it "testUnsafeIndxedFromList abc" (testUnsafeIndxedFromList "abc")
278 it "testUnsafeIndxedFromList \\22407"
279 (testUnsafeIndxedFromList "\22407")
280 describe "write" $ do
281 it "testWrite abc" (testWrite "abc")
282 it "testWrite \\22407" (testWrite "\22407")
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