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