From 5f67528761ca1a16377382cd836b80cb562f639b Mon Sep 17 00:00:00 2001 From: Adithya Kumar Date: Wed, 13 Sep 2023 15:35:26 +0530 Subject: [PATCH] Add a more robust offset ignoring tests in Serialize This is meant to catch any serialized absolute offsets --- test/Streamly/Test/Data/Serialize.hs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/Streamly/Test/Data/Serialize.hs b/test/Streamly/Test/Data/Serialize.hs index 72834f791..4d2b0d65b 100644 --- a/test/Streamly/Test/Data/Serialize.hs +++ b/test/Streamly/Test/Data/Serialize.hs @@ -21,6 +21,7 @@ module Streamly.Test.Data.Serialize (main) where -- Imports -------------------------------------------------------------------------------- +import System.Random (randomRIO) import Streamly.Internal.Data.Unbox (newBytes) import GHC.Generics (Generic) import Streamly.Test.Data.Serialize.TH (genDatatype) @@ -121,11 +122,13 @@ roundtrip val = do let sz = Serialize.size 0 val + let excessSize = 100 + randomOff <- randomRIO (10, excessSize) -- Use a proper slice to test instead of the array directly. This will catch -- any hardcoded 0 offsets - let arrSize = sz + 100 - serStartOff = 50 - serEndOff = 50 + sz + let arrSize = sz + excessSize + serStartOff = randomOff + serEndOff = randomOff + sz arr <- newBytes arrSize off1 <- Serialize.serialize serStartOff arr val @@ -134,7 +137,10 @@ roundtrip val = do off2 `shouldBe` off1 off2 `shouldBe` serEndOff val `shouldBe` Serialize.decode (Serialize.encode val) - val `shouldBe` Serialize.decode (Array.Array arr serStartOff serEndOff) + let slice = Array.Array arr serStartOff serEndOff + val `shouldBe` Serialize.decode slice + clonedSlice <- Array.clone slice + val `shouldBe` Serialize.decode clonedSlice testSerializeList :: forall a. (Eq a, Show a, Serialize.Serialize a)