From 31fc49818228976e49fb471a4c5896db938fa6e2 Mon Sep 17 00:00:00 2001 From: Hantao Hui Date: Sat, 7 May 2022 13:40:25 +0200 Subject: [PATCH] use tempdir to write matrix when running test --- nalgebra-sparse/Cargo.toml | 1 + nalgebra-sparse/src/io/matrix_market.rs | 5 +---- nalgebra-sparse/tests/unit_tests/matrix_market.rs | 10 ++++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/nalgebra-sparse/Cargo.toml b/nalgebra-sparse/Cargo.toml index 0dfe743f..4e414322 100644 --- a/nalgebra-sparse/Cargo.toml +++ b/nalgebra-sparse/Cargo.toml @@ -34,6 +34,7 @@ pest_derive = { version = "2", optional = true } itertools = "0.10" matrixcompare = { version = "0.3.0", features = [ "proptest-support" ] } nalgebra = { version="0.30", path = "../", features = ["compare"] } +tempfile = "3" [package.metadata.docs.rs] # Enable certain features when building docs for docs.rs diff --git a/nalgebra-sparse/src/io/matrix_market.rs b/nalgebra-sparse/src/io/matrix_market.rs index 5143447c..200aeff2 100644 --- a/nalgebra-sparse/src/io/matrix_market.rs +++ b/nalgebra-sparse/src/io/matrix_market.rs @@ -1487,10 +1487,7 @@ pub fn write_to_matrix_market_str(&str).unwrap(); -/// // create a temporary file 'temp.mtx' -/// let mut tempdir = std::env::temp_dir(); -/// tempdir.push("temp.mtx"); -/// write_to_matrix_market_file(&matrix,tempdir).unwrap(); +/// write_to_matrix_market_file(&matrix,"path/to/matrix.mtx").unwrap(); /// ``` pub fn write_to_matrix_market_file< T: MatrixMarketScalar, diff --git a/nalgebra-sparse/tests/unit_tests/matrix_market.rs b/nalgebra-sparse/tests/unit_tests/matrix_market.rs index 7bf6891c..245fa3f5 100644 --- a/nalgebra-sparse/tests/unit_tests/matrix_market.rs +++ b/nalgebra-sparse/tests/unit_tests/matrix_market.rs @@ -8,6 +8,7 @@ use nalgebra_sparse::io::{ use nalgebra_sparse::proptest::coo_no_duplicates; use nalgebra_sparse::CooMatrix; use proptest::prelude::*; +use tempfile::tempdir; type C64 = Complex; type C32 = Complex; @@ -457,10 +458,11 @@ proptest! { proptest! { #[test] fn coo_matrix_market_roundtrip_file(coo in coo_no_duplicates(-10 ..= 10, 0 ..= 10, 0..= 10, 100)) { - let mut tempdir = std::env::temp_dir(); - tempdir.push("temp.mtx"); - write_to_matrix_market_file(&coo,&tempdir).unwrap(); - let generated_matrix = load_coo_from_matrix_market_file(tempdir).unwrap(); + let temp_dir = tempdir().expect("Unable to create temporary directory"); + let file_path = temp_dir.path().join("temp.mtx"); + write_to_matrix_market_file(&coo,&file_path).unwrap(); + let generated_matrix = load_coo_from_matrix_market_file(file_path).unwrap(); assert_matrix_eq!(generated_matrix, coo); + temp_dir.close().expect("Unable to delete temporary directory"); } }