diff --git a/nalgebra-sparse/tests/unit_tests/csc.rs b/nalgebra-sparse/tests/unit_tests/csc.rs index 45158d65..7fb0de54 100644 --- a/nalgebra-sparse/tests/unit_tests/csc.rs +++ b/nalgebra-sparse/tests/unit_tests/csc.rs @@ -392,7 +392,140 @@ fn csc_matrix_get_index_entry() { #[test] fn csc_matrix_col_iter() { - // TODO + // Note: this is the transpose of the matrix used for the similar csr_matrix_row_iter test + // (this way the actual tests are almost identical, due to the transposed relationship + // between CSR and CSC) + #[rustfmt::skip] + let dense = DMatrix::from_row_slice(4, 3, &[ + 0, 3, 0, + 1, 0, 4, + 2, 0, 0, + 0, 0, 5, + ]); + let csc = CscMatrix::from(&dense); + + // Immutable iterator + { + let mut col_iter = csc.col_iter(); + + { + let col = col_iter.next().unwrap(); + assert_eq!(col.nrows(), 4); + assert_eq!(col.nnz(), 2); + assert_eq!(col.row_indices(), &[1, 2]); + assert_eq!(col.values(), &[1, 2]); + assert_eq!(col.get_entry(0), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(1), Some(SparseEntry::NonZero(&1))); + assert_eq!(col.get_entry(2), Some(SparseEntry::NonZero(&2))); + assert_eq!(col.get_entry(3), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(4), None); + } + + { + let col = col_iter.next().unwrap(); + assert_eq!(col.nrows(), 4); + assert_eq!(col.nnz(), 1); + assert_eq!(col.row_indices(), &[0]); + assert_eq!(col.values(), &[3]); + assert_eq!(col.get_entry(0), Some(SparseEntry::NonZero(&3))); + assert_eq!(col.get_entry(1), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(2), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(3), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(4), None); + } + + { + let col = col_iter.next().unwrap(); + assert_eq!(col.nrows(), 4); + assert_eq!(col.nnz(), 2); + assert_eq!(col.row_indices(), &[1, 3]); + assert_eq!(col.values(), &[4, 5]); + assert_eq!(col.get_entry(0), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(1), Some(SparseEntry::NonZero(&4))); + assert_eq!(col.get_entry(2), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(3), Some(SparseEntry::NonZero(&5))); + assert_eq!(col.get_entry(4), None); + } + + assert!(col_iter.next().is_none()); + } + + // Mutable iterator + { + let mut csc = csc; + let mut col_iter = csc.col_iter_mut(); + + { + let mut col = col_iter.next().unwrap(); + assert_eq!(col.nrows(), 4); + assert_eq!(col.nnz(), 2); + assert_eq!(col.row_indices(), &[1, 2]); + assert_eq!(col.values(), &[1, 2]); + assert_eq!(col.get_entry(0), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(1), Some(SparseEntry::NonZero(&1))); + assert_eq!(col.get_entry(2), Some(SparseEntry::NonZero(&2))); + assert_eq!(col.get_entry(3), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(4), None); + + assert_eq!(col.values_mut(), &mut [1, 2]); + assert_eq!( + col.rows_and_values_mut(), + ([1, 2].as_ref(), [1, 2].as_mut()) + ); + assert_eq!(col.get_entry_mut(0), Some(SparseEntryMut::Zero)); + assert_eq!(col.get_entry_mut(1), Some(SparseEntryMut::NonZero(&mut 1))); + assert_eq!(col.get_entry_mut(2), Some(SparseEntryMut::NonZero(&mut 2))); + assert_eq!(col.get_entry_mut(3), Some(SparseEntryMut::Zero)); + assert_eq!(col.get_entry_mut(4), None); + } + + { + let mut col = col_iter.next().unwrap(); + assert_eq!(col.nrows(), 4); + assert_eq!(col.nnz(), 1); + assert_eq!(col.row_indices(), &[0]); + assert_eq!(col.values(), &[3]); + assert_eq!(col.get_entry(0), Some(SparseEntry::NonZero(&3))); + assert_eq!(col.get_entry(1), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(2), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(3), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(4), None); + + assert_eq!(col.values_mut(), &mut [3]); + assert_eq!(col.rows_and_values_mut(), ([0].as_ref(), [3].as_mut())); + assert_eq!(col.get_entry_mut(0), Some(SparseEntryMut::NonZero(&mut 3))); + assert_eq!(col.get_entry_mut(1), Some(SparseEntryMut::Zero)); + assert_eq!(col.get_entry_mut(2), Some(SparseEntryMut::Zero)); + assert_eq!(col.get_entry_mut(3), Some(SparseEntryMut::Zero)); + assert_eq!(col.get_entry_mut(4), None); + } + + { + let mut col = col_iter.next().unwrap(); + assert_eq!(col.nrows(), 4); + assert_eq!(col.nnz(), 2); + assert_eq!(col.row_indices(), &[1, 3]); + assert_eq!(col.values(), &[4, 5]); + assert_eq!(col.get_entry(0), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(1), Some(SparseEntry::NonZero(&4))); + assert_eq!(col.get_entry(2), Some(SparseEntry::Zero)); + assert_eq!(col.get_entry(3), Some(SparseEntry::NonZero(&5))); + assert_eq!(col.get_entry(4), None); + + assert_eq!(col.values_mut(), &mut [4, 5]); + assert_eq!( + col.rows_and_values_mut(), + ([1, 3].as_ref(), [4, 5].as_mut()) + ); + assert_eq!(col.get_entry_mut(0), Some(SparseEntryMut::Zero)); + assert_eq!(col.get_entry_mut(1), Some(SparseEntryMut::NonZero(&mut 4))); + assert_eq!(col.get_entry_mut(2), Some(SparseEntryMut::Zero)); + assert_eq!(col.get_entry_mut(3), Some(SparseEntryMut::NonZero(&mut 5))); + assert_eq!(col.get_entry_mut(4), None); + } + + assert!(col_iter.next().is_none()); + } } proptest! { diff --git a/nalgebra-sparse/tests/unit_tests/csr.rs b/nalgebra-sparse/tests/unit_tests/csr.rs index 2fe3c910..dee1ae1e 100644 --- a/nalgebra-sparse/tests/unit_tests/csr.rs +++ b/nalgebra-sparse/tests/unit_tests/csr.rs @@ -392,7 +392,136 @@ fn csr_matrix_get_index_entry() { #[test] fn csr_matrix_row_iter() { - // TODO + #[rustfmt::skip] + let dense = DMatrix::from_row_slice(3, 4, &[ + 0, 1, 2, 0, + 3, 0, 0, 0, + 0, 4, 0, 5 + ]); + let csr = CsrMatrix::from(&dense); + + // Immutable iterator + { + let mut row_iter = csr.row_iter(); + + { + let row = row_iter.next().unwrap(); + assert_eq!(row.ncols(), 4); + assert_eq!(row.nnz(), 2); + assert_eq!(row.col_indices(), &[1, 2]); + assert_eq!(row.values(), &[1, 2]); + assert_eq!(row.get_entry(0), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(1), Some(SparseEntry::NonZero(&1))); + assert_eq!(row.get_entry(2), Some(SparseEntry::NonZero(&2))); + assert_eq!(row.get_entry(3), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(4), None); + } + + { + let row = row_iter.next().unwrap(); + assert_eq!(row.ncols(), 4); + assert_eq!(row.nnz(), 1); + assert_eq!(row.col_indices(), &[0]); + assert_eq!(row.values(), &[3]); + assert_eq!(row.get_entry(0), Some(SparseEntry::NonZero(&3))); + assert_eq!(row.get_entry(1), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(2), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(3), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(4), None); + } + + { + let row = row_iter.next().unwrap(); + assert_eq!(row.ncols(), 4); + assert_eq!(row.nnz(), 2); + assert_eq!(row.col_indices(), &[1, 3]); + assert_eq!(row.values(), &[4, 5]); + assert_eq!(row.get_entry(0), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(1), Some(SparseEntry::NonZero(&4))); + assert_eq!(row.get_entry(2), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(3), Some(SparseEntry::NonZero(&5))); + assert_eq!(row.get_entry(4), None); + } + + assert!(row_iter.next().is_none()); + } + + // Mutable iterator + { + let mut csr = csr; + let mut row_iter = csr.row_iter_mut(); + + { + let mut row = row_iter.next().unwrap(); + assert_eq!(row.ncols(), 4); + assert_eq!(row.nnz(), 2); + assert_eq!(row.col_indices(), &[1, 2]); + assert_eq!(row.values(), &[1, 2]); + assert_eq!(row.get_entry(0), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(1), Some(SparseEntry::NonZero(&1))); + assert_eq!(row.get_entry(2), Some(SparseEntry::NonZero(&2))); + assert_eq!(row.get_entry(3), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(4), None); + + assert_eq!(row.values_mut(), &mut [1, 2]); + assert_eq!( + row.cols_and_values_mut(), + ([1, 2].as_ref(), [1, 2].as_mut()) + ); + assert_eq!(row.get_entry_mut(0), Some(SparseEntryMut::Zero)); + assert_eq!(row.get_entry_mut(1), Some(SparseEntryMut::NonZero(&mut 1))); + assert_eq!(row.get_entry_mut(2), Some(SparseEntryMut::NonZero(&mut 2))); + assert_eq!(row.get_entry_mut(3), Some(SparseEntryMut::Zero)); + assert_eq!(row.get_entry_mut(4), None); + } + + { + let mut row = row_iter.next().unwrap(); + assert_eq!(row.ncols(), 4); + assert_eq!(row.nnz(), 1); + assert_eq!(row.col_indices(), &[0]); + assert_eq!(row.values(), &[3]); + assert_eq!(row.get_entry(0), Some(SparseEntry::NonZero(&3))); + assert_eq!(row.get_entry(1), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(2), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(3), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(4), None); + + assert_eq!(row.values_mut(), &mut [3]); + assert_eq!(row.cols_and_values_mut(), ([0].as_ref(), [3].as_mut())); + assert_eq!(row.get_entry_mut(0), Some(SparseEntryMut::NonZero(&mut 3))); + assert_eq!(row.get_entry_mut(1), Some(SparseEntryMut::Zero)); + assert_eq!(row.get_entry_mut(2), Some(SparseEntryMut::Zero)); + assert_eq!(row.get_entry_mut(3), Some(SparseEntryMut::Zero)); + assert_eq!(row.get_entry_mut(4), None); + } + + { + let mut row = row_iter.next().unwrap(); + assert_eq!(row.ncols(), 4); + assert_eq!(row.nnz(), 2); + assert_eq!(row.col_indices(), &[1, 3]); + assert_eq!(row.values(), &[4, 5]); + assert_eq!(row.get_entry(0), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(1), Some(SparseEntry::NonZero(&4))); + assert_eq!(row.get_entry(2), Some(SparseEntry::Zero)); + assert_eq!(row.get_entry(3), Some(SparseEntry::NonZero(&5))); + assert_eq!(row.get_entry(4), None); + + assert_eq!(row.values_mut(), &mut [4, 5]); + assert_eq!( + row.cols_and_values_mut(), + ([1, 3].as_ref(), [4, 5].as_mut()) + ); + assert_eq!(row.get_entry_mut(0), Some(SparseEntryMut::Zero)); + assert_eq!(row.get_entry_mut(1), Some(SparseEntryMut::NonZero(&mut 4))); + assert_eq!(row.get_entry_mut(2), Some(SparseEntryMut::Zero)); + assert_eq!(row.get_entry_mut(3), Some(SparseEntryMut::NonZero(&mut 5))); + assert_eq!(row.get_entry_mut(4), None); + } + + assert!(row_iter.next().is_none()); + } } proptest! {