1 -- !!! simple tests of primitive arrays
3 module Main ( main ) where
17 (test_chars ++ "\n" ++
20 test_floats ++ "\n" ++
21 test_doubles ++ "\n" ++
25 -- Arr# Char# -------------------------------------------
26 -- (main effort is in packString#)
32 shows (lookup_range arr# 42# 416#) "\n"
34 f :: Int -> ByteArray Int
38 -- allocate an array of the specified size
39 newCharArray (0, (size-1)) >>= \ arr# ->
41 -- fill in all elements; elem i has "i" put in it
42 fill_in arr# 0# (size# -# 1#) >>
48 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
50 fill_in arr_in# first# last#
51 = if (first# ># last#)
53 else writeCharArray arr_in# (I# first#) ((chr (I# first#))) >>
54 fill_in arr_in# (first# +# 1#) last#
56 lookup_range :: ByteArray Int -> Int# -> Int# -> [Char]
57 lookup_range arr from# to#
60 else (indexCharArray arr (I# from#))
61 : (lookup_range arr (from# +# 1#) to#)
63 -- Arr# Int# -------------------------------------------
69 shows (lookup_range arr# 42# 416#) "\n"
71 f :: Int -> ByteArray Int
75 -- allocate an array of the specified size
76 newIntArray (0, (size-1)) >>= \ arr# ->
78 -- fill in all elements; elem i has i^2 put in it
79 fill_in arr# 0# (size# -# 1#) >>
85 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
87 fill_in arr_in# first# last#
88 = if (first# ># last#)
90 else writeIntArray arr_in# (I# first#) (I# (first# *# first#)) >>
91 fill_in arr_in# (first# +# 1#) last#
93 lookup_range :: ByteArray Int -> Int# -> Int# -> [Int]
94 lookup_range arr from# to#
97 else (indexIntArray arr (I# from#))
98 : (lookup_range arr (from# +# 1#) to#)
100 -- Arr# Addr# -------------------------------------------
106 shows (lookup_range arr# 42# 416#) "\n"
108 f :: Int -> ByteArray Int
112 -- allocate an array of the specified size
113 newAddrArray (0, (size-1)) >>= \ arr# ->
115 -- fill in all elements; elem i has i^2 put in it
116 fill_in arr# 0# (size# -# 1#) >>
122 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
124 fill_in arr_in# first# last#
125 = if (first# ># last#)
127 else writeAddrArray arr_in# (I# first#)
128 (A# (int2Addr# (first# *# first#))) >>
129 fill_in arr_in# (first# +# 1#) last#
131 lookup_range :: ByteArray Int -> Int# -> Int# -> [ Int ]
132 lookup_range arr from# to#
134 a2i (A# a#) = I# (addr2Int# a#)
138 else (a2i (indexAddrArray arr (I# from#)))
139 : (lookup_range arr (from# +# 1#) to#)
141 -- Arr# Float# -------------------------------------------
143 test_floats :: String
147 shows (lookup_range arr# 42# 416#) "\n"
149 f :: Int -> ByteArray Int
153 -- allocate an array of the specified size
154 newFloatArray (0, (size-1)) >>= \ arr# ->
156 -- fill in all elements; elem i has "i * pi" put in it
157 fill_in arr# 0# (size# -# 1#) >>
163 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
165 fill_in arr_in# first# last#
166 = if (first# ># last#)
168 {- else let e = ((fromInt (I# first#)) * pi)
169 in trace (show e) $ writeFloatArray arr_in# (I# first#) e >>
170 fill_in arr_in# (first# +# 1#) last#
172 else writeFloatArray arr_in# (I# first#) ((fromInt (I# first#)) * pi) >>
173 fill_in arr_in# (first# +# 1#) last#
175 lookup_range :: ByteArray Int -> Int# -> Int# -> [Float]
176 lookup_range arr from# to#
179 else (indexFloatArray arr (I# from#))
180 : (lookup_range arr (from# +# 1#) to#)
182 -- Arr# Double# -------------------------------------------
184 test_doubles :: String
188 shows (lookup_range arr# 42# 416#) "\n"
190 f :: Int -> ByteArray Int
194 -- allocate an array of the specified size
195 newDoubleArray (0, (size-1)) >>= \ arr# ->
197 -- fill in all elements; elem i has "i * pi" put in it
198 fill_in arr# 0# (size# -# 1#) >>
204 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
206 fill_in arr_in# first# last#
207 = if (first# ># last#)
209 else writeDoubleArray arr_in# (I# first#) ((fromInt (I# first#)) * pi) >>
210 fill_in arr_in# (first# +# 1#) last#
212 lookup_range :: ByteArray Int -> Int# -> Int# -> [Double]
213 lookup_range arr from# to#
216 else (indexDoubleArray arr (I# from#))
217 : (lookup_range arr (from# +# 1#) to#)
219 -- Arr# (Ratio Int) (ptrs) ---------------------------------
220 -- just like Int# test
226 shows (lookup_range arr# 42 416) "\n"
228 f :: Int -> Array Int (Ratio Int)
232 newSTArray (1, size) (3 % 5) >>= \ arr# ->
233 -- don't fill in the whole thing
234 fill_in arr# 1 400 >>
238 fill_in :: STArray s Int (Ratio Int) -> Int -> Int -> ST s ()
240 fill_in arr_in# first last
243 else writeSTArray arr_in# first (fromInt (first * first)) >>
244 fill_in arr_in# (first + 1) last
246 lookup_range :: Array Int (Ratio Int) -> Int -> Int -> [Ratio Int]
247 lookup_range array from too
250 else (array ! from) : (lookup_range array (from + 1) too)