on c4794aa help sign in
102 lines 2.9 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)
19import Test.Hspec as H
21import Streamly.Internal.Data.Serialize.TH (deriveSerialize)
22-- import Streamly.Internal.Data.Serialize (Serialize(..))
24import qualified Streamly.Internal.Data.Serialize as Serialize
26import Streamly.Test.Data.Serialize.TH (genDatatype)
28import Test.Hspec.QuickCheck
30--------------------------------------------------------------------------------
31-- Generated types
32--------------------------------------------------------------------------------
34$(genDatatype "CustomDatatype" 15)
35$(deriveSerialize ''CustomDatatype)
37--------------------------------------------------------------------------------
38-- Test helpers
39--------------------------------------------------------------------------------
41roundtrip
42 :: forall a. (Eq a, Show a, Serialize.Serialize a)
43 => a
44 -> IO ()
45roundtrip val = do
47 let sz =
48 case Serialize.size :: Serialize.Size a of
49 Serialize.VarSize f -> f val
50 Serialize.ConstSize csz -> csz
52 arr <- newBytes sz
54 off1 <- Serialize.serialize 0 arr val
55 (off2, val2) <- Serialize.deserialize 0 arr
56 val2 `shouldBe` val
57 off2 `shouldBe` off1
59testSerializeList
60 :: forall a. (Eq a, Show a, Serialize.Serialize a)
61 => Int
62 -> a
63 -> IO ()
64testSerializeList sizeOfA val = do
66 let sz =
67 case Serialize.size :: Serialize.Size a of
68 Serialize.VarSize f -> f val
69 Serialize.ConstSize csz -> csz
71 sz `shouldBe` sizeOfA
73 roundtrip val
75--------------------------------------------------------------------------------
76-- Tests
77--------------------------------------------------------------------------------
79testCases :: Spec
80testCases = do
82 it "Serialize [Int]"
83 $ testSerializeList (8 + 4 * 8) ([1, 2, 3, 4] :: [Int])
84 it "Serialize [[Int]]"
85 $ testSerializeList
86 (8 + 3 * 8 + 6 * 8)
87 ([[1], [1, 2], [1, 2, 3]] :: [[Int]])
89 prop "CustomDatatype"
90 $ \(x :: CustomDatatype) -> roundtrip x
91 prop "[CustomDatatype]"
92 $ \(x :: [CustomDatatype]) -> roundtrip x
94--------------------------------------------------------------------------------
95-- Main function
96--------------------------------------------------------------------------------
98moduleName :: String
99moduleName = "Data.Unboxed"
101main :: IO ()
102main = hspec $ H.parallel $ describe moduleName testCases