{- | Test whether alignment of various malloc variants is correct. -}
module Main where

import Foreign
import Control.Monad (liftM2, replicateM, )

data Vector = Vector Double Double

instance Storable Vector where
   sizeOf _ = 16
   alignment _ = 16
   peek p =
      let q = castPtr p
      in  liftM2 Vector (peekElemOff q 0) (peekElemOff q 1)
   poke p (Vector d0 d1) =
      let q = castPtr p
      in  pokeElemOff q 0 d0 >> pokeElemOff q 1 d1


testAlloc0, testAlloc1, testAlloc2 :: Storable a => IO (Ptr a)
testAlloc0 = alloca return
testAlloc1 = malloc -- incorrect on GHC-6.12.3
testAlloc2 = mallocForeignPtrArray 1 >>= \fp -> withForeignPtr fp return

main :: IO ()
main =
   mapM_ print =<<
   fmap
      (filter (\p -> mod (minusPtr (p :: Ptr Vector) nullPtr) 16 /= 0))
      (replicateM 30 testAlloc2)
