2022-03-15 12:33:33 +03:00
|
|
|
module Streamly.Test.Data.Fold.Window (main) where
|
|
|
|
|
|
|
|
import Test.Hspec (hspec, describe, it, runIO)
|
2024-07-30 14:33:00 +03:00
|
|
|
import qualified Streamly.Internal.Data.Scanl as Scanl
|
2022-08-19 22:15:57 +03:00
|
|
|
import qualified Streamly.Internal.Data.Stream as S
|
|
|
|
import qualified Streamly.Internal.Data.Fold as Fold
|
2022-03-15 12:33:33 +03:00
|
|
|
|
|
|
|
import Prelude hiding (sum, maximum, minimum)
|
|
|
|
|
|
|
|
moduleName :: String
|
|
|
|
moduleName = "Window"
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = hspec $ do
|
|
|
|
describe moduleName $ do
|
|
|
|
let numElem = 80000
|
|
|
|
winSize = 800
|
|
|
|
testCaseChunk = [9007199254740992, 1, 1.0 :: Double,
|
|
|
|
9007199254740992, 1, 1, 1, 9007199254740992]
|
|
|
|
testCase = take numElem $ cycle testCaseChunk
|
|
|
|
deviationLimit = 1
|
|
|
|
testFunc f = do
|
|
|
|
let c = S.fromList testCase
|
2024-07-30 14:33:00 +03:00
|
|
|
f1 = Fold.fromScanl $ Scanl.windowScan winSize f
|
|
|
|
f2 = Fold.fromScanl f
|
|
|
|
a <- runIO $ S.fold f1 c
|
|
|
|
b <- runIO $ S.fold f2 $ S.drop (numElem - winSize)
|
2022-08-19 22:15:57 +03:00
|
|
|
$ fmap (, Nothing) c
|
2022-03-15 12:33:33 +03:00
|
|
|
let c1 = a - b
|
|
|
|
it ("should not deviate more than " ++ show deviationLimit)
|
|
|
|
$ c1 >= -1 * deviationLimit && c1 <= deviationLimit
|
|
|
|
|
2024-07-30 14:33:00 +03:00
|
|
|
describe "Sum" $ testFunc Scanl.windowSum
|
|
|
|
describe "mean" $ testFunc Scanl.windowMean
|
2022-03-15 12:33:33 +03:00
|
|
|
|
|
|
|
describe "Correctness" $ do
|
|
|
|
let winSize = 3
|
2022-06-07 14:32:58 +03:00
|
|
|
testCase1 =
|
|
|
|
[1.0, 4.0, 3.0, 2.1, -5.1, -2.0, 7.0, 3.0, -2.5] :: [Double]
|
|
|
|
|
2022-03-15 12:33:33 +03:00
|
|
|
testCase2 = replicate 5 1.0 ++ [7.0]
|
|
|
|
|
|
|
|
testFunc tc f sI sW = do
|
|
|
|
let c = S.fromList tc
|
2024-07-30 14:33:00 +03:00
|
|
|
a <- runIO $ S.fold Fold.toList $ S.postscanl f $ fmap (, Nothing) c
|
|
|
|
b <- runIO $ S.fold Fold.toList $ S.postscanl
|
|
|
|
(Scanl.windowScan winSize f) c
|
2022-03-15 12:33:33 +03:00
|
|
|
it "Infinite" $ a == sI
|
|
|
|
it ("Finite " ++ show winSize) $ b == sW
|
|
|
|
|
2022-06-07 14:32:58 +03:00
|
|
|
testFunc2 tc expec f = do
|
|
|
|
let c = S.fromList tc
|
2024-07-30 14:33:00 +03:00
|
|
|
a <- runIO $ S.fold (Fold.fromScanl (f winSize)) c
|
2022-06-07 14:32:58 +03:00
|
|
|
it (show tc) $ a == expec
|
|
|
|
|
2022-03-15 12:33:33 +03:00
|
|
|
describe "minimum" $ do
|
2024-07-30 14:33:00 +03:00
|
|
|
testFunc2 testCase1 (Just (-2.5)) Scanl.windowMinimum
|
2022-03-15 12:33:33 +03:00
|
|
|
describe "maximum" $ do
|
2024-07-30 14:33:00 +03:00
|
|
|
testFunc2 testCase1 (Just 7.0) Scanl.windowMaximum
|
2022-03-15 12:33:33 +03:00
|
|
|
describe "range" $ do
|
2024-07-30 14:33:00 +03:00
|
|
|
testFunc2 testCase1 (Just (-2.5, 7.0)) Scanl.windowRange
|
2022-03-15 12:33:33 +03:00
|
|
|
describe "sum" $ do
|
|
|
|
let scanInf = [1, 2, 3, 4, 5, 12] :: [Double]
|
|
|
|
scanWin = [1, 2, 3, 3, 3, 9] :: [Double]
|
2024-07-30 14:33:00 +03:00
|
|
|
testFunc testCase2 Scanl.windowSum scanInf scanWin
|
2022-03-24 19:49:42 +03:00
|
|
|
describe "mean" $ do
|
|
|
|
let scanInf = [1, 1, 1, 1, 1, 2] :: [Double]
|
|
|
|
scanWin = [1, 1, 1, 1, 1, 3] :: [Double]
|
2024-07-30 14:33:00 +03:00
|
|
|
testFunc testCase2 Scanl.windowMean scanInf scanWin
|