Skip to content

Commit

Permalink
refactor(journal): make it work regardless of target byteorder
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Jan 12, 2022
1 parent 3bb9264 commit 12fc98e
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/journal/test/Journal/Internal/ByteBufferTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import Data.Vector (Vector)
import qualified Data.Vector as Vector
import Data.Word (Word8)
import Foreign (sizeOf)
import System.Directory (removePathForcibly, canonicalizePath, getTemporaryDirectory)
import GHC.ByteOrder (targetByteOrder, ByteOrder(LittleEndian, BigEndian))
import System.Directory
(canonicalizePath, getTemporaryDirectory, removePathForcibly)
import System.IO (hClose, openTempFile)
import Test.QuickCheck
import Test.QuickCheck.Monadic
Expand Down Expand Up @@ -59,11 +61,16 @@ newFakeByteBuffer size = FakeByteBuffer
, fbbSize = size
}

fixEndianess :: [a] -> [a]
fixEndianess = case targetByteOrder of
LittleEndian -> reverse
BigEndian -> id -- Data.Binary always uses big endian.

readInt32Fake :: FakeByteBuffer -> Int -> (FakeByteBuffer, Int32)
readInt32Fake fbb offset =
let
bytes :: [Word8]
bytes = reverse -- Data.Binary always uses big endian.
bytes = fixEndianess
$ Vector.toList
$ Vector.take (sizeOf (4 :: Int32))
$ Vector.drop offset (fbbVector fbb)
Expand All @@ -74,7 +81,7 @@ writeInt32Fake :: FakeByteBuffer -> Int -> Int32 -> (FakeByteBuffer, ())
writeInt32Fake fbb offset value =
let
bytes :: [Word8]
bytes = reverse (LBS.unpack (encode value))
bytes = fixEndianess (LBS.unpack (encode value))

indexValues :: [(Int, Word8)]
indexValues = zip [offset .. offset + length bytes - 1] bytes
Expand Down

0 comments on commit 12fc98e

Please sign in to comment.