on 43dda21 help sign in
66 lines 2.8 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 = [31, 41, 59, 26, 53, 58, 97] :: [Double]
37 testCase2 = replicate 5 1.0 ++ [7.0]
39 testFunc tc f sI sW = do
40 let c = S.fromList tc
41 a <- runIO $ S.toList $ S.postscan f $ S.map (, Nothing) c
42 b <- runIO $ S.toList $ S.postscan
43 (Ring.slidingWindow winSize f) c
44 it "Infinite" $ a == sI
45 it ("Finite " ++ show winSize) $ b == sW
47 describe "minimum" $ do
48 let scanInf = [31, 31, 31, 26, 26, 26, 26] :: [Double]
49 scanWin = [31, 31, 31, 26, 26, 26, 53] :: [Double]
50 testFunc testCase1 minimum scanInf scanWin
51 describe "maximum" $ do
52 let scanInf = [31, 41, 59, 59, 59, 59, 97] :: [Double]
53 scanWin = [31, 41, 59, 59, 59, 58, 97] :: [Double]
54 testFunc testCase1 maximum scanInf scanWin
55 describe "range" $ do
56 let scanInf = [0, 10, 28, 33, 33, 33, 71] :: [Double]
57 scanWin = [0, 10, 28, 33, 33, 32, 44] :: [Double]
58 testFunc testCase1 range scanInf scanWin
59 describe "sum" $ do
60 let scanInf = [1, 2, 3, 4, 5, 12] :: [Double]
61 scanWin = [1, 2, 3, 3, 3, 9] :: [Double]
62 testFunc testCase2 sum scanInf scanWin
63 describe "mean" $ do
64 let scanInf = [1, 1, 1, 1, 1, 2] :: [Double]
65 scanWin = [1, 1, 1, 1, 1, 3] :: [Double]
66 testFunc testCase2 mean scanInf scanWin