@@ -10,6 +10,8 @@ pub struct Stats {
1010 cyclomatic_sum : f64 ,
1111 cyclomatic : f64 ,
1212 n : usize ,
13+ cyclomatic_max : f64 ,
14+ cyclomatic_min : f64 ,
1315}
1416
1517impl Default for Stats {
@@ -18,6 +20,8 @@ impl Default for Stats {
1820 cyclomatic_sum : 0. ,
1921 cyclomatic : 1. ,
2022 n : 1 ,
23+ cyclomatic_max : 0. ,
24+ cyclomatic_min : f64:: MAX ,
2125 }
2226 }
2327}
@@ -30,6 +34,8 @@ impl Serialize for Stats {
3034 let mut st = serializer. serialize_struct ( "cyclomatic" , 2 ) ?;
3135 st. serialize_field ( "sum" , & self . cyclomatic_sum ( ) ) ?;
3236 st. serialize_field ( "average" , & self . cyclomatic_average ( ) ) ?;
37+ st. serialize_field ( "min" , & self . cyclomatic_min ( ) ) ?;
38+ st. serialize_field ( "max" , & self . cyclomatic_max ( ) ) ?;
3339 st. end ( )
3440 }
3541}
@@ -38,16 +44,22 @@ impl fmt::Display for Stats {
3844 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
3945 write ! (
4046 f,
41- "sum: {}, average: {}" ,
47+ "sum: {}, average: {}, min: {}, max: {} " ,
4248 self . cyclomatic_sum( ) ,
4349 self . cyclomatic_average( ) ,
50+ self . cyclomatic_min( ) ,
51+ self . cyclomatic_max( )
4452 )
4553 }
4654}
4755
4856impl Stats {
4957 /// Merges a second `Cyclomatic` metric into the first one
5058 pub fn merge ( & mut self , other : & Stats ) {
59+ //Calculate minimum and maximum values
60+ self . cyclomatic_max = self . cyclomatic_max . max ( other. cyclomatic_max ) ;
61+ self . cyclomatic_min = self . cyclomatic_min . min ( other. cyclomatic_min ) ;
62+
5163 self . cyclomatic_sum += other. cyclomatic_sum ;
5264 self . n += other. n ;
5365 }
@@ -68,9 +80,18 @@ impl Stats {
6880 pub fn cyclomatic_average ( & self ) -> f64 {
6981 self . cyclomatic_sum ( ) / self . n as f64
7082 }
71-
83+ /// Returns the `Cyclomatic` maximum value
84+ pub fn cyclomatic_max ( & self ) -> f64 {
85+ self . cyclomatic_max
86+ }
87+ /// Returns the `Cyclomatic` minimum value
88+ pub fn cyclomatic_min ( & self ) -> f64 {
89+ self . cyclomatic_min
90+ }
7291 /// Last step for computing minimum and maximum value and update cyclomatic_sum
7392 pub fn compute_minmax ( & mut self ) {
93+ self . cyclomatic_max = self . cyclomatic_max . max ( self . cyclomatic ) ;
94+ self . cyclomatic_min = self . cyclomatic_min . min ( self . cyclomatic ) ;
7495 self . cyclomatic_sum += self . cyclomatic ;
7596 }
7697}
@@ -202,7 +223,9 @@ mod tests {
202223 cyclomatic,
203224 [ ( cyclomatic_sum, 6 , usize ) ] ,
204225 [
205- ( cyclomatic_average, 3.0 ) , // nspace = 2 (func and unit)
226+ ( cyclomatic_average, 3.0 ) , // nspace = 2 (func and unit)
227+ ( cyclomatic_max, 5.0 ) ,
228+ ( cyclomatic_min, 1.0 )
206229 ]
207230 ) ;
208231 }
@@ -220,6 +243,8 @@ mod tests {
220243 [ ( cyclomatic_sum, 4 , usize ) ] ,
221244 [
222245 ( cyclomatic_average, 2.0 ) , // nspace = 2 (func and unit)
246+ ( cyclomatic_max, 3.0 ) ,
247+ ( cyclomatic_min, 1.0 )
223248 ]
224249 ) ;
225250 }
@@ -240,7 +265,9 @@ mod tests {
240265 cyclomatic,
241266 [ ( cyclomatic_sum, 5 , usize ) ] ,
242267 [
243- ( cyclomatic_average, 2.5 ) , // nspace = 2 (func and unit)
268+ ( cyclomatic_average, 2.5 ) , // nspace = 2 (func and unit)
269+ ( cyclomatic_max, 4.0 ) ,
270+ ( cyclomatic_min, 1.0 )
244271 ]
245272 ) ;
246273 }
@@ -270,6 +297,8 @@ mod tests {
270297 [ ( cyclomatic_sum, 5 , usize ) ] ,
271298 [
272299 ( cyclomatic_average, 2.5 ) , // nspace = 2 (func and unit)
300+ ( cyclomatic_max, 4.0 ) ,
301+ ( cyclomatic_min, 1.0 )
273302 ]
274303 ) ;
275304 }
@@ -295,6 +324,8 @@ mod tests {
295324 [ ( cyclomatic_sum, 5 , usize ) ] ,
296325 [
297326 ( cyclomatic_average, 2.5 ) , // nspace = 2 (func and unit)
327+ ( cyclomatic_max, 4.0 ) ,
328+ ( cyclomatic_min, 1.0 )
298329 ]
299330 ) ;
300331 }
@@ -329,6 +360,8 @@ mod tests {
329360 [ ( cyclomatic_sum, 7 , usize ) ] ,
330361 [
331362 ( cyclomatic_average, 3.5 ) , // nspace = 2 (func and unit)
363+ ( cyclomatic_max, 4.0 ) ,
364+ ( cyclomatic_min, 3.0 )
332365 ]
333366 ) ;
334367 }
@@ -367,6 +400,8 @@ mod tests {
367400 [ ( cyclomatic_sum, 7 , usize ) ] ,
368401 [
369402 ( cyclomatic_average, 3.5 ) , // nspace = 2 (func and unit)
403+ ( cyclomatic_max, 4.0 ) ,
404+ ( cyclomatic_min, 3.0 )
370405 ]
371406 ) ;
372407 }
0 commit comments