on a819eb7 help sign in
125 lines 3.5 kB history raw
1{-# LANGUAGE TemplateHaskell #-}
3-- |
4-- Module : Streamly.Test.Data.Serialize
5-- Copyright : (c) 2022 Composewell technologies
6-- License : BSD-3-Clause
7-- Maintainer : streamly@composewell.com
8-- Stability : experimental
9-- Portability : GHC
11module Streamly.Test.Data.Serialize (main) where
13--------------------------------------------------------------------------------
14-- Imports
15--------------------------------------------------------------------------------
17import Streamly.Internal.Data.Unbox (newBytes)
18import GHC.Generics (Generic)
19import Streamly.Test.Data.Serialize.TH (genDatatype)
20import Streamly.Internal.Data.Serialize.TH (deriveSerialize)
22import qualified Streamly.Internal.Data.Serialize as Serialize
24import Test.Hspec.QuickCheck
25import Test.QuickCheck
26import Test.Hspec as H
28--------------------------------------------------------------------------------
29-- Generated types
30--------------------------------------------------------------------------------
32$(genDatatype "CustomDatatype" 15)
33$(deriveSerialize ''CustomDatatype)
35--------------------------------------------------------------------------------
36-- Recursive type
37--------------------------------------------------------------------------------
39-- Recursive ADT
40data BinTree a
41 = Tree (BinTree a) (BinTree a)
42 | Leaf a
43 deriving (Show, Read, Eq, Generic)
45$(deriveSerialize ''BinTree)
47instance Arbitrary a => Arbitrary (BinTree a) where
48 arbitrary = oneof [Leaf <$> arbitrary, Tree <$> arbitrary <*> arbitrary]
50--------------------------------------------------------------------------------
51-- Test helpers
52--------------------------------------------------------------------------------
54roundtrip
55 :: forall a. (Eq a, Show a, Serialize.Serialize a)
56 => a
57 -> IO ()
58roundtrip val = do
60 let sz =
61 case Serialize.size :: Serialize.Size a of
62 Serialize.VarSize f -> f val
63 Serialize.ConstSize csz -> csz
65 arr <- newBytes sz
67 off1 <- Serialize.serialize 0 arr val
68 (off2, val2) <- Serialize.deserialize 0 arr
69 val2 `shouldBe` val
70 off2 `shouldBe` off1
72testSerializeList
73 :: forall a. (Eq a, Show a, Serialize.Serialize a)
74 => Int
75 -> a
76 -> IO ()
77testSerializeList sizeOfA val = do
79 let sz =
80 case Serialize.size :: Serialize.Size a of
81 Serialize.VarSize f -> f val
82 Serialize.ConstSize csz -> csz
84 sz `shouldBe` sizeOfA
86 roundtrip val
88--------------------------------------------------------------------------------
89-- Tests
90--------------------------------------------------------------------------------
92testCases :: Spec
93testCases = do
95 it "Serialize [Int]"
96 $ testSerializeList (8 + 4 * 8) ([1, 2, 3, 4] :: [Int])
97 it "Serialize [[Int]]"
98 $ testSerializeList
99 (8 + 3 * 8 + 6 * 8)
100 ([[1], [1, 2], [1, 2, 3]] :: [[Int]])
102 limitQC
103 $ prop "CustomDatatype"
104 $ \(x :: CustomDatatype) -> roundtrip x
106 limitQC
107 $ prop "[CustomDatatype]"
108 $ \(x :: [CustomDatatype]) -> roundtrip x
110 limitQC
111 $ prop "BinTree" $ \(x :: BinTree Int) -> roundtrip x
113 where
115 limitQC = modifyMaxSize (const 50)
117--------------------------------------------------------------------------------
118-- Main function
119--------------------------------------------------------------------------------
121moduleName :: String
122moduleName = "Data.Serialize"
124main :: IO ()
125main = hspec $ H.parallel $ describe moduleName testCases