on 4e252a8 help sign in
67 lines 2.6 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.Foreign as Ring
6import qualified Streamly.Prelude as S
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 $ S.map (, 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 sum
32 describe "mean" $ testFunc mean
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.toList $ S.postscan f $ S.map (, Nothing) c
44 b <- runIO $ S.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)) minimum
56 describe "maximum" $ do
57 testFunc2 testCase1 (Just 7.0) maximum
58 describe "range" $ do
59 testFunc2 testCase1 (Just (-2.5, 7.0)) range
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 sum 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 mean scanInf scanWin