From 222539a86471c10eebfe753af4b43a1479f9f0ad Mon Sep 17 00:00:00 2001 From: Orion Yeung <11580988+orionyeung001@users.noreply.github.com> Date: Mon, 24 Jun 2024 19:27:51 -0500 Subject: [PATCH] test: improve ergonomics for test --- .gitignore | 3 +++ tests/gather_nist_data.sh | 16 ++++++++++------ tests/nist_tests.rs | 30 ++++++++++++++++-------------- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 3bb686bd..fe9e65ec 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,9 @@ # Executables *.exe +# Test data for integration tests +tests/*.dat + # Generated by Cargo /target/ *.lock diff --git a/tests/gather_nist_data.sh b/tests/gather_nist_data.sh index 9381ff08..2b663734 100755 --- a/tests/gather_nist_data.sh +++ b/tests/gather_nist_data.sh @@ -1,27 +1,31 @@ #! /bin/bash +# this script is to download and preprocess datafiles for the nist_tests.rs +# integration test for statrs downloads data to directory specified by env +# var STATRS_NIST_DATA_DIR process_file() { # Define input and output file names SOURCE=$1 FILENAME=$2 - curl -fsSL ${SOURCE}/$FILENAME > $FILENAME + TARGET=${STATRS_NIST_DATA_DIR-tests}/${FILENAME} + echo -e ${FILENAME} '\n\tDownloading...' + curl -fsSL ${SOURCE}/$FILENAME > ${TARGET} # Extract line numbers for Certified Values and Data from the header - INFO=$(grep "Certified Values:" $FILENAME) + INFO=$(grep "Certified Values:" $TARGET) CERTIFIED_VALUES_START=$(echo $INFO | awk '{print $4}') CERTIFIED_VALUES_END=$(echo $INFO | awk '{print $6}') - INFO=$(grep "Data :" $FILENAME) + INFO=$(grep "Data :" $TARGET) DATA_START=$(echo $INFO | awk '{print $4}') DATA_END=$(echo $INFO | awk '{print $6}') + echo -e '\tFormatting...' # Extract and reformat sections - # Certified values sed -n -i \ -e "${CERTIFIED_VALUES_START},${CERTIFIED_VALUES_END}p" \ -e "${DATA_START},${DATA_END}p" \ - $FILENAME - + $TARGET } URL='https://www.itl.nist.gov/div898/strd/univ/data' diff --git a/tests/nist_tests.rs b/tests/nist_tests.rs index 9efc94bf..a985820f 100644 --- a/tests/nist_tests.rs +++ b/tests/nist_tests.rs @@ -45,32 +45,34 @@ const FILENAMES: [&str; 7] = [ "NumAcc3.dat", ]; +fn get_path(fname: &str, prefix: Option<&str>) -> PathBuf { + if let Some(prefix) = prefix { + [prefix, fname].iter().collect() + } else { + ["tests", fname].iter().collect() + } +} + #[test] #[ignore = "NIST tests should not run from typical `cargo test` calls"] fn nist_strd_univariate_mean() { - let path_prefix = env::var(NIST_DATA_DIR_ENV).unwrap_or_else(|e| panic!("{}", e)); - for fname in FILENAMES { - let case = parse_file([&path_prefix, fname].iter().collect::()) - .unwrap_or_else(|e| panic!("failed parsing file {} with {:?}", fname, e)); - assert_relative_eq!( - case.values.iter().mean(), - case.certified.mean, - epsilon = 1e-12 - ); + let filepath = get_path(fname, env::var(NIST_DATA_DIR_ENV).ok().as_deref()); + let case = parse_file(filepath) + .unwrap_or_else(|e| panic!("failed parsing file {} with `{:?}`", fname, e)); + assert_relative_eq!(case.values.mean(), case.certified.mean, epsilon = 1e-12); } } #[test] #[ignore] fn nist_strd_univariate_std_dev() { - let path_prefix = env::var(NIST_DATA_DIR_ENV).unwrap_or_else(|e| panic!("{}", e)); - for fname in FILENAMES { - let case = parse_file([&path_prefix, fname].iter().collect::()) - .unwrap_or_else(|e| panic!("failed parsing file {} with {:?}", fname, e)); + let filepath = get_path(fname, env::var(NIST_DATA_DIR_ENV).ok().as_deref()); + let case = parse_file(filepath) + .unwrap_or_else(|e| panic!("failed parsing file {} with `{:?}`", fname, e)); assert_relative_eq!( - case.values.iter().std_dev(), + case.values.std_dev(), case.certified.std_dev, epsilon = 1e-10 );