on master help sign in
Add test cases for Unbox Generic derived instances (#2207)

Co-authored-by: Harendra Kumar <harendra@composewell.com>

commitdeb16f8670a4226f58177d8b1f0d6d840bff4dcd authorRanjeet Ranjan <ranjeetr@composewell.com> dateSat 7 Jan 2023, 19:05 (4 years ago) parent8adf0c3
5 files changed+91−2
.hlint.ignore+1 hie.yaml+2−2 streamly.cabal+1 test/Streamly/Test/Data/Unbox.hs+79 test/streamly-tests.cabal+8
.hlint.ignore+1
@@ -13,6 +13,7 @@ test/Streamly/Test/Prelude/Fold.hs
1313test/Streamly/Test/Prelude/Rate.hs
1414test/Streamly/Test/Prelude/Serial.hs
1515test/Streamly/Test/Unicode/Stream.hs
16+test/Streamly/Test/Data/Unbox.hs
1617benchmark/lib/Streamly/Benchmark/Common.hs
1718benchmark/lib/Streamly/Benchmark/Common/Handle.hs
1819benchmark/lib/Streamly/Benchmark/Prelude.hs
hie.yaml+2−2
@@ -84,8 +84,8 @@ cradle:
8484 cabal:
8585 - path: "./test/Streamly/Test/Data/Array.hs"
8686 component: "test:Data.Array"
87− - path: "./test/Streamly/Test/Data/Array.hs"
88− component: "test:Data.Array"
87+ - path: "./test/Streamly/Test/Data/Unbox.hs"
88+ component: "test:Data.Unbox"
8989 - path: "./test/Streamly/Test/Data/Array/Mut.hs"
9090 component: "test:Data.Array.Mut"
9191 - path: "./test/Streamly/Test/Data/Array/Stream.hs"
streamly.cabal+1
@@ -118,6 +118,7 @@ extra-source-files:
118118 test/version-bounds.hs
119119 test/Streamly/Test/Unicode/ucd/NormalizationTest.txt
120120 test/Streamly/Test/Unicode/extra/NormalizationTest.txt
121+ test/Streamly/Test/Data/Unbox.hs
121122 benchmark/Streamly/Benchmark/Unicode/data/AllChars.txt
122123 benchmark/Streamly/Benchmark/Unicode/data/Devanagari.txt
123124 benchmark/Streamly/Benchmark/Unicode/data/Japanese.txt
test/Streamly/Test/Data/Unbox.hsnew file+79
@@ -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
test/streamly-tests.cabal+8
@@ -253,6 +253,14 @@ test-suite Data.Ring.Unboxed
253253 if flag(use-streamly-core)
254254 buildable: False
255255
256+test-suite Data.Unbox
257+ import: test-options
258+ type: exitcode-stdio-1.0
259+ main-is: Streamly/Test/Data/Unbox.hs
260+ ghc-options: -main-is Streamly.Test.Data.Unbox.main
261+ if flag(use-streamly-core)
262+ buildable: False
263+
256264test-suite Data.Array.Stream
257265 import: test-options
258266 type: exitcode-stdio-1.0