1 -- !!! simple tests of primitive arrays
3 module Main ( main ) where
12 import Int( Num(fromInt) )
13 import CString (packString)
19 (test_chars ++ "\n" ++
22 test_floats ++ "\n" ++
23 test_doubles ++ "\n" ++
27 -- Arr# Char# -------------------------------------------
28 -- (main effort is in packString#)
30 foreign label "stdout" addrOfStdout :: Addr
33 stdout = indexAddrOffAddr addrOfStdout 0
37 = let str = reverse "Now is the time for all good men to come to...\n"
40 _ccall_ fprintf stdout (packString "%d %s\n") (93::Int) (packString str) >>
41 _ccall_ fflush stdout >>
45 -- Arr# Int# -------------------------------------------
51 shows (lookup_range arr# 42# 416#) "\n"
53 f :: Int -> ByteArray Int
57 -- allocate an array of the specified size
58 newIntArray (0, (size-1)) >>= \ arr# ->
60 -- fill in all elements; elem i has i^2 put in it
61 fill_in arr# 0# (size# -# 1#) >>
67 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
69 fill_in arr_in# first# last#
70 = if (first# ># last#)
72 else writeIntArray arr_in# (I# first#) (I# (first# *# first#)) >>
73 fill_in arr_in# (first# +# 1#) last#
75 lookup_range :: ByteArray Int -> Int# -> Int# -> [Int]
76 lookup_range arr from# to#
79 else (indexIntArray arr (I# from#))
80 : (lookup_range arr (from# +# 1#) to#)
82 -- Arr# Addr# -------------------------------------------
88 shows (lookup_range arr# 42# 416#) "\n"
90 f :: Int -> ByteArray Int
94 -- allocate an array of the specified size
95 newAddrArray (0, (size-1)) >>= \ arr# ->
97 -- fill in all elements; elem i has i^2 put in it
98 fill_in arr# 0# (size# -# 1#) >>
104 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
106 fill_in arr_in# first# last#
107 = if (first# ># last#)
109 else writeAddrArray arr_in# (I# first#)
110 (A# (int2Addr# (first# *# first#))) >>
111 fill_in arr_in# (first# +# 1#) last#
113 lookup_range :: ByteArray Int -> Int# -> Int# -> [ Int ]
114 lookup_range arr from# to#
116 a2i (A# a#) = I# (addr2Int# a#)
120 else (a2i (indexAddrArray arr (I# from#)))
121 : (lookup_range arr (from# +# 1#) to#)
123 -- Arr# Float# -------------------------------------------
125 test_floats :: String
129 shows (lookup_range arr# 42# 416#) "\n"
131 f :: Int -> ByteArray Int
135 -- allocate an array of the specified size
136 newFloatArray (0, (size-1)) >>= \ arr# ->
138 -- fill in all elements; elem i has "i * pi" put in it
139 fill_in arr# 0# (size# -# 1#) >>
145 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
147 fill_in arr_in# first# last#
148 = if (first# ># last#)
150 {- else let e = ((fromInt (I# first#)) * pi)
151 in trace (show e) $ writeFloatArray arr_in# (I# first#) e >>
152 fill_in arr_in# (first# +# 1#) last#
154 else writeFloatArray arr_in# (I# first#) ((fromInt (I# first#)) * pi) >>
155 fill_in arr_in# (first# +# 1#) last#
157 lookup_range :: ByteArray Int -> Int# -> Int# -> [Float]
158 lookup_range arr from# to#
161 else (indexFloatArray arr (I# from#))
162 : (lookup_range arr (from# +# 1#) to#)
164 -- Arr# Double# -------------------------------------------
166 test_doubles :: String
170 shows (lookup_range arr# 42# 416#) "\n"
172 f :: Int -> ByteArray Int
176 -- allocate an array of the specified size
177 newDoubleArray (0, (size-1)) >>= \ arr# ->
179 -- fill in all elements; elem i has "i * pi" put in it
180 fill_in arr# 0# (size# -# 1#) >>
186 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
188 fill_in arr_in# first# last#
189 = if (first# ># last#)
191 else writeDoubleArray arr_in# (I# first#) ((fromInt (I# first#)) * pi) >>
192 fill_in arr_in# (first# +# 1#) last#
194 lookup_range :: ByteArray Int -> Int# -> Int# -> [Double]
195 lookup_range arr from# to#
198 else (indexDoubleArray arr (I# from#))
199 : (lookup_range arr (from# +# 1#) to#)
201 -- Arr# (Ratio Int) (ptrs) ---------------------------------
202 -- just like Int# test
208 shows (lookup_range arr# 42 416) "\n"
210 f :: Int -> Array Int (Ratio Int)
214 newSTArray (1, size) (3 % 5) >>= \ arr# ->
215 -- don't fill in the whole thing
216 fill_in arr# 1 400 >>
220 fill_in :: STArray s Int (Ratio Int) -> Int -> Int -> ST s ()
222 fill_in arr_in# first last
225 else writeSTArray arr_in# first (fromInt (first * first)) >>
226 fill_in arr_in# (first + 1) last
228 lookup_range :: Array Int (Ratio Int) -> Int -> Int -> [Ratio Int]
229 lookup_range array from too
232 else (array ! from) : (lookup_range array (from + 1) too)