Add module for unit test data examples

This commit is contained in:
Anton 2021-10-17 21:57:36 +02:00
parent 4b41be75b0
commit 86eeb192db
3 changed files with 80 additions and 60 deletions

View File

@ -5,6 +5,8 @@ use nalgebra_sparse::{SparseEntry, SparseEntryMut, SparseFormatErrorKind};
use proptest::prelude::*; use proptest::prelude::*;
use proptest::sample::subsequence; use proptest::sample::subsequence;
use super::test_data_examples::InvalidCsrDataExamples;
use crate::assert_panics; use crate::assert_panics;
use crate::common::csr_strategy; use crate::common::csr_strategy;
@ -174,20 +176,20 @@ fn csr_matrix_valid_data() {
#[test] #[test]
fn csr_matrix_valid_data_unsorted_column_indices() { fn csr_matrix_valid_data_unsorted_column_indices() {
let csr = CsrMatrix::try_from_unsorted_csr_data( let csr = CsrMatrix::try_from_unsorted_csr_data(
3,
4, 4,
vec![0, 1, 2, 5], 5,
vec![1, 3, 2, 3, 0], vec![0, 3, 5, 8, 11],
vec![5, 4, 2, 3, 1], 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(); .unwrap();
let expected_csr = CsrMatrix::try_from_csr_data( let expected_csr = CsrMatrix::try_from_csr_data(
3,
4, 4,
vec![0, 1, 2, 5], 5,
vec![1, 3, 0, 2, 3], vec![0, 3, 5, 8, 11],
vec![5, 4, 1, 2, 3], 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(); .unwrap();
@ -195,11 +197,12 @@ fn csr_matrix_valid_data_unsorted_column_indices() {
} }
#[test] #[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) // Empty offset array (invalid length)
let matrix = let (offsets, indices, values) = invalid_data.empty_offset_array;
CsrMatrix::try_from_unsorted_csr_data(0, 0, Vec::new(), Vec::new(), Vec::<u32>::new()); let matrix = CsrMatrix::try_from_unsorted_csr_data(0, 0, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
&SparseFormatErrorKind::InvalidStructure &SparseFormatErrorKind::InvalidStructure
@ -208,10 +211,8 @@ fn csr_matrix_try_from_invalid_csr_data2() {
{ {
// Offset array invalid length for arbitrary data // Offset array invalid length for arbitrary data
let offsets = vec![0, 3, 5]; let (offsets, indices, values) =
let indices = vec![0, 1, 2, 3, 5]; invalid_data.offset_array_invalid_length_for_arbitrary_data;
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
@ -221,9 +222,7 @@ fn csr_matrix_try_from_invalid_csr_data2() {
{ {
// Invalid first entry in offsets array // Invalid first entry in offsets array
let offsets = vec![1, 2, 2, 5]; let (offsets, indices, values) = invalid_data.invalid_first_entry_in_offsets_array;
let indices = vec![0, 5, 1, 2, 3];
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
@ -233,9 +232,7 @@ fn csr_matrix_try_from_invalid_csr_data2() {
{ {
// Invalid last entry in offsets array // Invalid last entry in offsets array
let offsets = vec![0, 2, 2, 4]; let (offsets, indices, values) = invalid_data.invalid_last_entry_in_offsets_array;
let indices = vec![0, 5, 1, 2, 3];
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
@ -245,9 +242,7 @@ fn csr_matrix_try_from_invalid_csr_data2() {
{ {
// Invalid length of offsets array // Invalid length of offsets array
let offsets = vec![0, 2, 2]; let (offsets, indices, values) = invalid_data.invalid_length_of_offsets_array;
let indices = vec![0, 5, 1, 2, 3];
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
@ -257,9 +252,7 @@ fn csr_matrix_try_from_invalid_csr_data2() {
{ {
// Nonmonotonic offsets // Nonmonotonic offsets
let offsets = vec![0, 3, 2, 5]; let (offsets, indices, values) = invalid_data.nonmonotonic_offsets;
let indices = vec![0, 1, 2, 3, 4];
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
@ -269,9 +262,7 @@ fn csr_matrix_try_from_invalid_csr_data2() {
{ {
// Minor index out of bounds // Minor index out of bounds
let offsets = vec![0, 2, 2, 5]; let (offsets, indices, values) = invalid_data.minor_index_out_of_bounds;
let indices = vec![0, 6, 1, 2, 3];
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
@ -281,9 +272,7 @@ fn csr_matrix_try_from_invalid_csr_data2() {
{ {
// Duplicate entry // Duplicate entry
let offsets = vec![0, 2, 2, 5]; let (offsets, indices, values) = invalid_data.duplicate_entry;
let indices = vec![0, 5, 2, 2, 3];
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
@ -294,9 +283,11 @@ fn csr_matrix_try_from_invalid_csr_data2() {
#[test] #[test]
fn csr_matrix_try_from_invalid_csr_data() { fn csr_matrix_try_from_invalid_csr_data() {
let invalid_data: InvalidCsrDataExamples = InvalidCsrDataExamples::new();
{ {
// Empty offset array (invalid length) // Empty offset array (invalid length)
let matrix = CsrMatrix::try_from_csr_data(0, 0, Vec::new(), Vec::new(), Vec::<u32>::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!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
&SparseFormatErrorKind::InvalidStructure &SparseFormatErrorKind::InvalidStructure
@ -305,11 +296,9 @@ fn csr_matrix_try_from_invalid_csr_data() {
{ {
// Offset array invalid length for arbitrary data // Offset array invalid length for arbitrary data
let offsets = vec![0, 3, 5]; let (offsets, indices, values) =
let indices = vec![0, 1, 2, 3, 5]; invalid_data.offset_array_invalid_length_for_arbitrary_data;
let values = vec![0, 1, 2, 3, 4]; let matrix = CsrMatrix::try_from_unsorted_csr_data(3, 6, offsets, indices, values);
let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
&SparseFormatErrorKind::InvalidStructure &SparseFormatErrorKind::InvalidStructure
@ -318,9 +307,7 @@ fn csr_matrix_try_from_invalid_csr_data() {
{ {
// Invalid first entry in offsets array // Invalid first entry in offsets array
let offsets = vec![1, 2, 2, 5]; let (offsets, indices, values) = invalid_data.invalid_first_entry_in_offsets_array;
let indices = vec![0, 5, 1, 2, 3];
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
@ -330,9 +317,7 @@ fn csr_matrix_try_from_invalid_csr_data() {
{ {
// Invalid last entry in offsets array // Invalid last entry in offsets array
let offsets = vec![0, 2, 2, 4]; let (offsets, indices, values) = invalid_data.invalid_last_entry_in_offsets_array;
let indices = vec![0, 5, 1, 2, 3];
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
@ -342,9 +327,7 @@ fn csr_matrix_try_from_invalid_csr_data() {
{ {
// Invalid length of offsets array // Invalid length of offsets array
let offsets = vec![0, 2, 2]; let (offsets, indices, values) = invalid_data.invalid_length_of_offsets_array;
let indices = vec![0, 5, 1, 2, 3];
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
@ -354,9 +337,7 @@ fn csr_matrix_try_from_invalid_csr_data() {
{ {
// Nonmonotonic offsets // Nonmonotonic offsets
let offsets = vec![0, 3, 2, 5]; let (offsets, indices, values) = invalid_data.nonmonotonic_offsets;
let indices = vec![0, 1, 2, 3, 4];
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
@ -366,9 +347,7 @@ fn csr_matrix_try_from_invalid_csr_data() {
{ {
// Nonmonotonic minor indices // Nonmonotonic minor indices
let offsets = vec![0, 2, 2, 5]; let (offsets, indices, values) = invalid_data.nonmonotonic_minor_indices;
let indices = vec![0, 2, 3, 1, 4];
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
@ -378,9 +357,7 @@ fn csr_matrix_try_from_invalid_csr_data() {
{ {
// Minor index out of bounds // Minor index out of bounds
let offsets = vec![0, 2, 2, 5]; let (offsets, indices, values) = invalid_data.minor_index_out_of_bounds;
let indices = vec![0, 6, 1, 2, 3];
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),
@ -390,9 +367,7 @@ fn csr_matrix_try_from_invalid_csr_data() {
{ {
// Duplicate entry // Duplicate entry
let offsets = vec![0, 2, 2, 5]; let (offsets, indices, values) = invalid_data.duplicate_entry;
let indices = vec![0, 5, 2, 2, 3];
let values = vec![0, 1, 2, 3, 4];
let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values); let matrix = CsrMatrix::try_from_csr_data(3, 6, offsets, indices, values);
assert_eq!( assert_eq!(
matrix.unwrap_err().kind(), matrix.unwrap_err().kind(),

View File

@ -6,3 +6,4 @@ mod csr;
mod ops; mod ops;
mod pattern; mod pattern;
mod proptest; mod proptest;
mod test_data_examples;

View File

@ -0,0 +1,44 @@
/// Examples of *invalid* raw CSR data `(offsets, indices, values)`.
pub struct InvalidCsrDataExamples {
pub empty_offset_array: (Vec<usize>, Vec<usize>, Vec<i32>),
pub offset_array_invalid_length_for_arbitrary_data: (Vec<usize>, Vec<usize>, Vec<i32>),
pub invalid_first_entry_in_offsets_array: (Vec<usize>, Vec<usize>, Vec<i32>),
pub invalid_last_entry_in_offsets_array: (Vec<usize>, Vec<usize>, Vec<i32>),
pub invalid_length_of_offsets_array: (Vec<usize>, Vec<usize>, Vec<i32>),
pub nonmonotonic_offsets: (Vec<usize>, Vec<usize>, Vec<i32>),
pub nonmonotonic_minor_indices: (Vec<usize>, Vec<usize>, Vec<i32>),
pub minor_index_out_of_bounds: (Vec<usize>, Vec<usize>, Vec<i32>),
pub duplicate_entry: (Vec<usize>, Vec<usize>, Vec<i32>),
}
impl InvalidCsrDataExamples {
pub fn new() -> Self {
let empty_offset_array = (Vec::<usize>::new(), Vec::<usize>::new(), Vec::<i32>::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,
};
}
}