thirsty
thirsty

Reputation: 83

How can Tasty withResource have a bench?

https://hackage.haskell.org/package/tasty-bench-0.3.2/docs/Test-Tasty-Bench.html#v:env

I am actually trying to use withResource, but have not gotten env to work either. I can not figure out how to embed bench inside of withResource.

This is what I have:

import qualified Test.Tasty as A
import qualified Test.Tasty.Bench as B 
import qualified Test.Tasty.HUnit as H

initResource :: IO (Either String [Int])  
initResource = undefined -- read very large file

freeResource :: Either String [Int] -> IO ()
freeResource _ = pure ()

giveTestTree :: IO (Either String [Int]) -> A.TestTree
giveTestTree x = A.testGroup "" 
    [ H.testCase "" $ do
        eiNs <- x
        case eiNs of
            Left e -> H.assertFailure e   
            Right ns -> do
                _ <- B.bench "" $ B.whnf even $ length ns -- Compiler error: Couldn't match type ‘A.TestTree’ with ‘IO a0’
                pure ()
    ]

main :: IO ()
main = B.defaultMain 
    [ B.bench "" $ B.whnf even $ length [1,2,3]
    , A.withResource initResource freeResource giveTestTree
    ]

Upvotes: 1

Views: 182

Answers (1)

thirsty
thirsty

Reputation: 83

giveTestTree :: IO (Either String [Int]) -> A.TestTree
giveTestTree mx = B.bench ":)" $ B.nfIO $ do 
        eiNs <- mx
        case eiNs of
            Left e -> H.assertFailure e   
            Right ns -> pure $ even $ length ns 

Upvotes: 1

Related Questions