on master help sign in
Test reading of integral types

--HG-- extra : convert_revision : 344cb24a689f1b195a4e18ef8dbcf4d23d78e71d

commitf5347a714f01db88ecf778630db77b72c1485ee1 authorBryan O'Sullivan <bos@serpentine.com> dateMon 18 Oct 2010, 02:40 (16 years ago) parent4565bea
1 file changed+28−1
tests/Properties.hs+28−1
tests/Properties.hs+28−1
@@ -7,7 +7,8 @@ import Test.QuickCheck.Monadic
77import Text.Show.Functions ()
88
99import qualified Data.Bits as Bits (shiftL, shiftR)
10−import Data.Char (chr, isLower, isSpace, isUpper, ord)
10+import Numeric (showHex)
11+import Data.Char (chr, isDigit, isHexDigit, isLower, isSpace, isUpper, ord)
1112import Data.Monoid (Monoid(..))
1213import Data.String (fromString)
1314import Debug.Trace (trace)
@@ -21,6 +22,8 @@ import qualified Data.Text.Lazy.IO as TL
2122import qualified Data.Text.Lazy.Internal as TL
2223import qualified Data.Text.Lazy.Builder as TB
2324import qualified Data.Text.Encoding as E
25+import Data.Text.Read as T
26+import Data.Text.Lazy.Read as TL
2427import Data.Text.Encoding.Error
2528import Control.Exception (SomeException, bracket, catch, evaluate, try)
2629import Data.Text.Foreign
@@ -658,6 +661,23 @@ t_builderAssociative s1 s2 s3 =
658661 b2 = TB.fromText (packS s2)
659662 b3 = TB.fromText (packS s3)
660663
664+-- Reading.
665+
666+t_decimal (n::Int) s =
667+ T.signed T.decimal (T.pack (show n) `T.append` t) == Right (n,t)
668+ where t = T.dropWhile isDigit s
669+tl_decimal (n::Int) s =
670+ TL.signed TL.decimal (TL.pack (show n) `TL.append` t) == Right (n,t)
671+ where t = TL.dropWhile isDigit s
672+t_hexadecimal (n::Positive Int) s ox =
673+ T.hexadecimal (T.concat [p, T.pack (showHex n ""), t]) == Right (n,t)
674+ where t = T.dropWhile isHexDigit s
675+ p = if ox then "0x" else ""
676+tl_hexadecimal (n::Positive Int) s ox =
677+ TL.hexadecimal (TL.concat [p, TL.pack (showHex n ""), t]) == Right (n,t)
678+ where t = TL.dropWhile isHexDigit s
679+ p = if ox then "0x" else ""
680+
661681-- Input and output.
662682
663683-- Work around lack of Show instance for TextEncoding.
@@ -1139,6 +1159,13 @@ tests = [
11391159 testProperty "t_builderAssociative" t_builderAssociative
11401160 ],
11411161
1162+ testGroup "read" [
1163+ testProperty "t_decimal" t_decimal,
1164+ testProperty "tl_decimal" tl_decimal,
1165+ testProperty "t_hexadecimal" t_hexadecimal,
1166+ testProperty "tl_hexadecimal" tl_hexadecimal
1167+ ],
1168+
11421169 testGroup "input-output" [
11431170 testProperty "t_write_read" t_write_read,
11441171 testProperty "tl_write_read" tl_write_read,