@@ -20,6 +20,9 @@ module Streamly.Test.Data.Unbox (main) where
21--------------------------------------------------------------------------------
21--------------------------------------------------------------------------------
24+import Control.Monad (void)
23import Data.Complex (Complex ((:+)))
26import Data.Complex (Complex ((:+)))
24import Data.Functor.Const (Const (..))
27import Data.Functor.Const (Const (..))
25import Data.Functor.Identity (Identity (..))
28import Data.Functor.Identity (Identity (..))
@@ -27,10 +30,19 @@ import Data.Proxy (Proxy(..))
27import GHC.Generics (Generic, Rep(..))
30import GHC.Generics (Generic, Rep(..))
28import GHC.Real (Ratio(..))
31import GHC.Real (Ratio(..))
35+import Streamly.Internal.Data.Serialize (Serialize(..), Size(..))
36+import Streamly.Internal.Data.Serialize.TH
30#ifdef USE_TH
40#ifdef USE_TH
31import Streamly.Internal.Data.Unbox.TH
41import Streamly.Internal.Data.Unbox.TH
34import Streamly.Internal.Data.Unbox
46import Streamly.Internal.Data.Unbox
35 ( PeekRep(..)
47 ( PeekRep(..)
36 , PokeRep(..)
48 , PokeRep(..)
@@ -40,7 +52,6 @@ import Streamly.Internal.Data.Unbox
40 , genericPokeByteIndex
52 , genericPokeByteIndex
41 , genericSizeOf
53 , genericSizeOf
42 , newBytes
54 , newBytes
46import Test.Hspec as H
57import Test.Hspec as H
@@ -49,12 +60,32 @@ import Test.Hspec as H
50--------------------------------------------------------------------------------
61--------------------------------------------------------------------------------
65+#define MODULE_NAME "Data.Serialize.Deriving.TH"
66+#define DERIVE_UNBOX(typ) $(deriveSerialize ''typ)
67+#define PEEK(i, arr) fmap snd (deserialize i arr)
68+#define POKE(i, arr, val) void (serialize i arr val)
69+#define TYPE_CLASS Serialize
73+#define PEEK(i, arr) peekByteIndex i arr
74+#define POKE(i, arr, val) pokeByteIndex i arr val
75+#define TYPE_CLASS Unbox
52#ifdef USE_TH
77#ifdef USE_TH
53#define MODULE_NAME "Data.Unbox.Deriving.TH"
79#define MODULE_NAME "Data.Unbox.Deriving.TH"
54#define DERIVE_UNBOX(typ) $(deriveUnbox ''typ)
80#define DERIVE_UNBOX(typ) $(deriveUnbox ''typ)
56#define MODULE_NAME "Data.Unbox.Deriving.Generic"
84#define MODULE_NAME "Data.Unbox.Deriving.Generic"
57#define DERIVE_UNBOX(typ) deriving instance Unbox (typ)
85#define DERIVE_UNBOX(typ) deriving instance Unbox (typ)
60--------------------------------------------------------------------------------
91--------------------------------------------------------------------------------
@@ -132,24 +163,42 @@ DERIVE_UNBOX(NestedSOP)
132-- Ratio does not have a Generic instance by default
163-- Ratio does not have a Generic instance by default
133deriving instance Generic (Ratio Int)
164deriving instance Generic (Ratio Int)
166+#if defined(USE_SERIALIZE)
167+$(deriveSerialize ''Complex)
168+$(deriveSerialize ''Ratio)
169+$(deriveSerializeWith ["a"] ''Const)
170+$(deriveSerialize ''Identity)
135--------------------------------------------------------------------------------
173--------------------------------------------------------------------------------
136-- Test helpers
174-- Test helpers
137--------------------------------------------------------------------------------
175--------------------------------------------------------------------------------
139testSerialization ::
177testSerialization ::
140− forall a. (Eq a, Show a, Unbox a)
178+ forall a. (Eq a, Show a, TYPE_CLASS a)
143testSerialization val = do
181testSerialization val = do
184+ (case size :: Size a of
144− arr <- newBytes (sizeOf (Proxy :: Proxy a))
188+ (sizeOf (Proxy :: Proxy a))
145− pokeByteIndex 0 arr val
190+ POKE(0, arr, val)
146− peekByteIndex 0 arr `shouldReturn` val
191+ PEEK(0, arr) `shouldReturn` val
148testGenericConsistency ::
193testGenericConsistency ::
149 forall a.
194 forall a.
198+ , Serialize a, Unbox a
152 , Unbox a
200 , Unbox a
153 , Generic a
202 , Generic a
154 , SizeOfRep (Rep a)
203 , SizeOfRep (Rep a)
155 , PeekRep (Rep a)
204 , PeekRep (Rep a)
@@ -160,21 +209,32 @@ testGenericConsistency ::
160testGenericConsistency val = do
209testGenericConsistency val = do
162 -- Test the generic sizeOf
211 -- Test the generic sizeOf
213+ (case size :: Size a of
217+ sizeOf (Proxy :: Proxy a)
163− sizeOf (Proxy :: Proxy a) `shouldBe` genericSizeOf (Proxy :: Proxy a)
219+ `shouldBe` genericSizeOf (Proxy :: Proxy a)
165 -- Test the serialization and deserialization
221 -- Test the serialization and deserialization
166 arr <- newBytes (sizeOf (Proxy :: Proxy a))
222 arr <- newBytes (sizeOf (Proxy :: Proxy a))
168− pokeByteIndex 0 arr val
224+ POKE(0, arr, val)
169 genericPeekByteIndex arr 0 `shouldReturn` val
225 genericPeekByteIndex arr 0 `shouldReturn` val
171 genericPokeByteIndex arr 0 val
227 genericPokeByteIndex arr 0 val
172− peekByteIndex 0 arr `shouldReturn` val
228+ PEEK(0, arr) `shouldReturn` val
231+#ifndef USE_SERIALIZE
174-- Size is also implicitly tested while serializing and deserializing.
232-- Size is also implicitly tested while serializing and deserializing.
175checkSizeOf :: forall a. Unbox a => Proxy a -> Int -> IO ()
233checkSizeOf :: forall a. Unbox a => Proxy a -> Int -> IO ()
176checkSizeOf _ size = sizeOf (Proxy :: Proxy a) `shouldBe` size
234checkSizeOf _ size = sizeOf (Proxy :: Proxy a) `shouldBe` size
178--------------------------------------------------------------------------------
238--------------------------------------------------------------------------------
179-- CPP helpers
239-- CPP helpers
180--------------------------------------------------------------------------------
240--------------------------------------------------------------------------------
@@ -204,6 +264,7 @@ testCases = do
204 it "SumOfProducts SOP2" $ testSerialization (SOP2 1 'a')
264 it "SumOfProducts SOP2" $ testSerialization (SOP2 1 'a')
205 it "SumOfProducts SOP3" $ testSerialization (SOP3 1 2 3)
265 it "SumOfProducts SOP3" $ testSerialization (SOP3 1 2 3)
267+#ifndef USE_SERIALIZE
207 CHECK_SIZE(Unit, 1)
268 CHECK_SIZE(Unit, 1)
208 CHECK_SIZE(Unit1, 1)
269 CHECK_SIZE(Unit1, 1)
209 CHECK_SIZE(Unit2, 2)
270 CHECK_SIZE(Unit2, 2)
@@ -215,6 +276,7 @@ testCases = do
215 CHECK_SIZE(Product2, 12)
276 CHECK_SIZE(Product2, 12)
216 CHECK_SIZE(SumOfProducts, 25)
277 CHECK_SIZE(SumOfProducts, 25)
217 CHECK_SIZE(NestedSOP, 26)
278 CHECK_SIZE(NestedSOP, 26)
219 it "Bool" $ testSerialization True
281 it "Bool" $ testSerialization True
220 it "Complex Int" $ testSerialization (5 :+ 3 :: Complex Int)
282 it "Complex Int" $ testSerialization (5 :+ 3 :: Complex Int)