1{-# LANGUAGE TemplateHaskell #-}
4-- Module : Streamly.Test.Data.Unbox.TH
5-- Copyright : (c) 2022 Composewell technologies
6-- License : BSD-3-Clause
7-- Maintainer : streamly@composewell.com
8-- Stability : experimental
11module Streamly.Test.Data.Unbox.TH (main) where
13--------------------------------------------------------------------------------
15--------------------------------------------------------------------------------
17import Data.Proxy (Proxy(..))
18import Streamly.Internal.Data.Unbox.TH
19import Streamly.Internal.Data.Unbox (Unbox(..), newBytes, pokeByteIndex)
23--------------------------------------------------------------------------------
25--------------------------------------------------------------------------------
28 forall a. (Eq a, Show a, Unbox a)
31testSerialization val = do
32 arr <- newBytes (sizeOf (Proxy :: Proxy a))
33 pokeByteIndex 0 arr val
34 peekByteIndex 0 arr `shouldReturn` val
36-- Size is also implicitly tested while serializing and deserializing.
37checkSizeOf :: forall a. Unbox a => Proxy a -> Int -> IO ()
38checkSizeOf _ size = sizeOf (Proxy :: Proxy a) `shouldBe` size
40checkSizeOfNew :: forall a. Unbox a => String -> Proxy a -> Int -> Spec
41checkSizeOfNew tag proxy expectation =
42 it ("checkSizeOf " ++ tag) $ checkSizeOf proxy expectation
54data CustomDataType3 a b c
56 | CDT3Constructor2 a b
57 | CDT3Constructor3 a b c
60data CustomDataType4 a b
62 | CDT4Constructor2 Bool
63 | CDT4Constructor3 Bool b
66$(deriveUnbox ''CustomDataType1)
67$(deriveUnbox ''CustomDataType2)
68$(deriveUnbox ''CustomDataType3)
69$(deriveUnboxWith ["b"] ''CustomDataType4)
71--------------------------------------------------------------------------------
73--------------------------------------------------------------------------------
75#define CHECK_SIZE(type, expectation) \
76 it "checkSizeOf type" $ checkSizeOf (Proxy :: Proxy type) expectation
78--------------------------------------------------------------------------------
80--------------------------------------------------------------------------------
82testCustomDatatype1TH :: Spec
83testCustomDatatype1TH = do
84 it "CustomDataType1" $ testSerialization CustomDataType1
86 CHECK_SIZE(CustomDataType1, 1)
88testCustomDatatype2TH :: Spec
89testCustomDatatype2TH = do
90 it "CustomDataType2 1" $ testSerialization CDT2Constructor1
91 it "CustomDataType2 2" $ testSerialization CDT2Constructor2
92 it "CustomDataType2 3" $ testSerialization CDT2Constructor3
94 CHECK_SIZE(CustomDataType2, 1)
96testCustomDatatype3TH :: Spec
97testCustomDatatype3TH = do
98 it "CustomDataType3 1"
100 (CDT3Constructor1 3 :: CustomDataType3 Int Bool Double)
101 it "CustomDataType3 2"
103 (CDT3Constructor2 3 False :: CustomDataType3 Int Bool Double)
104 it "CustomDataType3 3"
105 $ testSerialization (CDT3Constructor3 (3 :: Int) False (5 :: Double))
109 (Proxy :: Proxy (CustomDataType3 Int Bool Double))
110 (1 + sizeOf (Proxy :: Proxy Int)
111 + sizeOf (Proxy :: Proxy Bool)
112 + sizeOf (Proxy :: Proxy Double))
114testCustomDatatype4TH :: Spec
115testCustomDatatype4TH = do
116 it "CustomDataType4 1"
117 $ testSerialization (CDT4Constructor1 :: CustomDataType4 a Int)
118 it "CustomDataType4 2"
120 (CDT4Constructor2 True :: CustomDataType4 a Int)
121 it "CustomDataType4 3"
122 $ testSerialization (CDT4Constructor3 False (5 :: Int))
126 (Proxy :: Proxy (CustomDataType4 a Int))
127 (1 + sizeOf (Proxy :: Proxy Bool) + sizeOf (Proxy :: Proxy Int))
132 testCustomDatatype1TH
133 testCustomDatatype2TH
134 testCustomDatatype3TH
135 testCustomDatatype4TH
137--------------------------------------------------------------------------------
139--------------------------------------------------------------------------------
142moduleName = "Data.Unboxed.TH"
145main = 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