Add a case to test equality of serialization encoding

This commit is contained in:
Adithya Kumar 2023-12-02 15:13:10 +05:30
parent d5342f62d7
commit 6eee16aebc
2 changed files with 31 additions and 0 deletions

View File

@ -21,6 +21,8 @@ module Streamly.Test.Data.Serialize (main) where
-- Imports
--------------------------------------------------------------------------------
import Data.Foldable (forM_)
import Data.Word (Word8)
import System.Random (randomRIO)
import Streamly.Internal.Data.MutByteArray (MutByteArray)
import GHC.Generics (Generic)
@ -170,8 +172,18 @@ poke val = do
serStartOff = randomOff
serEndOff = randomOff + sz
arr <- Serialize.new arrSize
arr2 <- Serialize.new arrSize
-- Re-initialize the array with random value
forM_ [0..(arrSize - 1)] $ \i -> Serialize.pokeAt i arr2 (8 :: Word8)
off1 <- Serialize.serializeAt serStartOff arr val
off2 <- Serialize.serializeAt 0 arr2 val
let slice1 = Array.Array arr serStartOff off1 :: Array.Array Word8
slice2 = Array.Array arr2 0 off2 :: Array.Array Word8
-- The serialized representation should be the same
slice1 `shouldBe` slice2
off1 `shouldBe` serEndOff
pure (arr, serStartOff, serEndOff)

View File

@ -20,6 +20,12 @@ module Streamly.Test.Data.Unbox (main) where
-- Imports
--------------------------------------------------------------------------------
#ifdef USE_SERIALIZE
import Data.Foldable (forM_)
import Data.Word (Word8)
import qualified Streamly.Internal.Data.Array as Array
#endif
import Data.Complex (Complex ((:+)))
import Data.Functor.Const (Const (..))
import Data.Functor.Identity (Identity (..))
@ -201,6 +207,19 @@ testSerialization val = do
#endif
arr <- MBA.new len
nextOff <- POKE(0, arr, val)
#ifdef USE_SERIALIZE
arr2 <- MBA.new len
-- Re-initialize the array with random value
forM_ [0..(len - 1)] $ \i -> POKE(i, arr2, (8 :: Word8))
_ <- POKE(0, arr2, val)
let slice1 = Array.Array arr 0 len :: Array.Array Word8
slice2 = Array.Array arr2 0 len :: Array.Array Word8
-- The serialized representation should be the same
slice1 `shouldBe` slice2
-- The serialized representation is not the same for "Unbox" as the Array
-- might not be fully utilized in case of "Unbox". This is because different
-- constructors might have different lengths.
#endif
(nextOff1, val1) <- PEEK(0, arr, len)
val1 `shouldBe` val
nextOff1 `shouldBe` len