on master help sign in
Add a case to test equality of serialization encoding
commit6eee16aebc5254157aefb425df98a2bb7adbe082 authorAdithya Kumar <adithya@composewell.com> dateSat 2 Dec 2023, 15:13 (3 years ago) parentd5342f6
2 files changed+31
test/Streamly/Test/Data/Serialize.hs+12 test/Streamly/Test/Data/Unbox.hs+19
test/Streamly/Test/Data/Serialize.hs+12
@@ -21,6 +21,8 @@ module Streamly.Test.Data.Serialize (main) where
2121-- Imports
2222--------------------------------------------------------------------------------
2323
24+import Data.Foldable (forM_)
25+import Data.Word (Word8)
2426import System.Random (randomRIO)
2527import Streamly.Internal.Data.MutByteArray (MutByteArray)
2628import GHC.Generics (Generic)
@@ -170,8 +172,18 @@ poke val = do
170172 serStartOff = randomOff
171173 serEndOff = randomOff + sz
172174 arr <- Serialize.new arrSize
175+ arr2 <- Serialize.new arrSize
176+ -- Re-initialize the array with random value
177+ forM_ [0..(arrSize - 1)] $ \i -> Serialize.pokeAt i arr2 (8 :: Word8)
173178
174179 off1 <- Serialize.serializeAt serStartOff arr val
180+ off2 <- Serialize.serializeAt 0 arr2 val
181+
182+ let slice1 = Array.Array arr serStartOff off1 :: Array.Array Word8
183+ slice2 = Array.Array arr2 0 off2 :: Array.Array Word8
184+ -- The serialized representation should be the same
185+ slice1 `shouldBe` slice2
186+
175187 off1 `shouldBe` serEndOff
176188 pure (arr, serStartOff, serEndOff)
177189
test/Streamly/Test/Data/Unbox.hs+19
@@ -20,6 +20,12 @@ module Streamly.Test.Data.Unbox (main) where
2020-- Imports
2121--------------------------------------------------------------------------------
2222
23+#ifdef USE_SERIALIZE
24+import Data.Foldable (forM_)
25+import Data.Word (Word8)
26+import qualified Streamly.Internal.Data.Array as Array
27+#endif
28+
2329import Data.Complex (Complex ((:+)))
2430import Data.Functor.Const (Const (..))
2531import Data.Functor.Identity (Identity (..))
@@ -201,6 +207,19 @@ testSerialization val = do
201207#endif
202208 arr <- MBA.new len
203209 nextOff <- POKE(0, arr, val)
210+#ifdef USE_SERIALIZE
211+ arr2 <- MBA.new len
212+ -- Re-initialize the array with random value
213+ forM_ [0..(len - 1)] $ \i -> POKE(i, arr2, (8 :: Word8))
214+ _ <- POKE(0, arr2, val)
215+ let slice1 = Array.Array arr 0 len :: Array.Array Word8
216+ slice2 = Array.Array arr2 0 len :: Array.Array Word8
217+ -- The serialized representation should be the same
218+ slice1 `shouldBe` slice2
219+ -- The serialized representation is not the same for "Unbox" as the Array
220+ -- might not be fully utilized in case of "Unbox". This is because different
221+ -- constructors might have different lengths.
222+#endif
204223 (nextOff1, val1) <- PEEK(0, arr, len)
205224 val1 `shouldBe` val
206225 nextOff1 `shouldBe` len