File: TestUtil.hs

package info (click to toggle)
haskell-th-orphans 0.13.16-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 88 kB
  • sloc: haskell: 240; makefile: 2
file content (38 lines) | stat: -rw-r--r-- 872 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE OverloadedStrings #-}

module TestUtil where

#if MIN_VERSION_template_haskell(2,16,0)

import Language.Haskell.TH.Syntax
import qualified Data.ByteString as BS
import qualified Data.ByteString.Internal as BS
import GHC.Prim (Addr#)
import GHC.ForeignPtr (newForeignPtr_)
import GHC.Ptr (Ptr(Ptr))

testBytes :: BS.ByteString
testBytes =
  "test bytes " <>
  (BS.take (len - 2) $ BS.drop 1 $ BS.replicate len 42)
  where
    len = 20 * 1024 * 1024

bsToBytes :: BS.ByteString -> Bytes
bsToBytes bs =
  Bytes
    { bytesPtr = fp
    , bytesOffset = fromIntegral offset
    , bytesSize = fromIntegral size
    }
  where
    (fp, offset, size) = BS.toForeignPtr bs

addrToBs :: Addr# -> Int -> IO BS.ByteString
addrToBs addr len = do
  fp <- newForeignPtr_ (Ptr addr)
  return $ BS.fromForeignPtr fp 0 len

#endif