Skip to content

Commit c0cbe97

Browse files
committed
More tests for Data.Text.Read.{double,rational}
1 parent cdcbbd5 commit c0cbe97

File tree

1 file changed

+61
-20
lines changed

1 file changed

+61
-20
lines changed

tests/Tests/Properties/Read.hs

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Tests.Properties.Read
88

99
import Data.Char (isDigit, isHexDigit)
1010
import Numeric (showHex)
11-
import Test.Tasty (TestTree, testGroup)
11+
import Test.Tasty (TestTree, testGroup, localOption, mkTimeout)
1212
import Test.Tasty.QuickCheck (testProperty)
1313
import Test.QuickCheck
1414
import Tests.QuickCheckUtils ()
@@ -38,23 +38,39 @@ tl_hexadecimal m s ox =
3838

3939
isFloaty c = c `elem` ("+-.0123456789eE" :: String)
4040

41-
t_read_rational p tol (n::Double) s =
42-
case p (T.pack (show n) `T.append` t) of
43-
Left err -> counterexample err $ property False
44-
Right (n',t') -> t === t' .&&. property (abs (n-n') <= tol)
45-
where t = T.dropWhile isFloaty s
41+
t_read_rational :: Double -> T.Text -> Property
42+
t_read_rational n s =
43+
case T.rational (T.pack (show n) `T.append` t) of
44+
Left err -> counterexample err $ property False
45+
Right (n', t') -> t === t' .&&. n' === n''
46+
where
47+
t = T.dropWhile isFloaty s
48+
n'' = read (show n) :: Double
4649

47-
tl_read_rational p tol (n::Double) s =
48-
case p (TL.pack (show n) `TL.append` t) of
49-
Left err -> counterexample err $ property False
50-
Right (n',t') -> t === t' .&&. property (abs (n-n') <= tol)
51-
where t = TL.dropWhile isFloaty s
50+
t_read_double :: Double -> Double -> T.Text -> Property
51+
t_read_double tol n s =
52+
case T.double (T.pack (show n) `T.append` t) of
53+
Left err -> counterexample err $ property False
54+
Right (n', t') -> t === t' .&&. property (abs (n - n') <= tol)
55+
where
56+
t = T.dropWhile isFloaty s
5257

53-
t_double = t_read_rational T.double 1e-13
54-
tl_double = tl_read_rational TL.double 1e-13
55-
t_rational = t_read_rational T.rational 1e-16
56-
tl_rational = tl_read_rational TL.rational 1e-16
58+
tl_read_rational :: Double -> TL.Text -> Property
59+
tl_read_rational n s =
60+
case TL.rational (TL.pack (show n) `TL.append` t) of
61+
Left err -> counterexample err $ property False
62+
Right (n', t') -> t === t' .&&. n' === n''
63+
where
64+
t = TL.dropWhile isFloaty s
65+
n'' = read (show n) :: Double
5766

67+
tl_read_double :: Double -> Double -> TL.Text -> Property
68+
tl_read_double tol n s =
69+
case TL.rational (TL.pack (show n) `TL.append` t) of
70+
Left err -> counterexample err $ property False
71+
Right (n', t') -> t === t' .&&. property (abs (n - n') <= tol)
72+
where
73+
t = TL.dropWhile isFloaty s
5874

5975
testRead :: TestTree
6076
testRead =
@@ -63,8 +79,33 @@ testRead =
6379
testProperty "tl_decimal" tl_decimal,
6480
testProperty "t_hexadecimal" t_hexadecimal,
6581
testProperty "tl_hexadecimal" tl_hexadecimal,
66-
testProperty "t_double" t_double,
67-
testProperty "tl_double" tl_double,
68-
testProperty "t_rational" t_rational,
69-
testProperty "tl_rational" tl_rational
70-
]
82+
83+
testProperty "t_double" $ t_read_double 1e-13,
84+
testProperty "tl_double" $ tl_read_double 1e-13,
85+
86+
testProperty "t_rational" t_read_rational,
87+
testProperty "t_rational 1.3e-2" (t_read_rational 1.3e-2),
88+
testProperty "tl_rational" tl_read_rational,
89+
testProperty "tl_rational 9e-3" (tl_read_rational 9e-3),
90+
91+
localOption (mkTimeout 100000) $ testGroup "DDoS attacks" [
92+
testProperty "t_double large positive exponent" $
93+
T.double (T.pack "1.1e1000000000") === Right (1 / 0, mempty),
94+
testProperty "t_double large negative exponent" $
95+
T.double (T.pack "1.1e-1000000000") === Right (0.0, mempty),
96+
testProperty "tl_double large positive exponent" $
97+
TL.double (TL.pack "1.1e1000000000") === Right (1 / 0, mempty),
98+
testProperty "tl_double large negative exponent" $
99+
TL.double (TL.pack "1.1e-1000000000") === Right (0.0, mempty),
100+
101+
testProperty "t_rational large positive exponent" $
102+
T.rational (T.pack "1.1e1000000000") === Right (1 / 0 :: Double, mempty),
103+
testProperty "t_rational large negative exponent" $
104+
T.rational (T.pack "1.1e-1000000000") === Right (0.0 :: Double, mempty),
105+
testProperty "tl_rational large positive exponent" $
106+
TL.rational (TL.pack "1.1e1000000000") === Right (1 / 0 :: Double, mempty),
107+
testProperty "tl_rational large negative exponent" $
108+
TL.rational (TL.pack "1.1e-1000000000") === Right (0.0 :: Double, mempty)
109+
]
110+
111+
]

0 commit comments

Comments
 (0)