on master help sign in
Add common Serialize deriving tests
dbf9fe8Adithya Kumar
test/Streamly/Test/Data/Unbox.hs+70−8
@@ -20,6 +20,9 @@ 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
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(..))
29
32
33+#ifdef USE_SERIALIZE
34+
35+import Streamly.Internal.Data.Serialize (Serialize(..), Size(..))
36+import Streamly.Internal.Data.Serialize.TH
37+
38+#else
39+
30#ifdef USE_TH
40#ifdef USE_TH
31import Streamly.Internal.Data.Unbox.TH
41import Streamly.Internal.Data.Unbox.TH
32#endif
42#endif
33
43
44+#endif
45+
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
43− , pokeByteIndex
44 )
55 )
45
56
46import Test.Hspec as H
57import Test.Hspec as H
@@ -49,12 +60,32 @@ import Test.Hspec as H
49-- Types
60-- Types
50--------------------------------------------------------------------------------
61--------------------------------------------------------------------------------
51
62
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+
52#ifdef USE_TH
77#ifdef USE_TH
78+
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)
81+
55#else
82#else
83+
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)
86+
87+#endif
88+
58#endif
89#endif
59
90
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)
134
165
166+#if defined(USE_SERIALIZE)
167+$(deriveSerialize ''Complex)
168+$(deriveSerialize ''Ratio)
169+$(deriveSerializeWith ["a"] ''Const)
170+$(deriveSerialize ''Identity)
171+#endif
172+
135--------------------------------------------------------------------------------
173--------------------------------------------------------------------------------
136-- Test helpers
174-- Test helpers
137--------------------------------------------------------------------------------
175--------------------------------------------------------------------------------
138
176
139testSerialization ::
177testSerialization ::
140− forall a. (Eq a, Show a, Unbox a)
178+ forall a. (Eq a, Show a, TYPE_CLASS a)
141 => a
179 => a
142 -> IO ()
180 -> IO ()
143testSerialization val = do
181testSerialization val = do
182+ arr <- newBytes
183+#ifdef USE_SERIALIZE
184+ (case size :: Size a of
185+ ConstSize x -> x
186+ VarSize f -> f val)
187+#else
144− arr <- newBytes (sizeOf (Proxy :: Proxy a))
188+ (sizeOf (Proxy :: Proxy a))
189+#endif
145− pokeByteIndex 0 arr val
190+ POKE(0, arr, val)
146− peekByteIndex 0 arr `shouldReturn` val
191+ PEEK(0, arr) `shouldReturn` val
147
192
148testGenericConsistency ::
193testGenericConsistency ::
149 forall a.
194 forall a.
150 ( Eq a
195 ( Eq a
151 , Show a
196 , Show a
197+#ifdef USE_SERIALIZE
198+ , Serialize a, Unbox a
199+#else
152 , Unbox a
200 , Unbox a
201+#endif
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
161
210
162 -- Test the generic sizeOf
211 -- Test the generic sizeOf
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
163− sizeOf (Proxy :: Proxy a) `shouldBe` genericSizeOf (Proxy :: Proxy a)
219+ `shouldBe` genericSizeOf (Proxy :: Proxy a)
164
220
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))
167
223
168− pokeByteIndex 0 arr val
224+ POKE(0, arr, val)
169 genericPeekByteIndex arr 0 `shouldReturn` val
225 genericPeekByteIndex arr 0 `shouldReturn` val
170
226
171 genericPokeByteIndex arr 0 val
227 genericPokeByteIndex arr 0 val
172− peekByteIndex 0 arr `shouldReturn` val
228+ PEEK(0, arr) `shouldReturn` val
229+
173
230
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
177
235
236+#endif
237+
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)
206
266
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)
279+#endif
218
280
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)
test/streamly-tests.cabal+7
@@ -276,6 +276,13 @@ test-suite Data.Unbox.Derive.TH
276 main-is: Streamly/Test/Data/Unbox.hs
276 main-is: Streamly/Test/Data/Unbox.hs
277 ghc-options: -main-is Streamly.Test.Data.Unbox.main
277 ghc-options: -main-is Streamly.Test.Data.Unbox.main
278
278
279+test-suite Data.Serialize.Derive.TH
280+ import: test-options
281+ type: exitcode-stdio-1.0
282+ cpp-options: -DUSE_SERIALIZE
283+ main-is: Streamly/Test/Data/Unbox.hs
284+ ghc-options: -main-is Streamly.Test.Data.Unbox.main
285+
279test-suite Data.Unbox.TH
286test-suite Data.Unbox.TH
280 import: test-options
287 import: test-options
281 type: exitcode-stdio-1.0
288 type: exitcode-stdio-1.0