simple test passed

This commit is contained in:
Filip Gralinski 2015-08-19 23:24:19 +02:00 committed by Filip Gralinski
parent 17844b5921
commit 85ec1fdccb
5 changed files with 50 additions and 8 deletions

View File

@ -39,6 +39,7 @@ test-suite geval-test
build-depends: base build-depends: base
, geval , geval
, hspec , hspec
, HUnit
ghc-options: -threaded -rtsopts -with-rtsopts=-N ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010 default-language: Haskell2010

View File

@ -10,16 +10,35 @@ import Control.Monad.Trans.Resource
import qualified Data.Conduit.List as CL import qualified Data.Conduit.List as CL
import Data.Text import Data.Text
import Data.Text.Read as TR import Data.Text.Read as TR
import Control.Applicative
geval :: String -> String -> IO (Double) geval :: String -> String -> IO (Double)
geval expectedFilePath outFilePath = do geval expectedFilePath outFilePath = do
runResourceT $ mse <- runResourceT $
CB.sourceFile expectedFilePath (getZipSource $ (,)
$$ CT.decode CT.utf8 <$> ZipSource (items expectedFilePath)
=$= CT.lines <*> ZipSource (items outFilePath))
=$= CL.map TR.double $$ (CL.map itemError
=$= CC.map getValue =$ averageC)
=$ CC.sum return $ mse ** 0.5
averageC :: MonadResource m => Sink Double m Double
averageC = getZipSink
$ (\total count -> total / fromIntegral count)
<$> ZipSink CC.sum
<*> ZipSink CC.length
items :: MonadResource m => String -> Source m Double
items filePath =
CB.sourceFile filePath
$= (CT.decode CT.utf8
=$= CT.lines
=$= CL.map TR.double
=$= CC.map getValue)
itemError :: (Double, Double) -> Double
itemError (exp, out) = (exp-out)**2
getValue :: Either String (Double, Text) -> Double getValue :: Either String (Double, Text) -> Double
getValue (Right (x, _)) = x getValue (Right (x, _)) = x

View File

@ -1,9 +1,25 @@
import Test.Hspec import Test.Hspec
import Lib import Lib
import qualified Test.HUnit as HU
main :: IO () main :: IO ()
main = hspec $ do main = hspec $ do
describe "mean square error" $ do describe "mean square error" $ do
it "simple test" $ do it "simple test" $ do
geval "test/mse-simple/mse-simple/test-A/expected.tsv" "test/mse-simple/mse-simple-solution/test-A/out.tsv" `shouldReturn` 1.118033988749 geval "test/mse-simple/mse-simple/test-A/expected.tsv" "test/mse-simple/mse-simple-solution/test-A/out.tsv" `shouldReturnAlmost` 0.64549722436790
class AEq a where
(=~) :: a -> a -> Bool
instance AEq Double where
x =~ y = abs ( x - y ) < (1.0e-8 :: Double)
(@=~?) :: (Show a, AEq a) => a -> a -> HU.Assertion
(@=~?) expected actual = expected =~ actual HU.@? assertionMsg
where
assertionMsg = "Expected : " ++ show expected ++
"\nActual : " ++ show actual
shouldReturnAlmost :: (AEq a, Show a, Eq a) => IO a -> a -> Expectation
shouldReturnAlmost action expected = action >>= (@=~? expected)

View File

@ -0,0 +1,3 @@
3.0
3.0
2.0
1 3.0
2 3.0
3 2.0

View File

@ -0,0 +1,3 @@
2.0
3.0
1.5
1 2.0
2 3.0
3 1.5