From c5c4abb95cadd72d9b7f9819289890e92cda0218 Mon Sep 17 00:00:00 2001 From: Anders Olsson Date: Wed, 24 Oct 2018 17:24:09 +0200 Subject: [PATCH] Added approx, maybe that will fix tests on ci. --- Cargo.toml | 3 +++ src/lib.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 75f9dc9..aa1ad51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,6 @@ authors = ["Anders Olsson "] [dependencies] statrs = "0.10" + +[dev-dependencies] +approx = "0.3" diff --git a/src/lib.rs b/src/lib.rs index e1ed2e0..1d8252c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,9 @@ extern crate statrs; +#[cfg(test)] +#[macro_use] +extern crate approx; + mod factor_graph; mod gaussian; mod math; @@ -278,6 +282,7 @@ where let _ata = BETA.powi(2) * &rotated_a_matrix * &a_matrix; let _atsa = &rotated_a_matrix * &variance_matrix * &a_matrix; + let start = mean_matrix.transpose() * &a_matrix; let middle = &_ata + &_atsa; let end = &rotated_a_matrix * &mean_matrix; @@ -290,14 +295,48 @@ where #[cfg(test)] mod tests { + use std::f64; + + use approx::{AbsDiffEq, RelativeEq}; + use super::*; + impl AbsDiffEq for Rating { + type Epsilon = f64; + + #[inline] + fn default_epsilon() -> Self::Epsilon { + f64::default_epsilon() + } + + #[inline] + fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { + self.mu.abs_diff_eq(&other.mu, epsilon) && self.sigma.abs_diff_eq(&other.sigma, epsilon) + } + } + + impl RelativeEq for Rating { + fn default_max_relative() -> Self::Epsilon { + f64::default_max_relative() + } + + fn relative_eq( + &self, + other: &Self, + epsilon: Self::Epsilon, + max_relative: Self::Epsilon, + ) -> bool { + self.mu.relative_eq(&other.mu, epsilon, max_relative) + && self.sigma.relative_eq(&other.sigma, epsilon, max_relative) + } + } + #[test] fn test_quality_1vs1() { let alice = Rating::new(MU, SIGMA); let bob = Rating::new(MU, SIGMA); - assert_eq!(quality(&[&[alice], &[bob]]), 0.4472135954999579); + assert_relative_eq!(quality(&[&[alice], &[bob]]), 0.4472135954999579); } #[test] @@ -307,16 +346,17 @@ mod tests { let chris = Rating::new(MU, SIGMA); let darren = Rating::new(MU, SIGMA); - let new_ratings = rate(&[&[alice], &[bob], &[chris], &[darren]]); + let expected_ratings = vec![ + Rating::new(33.20778932559525, 6.347937214998893), + Rating::new(27.401497882797486, 5.787057812482782), + Rating::new(22.598576351652632, 5.7871159419307645), + Rating::new(16.79337409436942, 6.348053083319977), + ]; - assert_eq!( - new_ratings, - vec![ - Rating::new(33.20778932559525, 6.347937214998893), - Rating::new(27.401497882797486, 5.787057812482782), - Rating::new(22.598576351652632, 5.7871159419307645), - Rating::new(16.79337409436942, 6.348053083319977), - ] - ); + let ratings = rate(&[&[alice], &[bob], &[chris], &[darren]]); + + for (rating, expected) in ratings.iter().zip(expected_ratings.iter()) { + assert_relative_eq!(rating, expected); + } } }