1 -- !!! simple tests of primitive arrays
3 module Main ( main ) where
16 (test_chars ++ "\n" ++
19 test_floats ++ "\n" ++
20 test_doubles ++ "\n" ++
24 -- Arr# Char# -------------------------------------------
25 -- (main effort is in packString#)
29 = let str = reverse "Now is the time for all good men to come to...\n"
32 _ccall_ fprintf (``stdout''::Addr) "%d %s\n" (93::Int) str >>
33 _ccall_ fflush (``stdout''::Addr) >>
37 -- Arr# Int# -------------------------------------------
43 shows (lookup_range arr# 42# 416#) "\n"
45 f :: Int -> ByteArray Int
49 -- allocate an array of the specified size
50 newIntArray (0, (size-1)) >>= \ arr# ->
52 -- fill in all elements; elem i has i^2 put in it
53 fill_in arr# 0# (size# -# 1#) >>
59 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
61 fill_in arr_in# first# last#
62 = if (first# ># last#)
64 else writeIntArray arr_in# (I# first#) (I# (first# *# first#)) >>
65 fill_in arr_in# (first# +# 1#) last#
67 lookup_range :: ByteArray Int -> Int# -> Int# -> [Int]
68 lookup_range arr from# to#
71 else (indexIntArray arr (I# from#))
72 : (lookup_range arr (from# +# 1#) to#)
74 -- Arr# Addr# -------------------------------------------
80 shows (lookup_range arr# 42# 416#) "\n"
82 f :: Int -> ByteArray Int
86 -- allocate an array of the specified size
87 newAddrArray (0, (size-1)) >>= \ arr# ->
89 -- fill in all elements; elem i has i^2 put in it
90 fill_in arr# 0# (size# -# 1#) >>
96 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
98 fill_in arr_in# first# last#
99 = if (first# ># last#)
101 else writeAddrArray arr_in# (I# first#)
102 (A# (int2Addr# (first# *# first#))) >>
103 fill_in arr_in# (first# +# 1#) last#
105 lookup_range :: ByteArray Int -> Int# -> Int# -> [ Int ]
106 lookup_range arr from# to#
108 a2i (A# a#) = I# (addr2Int# a#)
112 else (a2i (indexAddrArray arr (I# from#)))
113 : (lookup_range arr (from# +# 1#) to#)
115 -- Arr# Float# -------------------------------------------
117 test_floats :: String
121 shows (lookup_range arr# 42# 416#) "\n"
123 f :: Int -> ByteArray Int
127 -- allocate an array of the specified size
128 newFloatArray (0, (size-1)) >>= \ arr# ->
130 -- fill in all elements; elem i has "i * pi" put in it
131 fill_in arr# 0# (size# -# 1#) >>
134 freezeFloatArray arr#
137 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
139 fill_in arr_in# first# last#
140 = if (first# ># last#)
142 {- else let e = ((fromInt (I# first#)) * pi)
143 in trace (show e) $ writeFloatArray arr_in# (I# first#) e >>
144 fill_in arr_in# (first# +# 1#) last#
146 else writeFloatArray arr_in# (I# first#) ((fromInt (I# first#)) * pi) >>
147 fill_in arr_in# (first# +# 1#) last#
149 lookup_range :: ByteArray Int -> Int# -> Int# -> [Float]
150 lookup_range arr from# to#
153 else (indexFloatArray arr (I# from#))
154 : (lookup_range arr (from# +# 1#) to#)
156 -- Arr# Double# -------------------------------------------
158 test_doubles :: String
162 shows (lookup_range arr# 42# 416#) "\n"
164 f :: Int -> ByteArray Int
168 -- allocate an array of the specified size
169 newDoubleArray (0, (size-1)) >>= \ arr# ->
171 -- fill in all elements; elem i has "i * pi" put in it
172 fill_in arr# 0# (size# -# 1#) >>
175 freezeDoubleArray arr#
178 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
180 fill_in arr_in# first# last#
181 = if (first# ># last#)
183 else writeDoubleArray arr_in# (I# first#) ((fromInt (I# first#)) * pi) >>
184 fill_in arr_in# (first# +# 1#) last#
186 lookup_range :: ByteArray Int -> Int# -> Int# -> [Double]
187 lookup_range arr from# to#
190 else (indexDoubleArray arr (I# from#))
191 : (lookup_range arr (from# +# 1#) to#)
193 -- Arr# (Ratio Int) (ptrs) ---------------------------------
194 -- just like Int# test
200 shows (lookup_range arr# 42 416) "\n"
202 f :: Int -> Array Int (Ratio Int)
206 newArray (1, size) (3 % 5) >>= \ arr# ->
207 -- don't fill in the whole thing
208 fill_in arr# 1 400 >>
212 fill_in :: MutableArray s Int (Ratio Int) -> Int -> Int -> ST s ()
214 fill_in arr_in# first last
217 else writeArray arr_in# first (fromInt (first * first)) >>
218 fill_in arr_in# (first + 1) last
220 lookup_range :: Array Int (Ratio Int) -> Int -> Int -> [Ratio Int]
221 lookup_range array from too
224 else (array ! from) : (lookup_range array (from + 1) too)