on master help sign in
Add serialize size checks for constant size datatypes
commit50fc60cb509adcc01d8f22790abca8d9d9935ddf authorAdithya Kumar <adithya@composewell.com> dateTue 29 Aug 2023, 23:15 (3 years ago) parent1d0e20c
1 file changed+45−11
test/Streamly/Test/Data/Unbox.hs+45−11
test/Streamly/Test/Data/Unbox.hs+45−11
@@ -20,9 +20,6 @@ module Streamly.Test.Data.Unbox (main) where
2020-- Imports
2121--------------------------------------------------------------------------------
2222
23−#ifdef USE_SERIALIZE
24−import Control.Monad (void)
25−#endif
2623import Data.Complex (Complex ((:+)))
2724import Data.Functor.Const (Const (..))
2825import Data.Functor.Identity (Identity (..))
@@ -52,6 +49,9 @@ import Streamly.Internal.Data.Unbox
5249 , genericPokeByteIndex
5350 , genericSizeOf
5451 , newBytes
52+#ifndef USE_SERIALIZE
53+ , MutableByteArray
54+#endif
5555 )
5656
5757import Test.Hspec as H
@@ -64,14 +64,14 @@ import Test.Hspec as H
6464
6565#define MODULE_NAME "Data.Serialize.Deriving.TH"
6666#define DERIVE_UNBOX(typ) $(deriveSerialize ''typ)
67−#define PEEK(i, arr, sz) fmap snd (deserialize i arr sz)
68−#define POKE(i, arr, val) void (serialize i arr val)
67+#define PEEK(i, arr, sz) (deserialize i arr sz)
68+#define POKE(i, arr, val) (serialize i arr val)
6969#define TYPE_CLASS Serialize
7070
7171#else
7272
73−#define PEEK(i, arr, sz) peekByteIndex i arr
74−#define POKE(i, arr, val) pokeByteIndex i arr val
73+#define PEEK(i, arr, sz) peekByteIndexWithNextOff i arr
74+#define POKE(i, arr, val) pokeByteIndexWithNextOff i arr val
7575#define TYPE_CLASS Unbox
7676
7777#ifdef USE_TH
@@ -88,6 +88,33 @@ import Test.Hspec as H
8888
8989#endif
9090
91+--------------------------------------------------------------------------------
92+-- Helpers
93+--------------------------------------------------------------------------------
94+
95+#ifndef USE_SERIALIZE
96+
97+peekByteIndexWithNextOff ::
98+ forall a. Unbox a
99+ => Int
100+ -> MutableByteArray
101+ -> IO (Int, a)
102+peekByteIndexWithNextOff i arr = do
103+ val <- peekByteIndex i arr
104+ pure (i + sizeOf (Proxy :: Proxy a), val)
105+
106+pokeByteIndexWithNextOff ::
107+ forall a. Unbox a
108+ => Int
109+ -> MutableByteArray
110+ -> a
111+ -> IO Int
112+pokeByteIndexWithNextOff i arr val = do
113+ pokeByteIndex i arr val
114+ pure $ i + sizeOf (Proxy :: Proxy a)
115+
116+#endif
117+
91118--------------------------------------------------------------------------------
92119-- Types
93120--------------------------------------------------------------------------------
@@ -194,8 +221,11 @@ testSerialization val = do
194221 (sizeOf (Proxy :: Proxy a))
195222#endif
196223 arr <- newBytes len
197− POKE(0, arr, val)
198− PEEK(0, arr, len) `shouldReturn` val
224+ nextOff <- POKE(0, arr, val)
225+ (nextOff1, val1) <- PEEK(0, arr, len)
226+ val1 `shouldBe` val
227+ nextOff1 `shouldBe` len
228+ nextOff `shouldBe` len
199229
200230testGenericConsistency ::
201231 forall a.
@@ -226,11 +256,15 @@ testGenericConsistency val = do
226256 -- Test the serialization and deserialization
227257 arr <- newBytes (sizeOf (Proxy :: Proxy a))
228258
229− POKE(0, arr, val)
259+ nextOff <- POKE(0, arr, val)
230260 genericPeekByteIndex arr 0 `shouldReturn` val
231261
232262 genericPokeByteIndex arr 0 val
233− PEEK(0, arr, len) `shouldReturn` val
263+ (nextOff1, val1) <- PEEK(0, arr, len)
264+ val1 `shouldBe` val
265+
266+ nextOff1 `shouldBe` len
267+ nextOff `shouldBe` len
234268
235269
236270#ifndef USE_SERIALIZE