on master help sign in
Add a more robust offset ignoring tests in Serialize
5f67528Adithya Kumar
test/Streamly/Test/Data/Serialize.hs+10−4
@@ -21,6 +21,7 @@ module Streamly.Test.Data.Serialize (main) where
21-- Imports
21-- Imports
22--------------------------------------------------------------------------------
22--------------------------------------------------------------------------------
23
23
24+import System.Random (randomRIO)
24import Streamly.Internal.Data.Unbox (newBytes)
25import Streamly.Internal.Data.Unbox (newBytes)
25import GHC.Generics (Generic)
26import GHC.Generics (Generic)
26import Streamly.Test.Data.Serialize.TH (genDatatype)
27import Streamly.Test.Data.Serialize.TH (genDatatype)
@@ -121,11 +122,13 @@ roundtrip val = do
121
122
122 let sz = Serialize.size 0 val
123 let sz = Serialize.size 0 val
123
124
125+ let excessSize = 100
126+ randomOff <- randomRIO (10, excessSize)
124 -- Use a proper slice to test instead of the array directly. This will catch
127 -- Use a proper slice to test instead of the array directly. This will catch
125 -- any hardcoded 0 offsets
128 -- any hardcoded 0 offsets
126− let arrSize = sz + 100
129+ let arrSize = sz + excessSize
127− serStartOff = 50
130+ serStartOff = randomOff
128− serEndOff = 50 + sz
131+ serEndOff = randomOff + sz
129 arr <- newBytes arrSize
132 arr <- newBytes arrSize
130
133
131 off1 <- Serialize.serialize serStartOff arr val
134 off1 <- Serialize.serialize serStartOff arr val
@@ -134,7 +137,10 @@ roundtrip val = do
134 off2 `shouldBe` off1
137 off2 `shouldBe` off1
135 off2 `shouldBe` serEndOff
138 off2 `shouldBe` serEndOff
136 val `shouldBe` Serialize.decode (Serialize.encode val)
139 val `shouldBe` Serialize.decode (Serialize.encode val)
137− val `shouldBe` Serialize.decode (Array.Array arr serStartOff serEndOff)
140+ let slice = Array.Array arr serStartOff serEndOff
141+ val `shouldBe` Serialize.decode slice
142+ clonedSlice <- Array.clone slice
143+ val `shouldBe` Serialize.decode clonedSlice
138
144
139testSerializeList
145testSerializeList
140 :: forall a. (Eq a, Show a, Serialize.Serialize a)
146 :: forall a. (Eq a, Show a, Serialize.Serialize a)