Skip to content
Snippets Groups Projects
Commit 7f0f325c authored by Simon Marlow's avatar Simon Marlow
Browse files

[project @ 1997-12-19 12:22:57 by simonm]

Move array tests from ghc/lib to ghc/tests and put them in our test
framework.
parent 9b422952
No related merge requests found
Showing
with 0 additions and 184 deletions
-- Simple array creation
import Array
main =
let a1 = array (1,3) (zip [2,3,1] ['a'..'d']) in
print a1
-- Result:
TOP=../../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/target.mk
-- Array creation, (index,value) list with duplicates.
--
-- Haskell library report 1.3 (and earlier) specifies
-- that `array' values created with lists containing dups,
-- are undefined ( _|_ ).
--
-- GHC-2.02 (and earlier) does not flag this as such, the
-- last (index,value) is instead used.
--
-- The report also specifies `array' is spine strict in
-- the (index,value) list argument and to check the
-- validity of the index values upon creation, it also
-- strict for the indices. To test this, we do (a!1)
-- twice, expecting to see the same value..
--
import Array
main =
let a1 = array (1,3) (zip (1:[1..3]) ['a'..'d']) in
print (a1!1) >>
print a1 >>
print (a1!1)
TOP=../../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/target.mk
-- Array creation, (index,value) list with out of bound index.
--
-- Haskell library report 1.3 (and earlier) specifies
-- that `array' values created with lists containing out-of-bounds indices,
-- are undefined ( _|_ ).
--
-- GHC implementation of `array' catches this (or, rather,
-- `index' does) - the argument list to `array' is defined
-- to have its spine be evaluated - so the indexing below
-- should cause a failure.
--
import Array
main =
let a1 = array (1,3) (zip ([1..4]) ['a'..'d']) in
print (a1!2)
TOP=../../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/target.mk
-- Array - accessing undefined element
--
-- Sample Haskell implementation in the 1.3 Lib report defines
-- this as being undefined/error.
import Array
main =
let a1 = array (1,3) (zip ([1,2]) ['a'..'d']) in
print (a1!3)
-- output: Fail: (Array.!): undefined array element
TOP=../../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/target.mk
-- Array - recurrences
--
-- array does not evaluate the elements.
--
import Array
main =
let
a1 = array (1,100) ((1,1::Integer):[(i,i*a1!(i-1))|i<-[2..100]])
in
print a1
--
TOP=../../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/target.mk
-- Array - empty arrays
--
-- print a couple of them to try to expose empty arrays
-- to a GC or two.
import Array
main =
let
a1 = array (1,0) []
in
print (take 300 $ repeat (a1 :: Array Int Int))
TOP=../../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/target.mk
-- Array - accessing empty arrays
--
-- empty arrays are legal, but indexing them is undefined!
--
import Array
main =
let
a1 = array (1,0) [(1,'a')]
in
print (a1!0)
TOP=../../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/target.mk
-- Array - out-of-range (index,value) pairs
--
-- supplying a list containing one or more pairs
-- with out-of-range index is undefined.
--
--
import Array
main =
let
a1 = array (1,0) []
a2 = array (0,1) (zip [0..] ['a'..'z'])
in
print (a1::Array Int Int) >> print a2
TOP=../../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/target.mk
-- Array - derived ops
--
-- testing the well-behavedness of
-- derived ops for empty and non-empty arrays
--
import Array
main =
let
a1 = array (1,0) ([]::[(Int,Int)])
a2 = array (1,26) (zip [1..] ['a'..'z'])
dump a = (bounds a, indices a, elems a, assocs a)
in
print (dump a1) >>
print (dump a2)
TOP=../../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/target.mk
--
-- Array - accumulated arrays
--
--
module Main(main) where
import Array
import Ix
hist :: (Ix a, Num b) => (a,a) -> [a] -> Array a b
hist bnds is = accumArray (+) 0 bnds [(i,1) | i <- is , inRange bnds i]
main =
let
a1 = hist (0,10) (concat $ take 2 $ repeat [1..20])
in
print a1
TOP=../../../..
include $(TOP)/mk/boilerplate.mk
include $(TOP)/mk/target.mk
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment