Skip to content

fix case #56: Text builder instance writes to wrong offset #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Data/Double/Conversion/Internal/TextBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
26 changes: 26 additions & 0 deletions tests/Properties.hs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 ()
Expand Down