1 --!!! simple tests of primitive arrays
3 module Main ( main ) where
5 import PrelBase --ghc1.3
13 (test_chars ++ "\n" ++
16 test_floats ++ "\n" ++
17 test_doubles ++ "\n" ++
21 -- Arr# Char# -------------------------------------------
22 -- (main effort is in packString#)
26 = let str = reverse "Now is the time for all good men to come to...\n"
29 _ccall_ fprintf (``stdout''::Addr) "%d %s\n" 93 str >>
33 -- Arr# Int# -------------------------------------------
39 shows (lookup_range arr# 42# 416#) "\n"
41 f :: Int -> ByteArray Int
45 -- allocate an array of the specified size
46 newIntArray (0, (size-1)) >>= \ arr# ->
48 -- fill in all elements; elem i has i^2 put in it
49 fill_in arr# 0# (size# -# 1#) >>
55 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
57 fill_in arr_in# first# last#
58 = if (first# ># last#)
60 else writeIntArray arr_in# (I# first#) (I# (first# *# first#)) >>
61 fill_in arr_in# (first# +# 1#) last#
63 lookup_range :: ByteArray Int -> Int# -> Int# -> [Int]
64 lookup_range arr from# to#
67 else (indexIntArray arr (I# from#))
68 : (lookup_range arr (from# +# 1#) to#)
70 -- Arr# Addr# -------------------------------------------
76 shows (lookup_range arr# 42# 416#) "\n"
78 f :: Int -> ByteArray Int
82 -- allocate an array of the specified size
83 newAddrArray (0, (size-1)) >>= \ arr# ->
85 -- fill in all elements; elem i has i^2 put in it
86 fill_in arr# 0# (size# -# 1#) >>
92 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
94 fill_in arr_in# first# last#
95 = if (first# ># last#)
97 else writeAddrArray arr_in# (I# first#)
98 (A# (int2Addr# (first# *# first#))) >>
99 fill_in arr_in# (first# +# 1#) last#
101 lookup_range :: ByteArray Int -> Int# -> Int# -> [ Int ]
102 lookup_range arr from# to#
104 a2i (A# a#) = I# (addr2Int# a#)
108 else (a2i (indexAddrArray arr (I# from#)))
109 : (lookup_range arr (from# +# 1#) to#)
111 -- Arr# Float# -------------------------------------------
113 test_floats :: String
117 shows (lookup_range arr# 42# 416#) "\n"
119 f :: Int -> ByteArray Int
123 -- allocate an array of the specified size
124 newFloatArray (0, (size-1)) >>= \ arr# ->
126 -- fill in all elements; elem i has "i * pi" put in it
127 fill_in arr# 0# (size# -# 1#) >>
130 freezeFloatArray arr#
133 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
135 fill_in arr_in# first# last#
136 = if (first# ># last#)
138 else writeFloatArray arr_in# (I# first#) ((fromInt (I# first#)) * pi) >>
139 fill_in arr_in# (first# +# 1#) last#
141 lookup_range :: ByteArray Int -> Int# -> Int# -> [Float]
142 lookup_range arr from# to#
145 else (indexFloatArray arr (I# from#))
146 : (lookup_range arr (from# +# 1#) to#)
148 -- Arr# Double# -------------------------------------------
150 test_doubles :: String
154 shows (lookup_range arr# 42# 416#) "\n"
156 f :: Int -> ByteArray Int
160 -- allocate an array of the specified size
161 newDoubleArray (0, (size-1)) >>= \ arr# ->
163 -- fill in all elements; elem i has "i * pi" put in it
164 fill_in arr# 0# (size# -# 1#) >>
167 freezeDoubleArray arr#
170 fill_in :: MutableByteArray s Int -> Int# -> Int# -> ST s ()
172 fill_in arr_in# first# last#
173 = if (first# ># last#)
175 else writeDoubleArray arr_in# (I# first#) ((fromInt (I# first#)) * pi) >>
176 fill_in arr_in# (first# +# 1#) last#
178 lookup_range :: ByteArray Int -> Int# -> Int# -> [Double]
179 lookup_range arr from# to#
182 else (indexDoubleArray arr (I# from#))
183 : (lookup_range arr (from# +# 1#) to#)
185 -- Arr# (Ratio Int) (ptrs) ---------------------------------
186 -- just like Int# test
192 shows (lookup_range arr# 42 416) "\n"
194 f :: Int -> Array Int (Ratio Int)
198 newArray (1, size) (3 % 5) >>= \ arr# ->
199 -- don't fill in the whole thing
200 fill_in arr# 1 400 >>
204 fill_in :: MutableArray s Int (Ratio Int) -> Int -> Int -> ST s ()
206 fill_in arr_in# first last
209 else writeArray arr_in# first (fromInt (first * first)) >>
210 fill_in arr_in# (first + 1) last
212 lookup_range :: Array Int (Ratio Int) -> Int -> Int -> [Ratio Int]
213 lookup_range array from too
216 else (array ! from) : (lookup_range array (from + 1) too)