From 676e7988be060255d4a9a774ec82e9664e6a8fb8 Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Mon, 11 Jan 2021 10:38:58 +0100 Subject: [PATCH 1/2] Add a way to compare NAN values in tests --- src/macros.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/macros.rs b/src/macros.rs index a8b1702a8..20c3194fa 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -297,7 +297,12 @@ macro_rules! check_metrics { $( $( - assert!((func_space.metrics.$metric.$func_float() - $true_float_value).abs() < f64::EPSILON); + let value: f64 = $true_float_value; + if !value.is_nan() { + assert!((func_space.metrics.$metric.$func_float() - value).abs() < f64::EPSILON); + } else { + assert!(func_space.metrics.$metric.$func_float().is_nan()); + } )* )? } From 1601cc5fb40fd9231f0b3dcd6dd0271a055395b6 Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Mon, 11 Jan 2021 10:52:34 +0100 Subject: [PATCH 2/2] Set exit average to NAN when no functions --- src/metrics/exit.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/metrics/exit.rs b/src/metrics/exit.rs index 0827387c0..90685d262 100644 --- a/src/metrics/exit.rs +++ b/src/metrics/exit.rs @@ -57,6 +57,8 @@ impl Stats { /// /// This value is computed dividing the `NExit` value /// for the total number of functions/closures in a space. + /// + /// If there are no functions in a code, its value is `NAN`. pub fn exit_average(&self) -> f64 { self.exit() / self.total_space_functions as f64 } @@ -143,6 +145,54 @@ mod tests { use super::*; + #[test] + fn python_no_exit() { + check_metrics!( + "a = 42", + "foo.py", + PythonParser, + nexits, + [(exit, 0, usize)], + [(exit_average, f64::NAN)] // 0 functions + ); + } + + #[test] + fn rust_no_exit() { + check_metrics!( + "let a = 42;", + "foo.rs", + RustParser, + nexits, + [(exit, 0, usize)], + [(exit_average, f64::NAN)] // 0 functions + ); + } + + #[test] + fn c_no_exit() { + check_metrics!( + "int a = 42;", + "foo.c", + CppParser, + nexits, + [(exit, 0, usize)], + [(exit_average, f64::NAN)] // 0 functions + ); + } + + #[test] + fn javascript_no_exit() { + check_metrics!( + "var a = 42;", + "foo.js", + JavascriptParser, + nexits, + [(exit, 0, usize)], + [(exit_average, f64::NAN)] // 0 functions + ); + } + #[test] fn python_simple_function() { check_metrics!(