Skip to content

Commit

Permalink
Add implementation for min and max in cyclomatic metric
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannitangredi committed Nov 23, 2021
1 parent 2c46ab8 commit 9b11d67
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
10 changes: 5 additions & 5 deletions rust-code-analysis-web/src/web/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ mod tests {
"spaces": {"kind": "unit",
"start_line": 1,
"end_line": 4,
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
"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},
Expand All @@ -658,7 +658,7 @@ mod tests {
"spaces": [{"kind": "function",
"start_line": 3,
"end_line": 4,
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0},
"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},
Expand Down Expand Up @@ -711,7 +711,7 @@ mod tests {
"spaces": {"kind": "unit",
"start_line": 1,
"end_line": 2,
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
"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},
Expand Down Expand Up @@ -760,7 +760,7 @@ mod tests {
"spaces": {"kind": "unit",
"start_line": 1,
"end_line": 2,
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
"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},
Expand All @@ -787,7 +787,7 @@ mod tests {
"spaces": [{"kind": "function",
"start_line": 1,
"end_line": 2,
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0},
"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},
Expand Down
43 changes: 39 additions & 4 deletions src/metrics/cyclomatic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub struct Stats {
cyclomatic_sum: f64,
cyclomatic: f64,
n: usize,
cyclomatic_max: f64,
cyclomatic_min: f64,
}

impl Default for Stats {
Expand All @@ -19,6 +21,8 @@ impl Default for Stats {
cyclomatic_sum: 0.,
cyclomatic: 1.,
n: 1,
cyclomatic_max: 0.,
cyclomatic_min: f64::MAX,
}
}
}
Expand All @@ -31,6 +35,8 @@ impl Serialize for Stats {
let mut st = serializer.serialize_struct("cyclomatic", 2)?;
st.serialize_field("sum", &self.cyclomatic_sum())?;
st.serialize_field("average", &self.cyclomatic_average())?;
st.serialize_field("min", &self.cyclomatic_min())?;
st.serialize_field("max", &self.cyclomatic_max())?;
st.end()
}
}
Expand All @@ -39,16 +45,22 @@ impl fmt::Display for Stats {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"sum: {}, average: {}",
"sum: {}, average: {}, min: {}, max: {}",
self.cyclomatic_sum(),
self.cyclomatic_average(),
self.cyclomatic_min(),
self.cyclomatic_max()
)
}
}

impl Stats {
/// Merges a second `Cyclomatic` metric into the first one
pub fn merge(&mut self, other: &Stats) {
//Calculate minimum and maximum values
self.cyclomatic_max = self.cyclomatic_max.max(other.cyclomatic_max);
self.cyclomatic_min = self.cyclomatic_min.min(other.cyclomatic_min);

self.cyclomatic_sum += other.cyclomatic_sum;
self.n += other.n;
}
Expand All @@ -69,9 +81,18 @@ impl Stats {
pub fn cyclomatic_average(&self) -> f64 {
self.cyclomatic_sum() / self.n as f64
}

/// Last step for updating cyclomatic_sum
pub fn compute_sum(&mut self) {
/// Returns the `Cyclomatic` maximum value
pub fn cyclomatic_max(&self) -> f64 {
self.cyclomatic_max
}
/// Returns the `Cyclomatic` minimum value
pub fn cyclomatic_min(&self) -> f64 {
self.cyclomatic_min
}
/// Last step for computing minimum and maximum value and update cyclomatic_sum
pub fn compute_minmax(&mut self) {
self.cyclomatic_max = self.cyclomatic_max.max(self.cyclomatic);
self.cyclomatic_min = self.cyclomatic_min.min(self.cyclomatic);
self.cyclomatic_sum += self.cyclomatic;
}
}
Expand Down Expand Up @@ -204,6 +225,8 @@ mod tests {
[(cyclomatic_sum, 6, usize)],
[
(cyclomatic_average, 3.0), // nspace = 2 (func and unit)
(cyclomatic_max, 5.0),
(cyclomatic_min, 1.0)
]
);
}
Expand All @@ -221,6 +244,8 @@ mod tests {
[(cyclomatic_sum, 4, usize)],
[
(cyclomatic_average, 2.0), // nspace = 2 (func and unit)
(cyclomatic_max, 3.0),
(cyclomatic_min, 1.0)
]
);
}
Expand All @@ -242,6 +267,8 @@ mod tests {
[(cyclomatic_sum, 5, usize)],
[
(cyclomatic_average, 2.5), // nspace = 2 (func and unit)
(cyclomatic_max, 4.0),
(cyclomatic_min, 1.0)
]
);
}
Expand Down Expand Up @@ -271,6 +298,8 @@ mod tests {
[(cyclomatic_sum, 5, usize)],
[
(cyclomatic_average, 2.5), // nspace = 2 (func and unit)
(cyclomatic_max, 4.0),
(cyclomatic_min, 1.0)
]
);
}
Expand All @@ -296,6 +325,8 @@ mod tests {
[(cyclomatic_sum, 5, usize)],
[
(cyclomatic_average, 2.5), // nspace = 2 (func and unit)
(cyclomatic_max, 4.0),
(cyclomatic_min, 1.0)
]
);
}
Expand Down Expand Up @@ -330,6 +361,8 @@ mod tests {
[(cyclomatic_sum, 7, usize)],
[
(cyclomatic_average, 3.5), // nspace = 2 (func and unit)
(cyclomatic_max, 4.0),
(cyclomatic_min, 3.0)
]
);
}
Expand Down Expand Up @@ -368,6 +401,8 @@ mod tests {
[(cyclomatic_sum, 7, usize)],
[
(cyclomatic_average, 3.5), // nspace = 2 (func and unit)
(cyclomatic_max, 4.0),
(cyclomatic_min, 3.0)
]
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ fn compute_averages(state: &mut State) {
.finalize(nom_functions, nom_closures);
}
#[inline(always)]
fn compute_sums(state: &mut State) {
state.space.metrics.cyclomatic.compute_sum();
fn compute_minmax(state: &mut State) {
state.space.metrics.cyclomatic.compute_minmax();
}

fn finalize<T: ParserTrait>(state_stack: &mut Vec<State>, diff_level: usize) {
Expand All @@ -207,13 +207,13 @@ fn finalize<T: ParserTrait>(state_stack: &mut Vec<State>, diff_level: usize) {
for _ in 0..diff_level {
if state_stack.len() == 1 {
let mut last_state = state_stack.last_mut().unwrap();
compute_sums(&mut last_state);
compute_minmax(&mut last_state);
compute_halstead_and_mi::<T>(&mut last_state);
compute_averages(&mut last_state);
break;
} else {
let mut state = state_stack.pop().unwrap();
compute_sums(&mut state);
compute_minmax(&mut state);
compute_halstead_and_mi::<T>(&mut state);
compute_averages(&mut state);

Expand Down

0 comments on commit 9b11d67

Please sign in to comment.