1{-# LANGUAGE StandaloneDeriving #-}
2{-# LANGUAGE DeriveAnyClass #-}
3{-# LANGUAGE TemplateHaskell #-}
5-- This module has a lot of orphan instances as we are deriving it here. We can
7{-# OPTIONS_GHC -Wno-orphans #-}
10-- Module : Streamly.Test.Data.Unbox
11-- Copyright : (c) 2022 Composewell technologies
12-- License : BSD-3-Clause
13-- Maintainer : streamly@composewell.com
14-- Stability : experimental
17module Streamly.Test.Data.Unbox (main) where
19--------------------------------------------------------------------------------
21--------------------------------------------------------------------------------
24import Control.Monad (void)
26import Data.Complex (Complex ((:+)))
27import Data.Functor.Const (Const (..))
28import Data.Functor.Identity (Identity (..))
29import Data.Proxy (Proxy(..))
30import GHC.Generics (Generic, Rep(..))
31import GHC.Real (Ratio(..))
35import Streamly.Internal.Data.Serialize (Serialize(..), Size(..))
36import Streamly.Internal.Data.Serialize.TH
41import Streamly.Internal.Data.Unbox.TH
46import Streamly.Internal.Data.Unbox
51 , genericPeekByteIndex
52 , genericPokeByteIndex
59--------------------------------------------------------------------------------
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
79#define MODULE_NAME "Data.Unbox.Deriving.TH"
80#define DERIVE_UNBOX(typ) $(deriveUnbox ''typ)
84#define MODULE_NAME "Data.Unbox.Deriving.Generic"
85#define DERIVE_UNBOX(typ) deriving instance Unbox (typ)
91--------------------------------------------------------------------------------
93--------------------------------------------------------------------------------
95-- Unit instance uses a hack, so test all cases
98 deriving (Show, Generic, Eq)
104 deriving (Show, Generic, Eq)
105DERIVE_UNBOX(UnarySum)
111 deriving (Generic, Eq, Show)
112DERIVE_UNBOX(UnarySum2)
116 deriving (Generic, Eq, Show)
121 deriving (Generic, Eq, Show)
126 deriving (Generic, Eq, Show)
131 deriving (Generic, Eq, Show)
134{-# ANN Single "HLint: ignore" #-}
137 deriving (Show, Generic, Eq)
142 deriving (Show, Generic, Eq)
143DERIVE_UNBOX(Product2)
150 deriving (Show, Generic, Eq)
151DERIVE_UNBOX(SumOfProducts)
154 = NSOP0 SumOfProducts
155 | NSOP1 SumOfProducts
156 deriving (Show, Generic, Eq)
157DERIVE_UNBOX(NestedSOP)
159--------------------------------------------------------------------------------
160-- Standalone derivations
161--------------------------------------------------------------------------------
163-- Ratio does not have a Generic instance by default
164deriving instance Generic (Ratio Int)
166#if defined(USE_SERIALIZE)
167$(deriveSerialize ''Complex)
168$(deriveSerialize ''Ratio)
169$(deriveSerializeWith ["a"] ''Const)
170$(deriveSerialize ''Identity)
173--------------------------------------------------------------------------------
175--------------------------------------------------------------------------------
178 forall a. (Eq a, Show a, TYPE_CLASS a)
181testSerialization val = do
184 (case size :: Size a of
188 (sizeOf (Proxy :: Proxy a))
191 PEEK(0, arr) `shouldReturn` val
193testGenericConsistency ::
198 , Serialize a, Unbox a
209testGenericConsistency val = do
211 -- Test the generic sizeOf
213 (case size :: Size a of
217 sizeOf (Proxy :: Proxy a)
219 `shouldBe` genericSizeOf (Proxy :: Proxy a)
221 -- Test the serialization and deserialization
222 arr <- newBytes (sizeOf (Proxy :: Proxy a))
225 genericPeekByteIndex arr 0 `shouldReturn` val
227 genericPokeByteIndex arr 0 val
228 PEEK(0, arr) `shouldReturn` val
232-- Size is also implicitly tested while serializing and deserializing.
233checkSizeOf :: forall a. Unbox a => Proxy a -> Int -> IO ()
234checkSizeOf _ size = sizeOf (Proxy :: Proxy a) `shouldBe` size
238--------------------------------------------------------------------------------
240--------------------------------------------------------------------------------
242#define CHECK_SIZE(type, expectation) \
243 it "checkSizeOf type" $ checkSizeOf (Proxy :: Proxy type) expectation
245--------------------------------------------------------------------------------
247--------------------------------------------------------------------------------
251 it "Unit" $ testSerialization Unit
252 it "Unit1" $ testSerialization (Unit1 Unit)
253 it "Unit2" $ testSerialization (Unit2 Unit Unit)
254 it "Unit3" $ testSerialization (Unit3 1234 Unit 4567)
255 it "Unit4" $ testSerialization (Unit4 1234 (Unit1 Unit) 4567)
256 it "UnarySum Sum1" $ testSerialization Sum1
257 it "UnarySum Sum2" $ testSerialization Sum2
258 it "UnarySum2 UnitSum1" $ testSerialization (UnitSum1 Unit)
259 it "UnarySum2 UnitSum2" $ testSerialization (UnitSum2 Unit)
260 it "Single" $ testSerialization (Single 2)
261 it "Product2" $ testSerialization (Product2 2 'b')
262 it "SumOfProducts SOP0" $ testSerialization SOP0
263 it "SumOfProducts SOP1" $ testSerialization (SOP1 1)
264 it "SumOfProducts SOP2" $ testSerialization (SOP2 1 'a')
265 it "SumOfProducts SOP3" $ testSerialization (SOP3 1 2 3)
271 CHECK_SIZE(Unit3, 17)
272 CHECK_SIZE(Unit4, 17)
273 CHECK_SIZE(UnarySum, 1)
274 CHECK_SIZE(UnarySum2, 2)
275 CHECK_SIZE(Single, 8)
276 CHECK_SIZE(Product2, 12)
277 CHECK_SIZE(SumOfProducts, 25)
278 CHECK_SIZE(NestedSOP, 26)
281 it "Bool" $ testSerialization True
282 it "Complex Int" $ testSerialization (5 :+ 3 :: Complex Int)
283 it "Ratio Int" $ testSerialization (5 :% 3 :: Ratio Int)
284 it "Const Float Int" $ testSerialization (Const 333.5678 :: Const Float Int)
285 it "Identity Int" $ testSerialization (Identity 56760 :: Identity Int)
287 it "GenericConsistency Bool" $ testGenericConsistency True
288 it "GenericConsistency (Complex Int)"
289 $ testGenericConsistency (5 :+ 3 :: Complex Int)
290 it "GenericConsistency (Ratio Int)"
291 $ testGenericConsistency (5 :% 3 :: Ratio Int)
292 it "GenericConsistency (Const Float Int)"
293 $ testGenericConsistency (Const 333.5678 :: Const Float Int)
294 it "GenericConsistency (Identity Int)"
295 $ testGenericConsistency (Identity 56760 :: Identity Int)
297 -- Fingerprint does not work for GHC 8.6.5
298 -- it "Fingerprint" $ testSerialization (Fingerprint 123456 876588)
299 -- it "GenericConsistency Fingerprint"
300 -- $ testGenericConsistency (Fingerprint 123456 876588)
302--------------------------------------------------------------------------------
304--------------------------------------------------------------------------------
307moduleName = MODULE_NAME
310main = hspec $ H.parallel $ describe moduleName testCases
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list