Commit 4f7664a3 authored by bos's avatar bos
Browse files

Further generalise the formatRealFloat tests (gh-105)

parent 7e66b81e
......@@ -24,7 +24,7 @@ import Data.Text.Internal.Search (indices)
import Data.Text.Lazy.Read as TL
import Data.Text.Read as T
import Data.Word (Word, Word8, Word16, Word32, Word64)
import Numeric (showGFloat, showHex)
import Numeric (showEFloat, showFFloat, showGFloat, showHex)
import Prelude hiding (replicate)
import Test.Framework (Test, testGroup)
import Test.Framework.Providers.QuickCheck2 (testProperty)
......@@ -779,14 +779,19 @@ tb_realfloat = (TB.toLazyText . TB.realFloat) `eq` (TL.pack . show)
tb_realfloat_float (a::Float) = tb_realfloat a
tb_realfloat_double (a::Double) = tb_realfloat a
tb_formatRealFloat_G :: (RealFloat a, Show a) => a -> Precision a -> Property
tb_formatRealFloat_G a prec =
TB.formatRealFloat TB.Generic p a ===
TB.fromString (showGFloat p a "")
showFloat :: (RealFloat a) => TB.FPFormat -> Maybe Int -> a -> ShowS
showFloat TB.Exponent = showEFloat
showFloat TB.Fixed = showFFloat
showFloat TB.Generic = showGFloat
tb_formatRealFloat :: (RealFloat a, Show a) => a -> TB.FPFormat -> Precision a -> Property
tb_formatRealFloat a fmt prec =
TB.formatRealFloat fmt p a ===
TB.fromString (showFloat fmt p a "")
where p = precision a prec
tb_formatRealFloat_G_float (a::Float) = tb_formatRealFloat_G a
tb_formatRealFloat_G_double (a::Double) = tb_formatRealFloat_G a
tb_formatRealFloat_float (a::Float) = tb_formatRealFloat a
tb_formatRealFloat_double (a::Double) = tb_formatRealFloat a
-- Reading.
......@@ -1294,8 +1299,8 @@ tests =
testGroup "realfloat" [
testProperty "tb_realfloat_double" tb_realfloat_double,
testProperty "tb_realfloat_float" tb_realfloat_float,
testProperty "tb_formatRealFloat_G_float" tb_formatRealFloat_G_float,
testProperty "tb_formatRealFloat_G_double" tb_formatRealFloat_G_double
testProperty "tb_formatRealFloat_float" tb_formatRealFloat_float,
testProperty "tb_formatRealFloat_double" tb_formatRealFloat_double
],
testProperty "tb_fromText" tb_fromText,
testProperty "tb_singleton" tb_singleton
......
......@@ -40,10 +40,11 @@ import Control.DeepSeq (NFData (..), deepseq)
import Control.Exception (bracket)
import Data.String (IsString, fromString)
import Data.Text.Foreign (I16)
import Data.Text.Lazy.Builder.RealFloat (FPFormat(..))
import Data.Word (Word8, Word16)
import Debug.Trace (trace)
import System.Random (Random (..), RandomGen)
import Test.QuickCheck hiding (Small (..), (.&.))
import Test.QuickCheck hiding (Fixed(..), Small (..), (.&.))
import Test.QuickCheck.Monadic (assert, monadicIO, run)
import Test.QuickCheck.Unicode (string)
import Tests.Utils
......@@ -266,6 +267,9 @@ eqP f g s w = eql "orig" (f s) (g t) &&
| a =^= b = True
| otherwise = trace (d ++ ": " ++ show a ++ " /= " ++ show b) False
instance Arbitrary FPFormat where
arbitrary = elements [Exponent, Fixed, Generic]
newtype Precision a = Precision (Maybe Int)
deriving (Eq, Show)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment