diff --git a/Data/Double/Conversion/Internal/TextBuilder.hs b/Data/Double/Conversion/Internal/TextBuilder.hs index 8287854..2cacb89 100644 --- a/Data/Double/Conversion/Internal/TextBuilder.hs +++ b/Data/Double/Conversion/Internal/TextBuilder.hs @@ -36,7 +36,7 @@ convert :: (RealFloat a, RealFloat b, b ~ ForeignFloating a) => String -> CInt {-# SPECIALIZE convert :: String -> CInt -> (forall s. CDouble -> MutableByteArray# s -> IO CInt) -> Double -> Builder #-} {-# SPECIALIZE convert :: String -> CInt -> (forall s. CFloat -> MutableByteArray# s -> IO CInt) -> Float -> Builder #-} {-# INLINABLE convert #-} -convert func len act val = runST $ do +convert func len act val = runST $ do #if MIN_VERSION_text(2,0,0) mTempArr@(A.MutableByteArray tempMArr) <- A.new (fromIntegral len) #else @@ -48,7 +48,7 @@ convert func len act val = runST $ do error $ "Data.Double.Conversion.Text." ++ func ++ ": conversion failed." #if MIN_VERSION_text(2,0,0) - return $ writeN (fromIntegral size) $ \mArr _ -> A.copyI (fromIntegral size) mArr 0 tempArr 0 + return $ writeN (fromIntegral size) $ \mArr offset -> A.copyI (fromIntegral size) mArr offset tempArr 0 #else - return $ writeN (fromIntegral size) $ \mArr _ -> A.copyI mArr 0 tempArr 0 (fromIntegral size) + return $ writeN (fromIntegral size) $ \mArr offset -> A.copyI mArr offset tempArr 0 (fromIntegral size) #endif diff --git a/tests/Properties.hs b/tests/Properties.hs index 7f6c59d..2d17027 100644 --- a/tests/Properties.hs +++ b/tests/Properties.hs @@ -1,8 +1,12 @@ import Test.Framework (Test, defaultMain, testGroup) import Test.Framework.Providers.QuickCheck2 (testProperty) import qualified Data.ByteString.Char8 as B +import qualified Data.ByteString.Builder as BB import qualified Data.Double.Conversion.Convertable as C +import Data.Double.Conversion.Convertable import qualified Data.Text as T +import qualified Data.Text.Lazy as TL +import qualified Data.Text.Lazy.Builder as TLB import qualified Regressions shortest :: (Double -> String) -> Double -> Double -> Bool @@ -12,10 +16,32 @@ shortest f a b = case read (f ab) of | otherwise -> ba == ab where ab = a / b +-- | Compare concatenated sequence of numbers as Text and Builder +catTextEqualsCatBuilder :: [Either Float Double] -> Bool +catTextEqualsCatBuilder xs = + mconcat texts == (TL.toStrict . TLB.toLazyText . mconcat) builders + where + texts :: [T.Text] + texts = either (toExponential 3) (toExponential 3) <$> xs + builders :: [TLB.Builder] + builders = either (toExponential 3) (toExponential 3) <$> xs + +-- | Compare concatenated sequence of numbers as ByteString and Builder +catByteStringEqualsCatBuilder :: [Either Float Double] -> Bool +catByteStringEqualsCatBuilder xs = + mconcat byteStrings == (B.toStrict . BB.toLazyByteString . mconcat) builders + where + byteStrings :: [B.ByteString] + byteStrings = either (toExponential 3) (toExponential 3) <$> xs + builders :: [BB.Builder] + builders = either (toExponential 3) (toExponential 3) <$> xs + tests :: Test tests = testGroup "Properties" [ testProperty "b_shortest" $ shortest (B.unpack . C.toShortest) , testProperty "t_shortest" $ shortest (T.unpack . C.toShortest) + , testProperty "catTextEqualsCatBuilder" catTextEqualsCatBuilder + , testProperty "catByteStringEqualsCatBuilder" catByteStringEqualsCatBuilder ] main :: IO ()