on master help sign in
Add serialize size checks for constant size datatypes
50fc60cAdithya Kumar
test/Streamly/Test/Data/Unbox.hs+45−11
@@ -20,9 +20,6 @@ module Streamly.Test.Data.Unbox (main) where
20-- Imports
20-- Imports
21--------------------------------------------------------------------------------
21--------------------------------------------------------------------------------
22
22
23−#ifdef USE_SERIALIZE
24−import Control.Monad (void)
25−#endif
26import Data.Complex (Complex ((:+)))
23import Data.Complex (Complex ((:+)))
27import Data.Functor.Const (Const (..))
24import Data.Functor.Const (Const (..))
28import Data.Functor.Identity (Identity (..))
25import Data.Functor.Identity (Identity (..))
@@ -52,6 +49,9 @@ import Streamly.Internal.Data.Unbox
52 , genericPokeByteIndex
49 , genericPokeByteIndex
53 , genericSizeOf
50 , genericSizeOf
54 , newBytes
51 , newBytes
52+#ifndef USE_SERIALIZE
53+ , MutableByteArray
54+#endif
55 )
55 )
56
56
57import Test.Hspec as H
57import Test.Hspec as H
@@ -64,14 +64,14 @@ import Test.Hspec as H
64
64
65#define MODULE_NAME "Data.Serialize.Deriving.TH"
65#define MODULE_NAME "Data.Serialize.Deriving.TH"
66#define DERIVE_UNBOX(typ) $(deriveSerialize ''typ)
66#define DERIVE_UNBOX(typ) $(deriveSerialize ''typ)
67−#define PEEK(i, arr, sz) fmap snd (deserialize i arr sz)
67+#define PEEK(i, arr, sz) (deserialize i arr sz)
68−#define POKE(i, arr, val) void (serialize i arr val)
68+#define POKE(i, arr, val) (serialize i arr val)
69#define TYPE_CLASS Serialize
69#define TYPE_CLASS Serialize
70
70
71#else
71#else
72
72
73−#define PEEK(i, arr, sz) peekByteIndex i arr
73+#define PEEK(i, arr, sz) peekByteIndexWithNextOff i arr
74−#define POKE(i, arr, val) pokeByteIndex i arr val
74+#define POKE(i, arr, val) pokeByteIndexWithNextOff i arr val
75#define TYPE_CLASS Unbox
75#define TYPE_CLASS Unbox
76
76
77#ifdef USE_TH
77#ifdef USE_TH
@@ -88,6 +88,33 @@ import Test.Hspec as H
88
88
89#endif
89#endif
90
90
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+
91--------------------------------------------------------------------------------
118--------------------------------------------------------------------------------
92-- Types
119-- Types
93--------------------------------------------------------------------------------
120--------------------------------------------------------------------------------
@@ -194,8 +221,11 @@ testSerialization val = do
194 (sizeOf (Proxy :: Proxy a))
221 (sizeOf (Proxy :: Proxy a))
195#endif
222#endif
196 arr <- newBytes len
223 arr <- newBytes len
197− POKE(0, arr, val)
224+ nextOff <- POKE(0, arr, val)
198− PEEK(0, arr, len) `shouldReturn` val
225+ (nextOff1, val1) <- PEEK(0, arr, len)
226+ val1 `shouldBe` val
227+ nextOff1 `shouldBe` len
228+ nextOff `shouldBe` len
199
229
200testGenericConsistency ::
230testGenericConsistency ::
201 forall a.
231 forall a.
@@ -226,11 +256,15 @@ testGenericConsistency val = do
226 -- Test the serialization and deserialization
256 -- Test the serialization and deserialization
227 arr <- newBytes (sizeOf (Proxy :: Proxy a))
257 arr <- newBytes (sizeOf (Proxy :: Proxy a))
228
258
229− POKE(0, arr, val)
259+ nextOff <- POKE(0, arr, val)
230 genericPeekByteIndex arr 0 `shouldReturn` val
260 genericPeekByteIndex arr 0 `shouldReturn` val
231
261
232 genericPokeByteIndex arr 0 val
262 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
234
268
235
269
236#ifndef USE_SERIALIZE
270#ifndef USE_SERIALIZE