From 4cd47327c9bc4c756038cebc5e222cbdba4400a4 Mon Sep 17 00:00:00 2001 From: Andreas Longva Date: Fri, 25 Sep 2020 14:52:33 +0200 Subject: [PATCH] Test that CSR::disassemble avoids cloning when possible --- nalgebra-sparse/tests/unit_tests/csr.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/nalgebra-sparse/tests/unit_tests/csr.rs b/nalgebra-sparse/tests/unit_tests/csr.rs index 693e092f..ab6f698e 100644 --- a/nalgebra-sparse/tests/unit_tests/csr.rs +++ b/nalgebra-sparse/tests/unit_tests/csr.rs @@ -220,6 +220,29 @@ fn csr_matrix_try_from_invalid_csr_data() { } +#[test] +fn csr_disassemble_avoids_clone_when_owned() { + // Test that disassemble avoids cloning the sparsity pattern when it holds the sole reference + // to the pattern. We do so by checking that the pointer to the data is unchanged. + + let offsets = vec![0, 2, 2, 5]; + let indices = vec![0, 5, 1, 2, 3]; + let values = vec![0, 1, 2, 3, 4]; + let offsets_ptr = offsets.as_ptr(); + let indices_ptr = indices.as_ptr(); + let values_ptr = values.as_ptr(); + let matrix = CsrMatrix::try_from_csr_data(3, + 6, + offsets, + indices, + values).unwrap(); + + let (offsets, indices, values) = matrix.disassemble(); + assert_eq!(offsets.as_ptr(), offsets_ptr); + assert_eq!(indices.as_ptr(), indices_ptr); + assert_eq!(values.as_ptr(), values_ptr); +} + #[test] fn csr_matrix_get_index() { // TODO: Implement tests for ::get() and index()