Add minimum and maximum implementation for the Nexit (#712)

This commit is contained in:
giovannitangredi 2021-11-29 10:25:51 +01:00 коммит произвёл GitHub
Родитель b4a30c4e8c
Коммит 11cbb2b6ed
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 80 добавлений и 18 удалений

Просмотреть файл

@ -634,7 +634,7 @@ mod tests {
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min":1.0, "max":1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
"effort": 4.754_887_502_163_468,
@ -661,7 +661,7 @@ mod tests {
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0, "min":1.0, "max":1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
"effort": 4.754_887_502_163_468,
@ -714,7 +714,7 @@ mod tests {
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min":1.0, "max":1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
"effort": 4.754_887_502_163_468,
@ -763,7 +763,7 @@ mod tests {
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min": 1.0,"max": 1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
"effort": 4.754_887_502_163_468,
@ -790,7 +790,7 @@ mod tests {
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0, "min": 1.0,"max": 1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0},
"halstead": {"bugs": 0.000_942_552_557_372_941_4,
"difficulty": 1.0,
"effort": 4.754_887_502_163_468,

Просмотреть файл

@ -32,7 +32,7 @@ impl Serialize for Stats {
where
S: Serializer,
{
let mut st = serializer.serialize_struct("cyclomatic", 2)?;
let mut st = serializer.serialize_struct("cyclomatic", 4)?;
st.serialize_field("sum", &self.cyclomatic_sum())?;
st.serialize_field("average", &self.cyclomatic_average())?;
st.serialize_field("min", &self.cyclomatic_min())?;

Просмотреть файл

@ -12,14 +12,20 @@ use crate::*;
#[derive(Debug, Clone)]
pub struct Stats {
exit: usize,
exit_sum: usize,
total_space_functions: usize,
exit_min: usize,
exit_max: usize,
}
impl Default for Stats {
fn default() -> Self {
Self {
exit: 0,
exit_sum: 0,
total_space_functions: 1,
exit_min: usize::MAX,
exit_max: 0,
}
}
}
@ -29,29 +35,52 @@ impl Serialize for Stats {
where
S: Serializer,
{
let mut st = serializer.serialize_struct("nexits", 2)?;
let mut st = serializer.serialize_struct("nexits", 4)?;
st.serialize_field("sum", &self.exit())?;
st.serialize_field("average", &self.exit_average())?;
st.serialize_field("min", &self.exit())?;
st.serialize_field("max", &self.exit_average())?;
st.end()
}
}
impl fmt::Display for Stats {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "sum: {}, average: {}", self.exit(), self.exit_average())
write!(
f,
"sum: {}, average: {} min: {}, max: {}",
self.exit_sum(),
self.exit_average(),
self.exit_min(),
self.exit_max()
)
}
}
impl Stats {
/// Merges a second `NExit` metric into the first one
pub fn merge(&mut self, other: &Stats) {
self.exit += other.exit;
self.exit_max = self.exit_max.max(other.exit_max);
self.exit_min = self.exit_min.min(other.exit_min);
self.exit_sum += other.exit_sum;
}
/// Returns the `NExit` metric value
pub fn exit(&self) -> f64 {
self.exit as f64
}
/// Returns the `NExit` metric sum value
pub fn exit_sum(&self) -> f64 {
self.exit_sum as f64
}
/// Returns the `NExit` metric minimum value
pub fn exit_min(&self) -> f64 {
self.exit_min as f64
}
/// Returns the `NExit` metric maximum value
pub fn exit_max(&self) -> f64 {
self.exit_max as f64
}
/// Returns the `NExit` metric average value
///
@ -60,9 +89,13 @@ impl Stats {
///
/// 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
self.exit_sum() / self.total_space_functions as f64
}
pub fn compute_minmax(&mut self) {
self.exit_max = self.exit_max.max(self.exit);
self.exit_min = self.exit_min.min(self.exit);
self.exit_sum += self.exit;
}
pub(crate) fn finalize(&mut self, total_space_functions: usize) {
self.total_space_functions = total_space_functions;
}
@ -151,7 +184,11 @@ mod tests {
"foo.py",
PythonParser,
nexits,
[(exit, 0, usize)],
[
(exit_sum, 0, usize),
(exit_min, 0, usize),
(exit_max, 0, usize)
],
[(exit_average, f64::NAN)] // 0 functions
);
}
@ -163,7 +200,11 @@ mod tests {
"foo.rs",
RustParser,
nexits,
[(exit, 0, usize)],
[
(exit_sum, 0, usize),
(exit_min, 0, usize),
(exit_max, 0, usize)
],
[(exit_average, f64::NAN)] // 0 functions
);
}
@ -175,7 +216,11 @@ mod tests {
"foo.c",
CppParser,
nexits,
[(exit, 0, usize)],
[
(exit_sum, 0, usize),
(exit_min, 0, usize),
(exit_max, 0, usize)
],
[(exit_average, f64::NAN)] // 0 functions
);
}
@ -187,7 +232,11 @@ mod tests {
"foo.js",
JavascriptParser,
nexits,
[(exit, 0, usize)],
[
(exit_sum, 0, usize),
(exit_min, 0, usize),
(exit_max, 0, usize)
],
[(exit_average, f64::NAN)] // 0 functions
);
}
@ -201,7 +250,11 @@ mod tests {
"foo.py",
PythonParser,
nexits,
[(exit, 1, usize)],
[
(exit_sum, 1, usize),
(exit_min, 0, usize),
(exit_max, 1, usize)
],
[(exit_average, 1.0)] // 1 function
);
}
@ -218,7 +271,11 @@ mod tests {
"foo.py",
PythonParser,
nexits,
[(exit, 2, usize)],
[
(exit_sum, 2, usize),
(exit_min, 0, usize),
(exit_max, 1, usize)
],
[(exit_average, 1.0)] // 2 functions
);
}
@ -235,7 +292,11 @@ mod tests {
"foo.py",
PythonParser,
nexits,
[(exit, 2, usize)],
[
(exit_sum, 2, usize),
(exit_min, 0, usize),
(exit_max, 1, usize)
],
[(exit_average, 0.5)] // 2 functions + 2 lambdas = 4
);
}

Просмотреть файл

@ -198,6 +198,7 @@ fn compute_averages(state: &mut State) {
#[inline(always)]
fn compute_minmax(state: &mut State) {
state.space.metrics.cyclomatic.compute_minmax();
state.space.metrics.nexits.compute_minmax();
}
fn finalize<T: ParserTrait>(state_stack: &mut Vec<State>, diff_level: usize) {