@@ -0,0 +1,79 @@
1+{-# LANGUAGE DeriveAnyClass #-}
2+
3+-- |
4+-- Module : Streamly.Test.Data.Unbox
5+-- Copyright : (c) 2022 Composewell technologies
6+-- License : BSD-3-Clause
7+-- Maintainer : streamly@composewell.com
8+-- Stability : experimental
9+-- Portability : GHC
10+
11+module Streamly.Test.Data.Unbox (main) where
12+
13+import Data.Proxy (Proxy(..))
14+import GHC.Generics (Generic)
15+import Streamly.Internal.Data.Unboxed (Unbox(..))
16+import qualified Streamly.Internal.Data.IORef.Unboxed as IORef
17+
18+import Test.Hspec as H
19+import Test.Hspec.QuickCheck
20+import Test.QuickCheck (Property)
21+import Test.QuickCheck.Monadic (monadicIO, assert, run)
22+
23+data Unit = Unit deriving (Unbox, Show, Generic, Eq)
24+
25+{-# ANN Single "HLint: ignore" #-}
26+data Single = Single Int deriving (Unbox, Show, Generic, Eq)
27+
28+data Product2 = Product2 Int Char deriving (Unbox, Show, Generic, Eq)
29+
30+data SumOfProducts =
31+ SOP0
32+ | SOP1 Int
33+ | SOP2 Int Char
34+ | SOP3 Int Int Int deriving (Unbox, Show, Generic, Eq)
35+
36+data NestedSOP =
37+ NSOP0 SumOfProducts
38+ | NSOP1 SumOfProducts deriving (Unbox, Show, Generic, Eq)
39+
40+test ::(Unbox a, Eq a) => a -> a -> Property
41+test v1 v2 = monadicIO $ do
42+ ref <- run $ IORef.newIORef v1
43+ x1 <- run $ IORef.readIORef ref
44+ run $ IORef.writeIORef ref v2
45+ x2 <- run $ IORef.readIORef ref
46+ assert (x2 == v2)
47+ assert (x1 == v1)
48+
49+checkSizeOf :: forall a. Unbox a => Proxy a -> Int -> Property
50+checkSizeOf _ size = monadicIO $
51+ assert (sizeOf (undefined :: a) == size)
52+
53+moduleName :: String
54+moduleName = "Data.Unbox"
55+
56+tests :: Spec
57+tests = do
58+ prop "test Unit Unit" $ test Unit Unit
59+ prop "(Single 0) (Single 1)" $ test (Single 0) (Single 1)
60+ prop "(Product2 0 'a') (Product2 1 'b')"
61+ $ test (Product2 0 'a') (Product2 1 'b')
62+ prop "(SOP0) (SOP1 1)" $ test SOP0 (SOP1 1)
63+ prop "(SOP0) (SOP2 1 'a')" $ test SOP0 (SOP2 1 'a')
64+ prop "(SOP0) (SOP3 1 2 3)" $ test SOP0 (SOP3 1 2 3)
65+
66+ prop "checkSizeOf Unit" $ checkSizeOf (Proxy :: Proxy Unit) 1
67+ prop "checkSizeOf Single" $ checkSizeOf (Proxy :: Proxy Single) 8
68+ prop "checkSizeOf Product2" $ checkSizeOf (Proxy :: Proxy Product2) 12
69+ prop "checkSizeOf SumOfProducts"
70+ $ checkSizeOf (Proxy :: Proxy SumOfProducts) 25
71+ prop "checkSizeOf NestedSOP" $ checkSizeOf (Proxy :: Proxy NestedSOP) 26
72+
73+main :: IO ()
74+main =
75+ hspec $
76+ H.parallel $
77+ modifyMaxSuccess (const 1) $ do
78+ describe moduleName $ do
79+ describe "Generic Unboxed Type" tests