diff --git a/nalgebra-sparse/tests/unit_tests/csr.rs b/nalgebra-sparse/tests/unit_tests/csr.rs index 7cdde331..3c8207c8 100644 --- a/nalgebra-sparse/tests/unit_tests/csr.rs +++ b/nalgebra-sparse/tests/unit_tests/csr.rs @@ -5,6 +5,8 @@ use nalgebra_sparse::{SparseEntry, SparseEntryMut, SparseFormatErrorKind}; use proptest::prelude::*; use proptest::sample::subsequence; +use super::test_data_examples::InvalidCsrDataExamples; + use crate::assert_panics; use crate::common::csr_strategy; @@ -174,20 +176,20 @@ fn csr_matrix_valid_data() { #[test] fn csr_matrix_valid_data_unsorted_column_indices() { let csr = CsrMatrix::try_from_unsorted_csr_data( - 3, 4, - vec![0, 1, 2, 5], - vec![1, 3, 2, 3, 0], - vec![5, 4, 2, 3, 1], + 5, + vec![0, 3, 5, 8, 11], + vec![4, 1, 3, 3, 1, 2, 3, 0, 3, 4, 1], + vec![5, 1, 4, 7, 4, 2, 3, 1, 8, 9, 6], ) .unwrap(); let expected_csr = CsrMatrix::try_from_csr_data( - 3, 4, - vec![0, 1, 2, 5], - vec![1, 3, 0, 2, 3], - vec![5, 4, 1, 2, 3], + 5, + vec![0, 3, 5, 8, 11], + vec![1, 3, 4, 1, 3, 0, 2, 3, 1, 3, 4], + vec![1, 4, 5, 4, 7, 1, 2, 3, 6, 8, 9], ) .unwrap(); @@ -195,11 +197,12 @@ fn csr_matrix_valid_data_unsorted_column_indices() { } #[test] -fn csr_matrix_try_from_invalid_csr_data2() { +fn csr_matrix_try_from_invalid_csr_data_with_new_constructor() { + let invalid_data: InvalidCsrDataExamples = InvalidCsrDataExamples::new(); { // Empty offset array (invalid length) - let matrix = - CsrMatrix::try_from_unsorted_csr_data(0, 0, Vec::new(), Vec::new(), Vec::::new()); + let (offsets, indices, values) = invalid_data.empty_offset_array; + let matrix = CsrMatrix::try_from_unsorted_csr_data(0, 0, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), &SparseFormatErrorKind::InvalidStructure @@ -208,10 +211,8 @@ fn csr_matrix_try_from_invalid_csr_data2() { { // Offset array invalid length for arbitrary data - let offsets = vec![0, 3, 5]; - let indices = vec![0, 1, 2, 3, 5]; - let values = vec![0, 1, 2, 3, 4]; - + let (offsets, indices, values) = + invalid_data.offset_array_invalid_length_for_arbitrary_data; let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -221,9 +222,7 @@ fn csr_matrix_try_from_invalid_csr_data2() { { // Invalid first entry in offsets array - let offsets = vec![1, 2, 2, 5]; - let indices = vec![0, 5, 1, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.invalid_first_entry_in_offsets_array; let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -233,9 +232,7 @@ fn csr_matrix_try_from_invalid_csr_data2() { { // Invalid last entry in offsets array - let offsets = vec![0, 2, 2, 4]; - let indices = vec![0, 5, 1, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.invalid_last_entry_in_offsets_array; let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -245,9 +242,7 @@ fn csr_matrix_try_from_invalid_csr_data2() { { // Invalid length of offsets array - let offsets = vec![0, 2, 2]; - let indices = vec![0, 5, 1, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.invalid_length_of_offsets_array; let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -257,9 +252,7 @@ fn csr_matrix_try_from_invalid_csr_data2() { { // Nonmonotonic offsets - let offsets = vec![0, 3, 2, 5]; - let indices = vec![0, 1, 2, 3, 4]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.nonmonotonic_offsets; let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -269,9 +262,7 @@ fn csr_matrix_try_from_invalid_csr_data2() { { // Minor index out of bounds - let offsets = vec![0, 2, 2, 5]; - let indices = vec![0, 6, 1, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.minor_index_out_of_bounds; let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -281,9 +272,7 @@ fn csr_matrix_try_from_invalid_csr_data2() { { // Duplicate entry - let offsets = vec![0, 2, 2, 5]; - let indices = vec![0, 5, 2, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.duplicate_entry; let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -294,9 +283,11 @@ fn csr_matrix_try_from_invalid_csr_data2() { #[test] fn csr_matrix_try_from_invalid_csr_data() { + let invalid_data: InvalidCsrDataExamples = InvalidCsrDataExamples::new(); { // Empty offset array (invalid length) - let matrix = CsrMatrix::try_from_csr_data(0, 0, Vec::new(), Vec::new(), Vec::::new()); + let (offsets, indices, values) = invalid_data.empty_offset_array; + let matrix = CsrMatrix::try_from_unsorted_csr_data(0, 0, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), &SparseFormatErrorKind::InvalidStructure @@ -305,11 +296,9 @@ fn csr_matrix_try_from_invalid_csr_data() { { // Offset array invalid length for arbitrary data - let offsets = vec![0, 3, 5]; - let indices = vec![0, 1, 2, 3, 5]; - let values = vec![0, 1, 2, 3, 4]; - - let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); + let (offsets, indices, values) = + invalid_data.offset_array_invalid_length_for_arbitrary_data; + let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), &SparseFormatErrorKind::InvalidStructure @@ -318,9 +307,7 @@ fn csr_matrix_try_from_invalid_csr_data() { { // Invalid first entry in offsets array - let offsets = vec![1, 2, 2, 5]; - let indices = vec![0, 5, 1, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.invalid_first_entry_in_offsets_array; let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -330,9 +317,7 @@ fn csr_matrix_try_from_invalid_csr_data() { { // Invalid last entry in offsets array - let offsets = vec![0, 2, 2, 4]; - let indices = vec![0, 5, 1, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.invalid_last_entry_in_offsets_array; let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -342,9 +327,7 @@ fn csr_matrix_try_from_invalid_csr_data() { { // Invalid length of offsets array - let offsets = vec![0, 2, 2]; - let indices = vec![0, 5, 1, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.invalid_length_of_offsets_array; let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -354,9 +337,7 @@ fn csr_matrix_try_from_invalid_csr_data() { { // Nonmonotonic offsets - let offsets = vec![0, 3, 2, 5]; - let indices = vec![0, 1, 2, 3, 4]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.nonmonotonic_offsets; let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -366,9 +347,7 @@ fn csr_matrix_try_from_invalid_csr_data() { { // Nonmonotonic minor indices - let offsets = vec![0, 2, 2, 5]; - let indices = vec![0, 2, 3, 1, 4]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.nonmonotonic_minor_indices; let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -378,9 +357,7 @@ fn csr_matrix_try_from_invalid_csr_data() { { // Minor index out of bounds - let offsets = vec![0, 2, 2, 5]; - let indices = vec![0, 6, 1, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.minor_index_out_of_bounds; let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), @@ -390,9 +367,7 @@ fn csr_matrix_try_from_invalid_csr_data() { { // Duplicate entry - let offsets = vec![0, 2, 2, 5]; - let indices = vec![0, 5, 2, 2, 3]; - let values = vec![0, 1, 2, 3, 4]; + let (offsets, indices, values) = invalid_data.duplicate_entry; let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); assert_eq!( matrix.unwrap_err().kind(), diff --git a/nalgebra-sparse/tests/unit_tests/mod.rs b/nalgebra-sparse/tests/unit_tests/mod.rs index ee2166dc..0099a246 100644 --- a/nalgebra-sparse/tests/unit_tests/mod.rs +++ b/nalgebra-sparse/tests/unit_tests/mod.rs @@ -6,3 +6,4 @@ mod csr; mod ops; mod pattern; mod proptest; +mod test_data_examples; diff --git a/nalgebra-sparse/tests/unit_tests/test_data_examples.rs b/nalgebra-sparse/tests/unit_tests/test_data_examples.rs new file mode 100644 index 00000000..20721087 --- /dev/null +++ b/nalgebra-sparse/tests/unit_tests/test_data_examples.rs @@ -0,0 +1,44 @@ +/// Examples of *invalid* raw CSR data `(offsets, indices, values)`. +pub struct InvalidCsrDataExamples { + pub empty_offset_array: (Vec, Vec, Vec), + pub offset_array_invalid_length_for_arbitrary_data: (Vec, Vec, Vec), + pub invalid_first_entry_in_offsets_array: (Vec, Vec, Vec), + pub invalid_last_entry_in_offsets_array: (Vec, Vec, Vec), + pub invalid_length_of_offsets_array: (Vec, Vec, Vec), + pub nonmonotonic_offsets: (Vec, Vec, Vec), + pub nonmonotonic_minor_indices: (Vec, Vec, Vec), + pub minor_index_out_of_bounds: (Vec, Vec, Vec), + pub duplicate_entry: (Vec, Vec, Vec), +} + +impl InvalidCsrDataExamples { + pub fn new() -> Self { + let empty_offset_array = (Vec::::new(), Vec::::new(), Vec::::new()); + let offset_array_invalid_length_for_arbitrary_data = + (vec![0, 3, 5], vec![0, 1, 2, 3, 5], vec![0, 1, 2, 3, 4]); + let invalid_first_entry_in_offsets_array = + (vec![1, 2, 2, 5], vec![0, 5, 1, 2, 3], vec![0, 1, 2, 3, 4]); + let invalid_last_entry_in_offsets_array = + (vec![0, 2, 2, 4], vec![0, 5, 1, 2, 3], vec![0, 1, 2, 3, 4]); + let invalid_length_of_offsets_array = + (vec![0, 2, 2], vec![0, 5, 1, 2, 3], vec![0, 1, 2, 3, 4]); + let nonmonotonic_offsets = (vec![0, 3, 2, 5], vec![0, 1, 2, 3, 4], vec![0, 1, 2, 3, 4]); + let nonmonotonic_minor_indices = + (vec![0, 2, 2, 5], vec![0, 2, 3, 1, 4], vec![0, 1, 2, 3, 4]); + let minor_index_out_of_bounds = + (vec![0, 2, 2, 5], vec![0, 6, 1, 2, 3], vec![0, 1, 2, 3, 4]); + let duplicate_entry = (vec![0, 2, 2, 5], vec![0, 5, 2, 2, 3], vec![0, 1, 2, 3, 4]); + + return Self { + empty_offset_array, + offset_array_invalid_length_for_arbitrary_data, + invalid_first_entry_in_offsets_array, + invalid_last_entry_in_offsets_array, + invalid_length_of_offsets_array, + nonmonotonic_minor_indices, + nonmonotonic_offsets, + minor_index_out_of_bounds, + duplicate_entry, + }; + } +}