statrs/testing/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//! Provides testing helpers and utilities

use std::fs::File;
use std::io::{BufRead, BufReader};
use std::str;

/// Loads a test data file into a vector of `f64`'s.
/// Path is relative to /data.
///
/// # Panics
///
/// Panics if the file does not exist or could not be opened, or
/// there was an error reading the file.
#[cfg(test)]
pub fn load_data(path: &str) -> Vec<f64> {
    // note: the copious use of unwrap is because this is a test helper and
    // if reading the data file fails, we want to panic immediately

    let path_prefix = "./data/".to_string();
    let true_path = path_prefix + path.trim().trim_start_matches('/');

    let f = File::open(true_path).unwrap();
    let mut reader = BufReader::new(f);

    let mut buf = String::new();
    let mut data: Vec<f64> = vec![];
    while reader.read_line(&mut buf).unwrap() > 0 {
        data.push(buf.trim().parse::<f64>().unwrap());
        buf.clear();
    }
    data
}