[project @ 2000-07-14 13:28:35 by simonmar]
[ghc-hetmet.git] / ghc / tests / ccall / should_run / fed001.hs
1 import Foreign
2 import Monad
3
4 type CInt  = Int32
5 type CSize = Word32
6
7 foreign export dynamic mkComparator :: (Addr -> Addr -> IO CInt) -> IO Addr
8 foreign import qsort :: Addr -> CSize -> CSize -> Addr -> IO ()
9
10 compareInts :: Addr -> Addr -> IO CInt
11 compareInts a1 a2 = do
12    i1 <- peek a1
13    i2 <- peek a2
14    return (fromIntegral (i1 - i2 :: Int))
15
16 main :: IO ()
17 main = do
18    let values = [ 12, 56, 90, 34, 78 ] :: [Int]
19        n      = length values
20    buf <- mallocElems (head values) n
21    zipWithM_ (pokeElemOff buf) [ 0 .. ] values
22    c <- mkComparator compareInts
23    qsort buf (fromIntegral n) (fromIntegral (sizeOf (head values))) c
24    mapM (peekElemOff buf) [ 0 .. n-1 ] >>= (print :: [Int] -> IO ())