138 lines
4.2 KiB
Python
138 lines
4.2 KiB
Python
"""Test UCC counters for Java and Assembly."""
|
|
|
|
from pathlib import Path
|
|
from pyucc.core.ucc_java_counter import UCCJavaCounter
|
|
from pyucc.core.ucc_assembly_counter import UCCAssemblyCounter
|
|
import tempfile
|
|
|
|
|
|
def test_java_counter():
|
|
"""Test Java counter on a sample file."""
|
|
java_code = """
|
|
package com.example.test;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
|
|
/**
|
|
* This is a multi-line
|
|
* comment block
|
|
*/
|
|
public class TestClass {
|
|
private int count; // embedded comment
|
|
private String name;
|
|
|
|
public TestClass(int count, String name) {
|
|
this.count = count;
|
|
this.name = name;
|
|
}
|
|
|
|
public void execute() {
|
|
for (int i = 0; i < count; i++) {
|
|
System.out.println(name);
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".java", delete=False) as f:
|
|
f.write(java_code)
|
|
temp_path = Path(f.name)
|
|
|
|
try:
|
|
counter = UCCJavaCounter()
|
|
results = counter.analyze_file(temp_path)
|
|
|
|
print("=== Java Counter Test ===")
|
|
print(f"Blank: {results['blank_lines']}")
|
|
print(
|
|
f"Comments (W/E): {results['comment_whole']}/{results['comment_embedded']}"
|
|
)
|
|
print(f"Directives: {results['compiler_directives']}")
|
|
print(f"Data: {results['data_declarations']}")
|
|
print(f"Exec: {results['exec_instructions']}")
|
|
print(f"Logical SLOC: {results['logical_sloc']}")
|
|
print(f"Physical SLOC: {results['physical_sloc']}")
|
|
|
|
# Assertions
|
|
assert results["blank_lines"] > 0, "Should have blank lines"
|
|
assert results["comment_whole"] > 0, "Should have whole comments"
|
|
assert results["comment_embedded"] > 0, "Should have embedded comments"
|
|
assert results["compiler_directives"] >= 2, "Should have import directives"
|
|
assert results["data_declarations"] > 0, "Should have data declarations"
|
|
assert results["exec_instructions"] > 0, "Should have exec instructions"
|
|
assert results["logical_sloc"] > 0, "Should have logical SLOC"
|
|
assert results["physical_sloc"] > 0, "Should have physical SLOC"
|
|
|
|
print("✓ Java counter test PASSED\n")
|
|
|
|
finally:
|
|
temp_path.unlink()
|
|
|
|
|
|
def test_assembly_counter():
|
|
"""Test Assembly counter on a sample file."""
|
|
asm_code = """
|
|
; This is a comment
|
|
.data
|
|
myvar: .word 0 ; data declaration
|
|
buffer: .space 100
|
|
|
|
.text
|
|
.globl main
|
|
main:
|
|
# Another comment style
|
|
li $t0, 5 ; load immediate
|
|
move $a0, $t0 # move register
|
|
jal print ; jump and link
|
|
|
|
print:
|
|
/* multi-line
|
|
comment block */
|
|
syscall
|
|
jr $ra
|
|
"""
|
|
|
|
with tempfile.NamedTemporaryFile(mode="w", suffix=".asm", delete=False) as f:
|
|
f.write(asm_code)
|
|
temp_path = Path(f.name)
|
|
|
|
try:
|
|
counter = UCCAssemblyCounter()
|
|
results = counter.analyze_file(temp_path)
|
|
|
|
print("=== Assembly Counter Test ===")
|
|
print(f"Blank: {results['blank_lines']}")
|
|
print(
|
|
f"Comments (W/E): {results['comment_whole']}/{results['comment_embedded']}"
|
|
)
|
|
print(f"Directives: {results['compiler_directives']}")
|
|
print(f"Data: {results['data_declarations']}")
|
|
print(f"Exec: {results['exec_instructions']}")
|
|
print(f"Logical SLOC: {results['logical_sloc']}")
|
|
print(f"Physical SLOC: {results['physical_sloc']}")
|
|
|
|
# Assertions
|
|
assert results["blank_lines"] > 0, "Should have blank lines"
|
|
assert results["comment_whole"] > 0, "Should have whole comments"
|
|
assert results["comment_embedded"] > 0, "Should have embedded comments"
|
|
assert (
|
|
results["data_declarations"] > 0
|
|
), "Should have data declarations (in .data section)"
|
|
assert (
|
|
results["exec_instructions"] > 0
|
|
), "Should have exec instructions (in .text section)"
|
|
assert results["logical_sloc"] > 0, "Should have logical SLOC"
|
|
assert results["physical_sloc"] > 0, "Should have physical SLOC"
|
|
|
|
print("✓ Assembly counter test PASSED\n")
|
|
|
|
finally:
|
|
temp_path.unlink()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_java_counter()
|
|
test_assembly_counter()
|
|
print("✅ All tests passed!")
|