Skip to content

Commit

Permalink
Fix detection of endianness in test suite (#419)
Browse files Browse the repository at this point in the history
* Fix detection of endianness in test suite (#421)

* Work around when getFileSystemEncoding is ASCII (#421)
  • Loading branch information
Bodigrim committed Sep 11, 2021
1 parent e54d342 commit 23ef9f3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 0 additions & 2 deletions bytestring.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 11 additions & 4 deletions tests/Properties/ByteString.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
14 changes: 10 additions & 4 deletions tests/builder/Data/ByteString/Builder/Prim/TestUtils.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- |
-- Copyright : (c) 2011 Simon Meier
Expand Down Expand Up @@ -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 <ghcautoconf.h>

-- Helper functions
-------------------

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 23ef9f3

Please sign in to comment.