on master help sign in
Add a (failing) QuickCheck test for formatRealFloat (gh-105)
commit789c63f682810925521286b381a8e659af73e0fa authorBryan O'Sullivan <bos@serpentine.com> dateFri 12 Dec 2014, 13:06 (12 years ago) parent57e628e
2 files changed+37−2
tests/Tests/Properties.hs+13−2 tests/Tests/QuickCheckUtils.hs+24
tests/Tests/Properties.hs+13−2
@@ -24,7 +24,7 @@ import Data.Text.Internal.Search (indices)
2424import Data.Text.Lazy.Read as TL
2525import Data.Text.Read as T
2626import Data.Word (Word, Word8, Word16, Word32, Word64)
27−import Numeric (showHex)
27+import Numeric (showGFloat, showHex)
2828import Prelude hiding (replicate)
2929import Test.Framework (Test, testGroup)
3030import Test.Framework.Providers.QuickCheck2 (testProperty)
@@ -779,6 +779,15 @@ tb_realfloat = (TB.toLazyText . TB.realFloat) `eq` (TL.pack . show)
779779tb_realfloat_float (a::Float) = tb_realfloat a
780780tb_realfloat_double (a::Double) = tb_realfloat a
781781
782+tb_formatRealFloat_G :: (RealFloat a, Show a) => a -> Precision a -> Property
783+tb_formatRealFloat_G a prec =
784+ TB.formatRealFloat TB.Generic p a ===
785+ TB.fromString (showGFloat p a "")
786+ where p = precision a prec
787+
788+tb_formatRealFloat_G_float (a::Float) = tb_formatRealFloat_G a
789+tb_formatRealFloat_G_double (a::Double) = tb_formatRealFloat_G a
790+
782791-- Reading.
783792
784793t_decimal (n::Int) s =
@@ -1284,7 +1293,9 @@ tests =
12841293 ],
12851294 testGroup "realfloat" [
12861295 testProperty "tb_realfloat_double" tb_realfloat_double,
1287− testProperty "tb_realfloat_float" tb_realfloat_float
1296+ testProperty "tb_realfloat_float" tb_realfloat_float,
1297+ testProperty "tb_formatRealFloat_G_float" tb_formatRealFloat_G_float,
1298+ testProperty "tb_formatRealFloat_G_double" tb_formatRealFloat_G_double
12881299 ],
12891300 testProperty "tb_fromText" tb_fromText,
12901301 testProperty "tb_singleton" tb_singleton
tests/Tests/QuickCheckUtils.hs+24
@@ -17,6 +17,9 @@ module Tests.QuickCheckUtils
1717 , Small (..)
1818 , small
1919
20+ , Precision(..)
21+ , precision
22+
2023 , integralRandomR
2124
2225 , DecodeErr (..)
@@ -263,6 +266,27 @@ eqP f g s w = eql "orig" (f s) (g t) &&
263266 | a =^= b = True
264267 | otherwise = trace (d ++ ": " ++ show a ++ " /= " ++ show b) False
265268
269+newtype Precision a = Precision (Maybe Int)
270+ deriving (Eq, Show)
271+
272+precision :: a -> Precision a -> Maybe Int
273+precision _ (Precision prec) = prec
274+
275+arbitraryPrecision :: Int -> Gen (Precision a)
276+arbitraryPrecision maxDigits = Precision <$> do
277+ n <- choose (-1,maxDigits)
278+ return $ if n == -1
279+ then Nothing
280+ else Just n
281+
282+instance Arbitrary (Precision Float) where
283+ arbitrary = arbitraryPrecision 11
284+ shrink = map Precision . shrink . precision undefined
285+
286+instance Arbitrary (Precision Double) where
287+ arbitrary = arbitraryPrecision 22
288+ shrink = map Precision . shrink . precision undefined
289+
266290-- Work around lack of Show instance for TextEncoding.
267291data Encoding = E String IO.TextEncoding
268292