Skip to content

Commit

Permalink
test: improve ergonomics for test
Browse files Browse the repository at this point in the history
  • Loading branch information
YeungOnion committed Jun 25, 2024
1 parent e29b69c commit 222539a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# Executables
*.exe

# Test data for integration tests
tests/*.dat

# Generated by Cargo
/target/
*.lock
Expand Down
16 changes: 10 additions & 6 deletions tests/gather_nist_data.sh
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
30 changes: 16 additions & 14 deletions tests/nist_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<PathBuf>())
.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::<PathBuf>())
.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
);
Expand Down

0 comments on commit 222539a

Please sign in to comment.