From 92b324c007a3184ed9cbf3b3fd9554df93316166 Mon Sep 17 00:00:00 2001 From: Hantao Hui Date: Sun, 12 Dec 2021 12:10:20 +0100 Subject: [PATCH] code fmt; fix failing unit test and doc test --- nalgebra-sparse/src/io/matrix_market.rs | 30 ++++++++----------- nalgebra-sparse/tests/unit.rs | 6 +--- .../tests/unit_tests/matrix_market.rs | 8 ++--- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/nalgebra-sparse/src/io/matrix_market.rs b/nalgebra-sparse/src/io/matrix_market.rs index 1429d814..2620eea6 100644 --- a/nalgebra-sparse/src/io/matrix_market.rs +++ b/nalgebra-sparse/src/io/matrix_market.rs @@ -300,7 +300,7 @@ impl std::error::Error for MatrixMarketError {} impl MatrixMarketError { fn from_pest_error(error: pest::error::Error) -> Self where - T: fmt::Debug + std::hash::Hash + std::marker::Copy + Ord + T: fmt::Debug + std::hash::Hash + std::marker::Copy + Ord, { Self::from_kind_and_message( MatrixMarketErrorKind::ParsingError, @@ -739,8 +739,10 @@ struct MatrixMarketParser; /// -------- /// ``` /// use nalgebra_sparse::io::load_coo_from_matrix_market_file; -/// // Use e.g. `f64` for floating-point matrices -/// let matrix = load_coo_from_matrix_market_file::("path/to/matrix.mtx")?; +/// // Use e.g. `i32` for integer matrices +/// let matrix = load_coo_from_matrix_market_file::("path/to/matrix.mtx"); +/// // extract the real matrix here by +/// // let matrix = matrix.unwrap(); /// ``` pub fn load_coo_from_matrix_market_file>( path: P, @@ -767,12 +769,15 @@ where /// # use nalgebra_sparse::io::load_coo_from_matrix_market_str; /// # use nalgebra_sparse::io::MatrixMarketErrorKind; /// let str = r#" -/// %%matrixmarket matrix coordinate integer symmetric +/// %%matrixmarket matrix coordinate integer general /// 5 4 2 /// 1 1 10 /// 2 3 5 /// "#; -/// let matrix = load_coo_from_matrix_market_str::(str)?; +/// // Use e.g. `i32` for integer matrices +/// let matrix = load_coo_from_matrix_market_str::(str); +/// // extract the real matrix here by +/// // let matrix = matrix.unwrap(); /// ``` pub fn load_coo_from_matrix_market_str(data: &str) -> Result, MatrixMarketError> where @@ -781,7 +786,8 @@ where // unwrap() in this function are guaranteed by parsing the data let file = MatrixMarketParser::parse(Rule::Document, data) .map_err(MatrixMarketError::from_pest_error)? - .next().unwrap(); + .next() + .unwrap(); let mut rows: Vec = Vec::new(); let mut cols: Vec = Vec::new(); @@ -1042,18 +1048,6 @@ fn parse_sparse_shape( let c = inner.next().unwrap().as_str().parse::().unwrap(); let nnz = inner.next().unwrap().as_str().parse::().unwrap(); - // shape information can't use 0 as dimension - if r * c * nnz == 0 { - return Err(MatrixMarketError::from_kind_and_message( - MatrixMarketErrorKind::ZeroError, - String::from( - " - Matrix can't have 0 as shape dimensions. - ", - ), - )); - } - // check for square matrix, when it's not a general matrix if *storagescheme != StorageScheme::General && r != c { return Err(MatrixMarketError::from_kind_and_message(MatrixMarketErrorKind::NonSquare, format!("(Skew-)Symmetric or hermitian matrix should be square matrix, but it has dimension {} and {}", r, c))); diff --git a/nalgebra-sparse/tests/unit.rs b/nalgebra-sparse/tests/unit.rs index 1e61467c..74d32a40 100644 --- a/nalgebra-sparse/tests/unit.rs +++ b/nalgebra-sparse/tests/unit.rs @@ -1,9 +1,5 @@ //! Unit tests -#[cfg(not(all( -feature = "proptest-support", -feature = "compare", -feature = "io", -)))] +#[cfg(not(all(feature = "proptest-support", feature = "compare", feature = "io",)))] compile_error!( "Please enable the `proptest-support`, `compare` and `io` features in order to compile and run the tests. Example: `cargo test -p nalgebra-sparse --features proptest-support,compare,io`" diff --git a/nalgebra-sparse/tests/unit_tests/matrix_market.rs b/nalgebra-sparse/tests/unit_tests/matrix_market.rs index f0a9d217..58db6e02 100644 --- a/nalgebra-sparse/tests/unit_tests/matrix_market.rs +++ b/nalgebra-sparse/tests/unit_tests/matrix_market.rs @@ -10,14 +10,14 @@ fn test_matrixmarket_sparse_real_general_empty() { // Test several valid zero-shapes of a matrix let shapes = vec![ (0, 0), (1, 0), (0, 1) ]; let strings: Vec = shapes - .into_iter() + .iter() .map(|(m, n)| format!("%%MatrixMarket matrix coordinate real general\n {} {} 0", m, n)) .collect(); - for string in &strings { + for (shape,string) in shapes.iter().zip(strings.iter()) { let sparse_mat = load_coo_from_matrix_market_str::(string).unwrap(); - assert_eq!(sparse_mat.nrows(), 0); - assert_eq!(sparse_mat.ncols(), 0); + assert_eq!(sparse_mat.nrows(), shape.0); + assert_eq!(sparse_mat.ncols(), shape.1); assert_eq!(sparse_mat.nnz(), 0); } }