1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! Complexity Validation Test
//!
//! This file contains functions with manually calculated complexity
//! to validate pmat's complexity analysis accuracy.
/// Base complexity function (should be 1)
/// Manual calculation: 1 (base complexity, no branches)
pub fn base_complexity() -> i32 {
42
}
/// Single if statement (should be 2)
/// Manual calculation: 1 (base) + 1 (if branch) = 2
pub fn single_if(x: i32) -> i32 {
if x > 0 {
// +1
x
} else {
0
}
}
/// Two independent if statements (should be 3)
/// Manual calculation: 1 (base) + 1 (first if) + 1 (second if) = 3
pub fn two_independent_ifs(x: i32, y: i32) -> i32 {
let mut result = 0;
if x > 0 {
// +1
result += x;
}
if y > 0 {
// +1
result += y;
}
result
}
/// Nested if statements (should be 4)
/// Manual calculation: 1 (base) + 1 (outer if) + 1 (inner if) + 1 (else) = 4
/// Cognitive complexity should be higher due to nesting
pub fn nested_ifs(x: i32) -> i32 {
if x > 0 {
// +1 cyclomatic, +1 cognitive
if x > 10 {
// +1 cyclomatic, +2 cognitive (nested)
x * 2
} else {
// +1 cyclomatic, +1 cognitive
x + 1
}
} else {
0
}
}
/// Match statement (should count each arm)
/// Manual calculation: 1 (base) + 3 (match arms) = 4
pub fn match_statement(x: i32) -> &'static str {
match x {
1 => "one", // +1
2 => "two", // +1
_ => "other", // +1
}
}
/// Loop with condition (should count loop + condition)
/// Manual calculation: 1 (base) + 1 (for loop) + 1 (if condition) = 3
pub fn loop_with_condition(numbers: &[i32]) -> i32 {
let mut sum = 0;
for &num in numbers {
// +1
if num > 0 {
// +1
sum += num;
}
}
sum
}
/// Complex function with multiple control structures
/// Manual calculation:
/// 1 (base) + 1 (if x > 0) + 1 (for loop) + 1 (if num % 2) + 1 (match) (3 arms) = 7
pub fn complex_function(x: i32, numbers: &[i32]) -> i32 {
if x <= 0 {
// +1
return 0;
}
let mut result = x;
for &num in numbers {
// +1
if num % 2 == 0 {
// +1
result += num;
}
let modifier = match num {
// +1 for match, +1 for each arm
0 => 0, // +1
1..=10 => 1, // +1
_ => 2, // +1
};
result += modifier;
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_functions_work() {
assert_eq!(base_complexity(), 42);
assert_eq!(single_if(5), 5);
assert_eq!(single_if(-1), 0);
assert_eq!(two_independent_ifs(3, 4), 7);
assert_eq!(nested_ifs(15), 30);
assert_eq!(nested_ifs(5), 6);
assert_eq!(match_statement(1), "one");
assert_eq!(loop_with_condition(&[1, 2, -1, 3]), 6);
assert_eq!(complex_function(0, &[]), 0);
assert_eq!(complex_function(5, &[2, 3, 4]), 13);
}
}
fn main() {
println!("Complexity validation examples:");
println!("base_complexity(): {}", base_complexity());
println!("single_if(5): {}", single_if(5));
println!("two_independent_ifs(3, 4): {}", two_independent_ifs(3, 4));
println!("nested_ifs(15): {}", nested_ifs(15));
println!("match_statement(2): {}", match_statement(2));
println!(
"loop_with_condition([1,2,3]): {}",
loop_with_condition(&[1, 2, 3])
);
println!(
"complex_function(5, [2,4,6]): {}",
complex_function(5, &[2, 4, 6])
);
println!("\nRun complexity analysis with:");
println!("pmat analyze complexity --include \"server/examples/complexity_validation.rs\"");
println!("\nExpected complexity values:");
println!("- base_complexity: Cyclomatic=1, Cognitive=0");
println!("- single_if: Cyclomatic=2, Cognitive=1");
println!("- two_independent_ifs: Cyclomatic=3, Cognitive=2");
println!("- nested_ifs: Cyclomatic=4, Cognitive=4 (nesting penalty)");
println!("- match_statement: Cyclomatic=4, Cognitive=1");
println!("- loop_with_condition: Cyclomatic=3, Cognitive=2");
println!("- complex_function: Cyclomatic=7, Cognitive=6");
}