on master help sign in
Add test cases for Unbox Generic derived instances (#2207)
deb16f8Ranjeet Ranjan
.hlint.ignore+1
@@ -13,6 +13,7 @@ test/Streamly/Test/Prelude/Fold.hs
13test/Streamly/Test/Prelude/Rate.hs
13test/Streamly/Test/Prelude/Rate.hs
14test/Streamly/Test/Prelude/Serial.hs
14test/Streamly/Test/Prelude/Serial.hs
15test/Streamly/Test/Unicode/Stream.hs
15test/Streamly/Test/Unicode/Stream.hs
16+test/Streamly/Test/Data/Unbox.hs
16benchmark/lib/Streamly/Benchmark/Common.hs
17benchmark/lib/Streamly/Benchmark/Common.hs
17benchmark/lib/Streamly/Benchmark/Common/Handle.hs
18benchmark/lib/Streamly/Benchmark/Common/Handle.hs
18benchmark/lib/Streamly/Benchmark/Prelude.hs
19benchmark/lib/Streamly/Benchmark/Prelude.hs
hie.yaml+2−2
@@ -84,8 +84,8 @@ cradle:
84 cabal:
84 cabal:
85 - path: "./test/Streamly/Test/Data/Array.hs"
85 - path: "./test/Streamly/Test/Data/Array.hs"
86 component: "test:Data.Array"
86 component: "test:Data.Array"
87− - path: "./test/Streamly/Test/Data/Array.hs"
87+ - path: "./test/Streamly/Test/Data/Unbox.hs"
88− component: "test:Data.Array"
88+ component: "test:Data.Unbox"
89 - path: "./test/Streamly/Test/Data/Array/Mut.hs"
89 - path: "./test/Streamly/Test/Data/Array/Mut.hs"
90 component: "test:Data.Array.Mut"
90 component: "test:Data.Array.Mut"
91 - path: "./test/Streamly/Test/Data/Array/Stream.hs"
91 - path: "./test/Streamly/Test/Data/Array/Stream.hs"
streamly.cabal+1
@@ -118,6 +118,7 @@ extra-source-files:
118 test/version-bounds.hs
118 test/version-bounds.hs
119 test/Streamly/Test/Unicode/ucd/NormalizationTest.txt
119 test/Streamly/Test/Unicode/ucd/NormalizationTest.txt
120 test/Streamly/Test/Unicode/extra/NormalizationTest.txt
120 test/Streamly/Test/Unicode/extra/NormalizationTest.txt
121+ test/Streamly/Test/Data/Unbox.hs
121 benchmark/Streamly/Benchmark/Unicode/data/AllChars.txt
122 benchmark/Streamly/Benchmark/Unicode/data/AllChars.txt
122 benchmark/Streamly/Benchmark/Unicode/data/Devanagari.txt
123 benchmark/Streamly/Benchmark/Unicode/data/Devanagari.txt
123 benchmark/Streamly/Benchmark/Unicode/data/Japanese.txt
124 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
253 if flag(use-streamly-core)
253 if flag(use-streamly-core)
254 buildable: False
254 buildable: False
255
255
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+
256test-suite Data.Array.Stream
264test-suite Data.Array.Stream
257 import: test-options
265 import: test-options
258 type: exitcode-stdio-1.0
266 type: exitcode-stdio-1.0