code fmt; fix failing unit test and doc test

This commit is contained in:
Hantao Hui 2021-12-12 12:10:20 +01:00
parent 6cd009988c
commit 92b324c007
3 changed files with 17 additions and 27 deletions

View File

@ -300,7 +300,7 @@ impl std::error::Error for MatrixMarketError {}
impl MatrixMarketError { impl MatrixMarketError {
fn from_pest_error<T>(error: pest::error::Error<T>) -> Self fn from_pest_error<T>(error: pest::error::Error<T>) -> Self
where 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( Self::from_kind_and_message(
MatrixMarketErrorKind::ParsingError, MatrixMarketErrorKind::ParsingError,
@ -739,8 +739,10 @@ struct MatrixMarketParser;
/// -------- /// --------
/// ``` /// ```
/// use nalgebra_sparse::io::load_coo_from_matrix_market_file; /// use nalgebra_sparse::io::load_coo_from_matrix_market_file;
/// // Use e.g. `f64` for floating-point matrices /// // Use e.g. `i32` for integer matrices
/// let matrix = load_coo_from_matrix_market_file::<i32>("path/to/matrix.mtx")?; /// let matrix = load_coo_from_matrix_market_file::<i32,_>("path/to/matrix.mtx");
/// // extract the real matrix here by
/// // let matrix = matrix.unwrap();
/// ``` /// ```
pub fn load_coo_from_matrix_market_file<T, P: AsRef<Path>>( pub fn load_coo_from_matrix_market_file<T, P: AsRef<Path>>(
path: P, path: P,
@ -767,12 +769,15 @@ where
/// # use nalgebra_sparse::io::load_coo_from_matrix_market_str; /// # use nalgebra_sparse::io::load_coo_from_matrix_market_str;
/// # use nalgebra_sparse::io::MatrixMarketErrorKind; /// # use nalgebra_sparse::io::MatrixMarketErrorKind;
/// let str = r#" /// let str = r#"
/// %%matrixmarket matrix coordinate integer symmetric /// %%matrixmarket matrix coordinate integer general
/// 5 4 2 /// 5 4 2
/// 1 1 10 /// 1 1 10
/// 2 3 5 /// 2 3 5
/// "#; /// "#;
/// let matrix = load_coo_from_matrix_market_str::<i32>(str)?; /// // Use e.g. `i32` for integer matrices
/// let matrix = load_coo_from_matrix_market_str::<i32>(str);
/// // extract the real matrix here by
/// // let matrix = matrix.unwrap();
/// ``` /// ```
pub fn load_coo_from_matrix_market_str<T>(data: &str) -> Result<CooMatrix<T>, MatrixMarketError> pub fn load_coo_from_matrix_market_str<T>(data: &str) -> Result<CooMatrix<T>, MatrixMarketError>
where where
@ -781,7 +786,8 @@ where
// unwrap() in this function are guaranteed by parsing the data // unwrap() in this function are guaranteed by parsing the data
let file = MatrixMarketParser::parse(Rule::Document, data) let file = MatrixMarketParser::parse(Rule::Document, data)
.map_err(MatrixMarketError::from_pest_error)? .map_err(MatrixMarketError::from_pest_error)?
.next().unwrap(); .next()
.unwrap();
let mut rows: Vec<usize> = Vec::new(); let mut rows: Vec<usize> = Vec::new();
let mut cols: Vec<usize> = Vec::new(); let mut cols: Vec<usize> = Vec::new();
@ -1042,18 +1048,6 @@ fn parse_sparse_shape(
let c = inner.next().unwrap().as_str().parse::<usize>().unwrap(); let c = inner.next().unwrap().as_str().parse::<usize>().unwrap();
let nnz = inner.next().unwrap().as_str().parse::<usize>().unwrap(); let nnz = inner.next().unwrap().as_str().parse::<usize>().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 // check for square matrix, when it's not a general matrix
if *storagescheme != StorageScheme::General && r != c { 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))); 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)));

View File

@ -1,9 +1,5 @@
//! Unit tests //! Unit tests
#[cfg(not(all( #[cfg(not(all(feature = "proptest-support", feature = "compare", feature = "io",)))]
feature = "proptest-support",
feature = "compare",
feature = "io",
)))]
compile_error!( compile_error!(
"Please enable the `proptest-support`, `compare` and `io` features in order to compile and run the tests. "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`" Example: `cargo test -p nalgebra-sparse --features proptest-support,compare,io`"

View File

@ -10,14 +10,14 @@ fn test_matrixmarket_sparse_real_general_empty() {
// Test several valid zero-shapes of a matrix // Test several valid zero-shapes of a matrix
let shapes = vec![ (0, 0), (1, 0), (0, 1) ]; let shapes = vec![ (0, 0), (1, 0), (0, 1) ];
let strings: Vec<String> = shapes let strings: Vec<String> = shapes
.into_iter() .iter()
.map(|(m, n)| format!("%%MatrixMarket matrix coordinate real general\n {} {} 0", m, n)) .map(|(m, n)| format!("%%MatrixMarket matrix coordinate real general\n {} {} 0", m, n))
.collect(); .collect();
for string in &strings { for (shape,string) in shapes.iter().zip(strings.iter()) {
let sparse_mat = load_coo_from_matrix_market_str::<f32>(string).unwrap(); let sparse_mat = load_coo_from_matrix_market_str::<f32>(string).unwrap();
assert_eq!(sparse_mat.nrows(), 0); assert_eq!(sparse_mat.nrows(), shape.0);
assert_eq!(sparse_mat.ncols(), 0); assert_eq!(sparse_mat.ncols(), shape.1);
assert_eq!(sparse_mat.nnz(), 0); assert_eq!(sparse_mat.nnz(), 0);
} }
} }