statrs/testing/
mod.rs

1//! Provides testing helpers and utilities
2
3use std::fs::File;
4use std::io::{BufRead, BufReader};
5use std::str;
6
7/// Loads a test data file into a vector of `f64`'s.
8/// Path is relative to /data.
9///
10/// # Panics
11///
12/// Panics if the file does not exist or could not be opened, or
13/// there was an error reading the file.
14#[cfg(test)]
15pub fn load_data(path: &str) -> Vec<f64> {
16    // note: the copious use of unwrap is because this is a test helper and
17    // if reading the data file fails, we want to panic immediately
18
19    let path_prefix = "./data/".to_string();
20    let true_path = path_prefix + path.trim().trim_start_matches('/');
21
22    let f = File::open(true_path).unwrap();
23    let mut reader = BufReader::new(f);
24
25    let mut buf = String::new();
26    let mut data: Vec<f64> = vec![];
27    while reader.read_line(&mut buf).unwrap() > 0 {
28        data.push(buf.trim().parse::<f64>().unwrap());
29        buf.clear();
30    }
31    data
32}