on master help sign in
Move the Serialize tests into its own module
1339893Adithya Kumar
test/Streamly/Test/Data/Serialize.hsnew file+68
@@ -0,0 +1,68 @@
1+-- |
2+-- Module : Streamly.Test.Data.Serialize
3+-- Copyright : (c) 2022 Composewell technologies
4+-- License : BSD-3-Clause
5+-- Maintainer : streamly@composewell.com
6+-- Stability : experimental
7+-- Portability : GHC
8+
9+module Streamly.Test.Data.Serialize (main) where
10+
11+--------------------------------------------------------------------------------
12+-- Imports
13+--------------------------------------------------------------------------------
14+
15+import Streamly.Internal.Data.Unbox (newBytes)
16+
17+import Test.Hspec as H
18+
19+import qualified Streamly.Internal.Data.Serialize as Serialize
20+
21+--------------------------------------------------------------------------------
22+-- Test helpers
23+--------------------------------------------------------------------------------
24+
25+testSerializeList
26+ :: forall a. (Eq a, Show a, Serialize.Serialize a)
27+ => Int
28+ -> a
29+ -> IO ()
30+testSerializeList sizeOfA val = do
31+
32+ let sz =
33+ case Serialize.size :: Serialize.Size a of
34+ Serialize.VarSize f -> f val
35+ Serialize.ConstSize csz -> csz
36+
37+ sz `shouldBe` sizeOfA
38+
39+ arr <- newBytes sz
40+
41+ off1 <- Serialize.serialize 0 arr val
42+ (off2, val2) <- Serialize.deserialize 0 arr
43+ val2 `shouldBe` val
44+ off2 `shouldBe` off1
45+
46+--------------------------------------------------------------------------------
47+-- Tests
48+--------------------------------------------------------------------------------
49+
50+testCases :: Spec
51+testCases = do
52+
53+ it "Serialize [Int]"
54+ $ testSerializeList (8 + 4 * 8) ([1, 2, 3, 4] :: [Int])
55+ it "Serialize [[Int]]"
56+ $ testSerializeList
57+ (8 + 3 * 8 + 6 * 8)
58+ ([[1], [1, 2], [1, 2, 3]] :: [[Int]])
59+
60+--------------------------------------------------------------------------------
61+-- Main function
62+--------------------------------------------------------------------------------
63+
64+moduleName :: String
65+moduleName = "Data.Unboxed"
66+
67+main :: IO ()
68+main = hspec $ H.parallel $ describe moduleName testCases
test/Streamly/Test/Data/Unbox.hs+6−36
@@ -1,10 +1,10 @@
1−{-# LANGUAGE StandaloneDeriving, DeriveAnyClass #-}
1+{-# LANGUAGE StandaloneDeriving #-}
2+{-# LANGUAGE DeriveAnyClass #-}
2−{-# LANGUAGE DataKinds #-}
3+{-# LANGUAGE TemplateHaskell #-}
3−{-# LANGUAGE TypeApplications #-}
4+
4−{-# HLINT ignore "Use newtype instead of data" #-}
5+-- This module has a lot of orphan instances as we are deriving it here. We can
5−{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
6+-- ignore this warning.
6{-# OPTIONS_GHC -Wno-orphans #-}
7{-# OPTIONS_GHC -Wno-orphans #-}
7−{-# OPTIONS_GHC -Wno-simplifiable-class-constraints #-}
8
8
9-- |
9-- |
10-- Module : Streamly.Test.Data.Unbox
10-- Module : Streamly.Test.Data.Unbox
@@ -40,8 +40,6 @@ import Streamly.Internal.Data.Unbox
40
40
41import Test.Hspec as H
41import Test.Hspec as H
42
42
43−import qualified Streamly.Internal.Data.Serialize as Serialize
44−
45--------------------------------------------------------------------------------
43--------------------------------------------------------------------------------
46-- Types
44-- Types
47--------------------------------------------------------------------------------
45--------------------------------------------------------------------------------
@@ -148,27 +146,6 @@ testGenericConsistency val = do
148checkSizeOf :: forall a. Unbox a => Proxy a -> Int -> IO ()
146checkSizeOf :: forall a. Unbox a => Proxy a -> Int -> IO ()
149checkSizeOf _ size = sizeOf (Proxy :: Proxy a) `shouldBe` size
147checkSizeOf _ size = sizeOf (Proxy :: Proxy a) `shouldBe` size
150
148
151−testSerializeList
152− :: forall a. (Eq a, Show a, Serialize.Serialize a)
153− => Int
154− -> a
155− -> IO ()
156−testSerializeList sizeOfA val = do
157−
158− let sz =
159− case Serialize.size :: Serialize.Size a of
160− Serialize.VarSize f -> f val
161− Serialize.ConstSize csz -> csz
162−
163− sz `shouldBe` sizeOfA
164−
165− arr <- newBytes sz
166−
167− off1 <- Serialize.serialize 0 arr val
168− (off2, val2) <- Serialize.deserialize 0 arr
169− val2 `shouldBe` val
170− off2 `shouldBe` off1
171−
172--------------------------------------------------------------------------------
149--------------------------------------------------------------------------------
173-- CPP helpers
150-- CPP helpers
174--------------------------------------------------------------------------------
151--------------------------------------------------------------------------------
@@ -226,13 +203,6 @@ testCases = do
226 it "GenericConsistency (Identity Int)"
203 it "GenericConsistency (Identity Int)"
227 $ testGenericConsistency (Identity 56760 :: Identity Int)
204 $ testGenericConsistency (Identity 56760 :: Identity Int)
228
205
229− it "Serialize [Int]"
230− $ testSerializeList (8 + 4 * 8) ([1, 2, 3, 4] :: [Int])
231− it "Serialize [[Int]]"
232− $ testSerializeList
233− (8 + 3 * 8 + 6 * 8)
234− ([[1], [1, 2], [1, 2, 3]] :: [[Int]])
235−
236 -- Fingerprint does not work for GHC 8.6.5
206 -- Fingerprint does not work for GHC 8.6.5
237 -- it "Fingerprint" $ testSerialization (Fingerprint 123456 876588)
207 -- it "Fingerprint" $ testSerialization (Fingerprint 123456 876588)
238 -- it "GenericConsistency Fingerprint"
208 -- it "GenericConsistency Fingerprint"
test/streamly-tests.cabal+6
@@ -268,6 +268,12 @@ test-suite Data.Unbox
268 main-is: Streamly/Test/Data/Unbox.hs
268 main-is: Streamly/Test/Data/Unbox.hs
269 ghc-options: -main-is Streamly.Test.Data.Unbox.main
269 ghc-options: -main-is Streamly.Test.Data.Unbox.main
270
270
271+test-suite Data.Serialize
272+ import: test-options
273+ type: exitcode-stdio-1.0
274+ main-is: Streamly/Test/Data/Serialize.hs
275+ ghc-options: -main-is Streamly.Test.Data.Serialize.main
276+
271test-suite Data.Array.Stream
277test-suite Data.Array.Stream
272 import: test-options
278 import: test-options
273 type: exitcode-stdio-1.0
279 type: exitcode-stdio-1.0