1 -- !!! simple tests of primitive arrays
3 module Main ( main ) where
11 import Int( Num(fromInt) )
17 (test_chars ++ "\n" ++
20 test_floats ++ "\n" ++
21 test_doubles ++ "\n" ++
25 -- Arr# Char# -------------------------------------------
26 -- (main effort is in packString#)
30 = let str = reverse "Now is the time for all good men to come to...\n"
33 _ccall_ fprintf (``stdout''::Addr) "%d %s\n" (93::Int) str >>
34 _ccall_ fflush (``stdout''::Addr) >>
38 -- Arr# Int# -------------------------------------------
44 shows (lookup_range arr# 42# 416#) "\n"
46 f :: Int -> ByteArray Int
50 -- allocate an array of the specified size
51 newIntArray (0, (size-1)) >>= \ arr# ->
53 -- fill in all elements; elem i has i^2 put in it
54 fill_in arr# 0# (size# -# 1#) >>
60 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
62 fill_in arr_in# first# last#
63 = if (first# ># last#)
65 else writeIntArray arr_in# (I# first#) (I# (first# *# first#)) >>
66 fill_in arr_in# (first# +# 1#) last#
68 lookup_range :: ByteArray Int -> Int# -> Int# -> [Int]
69 lookup_range arr from# to#
72 else (indexIntArray arr (I# from#))
73 : (lookup_range arr (from# +# 1#) to#)
75 -- Arr# Addr# -------------------------------------------
81 shows (lookup_range arr# 42# 416#) "\n"
83 f :: Int -> ByteArray Int
87 -- allocate an array of the specified size
88 newAddrArray (0, (size-1)) >>= \ arr# ->
90 -- fill in all elements; elem i has i^2 put in it
91 fill_in arr# 0# (size# -# 1#) >>
97 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
99 fill_in arr_in# first# last#
100 = if (first# ># last#)
102 else writeAddrArray arr_in# (I# first#)
103 (A# (int2Addr# (first# *# first#))) >>
104 fill_in arr_in# (first# +# 1#) last#
106 lookup_range :: ByteArray Int -> Int# -> Int# -> [ Int ]
107 lookup_range arr from# to#
109 a2i (A# a#) = I# (addr2Int# a#)
113 else (a2i (indexAddrArray arr (I# from#)))
114 : (lookup_range arr (from# +# 1#) to#)
116 -- Arr# Float# -------------------------------------------
118 test_floats :: String
122 shows (lookup_range arr# 42# 416#) "\n"
124 f :: Int -> ByteArray Int
128 -- allocate an array of the specified size
129 newFloatArray (0, (size-1)) >>= \ arr# ->
131 -- fill in all elements; elem i has "i * pi" put in it
132 fill_in arr# 0# (size# -# 1#) >>
135 freezeFloatArray arr#
138 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
140 fill_in arr_in# first# last#
141 = if (first# ># last#)
143 {- else let e = ((fromInt (I# first#)) * pi)
144 in trace (show e) $ writeFloatArray arr_in# (I# first#) e >>
145 fill_in arr_in# (first# +# 1#) last#
147 else writeFloatArray arr_in# (I# first#) ((fromInt (I# first#)) * pi) >>
148 fill_in arr_in# (first# +# 1#) last#
150 lookup_range :: ByteArray Int -> Int# -> Int# -> [Float]
151 lookup_range arr from# to#
154 else (indexFloatArray arr (I# from#))
155 : (lookup_range arr (from# +# 1#) to#)
157 -- Arr# Double# -------------------------------------------
159 test_doubles :: String
163 shows (lookup_range arr# 42# 416#) "\n"
165 f :: Int -> ByteArray Int
169 -- allocate an array of the specified size
170 newDoubleArray (0, (size-1)) >>= \ arr# ->
172 -- fill in all elements; elem i has "i * pi" put in it
173 fill_in arr# 0# (size# -# 1#) >>
176 freezeDoubleArray arr#
179 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
181 fill_in arr_in# first# last#
182 = if (first# ># last#)
184 else writeDoubleArray arr_in# (I# first#) ((fromInt (I# first#)) * pi) >>
185 fill_in arr_in# (first# +# 1#) last#
187 lookup_range :: ByteArray Int -> Int# -> Int# -> [Double]
188 lookup_range arr from# to#
191 else (indexDoubleArray arr (I# from#))
192 : (lookup_range arr (from# +# 1#) to#)
194 -- Arr# (Ratio Int) (ptrs) ---------------------------------
195 -- just like Int# test
201 shows (lookup_range arr# 42 416) "\n"
203 f :: Int -> Array Int (Ratio Int)
207 newArray (1, size) (3 % 5) >>= \ arr# ->
208 -- don't fill in the whole thing
209 fill_in arr# 1 400 >>
213 fill_in :: MutableArray s Int (Ratio Int) -> Int -> Int -> ST s ()
215 fill_in arr_in# first last
218 else writeArray arr_in# first (fromInt (first * first)) >>
219 fill_in arr_in# (first + 1) last
221 lookup_range :: Array Int (Ratio Int) -> Int -> Int -> [Ratio Int]
222 lookup_range array from too
225 else (array ! from) : (lookup_range array (from + 1) too)