simple test passed
This commit is contained in:
parent
17844b5921
commit
85ec1fdccb
@ -39,6 +39,7 @@ test-suite geval-test
|
||||
build-depends: base
|
||||
, geval
|
||||
, hspec
|
||||
, HUnit
|
||||
ghc-options: -threaded -rtsopts -with-rtsopts=-N
|
||||
default-language: Haskell2010
|
||||
|
||||
|
33
src/Lib.hs
33
src/Lib.hs
@ -10,16 +10,35 @@ import Control.Monad.Trans.Resource
|
||||
import qualified Data.Conduit.List as CL
|
||||
import Data.Text
|
||||
import Data.Text.Read as TR
|
||||
import Control.Applicative
|
||||
|
||||
geval :: String -> String -> IO (Double)
|
||||
geval expectedFilePath outFilePath = do
|
||||
runResourceT $
|
||||
CB.sourceFile expectedFilePath
|
||||
$$ CT.decode CT.utf8
|
||||
=$= CT.lines
|
||||
=$= CL.map TR.double
|
||||
=$= CC.map getValue
|
||||
=$ CC.sum
|
||||
mse <- runResourceT $
|
||||
(getZipSource $ (,)
|
||||
<$> ZipSource (items expectedFilePath)
|
||||
<*> ZipSource (items outFilePath))
|
||||
$$ (CL.map itemError
|
||||
=$ averageC)
|
||||
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 (Right (x, _)) = x
|
||||
|
18
test/Spec.hs
18
test/Spec.hs
@ -1,9 +1,25 @@
|
||||
import Test.Hspec
|
||||
|
||||
import Lib
|
||||
import qualified Test.HUnit as HU
|
||||
|
||||
main :: IO ()
|
||||
main = hspec $ do
|
||||
describe "mean square error" $ 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)
|
||||
|
3
test/mse-simple/mse-simple-solution/test-A/out.tsv
Normal file
3
test/mse-simple/mse-simple-solution/test-A/out.tsv
Normal file
@ -0,0 +1,3 @@
|
||||
3.0
|
||||
3.0
|
||||
2.0
|
|
3
test/mse-simple/mse-simple/test-A/expected.tsv
Normal file
3
test/mse-simple/mse-simple/test-A/expected.tsv
Normal file
@ -0,0 +1,3 @@
|
||||
2.0
|
||||
3.0
|
||||
1.5
|
|
Loading…
Reference in New Issue
Block a user