-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75ecd2b
commit 5d6f4be
Showing
1 changed file
with
0 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1 @@ | ||
//! 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 | ||
} |