on master help sign in
Abstract the Unbox deriving process in Unbox tests
2241afaAdithya Kumar
test/Streamly/Test/Data/Unbox.hs+45−12
@@ -26,6 +26,13 @@ import Data.Functor.Identity (Identity (..))
26import Data.Proxy (Proxy(..))
26import Data.Proxy (Proxy(..))
27import GHC.Generics (Generic, Rep(..))
27import GHC.Generics (Generic, Rep(..))
28import GHC.Real (Ratio(..))
28import GHC.Real (Ratio(..))
29+
30+{-
31+XXX Will be added in the future
32+import Streamly.Internal.Data.Unbox.TH
33+import Language.Haskell.TH
34+-}
35+
29import Streamly.Internal.Data.Unbox
36import Streamly.Internal.Data.Unbox
30 ( PeekRep(..)
37 ( PeekRep(..)
31 , PokeRep(..)
38 , PokeRep(..)
@@ -44,57 +51,83 @@ import Test.Hspec as H
44-- Types
51-- Types
45--------------------------------------------------------------------------------
52--------------------------------------------------------------------------------
46
53
54+-- #define USE_TH
55+
56+#ifdef USE_TH
57+#define MODULE_NAME "Data.Unbox.Deriving.TH"
58+#define DERIVE_UNBOX(typ) $(makeUnbox ''typ)
59+#else
60+#define MODULE_NAME "Data.Unbox.Deriving.Generic"
61+#define DERIVE_UNBOX(typ) deriving instance Unbox (typ)
62+#endif
63+
64+--------------------------------------------------------------------------------
65+-- Types
66+--------------------------------------------------------------------------------
67+
47-- Unit instance uses a hack, so test all cases
68-- Unit instance uses a hack, so test all cases
48data Unit =
69data Unit =
49 Unit
70 Unit
50− deriving (Unbox, Show, Generic, Eq)
71+ deriving (Show, Generic, Eq)
72+DERIVE_UNBOX(Unit)
51
73
52data UnarySum
74data UnarySum
53 = Sum1
75 = Sum1
54 | Sum2
76 | Sum2
55− deriving (Unbox, Show, Generic, Eq)
77+ deriving (Show, Generic, Eq)
78+DERIVE_UNBOX(UnarySum)
79+
56
80
57data UnarySum2
81data UnarySum2
58 = UnitSum1 Unit
82 = UnitSum1 Unit
59 | UnitSum2 Unit
83 | UnitSum2 Unit
60− deriving (Generic, Unbox, Eq, Show)
84+ deriving (Generic, Eq, Show)
85+DERIVE_UNBOX(UnarySum2)
61
86
62data Unit1 =
87data Unit1 =
63 Unit1 Unit
88 Unit1 Unit
64− deriving (Generic, Unbox, Eq, Show)
89+ deriving (Generic, Eq, Show)
90+DERIVE_UNBOX(Unit1)
65
91
66data Unit2 =
92data Unit2 =
67 Unit2 Unit Unit
93 Unit2 Unit Unit
68− deriving (Generic, Unbox, Eq, Show)
94+ deriving (Generic, Eq, Show)
95+DERIVE_UNBOX(Unit2)
69
96
70data Unit3 =
97data Unit3 =
71 Unit3 Int Unit Int
98 Unit3 Int Unit Int
72− deriving (Generic, Unbox, Eq, Show)
99+ deriving (Generic, Eq, Show)
100+DERIVE_UNBOX(Unit3)
73
101
74data Unit4 =
102data Unit4 =
75 Unit4 Int Unit1 Int
103 Unit4 Int Unit1 Int
76− deriving (Generic, Unbox, Eq, Show)
104+ deriving (Generic, Eq, Show)
105+DERIVE_UNBOX(Unit4)
77
106
78{-# ANN Single "HLint: ignore" #-}
107{-# ANN Single "HLint: ignore" #-}
79data Single =
108data Single =
80 Single Int
109 Single Int
81− deriving (Unbox, Show, Generic, Eq)
110+ deriving (Show, Generic, Eq)
111+DERIVE_UNBOX(Single)
82
112
83data Product2 =
113data Product2 =
84 Product2 Int Char
114 Product2 Int Char
85− deriving (Unbox, Show, Generic, Eq)
115+ deriving (Show, Generic, Eq)
116+DERIVE_UNBOX(Product2)
86
117
87data SumOfProducts
118data SumOfProducts
88 = SOP0
119 = SOP0
89 | SOP1 Int
120 | SOP1 Int
90 | SOP2 Int Char
121 | SOP2 Int Char
91 | SOP3 Int Int Int
122 | SOP3 Int Int Int
92− deriving (Unbox, Show, Generic, Eq)
123+ deriving (Show, Generic, Eq)
124+DERIVE_UNBOX(SumOfProducts)
93
125
94data NestedSOP
126data NestedSOP
95 = NSOP0 SumOfProducts
127 = NSOP0 SumOfProducts
96 | NSOP1 SumOfProducts
128 | NSOP1 SumOfProducts
97− deriving (Unbox, Show, Generic, Eq)
129+ deriving (Show, Generic, Eq)
130+DERIVE_UNBOX(NestedSOP)
98
131
99--------------------------------------------------------------------------------
132--------------------------------------------------------------------------------
100-- Standalone derivations
133-- Standalone derivations
@@ -213,7 +246,7 @@ testCases = do
213--------------------------------------------------------------------------------
246--------------------------------------------------------------------------------
214
247
215moduleName :: String
248moduleName :: String
216−moduleName = "Data.Unboxed"
249+moduleName = MODULE_NAME
217
250
218main :: IO ()
251main :: IO ()
219main = hspec $ H.parallel $ describe moduleName testCases
252main = hspec $ H.parallel $ describe moduleName testCases