From 7101503567a5ef973260fbafc081e20706ced3e3 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan <107496615+maksym-arutyunyan@users.noreply.github.com> Date: Mon, 4 Jul 2022 09:19:17 +0200 Subject: [PATCH] fix #3 add complexity rank for relative comparison (#5) --- Cargo.toml | 8 ++- README.md | 1 + examples/stress.rs | 6 +- src/complexity.rs | 147 +++++++++++++++++++++++++++++++++++++++- src/lib.rs | 3 + src/name.rs | 144 +++++++++++++++++++++++++++++++++++++++ tests/api.rs | 105 +++++++++++++++------------- tests/debug.rs | 36 +++++----- tests/execution_time.rs | 32 ++++----- 9 files changed, 393 insertions(+), 89 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9d7ce23..d2d93bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,14 +1,16 @@ [package] name = "big_o" description = "Infers asymptotic computational complexity" -version = "0.1.1" +version = "0.1.2" edition = "2021" authors = ["Maksym Arutyunyan"] repository = "https://github.com/maksym-arutyunyan/big_o" - +homepage = "https://github.com/maksym-arutyunyan/big_o" +documentation = "https://docs.rs/big-o/" readme = "README.md" license = "Apache-2.0" -keywords = ["big_o", "asymptotic", "complexity"] +keywords = ["big_o", "big-o", "asymptotic", "complexity", "analysis", "performance", "algorithm"] +categories = ["development-tools", "development-tools::profiling", "development-tools::testing"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 1857968..8ca370f 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,5 @@ assert_eq!(complexity.name, big_o::Name::Quadratic); assert_eq!(complexity.notation, "O(n^2)"); assert_approx_eq!(complexity.params.gain.unwrap(), 1.0, 1e-6); assert_approx_eq!(complexity.params.offset.unwrap(), 0.0, 1e-6); +assert!(complexity.rank < big_o::complexity("O(n^3)").unwrap().rank); ``` diff --git a/examples/stress.rs b/examples/stress.rs index f7d47ea..dff22a2 100644 --- a/examples/stress.rs +++ b/examples/stress.rs @@ -60,7 +60,7 @@ fn main() { .map(|i| i as f64) .map(|x| (x, f(x))) .collect(); - let (best, _all) = big_o::infer_complexity(data).unwrap(); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); let (a, b) = match name { big_o::Name::Constant => (0.0, offset), @@ -69,9 +69,9 @@ fn main() { _ => (gain, offset), }; - let is_ok = name == best.name; + let is_ok = name == complexity.name; if !is_ok { - let line = format!("{},{:?},{},{},{:?}", is_ok, name, a, b, best.name); + let line = format!("{},{:?},{},{},{:?}", is_ok, name, a, b, complexity.name); if let Err(e) = writeln!(file, "{}", line) { eprintln!("Couldn't write to file: {}", e); } diff --git a/src/complexity.rs b/src/complexity.rs index 1c0bb79..95ea164 100644 --- a/src/complexity.rs +++ b/src/complexity.rs @@ -14,6 +14,9 @@ pub struct Complexity { /// Approximation function parameters pub params: Params, + + /// Relative rank to compare complexities + pub rank: u32, } impl Complexity { @@ -49,6 +52,37 @@ impl Complexity { } } +pub struct ComplexityBuilder { + name: Name, + params: Option, +} + +impl ComplexityBuilder { + pub fn new(name: Name) -> Self { + Self { name, params: None } + } + + #[allow(dead_code)] // Used in tests. + pub fn power(&mut self, x: f64) -> &mut Self { + self.params = Some(Params::new().power(x).build()); + self + } + + pub fn build(&self) -> Complexity { + let mut params = Params::new().build(); + if let Some(p) = &self.params { + params = p.clone(); + } + let rank = rank(self.name, params.clone()); + Complexity { + name: self.name, + notation: name::notation(self.name), + params, + rank, + } + } +} + /// Transforms input data into linear complexity. fn linearize(name: Name, x: f64, y: f64) -> (f64, f64) { match name { @@ -79,6 +113,35 @@ fn delinearize(name: Name, gain: f64, offset: f64, residuals: f64) -> Params { } } +fn rank(name: Name, params: Params) -> u32 { + // Rank is similar to a degree of a corresponding polynomial: + // - constant: 0, f(x) = x ^ 0.000 + // - logarithmic: 130, empirical value k for a big x in f(x) = x ^ k + // base 1_000_000 log of 6 is 0.130 + // approx. f(x) = x ^ 0.130 + // - linear: 1_000, f(x) = x ^ 1.000 + // - linearithmic: 1_130, approx. f(x) = x ^ 1.130 + // - quadratic: 2_000, f(x) = x ^ 2.000 + // - cubic: 3_000, f(x) = x ^ 3.000 + // - polynomial: depends on polynomial degree + // - exponential: 1_000_000, practically there is no sense in polynomial degree > 1_000.000 + match name { + Name::Constant => 0, + Name::Logarithmic => 130, + Name::Linear => 1_000, + Name::Linearithmic => 1_130, + Name::Quadratic => 2_000, + Name::Cubic => 3_000, + Name::Polynomial => { + match params.power { + Some(power) => std::cmp::min((1_000.0 * power) as u32, 1_000_000), + None => panic!("Polynomial is missing its power parameter"), + } + } + Name::Exponential => 1_000_000, + } +} + /// Fits a function of given complexity into input data. pub fn fit(name: Name, data: Vec<(f64, f64)>) -> Result { let linearized = data @@ -87,10 +150,92 @@ pub fn fit(name: Name, data: Vec<(f64, f64)>) -> Result Result { + let name: Name = string.try_into()?; + Ok(crate::complexity::ComplexityBuilder::new(name).build()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn constant() -> Complexity { + ComplexityBuilder::new(Name::Constant).build() + } + + fn logarithmic() -> Complexity { + ComplexityBuilder::new(Name::Logarithmic).build() + } + + fn linear() -> Complexity { + ComplexityBuilder::new(Name::Linear).build() + } + + fn linearithmic() -> Complexity { + ComplexityBuilder::new(Name::Linearithmic).build() + } + + fn quadratic() -> Complexity { + ComplexityBuilder::new(Name::Quadratic).build() + } + + fn cubic() -> Complexity { + ComplexityBuilder::new(Name::Cubic).build() + } + + fn exponential() -> Complexity { + ComplexityBuilder::new(Name::Exponential).build() + } + + fn polynomial(power: f64) -> Complexity { + ComplexityBuilder::new(Name::Polynomial) + .power(power) + .build() + } + + #[test] + fn test_complecity_rank() { + // O(1) < ... < O(n) + assert!(constant().rank < logarithmic().rank); + assert!(logarithmic().rank < polynomial(0.5).rank); + assert!(polynomial(0.5).rank < linear().rank); + + // O(n) < ... < O(n^2) + assert!(linear().rank < linearithmic().rank); + assert!(linearithmic().rank < polynomial(1.5).rank); + assert!(polynomial(1.5).rank < quadratic().rank); + + // O(n^2) < ... < O(n^3) + assert!(quadratic().rank < polynomial(2.5).rank); + assert!(polynomial(2.5).rank < cubic().rank); + + // O(n^3) < ... < O(c^n) + assert!(cubic().rank < polynomial(3.5).rank); + assert!(polynomial(3.5).rank < exponential().rank); + } +} diff --git a/src/lib.rs b/src/lib.rs index 2c94a79..bff3cc4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ //! assert_eq!(complexity.notation, "O(n^2)"); //! assert_approx_eq!(complexity.params.gain.unwrap(), 1.0, 1e-6); //! assert_approx_eq!(complexity.params.offset.unwrap(), 0.0, 1e-6); +//! assert!(complexity.rank < big_o::complexity("O(n^3)").unwrap().rank); //! ``` mod complexity; @@ -24,6 +25,7 @@ mod name; mod params; mod validate; +pub use crate::complexity::complexity; pub use crate::complexity::Complexity; pub use crate::name::Name; pub use crate::params::Params; @@ -42,6 +44,7 @@ pub use crate::params::Params; /// assert_eq!(complexity.notation, "O(n^2)"); /// assert_approx_eq!(complexity.params.gain.unwrap(), 1.0, 1e-6); /// assert_approx_eq!(complexity.params.offset.unwrap(), 0.0, 1e-6); +/// assert!(complexity.rank < big_o::complexity("O(n^3)").unwrap().rank); /// ``` pub fn infer_complexity( data: Vec<(f64, f64)>, diff --git a/src/name.rs b/src/name.rs index b7f18a5..3f19ace 100644 --- a/src/name.rs +++ b/src/name.rs @@ -1,3 +1,6 @@ +use std::fmt; +use std::str::FromStr; + /// Names of asymptotic computational complexities. #[derive(PartialEq, Debug, Copy, Clone)] pub enum Name { @@ -38,3 +41,144 @@ pub fn notation(name: Name) -> &'static str { Name::Exponential => "O(c^n)", } } + +impl From for &str { + fn from(name: Name) -> &'static str { + notation(name) + } +} + +impl TryFrom<&str> for Name { + type Error = &'static str; + + fn try_from(string: &str) -> Result { + match &string.to_lowercase()[..] { + "o(1)" => Ok(Name::Constant), + "constant" => Ok(Name::Constant), + + "o(log n)" => Ok(Name::Logarithmic), + "logarithmic" => Ok(Name::Logarithmic), + + "o(n)" => Ok(Name::Linear), + "linear" => Ok(Name::Linear), + + "o(n log n)" => Ok(Name::Linearithmic), + "linearithmic" => Ok(Name::Linearithmic), + + "o(n^2)" => Ok(Name::Quadratic), + "quadratic" => Ok(Name::Quadratic), + + "o(n^3)" => Ok(Name::Cubic), + "cubic" => Ok(Name::Cubic), + + "o(n^m)" => Ok(Name::Polynomial), + "polynomial" => Ok(Name::Polynomial), + + "o(c^n)" => Ok(Name::Exponential), + "exponential" => Ok(Name::Exponential), + _ => Err("Can't convert string to Name"), + } + } +} + +impl FromStr for Name { + type Err = &'static str; + + fn from_str(s: &str) -> Result { + s.try_into() + } +} + +impl fmt::Display for Name { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", notation(*self)) + } +} + + +#[cfg(test)] +mod tests{ + use super::*; + + const NOTATION_TEST_CASES: [(&str, Name); 8] = [ + ("O(1)", Name::Constant), + ("O(log n)", Name::Logarithmic), + ("O(n)", Name::Linear), + ("O(n log n)", Name::Linearithmic), + ("O(n^2)", Name::Quadratic), + ("O(n^3)", Name::Cubic), + ("O(n^m)", Name::Polynomial), + ("O(c^n)", Name::Exponential), + ]; + + const NAMED_TEST_CASES: [(&str, Name); 8] = [ + ("Constant", Name::Constant), + ("Logarithmic", Name::Logarithmic), + ("Linear", Name::Linear), + ("Linearithmic", Name::Linearithmic), + ("Quadratic", Name::Quadratic), + ("Cubic", Name::Cubic), + ("Polynomial", Name::Polynomial), + ("Exponential", Name::Exponential), + ]; + + #[test] + fn name_into_string() { + for (string, name) in NOTATION_TEST_CASES { + let converted: &str = name.into(); + assert_eq!(converted, string); + assert_eq!(Into::<&str>::into(name), string); + } + } + + #[test] + fn name_to_string() { + for (string, name) in NOTATION_TEST_CASES { + assert_eq!(name.to_string(), string); + } + } + + #[test] + fn string_try_from() { + let test_cases = [NOTATION_TEST_CASES, NAMED_TEST_CASES].concat(); + for (string, name) in test_cases { + assert_eq!(Name::try_from(string).unwrap(), name); + } + } + + #[test] + #[should_panic] + fn string_try_from_fails() { + Name::try_from("irrlevant text").unwrap(); + } + + #[test] + fn string_try_into() { + let test_cases = [NOTATION_TEST_CASES, NAMED_TEST_CASES].concat(); + for (string, name) in test_cases { + let into: Name = string.try_into().unwrap(); + assert_eq!(into, name); + } + } + + #[test] + #[should_panic] + fn string_try_into_fails() { + let _: Name = "irrlevant text".try_into().unwrap(); + } + + #[test] + fn string_parse() { + let test_cases = [NOTATION_TEST_CASES, NAMED_TEST_CASES].concat(); + for (string, name) in test_cases { + let parse: Name = string.parse().unwrap(); + assert_eq!(parse, name); + } + } + + #[test] + #[should_panic] + fn string_parse_fails() { + let _: Name = "irrlevant text".parse().unwrap(); + } +} diff --git a/tests/api.rs b/tests/api.rs index 55de94f..b754f6e 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -60,26 +60,26 @@ fn infer_each() { for (f, name, notation, params) in test_cases { let data: Vec<(f64, f64)> = (1..100).map(|i| i as f64).map(|x| (x, f(x))).collect(); - let (best, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, name); - assert_eq!(best.notation, notation); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); + assert_eq!(complexity.name, name); + assert_eq!(complexity.notation, notation); assert_approx_eq!( - best.params.gain.unwrap_or(0.), + complexity.params.gain.unwrap_or(0.), params.gain.unwrap_or(0.), EPSILON ); assert_approx_eq!( - best.params.offset.unwrap_or(0.), + complexity.params.offset.unwrap_or(0.), params.offset.unwrap_or(0.), EPSILON ); assert_approx_eq!( - best.params.power.unwrap_or(0.), + complexity.params.power.unwrap_or(0.), params.power.unwrap_or(0.), EPSILON ); assert_approx_eq!( - best.params.base.unwrap_or(0.), + complexity.params.base.unwrap_or(0.), params.base.unwrap_or(0.), EPSILON ); @@ -94,12 +94,13 @@ fn infer_constant() { let f = Box::new(|x: f64| gain * x + offset); let data: Vec<(f64, f64)> = (1..100).map(|i| i as f64).map(|x| (x, f(x))).collect(); - let (best, _all) = big_o::infer_complexity(data).unwrap(); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, name); - assert_eq!(best.notation, notation); - assert_approx_eq!(best.params.gain.unwrap(), gain, EPSILON); - assert_approx_eq!(best.params.offset.unwrap(), offset, EPSILON); + assert_eq!(complexity.name, name); + assert_eq!(complexity.notation, notation); + assert_approx_eq!(complexity.params.gain.unwrap(), gain, EPSILON); + assert_approx_eq!(complexity.params.offset.unwrap(), offset, EPSILON); + assert!(complexity.rank <= big_o::complexity("O(1)").unwrap().rank); } #[test] @@ -110,12 +111,13 @@ fn infer_logarithmic() { let f = Box::new(|x: f64| gain * x.ln() + offset); let data: Vec<(f64, f64)> = (1..100).map(|i| i as f64).map(|x| (x, f(x))).collect(); - let (best, _all) = big_o::infer_complexity(data).unwrap(); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, name); - assert_eq!(best.notation, notation); - assert_approx_eq!(best.params.gain.unwrap(), gain, EPSILON); - assert_approx_eq!(best.params.offset.unwrap(), offset, EPSILON); + assert_eq!(complexity.name, name); + assert_eq!(complexity.notation, notation); + assert_approx_eq!(complexity.params.gain.unwrap(), gain, EPSILON); + assert_approx_eq!(complexity.params.offset.unwrap(), offset, EPSILON); + assert!(complexity.rank <= big_o::complexity("O(log n)").unwrap().rank); } #[test] @@ -126,12 +128,13 @@ fn infer_linear() { let f = Box::new(|x: f64| gain * x + offset); let data: Vec<(f64, f64)> = (1..100).map(|i| i as f64).map(|x| (x, f(x))).collect(); - let (best, _all) = big_o::infer_complexity(data).unwrap(); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, name); - assert_eq!(best.notation, notation); - assert_approx_eq!(best.params.gain.unwrap(), gain, EPSILON); - assert_approx_eq!(best.params.offset.unwrap(), offset, EPSILON); + assert_eq!(complexity.name, name); + assert_eq!(complexity.notation, notation); + assert_approx_eq!(complexity.params.gain.unwrap(), gain, EPSILON); + assert_approx_eq!(complexity.params.offset.unwrap(), offset, EPSILON); + assert!(complexity.rank <= big_o::complexity("O(n)").unwrap().rank); } #[test] @@ -142,12 +145,13 @@ fn infer_linearithmic() { let f = Box::new(|x: f64| gain * x * x.ln() + offset); let data: Vec<(f64, f64)> = (1..100).map(|i| i as f64).map(|x| (x, f(x))).collect(); - let (best, _all) = big_o::infer_complexity(data).unwrap(); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, name); - assert_eq!(best.notation, notation); - assert_approx_eq!(best.params.gain.unwrap(), gain, EPSILON); - assert_approx_eq!(best.params.offset.unwrap(), offset, EPSILON); + assert_eq!(complexity.name, name); + assert_eq!(complexity.notation, notation); + assert_approx_eq!(complexity.params.gain.unwrap(), gain, EPSILON); + assert_approx_eq!(complexity.params.offset.unwrap(), offset, EPSILON); + assert!(complexity.rank <= big_o::complexity("O(n log n)").unwrap().rank); } #[test] @@ -158,12 +162,13 @@ fn infer_quadratic() { let f = Box::new(|x: f64| gain * x.powi(2) + offset); let data: Vec<(f64, f64)> = (1..100).map(|i| i as f64).map(|x| (x, f(x))).collect(); - let (best, _all) = big_o::infer_complexity(data).unwrap(); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, name); - assert_eq!(best.notation, notation); - assert_approx_eq!(best.params.gain.unwrap(), gain, EPSILON); - assert_approx_eq!(best.params.offset.unwrap(), offset, EPSILON); + assert_eq!(complexity.name, name); + assert_eq!(complexity.notation, notation); + assert_approx_eq!(complexity.params.gain.unwrap(), gain, EPSILON); + assert_approx_eq!(complexity.params.offset.unwrap(), offset, EPSILON); + assert!(complexity.rank <= big_o::complexity("O(n^2)").unwrap().rank); } #[test] @@ -174,12 +179,13 @@ fn infer_cubic() { let f = Box::new(|x: f64| gain * x.powi(3) + offset); let data: Vec<(f64, f64)> = (1..100).map(|i| i as f64).map(|x| (x, f(x))).collect(); - let (best, _all) = big_o::infer_complexity(data).unwrap(); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, name); - assert_eq!(best.notation, notation); - assert_approx_eq!(best.params.gain.unwrap(), gain, EPSILON); - assert_approx_eq!(best.params.offset.unwrap(), offset, EPSILON); + assert_eq!(complexity.name, name); + assert_eq!(complexity.notation, notation); + assert_approx_eq!(complexity.params.gain.unwrap(), gain, EPSILON); + assert_approx_eq!(complexity.params.offset.unwrap(), offset, EPSILON); + assert!(complexity.rank <= big_o::complexity("O(n^3)").unwrap().rank); } #[test] @@ -190,12 +196,14 @@ fn infer_polynomial() { let f = Box::new(|x: f64| gain * x.powf(power)); let data: Vec<(f64, f64)> = (1..100).map(|i| i as f64).map(|x| (x, f(x))).collect(); - let (best, _all) = big_o::infer_complexity(data).unwrap(); - - assert_eq!(best.name, name); - assert_eq!(best.notation, notation); - assert_approx_eq!(best.params.gain.unwrap(), gain, EPSILON); - assert_approx_eq!(best.params.power.unwrap(), power, EPSILON); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); + + assert_eq!(complexity.name, name); + assert_eq!(complexity.notation, notation); + assert_approx_eq!(complexity.params.gain.unwrap(), gain, EPSILON); + assert_approx_eq!(complexity.params.power.unwrap(), power, EPSILON); + // Note: impossible to create a generic complexity O(n^m) without providing its degree. + assert!(complexity.rank < big_o::complexity("O(c^n)").unwrap().rank); } #[test] @@ -206,10 +214,11 @@ fn infer_exponential() { let f = Box::new(|x: f64| gain * base.powf(x)); let data: Vec<(f64, f64)> = (1..100).map(|i| i as f64).map(|x| (x, f(x))).collect(); - let (best, _all) = big_o::infer_complexity(data).unwrap(); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, name); - assert_eq!(best.notation, notation); - assert_approx_eq!(best.params.gain.unwrap(), gain, EPSILON); - assert_approx_eq!(best.params.base.unwrap(), base, EPSILON); + assert_eq!(complexity.name, name); + assert_eq!(complexity.notation, notation); + assert_approx_eq!(complexity.params.gain.unwrap(), gain, EPSILON); + assert_approx_eq!(complexity.params.base.unwrap(), base, EPSILON); + assert!(complexity.rank <= big_o::complexity("O(c^n)").unwrap().rank); } diff --git a/tests/debug.rs b/tests/debug.rs index 78487a5..9c38728 100644 --- a/tests/debug.rs +++ b/tests/debug.rs @@ -10,14 +10,14 @@ fn infer_exponential_stress_1() { let f = Box::new(|x: f64| gain * base.powf(x)); let data: Vec<(f64, f64)> = (1..100).map(|i| i as f64).map(|x| (x, f(x))).collect(); - let (best, _all) = big_o::infer_complexity(data).unwrap(); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); - println!("{:?}", best.params); + println!("{:?}", complexity.params); - assert_eq!(best.name, name); - assert_eq!(best.notation, notation); - assert_approx_eq!(best.params.gain.unwrap(), gain, EPSILON); - assert_approx_eq!(best.params.base.unwrap(), base, EPSILON); + assert_eq!(complexity.name, name); + assert_eq!(complexity.notation, notation); + assert_approx_eq!(complexity.params.gain.unwrap(), gain, EPSILON); + assert_approx_eq!(complexity.params.base.unwrap(), base, EPSILON); } #[test] @@ -28,14 +28,14 @@ fn infer_exponential_stress_2() { let f = Box::new(|x: f64| gain * base.powf(x)); let data: Vec<(f64, f64)> = (1..100).map(|i| i as f64).map(|x| (x, f(x))).collect(); - let (best, _all) = big_o::infer_complexity(data).unwrap(); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); - println!("{:?}", best.params); + println!("{:?}", complexity.params); - assert_eq!(best.name, name); - assert_eq!(best.notation, notation); - assert_approx_eq!(best.params.gain.unwrap(), gain, EPSILON); - assert_approx_eq!(best.params.base.unwrap(), base, EPSILON); + assert_eq!(complexity.name, name); + assert_eq!(complexity.notation, notation); + assert_approx_eq!(complexity.params.gain.unwrap(), gain, EPSILON); + assert_approx_eq!(complexity.params.base.unwrap(), base, EPSILON); } #[test] @@ -46,12 +46,12 @@ fn infer_logarithmic_stress_1() { let f = Box::new(|x: f64| gain * x.ln() + offset); let data: Vec<(f64, f64)> = (1..100).map(|i| i as f64).map(|x| (x, f(x))).collect(); - let (best, _all) = big_o::infer_complexity(data).unwrap(); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); - println!("{:?}", best.params); + println!("{:?}", complexity.params); - assert_eq!(best.name, name); - assert_eq!(best.notation, notation); - assert_approx_eq!(best.params.gain.unwrap(), gain, EPSILON); - assert_approx_eq!(best.params.offset.unwrap(), offset, EPSILON); + assert_eq!(complexity.name, name); + assert_eq!(complexity.notation, notation); + assert_approx_eq!(complexity.params.gain.unwrap(), gain, EPSILON); + assert_approx_eq!(complexity.params.offset.unwrap(), offset, EPSILON); } diff --git a/tests/execution_time.rs b/tests/execution_time.rs index aff1b61..a3e65ae 100644 --- a/tests/execution_time.rs +++ b/tests/execution_time.rs @@ -74,8 +74,8 @@ fn time_infer_complexity_constant() { let runs = 3; let n: Vec = vec![1, 1_000, 1_000_000, 1_000_000_000, 1_000_000_000_000]; let data = measure_execution_time(&n, dummy_constant, runs); - let (best, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, big_o::Name::Constant); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); + assert_eq!(complexity.name, big_o::Name::Constant); } #[test] @@ -83,8 +83,8 @@ fn time_infer_complexity_logarithmic() { let runs = 10; let n: Vec = vec![1, 10, 100, 1_000, 10_000, 100_000]; let data = measure_execution_time(&n, dummy_logarithmic, runs); - let (best, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, big_o::Name::Logarithmic); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); + assert_eq!(complexity.name, big_o::Name::Logarithmic); } #[test] @@ -92,8 +92,8 @@ fn time_infer_complexity_linear() { let runs = 10; let n: Vec = vec![1, 2, 5, 10, 50, 100, 500]; let data = measure_execution_time(&n, dummy_linear, runs); - let (best, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, big_o::Name::Linear); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); + assert_eq!(complexity.name, big_o::Name::Linear); } #[test] @@ -101,8 +101,8 @@ fn time_infer_complexity_linearithmic() { let runs = 10; let n: Vec = vec![1, 2, 5, 10, 20, 50, 100]; let data = measure_execution_time(&n, dummy_linearithmic, runs); - let (best, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, big_o::Name::Linearithmic); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); + assert_eq!(complexity.name, big_o::Name::Linearithmic); } #[test] @@ -110,8 +110,8 @@ fn time_infer_complexity_quadratic() { let runs = 10; let n: Vec = vec![1, 2, 5, 10, 20, 50]; let data = measure_execution_time(&n, dummy_quadratic, runs); - let (best, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, big_o::Name::Quadratic); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); + assert_eq!(complexity.name, big_o::Name::Quadratic); } #[test] @@ -119,8 +119,8 @@ fn time_infer_complexity_cubic() { let runs = 5; let n: Vec = vec![1, 2, 5, 10, 20]; let data = measure_execution_time(&n, dummy_cubic, runs); - let (best, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, big_o::Name::Cubic); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); + assert_eq!(complexity.name, big_o::Name::Cubic); } #[test] @@ -128,8 +128,8 @@ fn time_infer_complexity_polynomial() { let runs = 1; let n: Vec = vec![3, 5, 7, 9, 11, 13]; let data = measure_execution_time(&n, dummy_polynomial, runs); - let (best, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, big_o::Name::Polynomial); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); + assert_eq!(complexity.name, big_o::Name::Polynomial); } #[test] @@ -137,6 +137,6 @@ fn time_infer_complexity_exponential() { let runs = 1; let n: Vec = vec![1, 3, 5, 7, 9, 11]; let data = measure_execution_time(&n, dummy_exponential, runs); - let (best, _all) = big_o::infer_complexity(data).unwrap(); - assert_eq!(best.name, big_o::Name::Exponential); + let (complexity, _all) = big_o::infer_complexity(data).unwrap(); + assert_eq!(complexity.name, big_o::Name::Exponential); }