@@ -20,6 +20,9 @@ module Streamly.Test.Data.Unbox (main) where
2020-- Imports
2121--------------------------------------------------------------------------------
2222
23+#ifdef USE_SERIALIZE
24+import Control.Monad (void)
25+#endif
2326import Data.Complex (Complex ((:+)))
2427import Data.Functor.Const (Const (..))
2528import Data.Functor.Identity (Identity (..))
@@ -27,10 +30,19 @@ import Data.Proxy (Proxy(..))
2730import GHC.Generics (Generic, Rep(..))
2831import GHC.Real (Ratio(..))
2932
33+#ifdef USE_SERIALIZE
34+
35+import Streamly.Internal.Data.Serialize (Serialize(..), Size(..))
36+import Streamly.Internal.Data.Serialize.TH
37+
38+#else
39+
3040#ifdef USE_TH
3141import Streamly.Internal.Data.Unbox.TH
3242#endif
3343
44+#endif
45+
3446import Streamly.Internal.Data.Unbox
3547 ( PeekRep(..)
3648 , PokeRep(..)
@@ -40,7 +52,6 @@ import Streamly.Internal.Data.Unbox
4052 , genericPokeByteIndex
4153 , genericSizeOf
4254 , newBytes
43− , pokeByteIndex
4455 )
4556
4657import Test.Hspec as H
@@ -49,12 +60,32 @@ import Test.Hspec as H
4960-- Types
5061--------------------------------------------------------------------------------
5162
63+#ifdef USE_SERIALIZE
64+
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
70+
71+#else
72+
73+#define PEEK(i, arr) peekByteIndex i arr
74+#define POKE(i, arr, val) pokeByteIndex i arr val
75+#define TYPE_CLASS Unbox
76+
5277#ifdef USE_TH
78+
5379#define MODULE_NAME "Data.Unbox.Deriving.TH"
5480#define DERIVE_UNBOX(typ) $(deriveUnbox ''typ)
81+
5582#else
83+
5684#define MODULE_NAME "Data.Unbox.Deriving.Generic"
5785#define DERIVE_UNBOX(typ) deriving instance Unbox (typ)
86+
87+#endif
88+
5889#endif
5990
6091--------------------------------------------------------------------------------
@@ -132,24 +163,42 @@ DERIVE_UNBOX(NestedSOP)
132163-- Ratio does not have a Generic instance by default
133164deriving instance Generic (Ratio Int)
134165
166+#if defined(USE_SERIALIZE)
167+$(deriveSerialize ''Complex)
168+$(deriveSerialize ''Ratio)
169+$(deriveSerializeWith ["a"] ''Const)
170+$(deriveSerialize ''Identity)
171+#endif
172+
135173--------------------------------------------------------------------------------
136174-- Test helpers
137175--------------------------------------------------------------------------------
138176
139177testSerialization ::
140− forall a. (Eq a, Show a, Unbox a)
178+ forall a. (Eq a, Show a, TYPE_CLASS a)
141179 => a
142180 -> IO ()
143181testSerialization val = do
144− arr <- newBytes (sizeOf (Proxy :: Proxy a))
145− pokeByteIndex 0 arr val
146− peekByteIndex 0 arr `shouldReturn` val
182+ arr <- newBytes
183+#ifdef USE_SERIALIZE
184+ (case size :: Size a of
185+ ConstSize x -> x
186+ VarSize f -> f val)
187+#else
188+ (sizeOf (Proxy :: Proxy a))
189+#endif
190+ POKE(0, arr, val)
191+ PEEK(0, arr) `shouldReturn` val
147192
148193testGenericConsistency ::
149194 forall a.
150195 ( Eq a
151196 , Show a
197+#ifdef USE_SERIALIZE
198+ , Serialize a, Unbox a
199+#else
152200 , Unbox a
201+#endif
153202 , Generic a
154203 , SizeOfRep (Rep a)
155204 , PeekRep (Rep a)
@@ -160,21 +209,32 @@ testGenericConsistency ::
160209testGenericConsistency val = do
161210
162211 -- Test the generic sizeOf
163− sizeOf (Proxy :: Proxy a) `shouldBe` genericSizeOf (Proxy :: Proxy a)
212+#ifdef USE_SERIALIZE
213+ (case size :: Size a of
214+ ConstSize x -> x
215+ VarSize f -> f val)
216+#else
217+ sizeOf (Proxy :: Proxy a)
218+#endif
219+ `shouldBe` genericSizeOf (Proxy :: Proxy a)
164220
165221 -- Test the serialization and deserialization
166222 arr <- newBytes (sizeOf (Proxy :: Proxy a))
167223
168− pokeByteIndex 0 arr val
224+ POKE(0, arr, val)
169225 genericPeekByteIndex arr 0 `shouldReturn` val
170226
171227 genericPokeByteIndex arr 0 val
172− peekByteIndex 0 arr `shouldReturn` val
228+ PEEK(0, arr) `shouldReturn` val
229+
173230
231+#ifndef USE_SERIALIZE
174232-- Size is also implicitly tested while serializing and deserializing.
175233checkSizeOf :: forall a. Unbox a => Proxy a -> Int -> IO ()
176234checkSizeOf _ size = sizeOf (Proxy :: Proxy a) `shouldBe` size
177235
236+#endif
237+
178238--------------------------------------------------------------------------------
179239-- CPP helpers
180240--------------------------------------------------------------------------------
@@ -204,6 +264,7 @@ testCases = do
204264 it "SumOfProducts SOP2" $ testSerialization (SOP2 1 'a')
205265 it "SumOfProducts SOP3" $ testSerialization (SOP3 1 2 3)
206266
267+#ifndef USE_SERIALIZE
207268 CHECK_SIZE(Unit, 1)
208269 CHECK_SIZE(Unit1, 1)
209270 CHECK_SIZE(Unit2, 2)
@@ -215,6 +276,7 @@ testCases = do
215276 CHECK_SIZE(Product2, 12)
216277 CHECK_SIZE(SumOfProducts, 25)
217278 CHECK_SIZE(NestedSOP, 26)
279+#endif
218280
219281 it "Bool" $ testSerialization True
220282 it "Complex Int" $ testSerialization (5 :+ 3 :: Complex Int)