File size: 3,639 Bytes
d26f22a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Assertion evaluators UT-D001 through UT-D003."""

from __future__ import annotations

import re
from typing import Any

from .contracts import EmptyParams, EvaluationContext, EvaluatorResult
from .ut_common import check, measurement, unavailable
from .ut_phase import _docs


def _assertions(plan: dict[str, Any]) -> list[dict[str, Any]]:
    design = plan.get("component_design") if isinstance(plan.get("component_design"), dict) else {}
    return [item for item in design.get("assertions") or [] if isinstance(item, dict)]


def ut_d001_assertion_code(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult:
    del params
    docs = _docs(context)
    if docs is None or not docs["plan"] or docs["checker"] is None or not docs["checker"].is_file():
        return unavailable("UT-D001", "计划或 checker 不存在")
    body = docs["checker"].read_text(encoding="utf-8", errors="replace")
    items = _assertions(docs["plan"])
    if not items:
        return unavailable("UT-D001", "计划里没有断言")
    generated = all(
        item.get("name") and str(item["name"]) in body and "assert property" in body and "$error" in body
        for item in items
    )
    clocked = "posedge" in body or "@(" in body
    return check("UT-D001", {"generated": generated and clocked}, "断言代码")


def _pair(docs: dict[str, Any], golden_name: str, bad_name: str) -> tuple[str, str]:
    sim = docs["env"] / "sim" if docs.get("env") is not None else None
    if sim is None:
        return "", ""
    golden = sim / golden_name
    bad = sim / bad_name
    return (
        golden.read_text(encoding="utf-8", errors="replace") if golden.is_file() else "",
        bad.read_text(encoding="utf-8", errors="replace") if bad.is_file() else "",
    )


def ut_d002_assertion_runtime(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult:
    del params
    docs = _docs(context)
    if docs is None or not docs["plan"]:
        return unavailable("UT-D002", "计划不存在")
    names = [str(item["name"]) for item in _assertions(docs["plan"]) if item.get("name")]
    golden, bad = _pair(docs, "assert_golden.log", "assert_bad.log")
    if not names or not golden or not bad:
        return unavailable("UT-D002", "缺少断言名或 golden/违例日志")
    clean = not any(name in golden and ("Error" in golden or "assert" in golden.lower()) for name in names)
    reported = any(name in bad and re.search(r"\d+", bad) is not None for name in names)
    return check("UT-D002", {"reported": clean and reported}, "断言运行时报错")


def ut_d003_assertion_discrimination(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult:
    del params
    docs = _docs(context)
    if docs is None or not docs["plan"]:
        return unavailable("UT-D003", "计划不存在")
    names = [str(item["name"]) for item in _assertions(docs["plan"]) if item.get("name")]
    golden, bad = _pair(docs, "assert_golden.log", "assert_bad.log")
    if not names or not golden or not bad:
        return unavailable("UT-D003", "缺少断言名或对照日志")
    no_false = all(name not in golden for name in names)
    no_miss = all(name in bad for name in names)
    return check("UT-D003", {"discriminates": no_false and no_miss}, "断言误报和漏报")


UT_ASSERT_EVALUATORS = {
    item[0]: measurement(item[0], item[1], item[2])
    for item in (
        ("ut_d001_assertion_code", ut_d001_assertion_code, "UT-D001"),
        ("ut_d002_assertion_runtime", ut_d002_assertion_runtime, "UT-D002"),
        ("ut_d003_assertion_discrimination", ut_d003_assertion_discrimination, "UT-D003"),
    )
}