content: import Data.Primitive.ByteArray
  import Data.Word
  
  main :: IO ()
  main = do
    -- ---------------------------------------------------------------------
    -- MEMSET (setByteArray uses memset internally)
    -- ---------------------------------------------------------------------
    dst <- newPinnedByteArray 32
    unfreezeAndPrint "0: " dst
    -- Expected (little-endian):
    -- 0: [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]
  
    -- WORD32
    ---------
    setByteArray dst 0 1 (0xeeeeeeee :: Word32)
    setByteArray dst 4 2 (0xff1234ff :: Word32)
    unfreezeAndPrint "1: " dst
    -- Expected (little-endian):
    -- 1: [0xee, 0xee, 0xee, 0xee, 0xff, 0x34, 0x12, 0xff, 0xff, 0x34, 0x12, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]
    readByteArray dst 0 >>= \w -> print (w == (0xeeeeeeee :: Word32))
    -- Expected (endianness-independent): True
    readByteArray dst 1 >>= \w -> print (w == (0xff1234ff :: Word32))
    -- Expected (endianness-independent): True
  
    -- WORD16
    -- ------
    setByteArray dst 2 2 (0x1122 :: Word16)
    unfreezeAndPrint "2: " dst
    -- Expected (little-endian):
    -- 2: [0xee, 0xee, 0x22, 0x11, 0x22, 0x11, 0x12, 0xff, 0xff, 0x34, 0x12, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0]
    readByteArray dst 1 >>= \w -> print (w == (0x1122 :: Word16))
    -- Expected (endianness-independent): True
  
    -- CHAR
    -- ----
    setByteArray dst 24 2 ('a' :: Char)
    unfreezeAndPrint "3: " dst
    -- Expected (little-endian):
    -- 3: [0xee, 0xee, 0x22, 0x11, 0x22, 0x11, 0x12, 0xff, 0xff, 0x34, 0x12, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x61, 0x0, 0x0, 0x0, 0x61, 0x0, 0x0, 0x0]
    readByteArray dst 6 >>= \c -> print (c == 'a')
    -- Expected (endianness-independent): True
    readByteArray dst 7 >>= \c -> print (c == 'a')
    -- Expected (endianness-independent): True
  
    -- WORD8
    -- -----
    setByteArray dst 30 1 (0xec :: Word8)
    unfreezeAndPrint "4: " dst
    -- Expected (endianness-independent):
    -- 4: [0xee, 0xee, 0x22, 0x11, 0x22, 0x11, 0x12, 0xff, 0xff, 0x34, 0x12, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x61, 0x0, 0x0, 0x0, 0x61, 0x0, 0xec, 0x0]
    readByteArray dst 30 >>= \w -> print (w == (0xec :: Word8))
    -- Expected (endianness-independent): True
  
    -- FLOAT
    -- -----
    setByteArray dst 16 1 (-1 :: Float)
    unfreezeAndPrint "5: " dst
    -- Expected (little-endian):
    -- 5: [0xee, 0xee, 0x22, 0x11, 0x22, 0x11, 0x12, 0xff, 0xff, 0x34, 0x12, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0xbf, 0x0, 0x0, 0x0, 0x0, 0x61, 0x0, 0x0, 0x0, 0x61, 0x0, 0xec, 0x0]
    readByteArray dst 4 >>= \f -> print (f == (-1 :: Float))
    -- Expected (endianness-independent): True
  
    -- DOUBLE
    -- ------
    setByteArray dst 8 1 (100 :: Double)
    unfreezeAndPrint "6: " dst
    -- Expected (little-endian):
    -- TODO
    readByteArray dst 1 >>= \d -> print (d == (100 :: Double))
    -- Expected (endianness-independent): True
  
    -- WORD64
    -- ------
    setByteArray dst 24 1 (9007199254740991 :: Word64) -- Maximum safely represented: 2^53 -1
    unfreezeAndPrint "7: " dst
    -- Expected (little-endian):
    -- 7: [0xee, 0xee, 0x22, 0x11, 0x22, 0x11, 0x12, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x59, 0x40, 0x0, 0x0, 0x80, 0xbf, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x0]
    readByteArray dst 3 >>= \w -> print (w == (9007199254740991 :: Word64))
    -- Expected (endianness-independent): True
  
    -- WORD (should behave as the Word64)
    -- ----------------------------------
    setByteArray dst 0 1 (9007199254740991 :: Word) -- Maximum safely represented: 2^53 -1
    setByteArray dst 8 1 (0x00123ef45ca9687b :: Word)
    unfreezeAndPrint "8: " dst
    -- Expected (little-endian):
    -- 8: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x0, 0x7b, 0x68, 0xa9, 0x5c, 0xf4, 0x3e, 0x12, 0x0, 0x0, 0x0, 0x80, 0xbf, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x0]
    readByteArray dst 0 >>= \w -> print (w == (9007199254740991 :: Word))
    readByteArray dst 1 >>= \w -> print (w == (0x00123ef45ca9687b :: Word))
    -- Expected (endianness-independent): True
  
    -- ---------------------------------------------------------------------
    -- FAILING CASES
    -- ---------------------------------------------------------------------
    -- WORD64 / WORD
    -- -------------
    setByteArray dst 16 1 (0xdddddddddddddddd :: Word)
    setByteArray dst 24 1 (0xcccccccccccccccc :: Word64)
    unfreezeAndPrint "9: " dst
    -- Expected (little-endian):
    -- 9: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x0, 0x7b, 0x68, 0xa9, 0x5c, 0xf4, 0x3e, 0x12, 0x0, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc]
    readByteArray dst 2 >>= \w -> print (w == (0xdddddddddddddddd :: Word))
    readByteArray dst 3 >>= \w -> print (w == (0xcccccccccccccccc :: Word64))
    -- Expected (endianness-independent): True
    where
      unfreezeAndPrint s ba =
        unsafeFreezeByteArray ba >>= \a -> putStr s >> print a
  
  
  