Skip to content

Commit

Permalink
ADD unit tests for sample line
Browse files Browse the repository at this point in the history
 - small fix to sample line
 - added two unit test
 - extracted sample_line into new file
  • Loading branch information
yxdunc committed Dec 16, 2019
1 parent 1d8f2c1 commit 8193d8f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use core::time;


mod app;
mod plotting_utils;
mod widget_progress_bar;
mod widget_main_chart;
mod widget_text_output;
Expand Down
35 changes: 35 additions & 0 deletions src/plotting_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
pub fn sample_line(a: f64, b: f64, min: (f64, f64), max: (f64, f64), sample_rate: f64) -> Vec<(f64, f64)> {
let mut x = min.0;
let mut sampled_line: Vec<(f64, f64)> = vec![];

while x < max.0 {
sampled_line.push((x, a * x + b));
x += sample_rate;
}

sampled_line
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_sample_horizontal_line() {
let line = sample_line(0.0, 42.0, (0.0, 0.0), (100.0, 100.0), 0.1);

assert_eq!(line.len(), 1001, "not the good number of samples");
assert_eq!((line[0].0.round(), line[0].1.round()), (0.0, 42.0));
assert_eq!((line[10].0.round(), line[10].1.round()), (1.0, 42.0));
assert_eq!((line[1000].0.round(), line[1000].1.round()), (100.0, 42.0));
}

#[test]
fn test_sample_vertical_line() {
let line = sample_line(std::f64::MAX, 0.0, (0.0, 0.0), (100.0, 100.0), 0.1);

assert_eq!(line.len(), 1001, "not the good number of samples");
assert_eq!(line[0], (0.0, 0.0));
assert_eq!((line[1000].0.round(), line[1000].1.round()), (100.0, std::f64::INFINITY));
}
}
15 changes: 2 additions & 13 deletions src/widget_main_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,9 @@ use termion::raw::RawTerminal;
use std::io::Stdout;
use tui::widgets::{Dataset, Marker, Chart, Block, Axis, Widget};
use tui::style::{Style, Color};
use crate::plotting_utils::sample_line;

const SAMPLE_RATE: f64 = 0.1;

fn sample_line(a: f64, b: f64, min: (f64, f64), max: (f64, f64), sample_rate: f64) -> Vec<(f64, f64)> {
let mut x = min.0;
let mut sampled_line: Vec<(f64, f64)> = vec![];

while x < max.0 {
x += sample_rate;
sampled_line.push((x, a * x + b));
}

sampled_line
}
const SAMPLE_RATE: f64 = 0.01;

pub fn main_chart(frame: &mut Frame<TermionBackend<RawTerminal<Stdout>>>,
regression: (f64, f64),
Expand Down

0 comments on commit 8193d8f

Please sign in to comment.