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#)
32 = let str = reverse "Now is the time for all good men to come to...\n"
35 _ccall_ fprintf (``stdout''::Addr) (packString "%d %s\n") (93::Int) (packString str) >>
36 _ccall_ fflush (``stdout''::Addr) >>
40 -- Arr# Int# -------------------------------------------
46 shows (lookup_range arr# 42# 416#) "\n"
48 f :: Int -> ByteArray Int
52 -- allocate an array of the specified size
53 newIntArray (0, (size-1)) >>= \ arr# ->
55 -- fill in all elements; elem i has i^2 put in it
56 fill_in arr# 0# (size# -# 1#) >>
62 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
64 fill_in arr_in# first# last#
65 = if (first# ># last#)
67 else writeIntArray arr_in# (I# first#) (I# (first# *# first#)) >>
68 fill_in arr_in# (first# +# 1#) last#
70 lookup_range :: ByteArray Int -> Int# -> Int# -> [Int]
71 lookup_range arr from# to#
74 else (indexIntArray arr (I# from#))
75 : (lookup_range arr (from# +# 1#) to#)
77 -- Arr# Addr# -------------------------------------------
83 shows (lookup_range arr# 42# 416#) "\n"
85 f :: Int -> ByteArray Int
89 -- allocate an array of the specified size
90 newAddrArray (0, (size-1)) >>= \ arr# ->
92 -- fill in all elements; elem i has i^2 put in it
93 fill_in arr# 0# (size# -# 1#) >>
99 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
101 fill_in arr_in# first# last#
102 = if (first# ># last#)
104 else writeAddrArray arr_in# (I# first#)
105 (A# (int2Addr# (first# *# first#))) >>
106 fill_in arr_in# (first# +# 1#) last#
108 lookup_range :: ByteArray Int -> Int# -> Int# -> [ Int ]
109 lookup_range arr from# to#
111 a2i (A# a#) = I# (addr2Int# a#)
115 else (a2i (indexAddrArray arr (I# from#)))
116 : (lookup_range arr (from# +# 1#) to#)
118 -- Arr# Float# -------------------------------------------
120 test_floats :: String
124 shows (lookup_range arr# 42# 416#) "\n"
126 f :: Int -> ByteArray Int
130 -- allocate an array of the specified size
131 newFloatArray (0, (size-1)) >>= \ arr# ->
133 -- fill in all elements; elem i has "i * pi" put in it
134 fill_in arr# 0# (size# -# 1#) >>
140 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
142 fill_in arr_in# first# last#
143 = if (first# ># last#)
145 {- else let e = ((fromInt (I# first#)) * pi)
146 in trace (show e) $ writeFloatArray arr_in# (I# first#) e >>
147 fill_in arr_in# (first# +# 1#) last#
149 else writeFloatArray arr_in# (I# first#) ((fromInt (I# first#)) * pi) >>
150 fill_in arr_in# (first# +# 1#) last#
152 lookup_range :: ByteArray Int -> Int# -> Int# -> [Float]
153 lookup_range arr from# to#
156 else (indexFloatArray arr (I# from#))
157 : (lookup_range arr (from# +# 1#) to#)
159 -- Arr# Double# -------------------------------------------
161 test_doubles :: String
165 shows (lookup_range arr# 42# 416#) "\n"
167 f :: Int -> ByteArray Int
171 -- allocate an array of the specified size
172 newDoubleArray (0, (size-1)) >>= \ arr# ->
174 -- fill in all elements; elem i has "i * pi" put in it
175 fill_in arr# 0# (size# -# 1#) >>
181 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
183 fill_in arr_in# first# last#
184 = if (first# ># last#)
186 else writeDoubleArray arr_in# (I# first#) ((fromInt (I# first#)) * pi) >>
187 fill_in arr_in# (first# +# 1#) last#
189 lookup_range :: ByteArray Int -> Int# -> Int# -> [Double]
190 lookup_range arr from# to#
193 else (indexDoubleArray arr (I# from#))
194 : (lookup_range arr (from# +# 1#) to#)
196 -- Arr# (Ratio Int) (ptrs) ---------------------------------
197 -- just like Int# test
203 shows (lookup_range arr# 42 416) "\n"
205 f :: Int -> Array Int (Ratio Int)
209 newSTArray (1, size) (3 % 5) >>= \ arr# ->
210 -- don't fill in the whole thing
211 fill_in arr# 1 400 >>
215 fill_in :: STArray s Int (Ratio Int) -> Int -> Int -> ST s ()
217 fill_in arr_in# first last
220 else writeSTArray arr_in# first (fromInt (first * first)) >>
221 fill_in arr_in# (first + 1) last
223 lookup_range :: Array Int (Ratio Int) -> Int -> Int -> [Ratio Int]
224 lookup_range array from too
227 else (array ! from) : (lookup_range array (from + 1) too)