droid-code-checker / test_forensics.py
legitcoconut
DroidDetect-Large code authorship checker
e84edab
Raw
History Blame Contribute Delete
2.06 kB
"""Self-check for the static forensics layer. Run: python3 test_forensics.py"""
from forensics import forensic_signals, static_features
TYPED_BY_HAND = """\
n = int(input())
a = list(map(int, input().split()))
best = 0
for i in range(n):
s = 0
for j in range(i, n):
s += a[j]
if s > best: best = s
print(best)
"""
PASTED_FROM_CHAT = """\
def max_subarray_sum(arr: list) -> int:
\"\"\"Return the maximum subarray sum using Kadane’s algorithm.\"\"\"
# Step 1: initialize the running and best totals
current_sum = 0
best_sum = arr[0]
# Step 2: iterate through the array
for value in arr:
# Step 3: extend the window or start a new one
current_sum = max(value, current_sum + value)
# Step 4: update the best total seen so far
best_sum = max(best_sum, current_sum)
return best_sum
"""
def run():
typed = static_features(TYPED_BY_HAND)
pasted = static_features(PASTED_FROM_CHAT)
assert typed["smart_punct"] == 0
assert pasted["smart_punct"] == 1, "curly apostrophe should be caught"
assert pasted["ai_style_comments"] >= 3, pasted["ai_style_comments"]
assert typed["ai_style_comments"] == 0
assert typed["single_char_ratio"] > pasted["single_char_ratio"]
typed_score, typed_signals = forensic_signals(typed)
pasted_score, pasted_signals = forensic_signals(pasted)
assert typed_score == 0, typed_signals
assert pasted_score >= 60, (pasted_score, pasted_signals)
assert {s["code"] for s in pasted_signals} >= {
"unicode_punctuation",
"ai_narration_comments",
}
# Indentation forensics: a snippet that mixes tabs and spaces was assembled.
mixed = static_features("def f():\n\tx = 1\n y = 2\n return x\n")
assert mixed["mixed_indent"] is True
assert "mixed_indentation" in {s["code"] for s in forensic_signals(mixed)[1]}
# Empty input must not divide by zero.
assert static_features("")["lines"] == 0
print("forensics self-check OK")
if __name__ == "__main__":
run()