floatshow / tests / quickcheck / qcTests.hs

module Main (main) where

import System.Environment (getArgs)

import Test.QuickCheck
import Text.FShow.RealFloat
import Text.FShow.Raw
import Numeric

myArgs :: Args
myArgs = stdArgs
    { replay = Nothing
    , maxSuccess = 10000
    , maxDiscard = 100
    , maxSize = 10000
    , chatty = False
    }

confArgs :: [String] -> Args
confArgs [] = myArgs
confArgs [s] = myArgs{ maxSuccess = read s }
confArgs [s,z] = myArgs{ maxSuccess = read s, maxSize = read z }
confArgs (s:z:d:_) = myArgs{ maxSuccess = read s, maxDiscard = read d, maxSize = read z }

rdDouble :: Double -> Double
rdDouble x = read (fshow x)

rdFloat :: Float -> Float
rdFloat x = read (fshow x)

propDouble :: Property
propDouble = forAll arbitrary (\d -> d == rdDouble d)

propFullDouble :: Property
propFullDouble = forAll arbitrary (\d -> d == read (show $ FD d))

propFloat :: Property
propFloat = forAll arbitrary (\f -> f == rdFloat f)

propFullFloat :: Property
propFullFloat = forAll arbitrary (\d -> d == read (show $ FF d))

verify :: Args -> (String, Property) -> IO ()
verify args (what, prop) = do
    putStrLn what
    quickCheckWithResult args prop >>= print

main :: IO ()
main = do
    args <- fmap confArgs getArgs
    mapM_ (verify args)
        [ ("Float",propFloat)
        , ("FullFloat",propFullFloat)
        , ("Double",propDouble)
        , ("FullDouble",propFullDouble)
        , ("Raw",propRaw)
        , ("Bin",propBin)
        , ("Dec",propDec)
        ]

instance BinDecode Double where
  decode = decodeFloat
  decodeL x = (52, m, e) where (m, e) = decodeFloat x
  showDigits _ = 17

instance DecimalFormat Double where
  nanTest = isNaN
  infTest = isInfinite
  negTest x = x < 0 || isNegativeZero x

draw :: Double -> String
draw = rawFormat decodeL 17 (Generic Nothing) Nothing

dbin :: Double -> String
dbin = binDecFormat (Generic Nothing) Nothing

ddec :: Double -> String
ddec = decimalFormat (Generic Nothing) Nothing

propRaw :: Property
propRaw = forAll arbitrary (\d -> d == read (draw d))

propBin :: Property
propBin = forAll arbitrary (\d -> d == read (dbin d))

propDec :: Property
propDec = forAll arbitrary (\d -> d == read (ddec d))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.