Source

hs-matrix-market / runTests.hs

Full commit
import Test.HUnit
import Control.Monad
import Data.Array
import Data.Array.IO

import Data.MatrixMarket

main = runTestTT tests

tests = TestList
  [ TestLabel "read real sparse" test_read_real_sparse
  , TestLabel "read complex sparse" test_read_complex_sparse
  , TestLabel "read real sparse (freeformat)" test_read_real_sparse_freeformat
  , TestLabel "read real dense" test_read_real_dense
  , TestLabel "convert to array" test_toArray_general
  , TestLabel "convert to mutable array" test_toArrayM_general
  , TestLabel "symmetric complement dense" test_symmetric_dense
  , TestLabel "symmetric complement sparse" test_symmetric_sparse
  , TestLabel "skew-symmetric complement dense" test_skewsymmetric_dense
  , TestLabel "hermitian complement dense" test_hermitian_sparse
  , TestLabel "dump int dense" test_dump_int_dense
  , TestLabel "dump int sparse" test_dump_int_sparse
  ]

test_read_real_sparse = TestCase $ do
  let f = "examples/coord-real.mtx"
  r <- readMM `liftM` readFile f :: IO (ReadMatrix Double)
  assertEqual "real sparse"
    (
      Right ( MM { mm'data = CoordinateM { coords'm = CM { cm'rows = 5 ,
      cm'cols = 5, cm'size = 8 , cm'values =
      [((1,1),1.0),((2,2),10.5),((4,2),250.5)
      ,((3,3),1.5e-2),((1,4),6.0),((4,4),-280.0)
      ,((4,5),33.32),((5,5),12.0)]}} , mm'field = MReal , mm'symmetry =
      General , mm'comments = [" A 5x5 sparse matrix with 8 nonzeros"]})
    ) r

test_read_complex_sparse = TestCase $ do
  let f = "examples/coord-complex.mtx"
  r <- readMM `liftM` readFile f :: IO (ReadMatrix (Complex Double))
  assertEqual "complex sparse"
    (
      Right (MM {mm'data = CoordinateM {coords'm = CM {cm'rows = 5, cm'cols =
      5, cm'size = 7, cm'values = [((1,1),1.0 :+ 0.0),((2,2),10.5 :+
      0.0),((4,2),250.5 :+ 22.22),((3,3),1.5e-2 :+ 0.0),((4,4),(-280.0) :+
      0.0),((5,5),12.0 :+ 0.0),((5,4),0.0 :+ 33.32)]}}, mm'field = MComplex,
      mm'symmetry = Hermitian, mm'comments = []})
    ) r

test_read_real_sparse_freeformat = TestCase $ do
  let f = "examples/coord-real-freeformat.mtx"
  r <- readMM `liftM` readFile f :: IO (ReadMatrix Double)
  assertEqual "real sparse (freeformat)"
    (
      Right (MM {mm'data = CoordinateM {coords'm = CM {cm'rows = 5, cm'cols =
      5, cm'size = 8, cm'values = [((1,1),1.0),((2,2),10.5),((3,3),1.5e-2)
      ,((4,4),-280.0),((5,5),12.0),((1,4),6.0),((4,2),250.5),((4,5),33.32)]}},
      mm'field = MReal, mm'symmetry = General, mm'comments =
      [" ---------------------------------------------------------------" 
      ," Same matrix as in Example 1"
      ," ---------------------------------------------------------------"
      ,""
      ," See http://math.nist.gov/MartrixMarket for more information.",""]})
    ) r

test_read_real_dense = TestCase $ do
  let f = "examples/array-real.mtx"
  r <- readMM `liftM` readFile f :: IO (ReadMatrix Double)
  assertEqual "real dense"
    (
      Right (MM {mm'data = ArrayM {array'm = AM {am'rows = 4, am'cols = 3,
      am'values = [1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0]}},
      mm'field = MReal, mm'symmetry = General, mm'comments =
      [" A 4x3 dense matrix"]})
    ) r

test_toArray_general = TestCase $ do
  let f = "examples/array-real.mtx"
  r <- readMM `liftM` readFile f :: IO (ReadMatrix Double)
  case r of
    (Right m) ->
      assertEqual "immutable array"
        ( listArray ((0,0), (3,2))
                  [1, 5, 9
                  , 2, 6, 10
                  , 3, 7, 11
                  , 4, 8, 12 :: Double ] ) (toArray 0 m)
    (Left e) -> assertFailure $ "Cannot read matrix file: " ++ show e

test_toArrayM_general = TestCase $ do
  let f = "examples/array-real.mtx"
  r <- readMM `liftM` readFile f :: IO (ReadMatrix Double)
  case r of
    (Right m) -> do
      ioa <- toArrayM 0 m :: IO (IOArray (Int,Int) Double)
      a <- freeze ioa
      assertEqual "mutable array"
        ( listArray ((0,0), (3,2))
                  [1, 5, 9
                  , 2, 6, 10
                  , 3, 7, 11
                  , 4, 8, 12 :: Double ] ) a
    (Left e) -> assertFailure $ "Cannot read matrix file: " ++ show e


test_symmetric_dense = TestCase $
  let sym_dense = unlines
       [ "%%MatrixMarket  matrix array integer symmetric"
       , "3 3"
       , "1", "2", "3", "4", "5", "6" ]   -- defined only below diagonal
      r = readMM sym_dense :: ReadMatrix Int
  in  case r of
    (Left e) -> assertFailure $ "Cannot read matrix: " ++ show e
    (Right m) ->
      assertEqual "dense matrix symmetric complement"
        ( listArray ((1,1), (3,3)) [ 1, 2, 3, 2, 4, 5, 3, 5, 6 ] )
        ( toArray 1 m)

test_symmetric_sparse = TestCase $
  let sym_sparse = unlines
       [ "%%MatrixMarket  matrix coordinate integer symmetric"
       , "3 3 6"
       , "1 1 1", "2 1 2", "3 1 3"
       , "2 2 4", "3 2 5"
       , "3 3 6" ]   -- defined only below diagonal
      r = readMM sym_sparse :: ReadMatrix Int
  in  case r of
    (Left e) -> assertFailure $ "Cannot read matrix: " ++ show e
    (Right m) ->
      assertEqual "dense matrix symmetric complement"
        ( listArray ((1,1), (3,3)) [ 1, 2, 3, 2, 4, 5, 3, 5, 6 ] )
        ( toArray 1 m)

test_skewsymmetric_dense = TestCase $
  let skew_dense = unlines
       [ "%%MatrixMarket  matrix array integer skew-symmetric"
       , "3 3"
       , "1", "2", "3", "4", "5", "6" ]   -- defined only below diagonal
      r = readMM skew_dense :: ReadMatrix Int
  in  case r of
    (Left e) -> assertFailure $ "Cannot read matrix: " ++ show e
    (Right m) ->
      assertEqual "dense matrix skew-symmetric complement"
        ( listArray ((1,1), (3,3)) [ 1, -2, -3, 2, 4, -5, 3, 5, 6 ] )
        ( toArray 1 m)

test_hermitian_sparse = TestCase $
  let herm_sparse = unlines
       [ "%%MatrixMarket  matrix coordinate complex hermitian"
       , "3 3 4"
       , "1 1 1 2"
       , "2 1 3 4"
       , "3 2 5 6"
       , "3 3 7 8"
       ]   -- defined only below diagonal
      r = readMM herm_sparse :: ReadMatrix (Complex Double)
  in  case r of
    (Left e) -> assertFailure $ "Cannot read matrix: " ++ show e
    (Right m) ->
      assertEqual "sparse matrix Hermitian complement"
        ( listArray ((1,1), (3,3))
            [ 1 :+ 2, 3 :+ (-4), 0
            , 3 :+ 4, 0        , 5 :+ (-6)
            , 0     , 5 :+ 6   , 7 :+ 8 ] )
        ( toArray 1 m)

test_dump_int_dense = TestCase $ do
  let m = MM
          { mm'data =
              ArrayM $ AM
              { am'rows = 2
              , am'cols = 3
              , am'values = [ 1..6::Int ] }  -- column major order
          , mm'field = MInt
          , mm'symmetry = General
          , mm'comments = ["Test"] }
  let mm = dumpMM m
  assertEqual "written MatrixMarket data"
    ( [ "%%MatrixMarket matrix array integer general"
      , "%Test"
      , "2 3"
      , "1", "2", "3", "4", "5", "6" ]
    ) (lines mm)

test_dump_int_sparse = TestCase $ do
  let m = MM
          { mm'data =
              CoordinateM $ CM
              { cm'rows = 3
              , cm'cols = 3
              , cm'size = 6
              , cm'values = [ ((i,j),(i+j)) | j <- [1..3], i <- [j..3] ] }
          , mm'field = MInt
          , mm'symmetry = Symmetric
          , mm'comments = [] }
  let mm = dumpMM m
  assertEqual "written sparse MatrixMarket data"
    ( [ "%%MatrixMarket matrix coordinate integer symmetric"
      , "3 3 6"
      , "1 1 2"
      , "2 1 3"
      , "3 1 4"
      , "2 2 4"
      , "3 2 5"
      , "3 3 6" ]
    ) (lines mm)