Skip to content

Commit

Permalink
Readjusting Halstead Metrics (#98)
Browse files Browse the repository at this point in the history
* Changes to program length, vocab size and difficulty based on definition

* Fix tests for changes

* Update tests to reflect halstead corrections

* Update another test case

---------

Co-authored-by: sgb-io <[email protected]>
  • Loading branch information
Maimunar and sgb-io authored Mar 9, 2024
1 parent 8941dd4 commit c7c4396
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-dry-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
# Verify the output is what we expect
sudo apt-get install -y jq
EXPECTED_OUTPUT=$(cat <<'EOF'
[{"file_name":"foo.ts","cyclo":3,"halstead":{"uniq_operators":13,"uniq_operands":21,"total_operators":39,"total_operands":44,"program_length":34,"vocabulary_size":83,"volume":216.75134066579542,"difficulty":9.068181818181818,"effort":1965.5405664920995,"time":109.19669813844997,"bugs":0.07225044688859847},"line_count":16,"fta_score":39.216569387974424,"assessment":"OK"}]
[{"file_name":"foo.ts","cyclo":3,"halstead":{"uniq_operators":13,"uniq_operands":21,"total_operators":39,"total_operands":44,"program_length":83,"vocabulary_size":34,"volume":422.25941582377817,"difficulty":12.571428571428571,"effort":5308.404084641783,"time":294.9113380356546,"bugs":0.14075313860792607},"line_count":16,"fta_score":36.502594866022214,"assessment":"OK"}]
EOF
)
OUTPUT=$(npx fta-cli .github --json)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:
shell: bash
run: |
EXPECTED_OUTPUT=$(cat <<'EOF'
[{"file_name":"foo.ts","cyclo":3,"halstead":{"uniq_operators":13,"uniq_operands":21,"total_operators":39,"total_operands":44,"program_length":34,"vocabulary_size":83,"volume":216.75134066579542,"difficulty":9.068181818181818,"effort":1965.5405664920995,"time":109.19669813844997,"bugs":0.07225044688859847},"line_count":16,"fta_score":39.216569387974424,"assessment":"OK"}]
[{"file_name":"foo.ts","cyclo":3,"halstead":{"uniq_operators":13,"uniq_operands":21,"total_operators":39,"total_operands":44,"program_length":83,"vocabulary_size":34,"volume":422.25941582377817,"difficulty":12.571428571428571,"effort":5308.404084641783,"time":294.9113380356546,"bugs":0.14075313860792607},"line_count":16,"fta_score":36.502594866022214,"assessment":"OK"}]
EOF
)
if [[ "${{ runner.os }}" == "Windows" ]]; then
Expand Down
16 changes: 8 additions & 8 deletions crates/fta-wasm/src/lib_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ mod tests {
let expected_output = r#"
{
"cyclo": 1,
"fta_score": 9.534164185651022,
"fta_score": 8.159706499414824,
"line_count": 5,
"halstead_metrics": {
"bugs": 0.020810680886974055,
"difficulty": 3.3333333333333335,
"effort": 208.10680886974055,
"program_length": 14,
"time": 11.561489381652253,
"bugs": 0.027920602761755765,
"difficulty": 4.5,
"effort": 376.9281372837028,
"program_length": 22,
"time": 20.940452071316823,
"total_operands": 12,
"total_operators": 10,
"uniq_operands": 8,
"uniq_operators": 6,
"vocabulary_size": 22,
"volume": 62.43204266092216
"vocabulary_size": 14,
"volume": 83.76180828526729
}
}
"#;
Expand Down
6 changes: 3 additions & 3 deletions crates/fta/src/halstead/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ impl HalsteadMetrics {
total_operators: usize,
total_operands: usize,
) -> HalsteadMetrics {
let program_length = uniq_operators + uniq_operands;
let vocabulary_size = total_operators + total_operands;
let program_length = total_operators + total_operands;
let vocabulary_size = uniq_operators + uniq_operands;
let volume = if vocabulary_size == 0 {
0.0
} else {
Expand All @@ -422,7 +422,7 @@ impl HalsteadMetrics {
let difficulty = if total_operators == 0 || total_operands == 0 {
0.0
} else {
((total_operators / 2) as f64) * (uniq_operands as f64) / (total_operands as f64)
((uniq_operators / 2) as f64) * (total_operands as f64) / (uniq_operands as f64)
};
let effort = difficulty * volume;
let time = effort / 18.0;
Expand Down
126 changes: 63 additions & 63 deletions crates/fta/src/halstead/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ mod tests {
uniq_operands: 8,
total_operators: 9,
total_operands: 12,
program_length: 11,
vocabulary_size: 21,
volume: 48.315491650566365,
difficulty: 2.6666666666666665,
effort: 128.84131106817696,
time: 7.15785061489872,
bugs: 0.016105163883522122,
program_length: 21,
vocabulary_size: 11,
volume: 72.64806399138324,
difficulty: 1.5,
effort: 108.97209598707485,
time: 6.05400533261527,
bugs: 0.02421602133046108,
};
assert_eq!(analyze(&module), expected);
}
Expand Down Expand Up @@ -121,13 +121,13 @@ mod tests {
uniq_operands: 26,
total_operators: 43,
total_operands: 47,
program_length: 47,
vocabulary_size: 90,
volume: 305.1170955274947,
difficulty: 11.617021276595745,
effort: 3544.5517905960023,
time: 196.91954392200012,
bugs: 0.1017056985091649,
program_length: 90,
vocabulary_size: 47,
volume: 499.9129966509874,
difficulty: 18.076923076923077,
effort: 9036.888785614003,
time: 502.0493769785557,
bugs: 0.16663766555032913,
};
assert_eq!(analyze(&module), expected);
}
Expand Down Expand Up @@ -177,13 +177,13 @@ mod tests {
uniq_operands: 25,
total_operators: 31,
total_operands: 44,
program_length: 35,
vocabulary_size: 75,
volume: 218.00865416735581,
difficulty: 8.522727272727273,
effort: 1858.0283025626918,
time: 103.22379458681621,
bugs: 0.07266955138911861,
program_length: 75,
vocabulary_size: 35,
volume: 384.6962262708725,
difficulty: 8.8,
effort: 3385.3267911836783,
time: 188.07371062131546,
bugs: 0.12823207542362416,
};
assert_eq!(analyze(&module), expected);
}
Expand Down Expand Up @@ -227,13 +227,13 @@ mod tests {
uniq_operands: 27,
total_operators: 39,
total_operands: 41,
program_length: 42,
vocabulary_size: 80,
volume: 265.5209799852692,
difficulty: 12.512195121951219,
effort: 3322.2503105473925,
time: 184.56946169707737,
bugs: 0.08850699332842307,
program_length: 80,
vocabulary_size: 42,
volume: 431.38539382230084,
difficulty: 10.62962962962963,
effort: 4585.466963962976,
time: 254.74816466460976,
bugs: 0.1437951312741003,
};
assert_eq!(analyze(&module), expected);
}
Expand Down Expand Up @@ -353,13 +353,13 @@ mod tests {
uniq_operands: 75,
total_operators: 130,
total_operands: 139,
program_length: 103,
vocabulary_size: 269,
volume: 831.3606233433322,
difficulty: 35.07194244604317,
effort: 29157.43193380392,
time: 1619.8573296557734,
bugs: 0.27712020778111074,
program_length: 269,
vocabulary_size: 103,
volume: 1798.6686418122858,
difficulty: 25.946666666666665,
effort: 46669.45569288944,
time: 2592.7475384938575,
bugs: 0.5995562139374286,
};
assert_eq!(analyze(&module), expected);
}
Expand Down Expand Up @@ -389,13 +389,13 @@ mod tests {
uniq_operands: 13,
total_operators: 13,
total_operands: 17,
program_length: 20,
vocabulary_size: 30,
volume: 98.13781191217038,
difficulty: 4.588235294117647,
effort: 450.27937230289933,
time: 25.015520683494408,
bugs: 0.03271260397072346,
program_length: 30,
vocabulary_size: 20,
volume: 129.65784284662087,
difficulty: 3.923076923076923,
effort: 508.6576911675126,
time: 28.258760620417366,
bugs: 0.043219280948873624,
};
assert_eq!(analyze(&module), expected);
}
Expand Down Expand Up @@ -424,13 +424,13 @@ mod tests {
uniq_operands: 16,
total_operators: 22,
total_operands: 27,
program_length: 22,
vocabulary_size: 49,
volume: 123.52361657053459,
difficulty: 6.518518518518518,
effort: 805.1909820894106,
time: 44.73283233830059,
bugs: 0.04117453885684486,
program_length: 49,
vocabulary_size: 22,
volume: 218.51214931322758,
difficulty: 5.0625,
effort: 1106.2177558982146,
time: 61.45654199434526,
bugs: 0.0728373831044092,
};
assert_eq!(analyze(&module), expected);
}
Expand Down Expand Up @@ -472,13 +472,13 @@ mod tests {
uniq_operands: 21,
total_operators: 41,
total_operands: 46,
program_length: 27,
vocabulary_size: 87,
volume: 173.95947438791566,
difficulty: 9.130434782608695,
effort: 1588.3256357157516,
time: 88.24031309531954,
bugs: 0.057986491462638554,
program_length: 87,
vocabulary_size: 27,
volume: 413.67521268822173,
difficulty: 6.571428571428571,
effort: 2718.437111951171,
time: 151.02428399728728,
bugs: 0.1378917375627406,
};
assert_eq!(analyze(&module), expected);
}
Expand Down Expand Up @@ -508,13 +508,13 @@ mod tests {
uniq_operands: 11,
total_operators: 11,
total_operands: 13,
program_length: 17,
vocabulary_size: 24,
volume: 77.94436251225966,
difficulty: 4.230769230769231,
effort: 329.7646106287909,
time: 18.32025614604394,
bugs: 0.025981454170753218,
program_length: 24,
vocabulary_size: 17,
volume: 98.09910819000814,
difficulty: 3.5454545454545454,
effort: 347.80592903730155,
time: 19.32255161318342,
bugs: 0.032699702730002715,
};
assert_eq!(analyze(&module), expected);
}
Expand Down

0 comments on commit c7c4396

Please sign in to comment.