on master help sign in
Add a case to test equality of serialization encoding
6eee16aAdithya Kumar
test/Streamly/Test/Data/Serialize.hs+12
@@ -21,6 +21,8 @@ module Streamly.Test.Data.Serialize (main) where
21-- Imports
21-- Imports
22--------------------------------------------------------------------------------
22--------------------------------------------------------------------------------
23
23
24+import Data.Foldable (forM_)
25+import Data.Word (Word8)
24import System.Random (randomRIO)
26import System.Random (randomRIO)
25import Streamly.Internal.Data.MutByteArray (MutByteArray)
27import Streamly.Internal.Data.MutByteArray (MutByteArray)
26import GHC.Generics (Generic)
28import GHC.Generics (Generic)
@@ -170,8 +172,18 @@ poke val = do
170 serStartOff = randomOff
172 serStartOff = randomOff
171 serEndOff = randomOff + sz
173 serEndOff = randomOff + sz
172 arr <- Serialize.new arrSize
174 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)
173
178
174 off1 <- Serialize.serializeAt serStartOff arr val
179 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+
175 off1 `shouldBe` serEndOff
187 off1 `shouldBe` serEndOff
176 pure (arr, serStartOff, serEndOff)
188 pure (arr, serStartOff, serEndOff)
177
189
test/Streamly/Test/Data/Unbox.hs+19
@@ -20,6 +20,12 @@ module Streamly.Test.Data.Unbox (main) where
20-- Imports
20-- Imports
21--------------------------------------------------------------------------------
21--------------------------------------------------------------------------------
22
22
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+
23import Data.Complex (Complex ((:+)))
29import Data.Complex (Complex ((:+)))
24import Data.Functor.Const (Const (..))
30import Data.Functor.Const (Const (..))
25import Data.Functor.Identity (Identity (..))
31import Data.Functor.Identity (Identity (..))
@@ -201,6 +207,19 @@ testSerialization val = do
201#endif
207#endif
202 arr <- MBA.new len
208 arr <- MBA.new len
203 nextOff <- POKE(0, arr, val)
209 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
204 (nextOff1, val1) <- PEEK(0, arr, len)
223 (nextOff1, val1) <- PEEK(0, arr, len)
205 val1 `shouldBe` val
224 val1 `shouldBe` val
206 nextOff1 `shouldBe` len
225 nextOff1 `shouldBe` len