@@ -4,17 +4,81 @@ module Streamly.Test.Unicode.Parser
7+import Control.Monad.Identity (Identity(runIdentity))
8+import Debug.Trace (trace)
7import Streamly.Internal.Data.Parser (ParseError (..))
9import Streamly.Internal.Data.Parser (ParseError (..))
10+import Streamly.Test.Common (chooseDouble)
8import Test.Hspec.QuickCheck (prop)
11import Test.Hspec.QuickCheck (prop)
9−import Test.QuickCheck (Property)
12+import Test.QuickCheck
13+ (forAll, Property, property)
10−import Test.QuickCheck.Monadic (run, monadicIO, assert)
14+import Test.QuickCheck.Monadic (monadicIO, assert, run)
16+import qualified Data.Scientific as Scientific
12import qualified Streamly.Data.Stream as Stream
17import qualified Streamly.Data.Stream as Stream
14−import qualified Streamly.Internal.Data.Stream as Stream (parseBreak, toList)
18+import qualified Streamly.Internal.Data.Stream as Stream (parseBreak)
15import qualified Streamly.Unicode.Parser as Unicode
19import qualified Streamly.Unicode.Parser as Unicode
20+import qualified Streamly.Internal.Unicode.Parser as Parser
16import qualified Test.Hspec as H
21import qualified Test.Hspec as H
17−import Debug.Trace (trace)
22+
23+-- Scientific notation
24+scientificExpFP :: Property
26+ forAll (chooseDouble (-99.99e-12, 1234.4567e+234)) $ \ls ->
27+ case runIdentity $ Stream.parse parser (Stream.fromList (show ls)) of
28+ Right val -> if val == show ls
29+ then property (val == show ls)
31+ ("Expected = " ++ show ls ++ " Got = " ++ val)
32+ property (val == show ls)
33+ Left _ -> property False
37+ formatter = Scientific.formatScientific Scientific.Exponent Nothing
38+ toScientific (c, m) = Scientific.scientific c m
39+ parser = formatter . toScientific <$> Parser.number
41+-- Standard decimal notation.
42+scientificFixFP :: Property
44+ forAll (chooseDouble (-0.00099, 123445.67998)) $ \ls ->
45+ case runIdentity $ Stream.parse parser (Stream.fromList (show ls)) of
46+ Right val -> if val == show ls
47+ then property (val == show ls)
49+ ("Expected = " ++ show ls ++ " Got = " ++ val)
50+ property (val == show ls)
51+ Left _ -> property False
55+ formatter = Scientific.formatScientific Scientific.Fixed Nothing
56+ toScientific (c, m) = Scientific.scientific c m
57+ parser = formatter . toScientific <$> Parser.number
59+doubleExpFP :: Property
61+ forAll (chooseDouble (-99.99e-12, 1234.4567e+234)) $ \ls -> do
63+ case runIdentity $ Stream.parse Parser.double (Stream.fromList sls) of
64+ Right val -> if val == ls
65+ then property (val == ls)
67+ ("Read = "++ show (read sls :: Double) ++ " Expected = " ++ sls ++ " Got = " ++ show val)
69+ Left _ -> property False
71+-- Standard decimal notation.
72+doubleFixFP :: Property
74+ forAll (chooseDouble (-0.00099, 123445.67998)) $ \ls ->
75+ case runIdentity $ Stream.parse Parser.double (Stream.fromList (show ls)) of
76+ Right val -> if val == ls
77+ then property (val == ls)
79+ ("Expected = " ++ show ls ++ " Got = " ++ show val)
81+ Left _ -> property False
19doubleParser :: String -> IO (Either ParseError Double)
83doubleParser :: String -> IO (Either ParseError Double)
20doubleParser = Stream.parse Unicode.double . Stream.fromList
84doubleParser = Stream.parse Unicode.double . Stream.fromList
@@ -23,15 +87,19 @@ double :: String -> Double -> Property
23double s d = monadicIO $ do
87double s d = monadicIO $ do
24 x <- run $ doubleParser s
88 x <- run $ doubleParser s
90+ Right val -> if val == d
91+ then assert (val == d)
26− Right val -> trace ("parsed " ++ show val) (assert (val == d))
92+ else trace ("Expectedc = " ++ show d ++ " Got = "++ show val) (assert (val == d))
27− Left (ParseError _) -> assert (False)
93+ Left (ParseError _) -> assert False
29doubleErr :: String -> String -> Property
95doubleErr :: String -> String -> Property
30doubleErr s msg = monadicIO $ do
96doubleErr s msg = monadicIO $ do
31 x <- run $ doubleParser s
97 x <- run $ doubleParser s
33− Right _ -> assert (False)
99+ Right _ -> assert False
100+ Left (ParseError err) -> if err == msg
101+ then assert (err == msg)
34− Left (ParseError err) -> trace err (assert (err == msg))
102+ else trace err (assert (err == msg))
36remainingStream :: String -> [String]
104remainingStream :: String -> [String]
37remainingStream x =
105remainingStream x =
@@ -118,3 +186,9 @@ main = do
118 prop "afterParse \"..\" \"..\"" $ afterParse ".." ".."
186 prop "afterParse \"..\" \"..\"" $ afterParse ".." ".."
119 prop "afterParse \"+\" \"+\"" $ afterParse "+" "+"
187 prop "afterParse \"+\" \"+\"" $ afterParse "+" "+"
120 prop "afterParse \"++\" \"++\"" $ afterParse "++" "++"
188 prop "afterParse \"++\" \"++\"" $ afterParse "++" "++"
189+ H.describe "Scientific parser property test" $ do
190+ prop "Exponent format" scientificExpFP
191+ prop "Decimal format" scientificFixFP
192+ H.describe "double parser property test" $ do
193+ prop "Exponent format" doubleExpFP
194+ prop "Decimal format" doubleFixFP