Browse Source

Add some unit tests

pull/2/head
JuanLeon Lahoz 5 years ago
parent
commit
4a18d805f9
  1. 106
      Cargo.lock
  2. 1
      Cargo.toml
  3. 37
      src/histogram.rs
  4. 7
      src/main.rs
  5. 97
      src/reader.rs

106
Cargo.lock generated

@ -38,6 +38,12 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "3.0.0-beta.2"
@ -149,6 +155,17 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.9.1"
@ -195,9 +212,9 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e31a8281fc93ec9693494da65fbf28c0c2aa60a2eaec25dc58e2f31952e95edc"
dependencies = [
"cfg-if",
"cfg-if 0.1.10",
"libc",
"redox_syscall",
"redox_syscall 0.1.57",
"winapi",
]
@ -222,6 +239,7 @@ dependencies = [
"float_eq",
"isatty",
"regex",
"tempfile",
"yansi",
]
@ -237,6 +255,12 @@ version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85"
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@ -279,12 +303,61 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
"rand_core",
]
[[package]]
name = "redox_syscall"
version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "redox_syscall"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8270314b5ccceb518e7e578952f0b72b88222d02e8f77f5ecf7abbb673539041"
dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.4.5"
@ -302,6 +375,15 @@ version = "0.6.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5f089152e60f62d28b835fbff2cd2e8dc0baf1ac13343bef92ab7eed84548"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi",
]
[[package]]
name = "strsim"
version = "0.10.0"
@ -319,6 +401,20 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "tempfile"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"
dependencies = [
"cfg-if 1.0.0",
"libc",
"rand",
"redox_syscall 0.2.6",
"remove_dir_all",
"winapi",
]
[[package]]
name = "termcolor"
version = "1.1.2"
@ -367,6 +463,12 @@ version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "winapi"
version = "0.3.9"

1
Cargo.toml

@ -13,3 +13,4 @@ regex = "1.4.5"
[dev-dependencies]
float_eq = "0.5.0"
tempfile = "3"

37
src/histogram.rs

@ -140,3 +140,40 @@ impl HistWriter {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use yansi::Paint;
#[test]
fn basic_test() {
let stats = Stats::new(&[-2.0, 14.0]);
let mut hist = Histogram::new(8, 2.5, stats);
hist.load(&[
-1.0, -1.1, 2.0, 2.0, 2.1, -0.9, 11.0, 11.2, 1.9, 1.99, 1.98, 1.97, 1.96,
]);
assert_eq!(hist.top, 8);
let bucket = &hist.vec[0];
assert_eq!(bucket.range, -2.0..0.5);
assert_eq!(bucket.count, 3);
let bucket = &hist.vec[1];
assert_eq!(bucket.count, 8);
assert_eq!(bucket.range, 0.5..3.0);
}
#[test]
fn display_test() {
let stats = Stats::new(&[-2.0, 14.0]);
let mut hist = Histogram::new(8, 2.5, stats);
hist.load(&[
-1.0, -1.1, 2.0, 2.0, 2.1, -0.9, 11.0, 11.2, 1.9, 1.99, 1.98, 1.97, 1.96,
]);
Paint::disable();
let display = format!("{}", hist);
assert!(display.find("[-2.000 .. 0.500] [3] ∎∎∎\n").is_some());
assert!(display.find("[ 0.500 .. 3.000] [8] ∎∎∎∎∎∎∎∎\n").is_some());
assert!(display.find("[10.500 .. 13.000] [2] ∎∎\n").is_some());
}
}

7
src/main.rs

@ -102,12 +102,15 @@ fn main() {
Ok(re) => {
builder.regex(re);
}
_ => eprintln!("[{}]: Failed to parse regex {}", Red.paint("ERROR"), string),
_ => {
eprintln!("[{}]: Failed to parse regex {}", Red.paint("ERROR"), string);
std::process::exit(1);
}
};
}
let reader = builder.build().unwrap();
let vec = reader.read(opts.input);
let vec = reader.read(&opts.input);
if vec.is_empty() {
eprintln!("[{}]: No data", Yellow.paint("WARN"));
std::process::exit(0);

97
src/reader.rs

@ -16,9 +16,9 @@ pub struct DataReader {
}
impl DataReader {
pub fn read(&self, path: String) -> Vec<f64> {
pub fn read(&self, path: &str) -> Vec<f64> {
let mut vec: Vec<f64> = vec![];
match path.as_str() {
match path {
"-" => {
vec = self.read_data(io::stdin().lock().lines());
}
@ -100,3 +100,96 @@ impl DataReader {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
fn basic_reader_test() {
let reader = DataReader::default();
match NamedTempFile::new() {
Ok(ref mut file) => {
writeln!(file, "1.3").unwrap();
writeln!(file, "foobar").unwrap();
writeln!(file, "2").unwrap();
writeln!(file, "-2.7").unwrap();
let vec = reader.read(file.path().to_str().unwrap());
assert_eq!(vec, [1.3, 2.0, -2.7]);
}
Err(_) => assert!(false, "Could not create temp file"),
}
}
#[test]
fn regex_first_match() {
let re = Regex::new("^foo ([0-9.-]+) ([0-9.-]+)").unwrap();
let reader = DataReaderBuilder::default().regex(re).build().unwrap();
match NamedTempFile::new() {
Ok(ref mut file) => {
writeln!(file, "foo 1.3 1.6").unwrap();
writeln!(file, "nothing").unwrap();
writeln!(file, "1.1").unwrap();
writeln!(file, "1.1 1.2").unwrap();
writeln!(file, "foo -2 3").unwrap();
writeln!(file, "foo 5").unwrap();
let vec = reader.read(file.path().to_str().unwrap());
assert_eq!(vec, [1.3, -2.0]);
}
Err(_) => assert!(false, "Could not create temp file"),
}
}
#[test]
fn regex_named_match() {
let re = Regex::new("^foo ([0-9.-]+) (?P<value>[0-9.-]+)").unwrap();
let reader = DataReaderBuilder::default().regex(re).build().unwrap();
match NamedTempFile::new() {
Ok(ref mut file) => {
writeln!(file, "foo 1.3 1.6").unwrap();
writeln!(file, "nothing").unwrap();
writeln!(file, "1.1").unwrap();
writeln!(file, "1.1 1.2").unwrap();
writeln!(file, "foo -2 3").unwrap();
writeln!(file, "foo 5").unwrap();
let vec = reader.read(file.path().to_str().unwrap());
assert_eq!(vec, [1.6, 3.0]);
}
Err(_) => assert!(false, "Could not create temp file"),
}
}
#[test]
fn regex_empty_file() {
let reader = DataReader::default();
match NamedTempFile::new() {
Ok(ref mut file) => {
let vec = reader.read(file.path().to_str().unwrap());
assert_eq!(vec, []);
}
Err(_) => assert!(false, "Could not create temp file"),
}
}
#[test]
fn range() {
let reader = DataReaderBuilder::default()
.range(-1.0..1.0)
.build()
.unwrap();
match NamedTempFile::new() {
Ok(ref mut file) => {
writeln!(file, "1.3").unwrap();
writeln!(file, "2").unwrap();
writeln!(file, "-0.5").unwrap();
writeln!(file, "0.5").unwrap();
let vec = reader.read(file.path().to_str().unwrap());
assert_eq!(vec, [-0.5, 0.5]);
}
Err(_) => assert!(false, "Could not create temp file"),
}
}
}

Loading…
Cancel
Save