From 23ef9f3d13b6158842106da4b84a888f0cb3342e Mon Sep 17 00:00:00 2001 From: Bodigrim Date: Sun, 12 Sep 2021 00:35:19 +0100 Subject: [PATCH] Fix detection of endianness in test suite (#419) * Fix detection of endianness in test suite (#421) * Work around when getFileSystemEncoding is ASCII (#421) --- bytestring.cabal | 2 -- tests/Properties/ByteString.hs | 15 +++++++++++---- .../Data/ByteString/Builder/Prim/TestUtils.hs | 14 ++++++++++---- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/bytestring.cabal b/bytestring.cabal index 7f3c782db..909c154b9 100644 --- a/bytestring.cabal +++ b/bytestring.cabal @@ -160,8 +160,6 @@ test-suite test-builder tasty, tasty-hunit, tasty-quickcheck - if impl(ghc < 8.4) - build-depends: ghc-byteorder ghc-options: -Wall -fwarn-tabs -threaded -rtsopts default-language: Haskell2010 diff --git a/tests/Properties/ByteString.hs b/tests/Properties/ByteString.hs index c014cb02a..7461d502b 100644 --- a/tests/Properties/ByteString.hs +++ b/tests/Properties/ByteString.hs @@ -23,6 +23,7 @@ #ifndef BYTESTRING_LAZY module Properties.ByteString (tests) where import qualified Data.ByteString as B +import GHC.IO.Encoding #else module Properties.ByteStringLazy (tests) where import qualified Data.ByteString.Lazy as B @@ -83,10 +84,16 @@ tests = \x -> ioProperty $ do r <- B.toFilePath x >>= B.fromFilePath pure (r === x) - , testProperty "fromFilePath >>= toFilePath" $ - \x -> ioProperty $ do - r <- B.fromFilePath x >>= B.toFilePath - pure (r === x) + , testProperty "fromFilePath >>= toFilePath" $ ioProperty $ do + let prop x = ioProperty $ do + r <- B.fromFilePath x >>= B.toFilePath + pure (r === x) + -- Normally getFileSystemEncoding returns a Unicode encoding, + -- but if it is ASCII, we should not generate Unicode filenames. + enc <- getFileSystemEncoding + pure $ case textEncodingName enc of + "ASCII" -> property (prop . getASCIIString) + _ -> property prop #endif #endif diff --git a/tests/builder/Data/ByteString/Builder/Prim/TestUtils.hs b/tests/builder/Data/ByteString/Builder/Prim/TestUtils.hs index 2a72b4a64..94ac1ac65 100644 --- a/tests/builder/Data/ByteString/Builder/Prim/TestUtils.hs +++ b/tests/builder/Data/ByteString/Builder/Prim/TestUtils.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE ScopedTypeVariables #-} -- | -- Copyright : (c) 2011 Simon Meier @@ -78,13 +79,14 @@ import Data.Int import Data.Word import Foreign (Storable(..), castPtr, minusPtr, with) import Numeric (showHex) -import GHC.ByteOrder import System.IO.Unsafe (unsafePerformIO) import Test.Tasty import Test.Tasty.HUnit (assertBool, testCase) import Test.Tasty.QuickCheck (Arbitrary(..), testProperty) +#include + -- Helper functions ------------------- @@ -332,10 +334,14 @@ littleEndian_list :: (Storable a, Bits a, Integral a) => a -> [Word8] littleEndian_list x = map (fromIntegral . (x `shiftR`) . (8*)) $ [0..sizeOf x - 1] +-- See https://gitlab.haskell.org/ghc/ghc/-/issues/20338 +-- and https://gitlab.haskell.org/ghc/ghc/-/issues/18445 hostEndian_list :: (Storable a, Bits a, Integral a) => a -> [Word8] -hostEndian_list = case targetByteOrder of - LittleEndian -> littleEndian_list - BigEndian -> bigEndian_list +#if defined(WORDS_BIGENDIAN) +hostEndian_list = bigEndian_list +#else +hostEndian_list = littleEndian_list +#endif float_list :: (Word32 -> [Word8]) -> Float -> [Word8] float_list f = f . coerceFloatToWord32