Add some test cases for unboxed array

This commit is contained in:
Adithya Kumar 2022-07-08 01:10:42 +05:30
parent 71b0c0dd39
commit c361a315e3
2 changed files with 68 additions and 1 deletions

View File

@ -191,6 +191,25 @@ testByteLengthWithMA _ = do
let arrW8 = MA.castUnsafe arrA :: MA.Array Word8
MA.byteLength arrA `shouldBe` MA.length arrW8
testBreakOn :: [Word8] -> Word8 -> [Word8] -> (Maybe [Word8]) -> IO ()
testBreakOn inp sep bef aft = do
(bef_, aft_) <- A.breakOn sep (A.fromList inp)
bef_ `shouldBe` A.fromList bef
aft_ `shouldBe` fmap A.fromList aft
testWrite :: [Char] -> IO ()
testWrite inp = do
arr <- S.fold A.write (S.fromList inp)
A.toList arr `shouldBe` inp
testFromToList :: [Char] -> IO ()
testFromToList inp = A.toList (A.fromList inp) `shouldBe` inp
testUnsafeIndxedFromList :: [Char] -> IO ()
testUnsafeIndxedFromList inp =
let arr = A.fromList inp
in fmap (flip A.unsafeIndex arr) [0 .. (length inp - 1)] `shouldBe` inp
testFromForeignPtrUnsafeMA :: IO ()
testFromForeignPtrUnsafeMA = do
fp <- Malloc.mallocForeignPtrAlignedBytes numBytes alignmentInt
@ -229,6 +248,18 @@ testAsPtrUnsafeMA = do
rest <- getList (i + 1) (p `plusPtr` sizeOfInt)
return $ val : rest
reallocMA :: Property
reallocMA =
let len = 10000
bSize = len * sizeOf (undefined :: Char)
in forAll (vectorOf len (arbitrary :: Gen Char)) $ \vec ->
forAll (chooseInt (bSize - 2000, bSize + 2000)) $ \newBLen -> do
arr <- MA.fromList vec
arr1 <- MA.realloc newBLen arr
lst <- MA.toList arr
lst1 <- MA.toList arr1
lst `shouldBe` lst1
main :: IO ()
main =
hspec $
@ -270,9 +301,26 @@ main =
it "stripZero" (testStripZero `shouldReturn` True)
it "stripEmpty" (testStripEmpty `shouldReturn` True)
it "stripNull" (testStripNull `shouldReturn` True)
describe "Mut" $ do
it "testByteLengthWithMA Int"
(testByteLengthWithMA (undefined :: Int))
it "testByteLengthWithMA Char"
(testByteLengthWithMA (undefined :: Char))
it "testFromForeignPtrUnsafeMA" testFromForeignPtrUnsafeMA
it "testAsPtrUnsafeMA" testAsPtrUnsafeMA
it "reallocMA" reallocMA
describe "breakOn" $ do
it "testBreakOn [1, 0, 2] 0"
(testBreakOn [1, 0, 2] 0 [1] (Just [2]))
it "testBreakOn [1, 0] 0" (testBreakOn [1, 0] 0 [1] (Just []))
it "testBreakOn [1] 0" (testBreakOn [1] 0 [1] Nothing)
describe "toList . fromList" $ do
it "testFromToList abc" (testFromToList "abc")
it "testFromToList \\22407" (testFromToList "\22407")
describe "unsafeIndex . fromList" $ do
it "testUnsafeIndxedFromList abc" (testUnsafeIndxedFromList "abc")
it "testUnsafeIndxedFromList \\22407"
(testUnsafeIndxedFromList "\22407")
describe "write" $ do
it "testWrite abc" (testWrite "abc")
it "testWrite \\22407" (testWrite "\22407")

View File

@ -1,7 +1,8 @@
module Main (main) where
import Data.Word (Word8)
import Streamly.Test.Common (listEquals, chooseInt)
import Test.Hspec (hspec, describe)
import Test.Hspec (hspec, describe, shouldBe)
import Test.Hspec.QuickCheck
import Test.QuickCheck (forAll, Property, vectorOf, Gen)
import Test.QuickCheck.Monadic (monadicIO, run)
@ -55,6 +56,14 @@ parseBreak = do
ls2 <- run $ Stream.toList $ ArrayStream.concat str
listEquals (==) (ls1 ++ ls2) ls
splitOnSuffix :: Word8 -> [Word8] -> [[Word8]] -> IO ()
splitOnSuffix sep inp out = do
res <-
Stream.toList
$ ArrayStream.splitOnSuffix sep
$ Stream.chunksOf 2 (Array.writeN 2) $ Stream.fromList inp
fmap Array.toList res `shouldBe` out
-------------------------------------------------------------------------------
-- Main
-------------------------------------------------------------------------------
@ -70,3 +79,13 @@ main =
describe moduleName $ do
describe "Stream parsing" $ do
prop "parseBreak" parseBreak
describe "splifOnSuffix" $ do
Hspec.it "splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6]"
$ splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6]
[[1, 2], [4], [5, 6]]
Hspec.it "splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6, 0]"
$ splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6, 0]
[[1, 2], [4], [5, 6]]
Hspec.it "splitOnSuffix 0 [0, 1, 2, 0, 4, 0, 5, 6]"
$ splitOnSuffix 0 [0, 1, 2, 0, 4, 0, 5, 6]
[[], [1, 2], [4], [5, 6]]