on 52ddfee help sign in
67 lines 2.7 kB history raw
1module Streamly.Test.Data.Fold.Window (main) where
3import Test.Hspec (hspec, describe, it, runIO)
4import qualified Streamly.Internal.Data.Ring as Ring
5import qualified Streamly.Internal.Data.Stream as S
6import qualified Streamly.Internal.Data.Fold as Fold
8import Prelude hiding (sum, maximum, minimum)
10moduleName :: String
11moduleName = "Window"
13main :: IO ()
14main = hspec $ do
15 describe moduleName $ do
16 let numElem = 80000
17 winSize = 800
18 testCaseChunk = [9007199254740992, 1, 1.0 :: Double,
19 9007199254740992, 1, 1, 1, 9007199254740992]
20 testCase = take numElem $ cycle testCaseChunk
21 deviationLimit = 1
22 testFunc f = do
23 let c = S.fromList testCase
24 a <- runIO $ S.fold (Ring.slidingWindow winSize f) c
25 b <- runIO $ S.fold f $ S.drop (numElem - winSize)
26 $ fmap (, Nothing) c
27 let c1 = a - b
28 it ("should not deviate more than " ++ show deviationLimit)
29 $ c1 >= -1 * deviationLimit && c1 <= deviationLimit
31 describe "Sum" $ testFunc Fold.windowSum
32 describe "mean" $ testFunc Fold.windowMean
34 describe "Correctness" $ do
35 let winSize = 3
36 testCase1 =
37 [1.0, 4.0, 3.0, 2.1, -5.1, -2.0, 7.0, 3.0, -2.5] :: [Double]
39 testCase2 = replicate 5 1.0 ++ [7.0]
41 testFunc tc f sI sW = do
42 let c = S.fromList tc
43 a <- runIO $ S.fold Fold.toList $ S.postscan f $ fmap (, Nothing) c
44 b <- runIO $ S.fold Fold.toList $ S.postscan
45 (Ring.slidingWindow winSize f) c
46 it "Infinite" $ a == sI
47 it ("Finite " ++ show winSize) $ b == sW
49 testFunc2 tc expec f = do
50 let c = S.fromList tc
51 a <- runIO $ S.fold (f winSize) c
52 it (show tc) $ a == expec
54 describe "minimum" $ do
55 testFunc2 testCase1 (Just (-2.5)) Fold.windowMinimum
56 describe "maximum" $ do
57 testFunc2 testCase1 (Just 7.0) Fold.windowMaximum
58 describe "range" $ do
59 testFunc2 testCase1 (Just (-2.5, 7.0)) Fold.windowRange
60 describe "sum" $ do
61 let scanInf = [1, 2, 3, 4, 5, 12] :: [Double]
62 scanWin = [1, 2, 3, 3, 3, 9] :: [Double]
63 testFunc testCase2 Fold.windowSum scanInf scanWin
64 describe "mean" $ do
65 let scanInf = [1, 1, 1, 1, 1, 2] :: [Double]
66 scanWin = [1, 1, 1, 1, 1, 3] :: [Double]
67 testFunc testCase2 Fold.windowMean scanInf scanWin