SXXXXXXX_PyUCC/tests/test_ucc_extended_counting.py

140 lines
4.4 KiB
Python

"""Test UCC-style extended counting on sample files."""
from pathlib import Path
import sys
# Add project root to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from pyucc.core.ucc_extended_counting import (
analyze_file_ucc_style,
format_ucc_table_header,
format_ucc_table_line,
)
def test_sample_file():
"""Test on actual file from UCC output."""
# Test file from your CSV
test_file = Path(
r"C:\__temp\Metrics\attuale\REP\Projects\DSP\ChimeraMCK\dev\mck_c66\mck_c66_boot_multicore.c"
)
if not test_file.exists():
print(f"⚠️ Test file not found: {test_file}")
print("Using mock data for demonstration...")
demonstrate_with_mock()
return
print(f"Analyzing: {test_file.name}")
print("=" * 80)
result = analyze_file_ucc_style(test_file)
# UCC output for comparison:
# Total=402, Blank=86, Comments(Whole=45, Embedded=13)
# Compiler=22, Data=57, Exec=127
# Logical=206, Physical=271
print("\nPyUCC Extended Results:")
print(f" Total Lines: {result['total_lines']:>6}")
print(f" Blank Lines: {result['blank_lines']:>6}")
print(f" Comments (Whole): {result['comment_whole']:>6}")
print(f" Comments (Embedded): {result['comment_embedded']:>6}")
print(f" Compiler Directives: {result['compiler_directives']:>6}")
print(f" Data Declarations: {result['data_declarations']:>6}")
print(f" Exec Instructions: {result['exec_instructions']:>6}")
print(f" Logical SLOC: {result['logical_sloc']:>6}")
print(f" Physical SLOC: {result['physical_sloc']:>6}")
print("\n" + "=" * 80)
print("UCC Expected Values (from output):")
print(f" Total Lines: {'402':>6}")
print(f" Blank Lines: {'86':>6}")
print(f" Comments (Whole): {'45':>6}")
print(f" Comments (Embedded): {'13':>6}")
print(f" Compiler Directives: {'22':>6}")
print(f" Data Declarations: {'57':>6}")
print(f" Exec Instructions: {'127':>6}")
print(f" Logical SLOC: {'206':>6}")
print(f" Physical SLOC: {'271':>6}")
print("\n" + "=" * 80)
print("Differences:")
print(f" Total: {result['total_lines'] - 402:>+6}")
print(f" Blank: {result['blank_lines'] - 86:>+6}")
print(f" Whole Cmt: {result['comment_whole'] - 45:>+6}")
print(f" Embed Cmt: {result['comment_embedded'] - 13:>+6}")
print(f" Directive: {result['compiler_directives'] - 22:>+6}")
print(f" Data Decl: {result['data_declarations'] - 57:>+6}")
print(f" Exec Inst: {result['exec_instructions'] - 127:>+6}")
print(f" Logical: {result['logical_sloc'] - 206:>+6}")
print(f" Physical: {result['physical_sloc'] - 271:>+6}")
print("\n" + "=" * 80)
print("UCC-Style Formatted Output:")
print(format_ucc_table_header())
print(format_ucc_table_line(result, test_file.name))
def demonstrate_with_mock():
"""Demonstrate with mock C code."""
mock_code = """// File: example.c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
/* Multi-line comment
explaining the function */
int calculate(int x, int y) {
int result = 0; // Initialize result
if (x > 0) {
result = x + y; /* Add values */
} else {
result = x - y;
}
return result;
}
int main() {
int value = 10;
printf("Value: %d\\n", value);
return 0;
}
"""
# Write to temp file
import tempfile
with tempfile.NamedTemporaryFile(mode="w", suffix=".c", delete=False) as f:
f.write(mock_code)
temp_path = Path(f.name)
print(f"Testing with mock C code...")
print("=" * 80)
result = analyze_file_ucc_style(temp_path)
print("\nMock Code Results:")
print(f" Total Lines: {result['total_lines']:>6}")
print(f" Blank Lines: {result['blank_lines']:>6}")
print(f" Comments (Whole): {result['comment_whole']:>6}")
print(f" Comments (Embedded): {result['comment_embedded']:>6}")
print(f" Compiler Directives: {result['compiler_directives']:>6}")
print(f" Data Declarations: {result['data_declarations']:>6}")
print(f" Exec Instructions: {result['exec_instructions']:>6}")
print(f" Logical SLOC: {result['logical_sloc']:>6}")
print(f" Physical SLOC: {result['physical_sloc']:>6}")
# Cleanup
temp_path.unlink()
if __name__ == "__main__":
test_sample_file()