"""Phase-consistency evaluators UT-A001 through UT-A013.""" from __future__ import annotations import re from pathlib import Path from typing import Any from .contracts import EmptyParams, EvaluationContext, EvaluatorResult from .ut_common import check, measurement, unavailable from .ut_mechanical import _manifest, _path_from_manifest, _task_dir def _yaml(path: Path | None) -> dict[str, Any] | None: if path is None or not path.is_file(): return None try: import yaml except ImportError: return None try: data = yaml.safe_load(path.read_text(encoding="utf-8")) except (OSError, yaml.YAMLError): return None return data if isinstance(data, dict) else None def _docs(context: EvaluationContext) -> dict[str, Any] | None: task = _task_dir(context) manifest = _manifest(task) if task is not None else None if task is None or manifest is None: return None output = _path_from_manifest(task, manifest, "output_dir") env = _path_from_manifest(task, manifest, "env_output_dir") spec = _path_from_manifest(task, manifest, "task_as_path") if output is None: return None identity = manifest.get("identity") if isinstance(manifest.get("identity"), dict) else {} dut = identity.get("dut_name") if isinstance(identity.get("dut_name"), str) else "" plan = output / f"V_{dut}_verification_plan.yaml" return { "tp": _yaml(output / "testpoint-decomposition.yaml"), "plan": _yaml(plan), "spec": spec.read_text(encoding="utf-8", errors="replace") if spec is not None and spec.is_file() else "", "env": env, "dut": dut, "tb": (env / "tb" / f"tb_{dut}.sv") if env is not None else None, "checker": (env / "tb" / f"{dut}_checker.sv") if env is not None else None, "inventory": (env / "sim" / "testcase_inventory.yaml") if env is not None else None, } def _points(report: dict[str, Any]) -> list[dict[str, Any]]: groups = report.get("testpoints") if not isinstance(groups, dict): return [] found = [] for items in groups.values(): if isinstance(items, list): found.extend(item for item in items if isinstance(item, dict)) return found def _cases(report: dict[str, Any]) -> list[dict[str, Any]]: groups = report.get("testcases") if not isinstance(groups, dict): return [] found = [] for items in groups.values(): if isinstance(items, list): found.extend(item for item in items if isinstance(item, dict)) return found def _ids(text: str) -> set[str]: return set(re.findall(r"\bTP-\d+\b", text)) def ut_a001_feature_coverage(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params docs = _docs(context) if docs is None or not docs["tp"] or not docs["spec"]: return unavailable("UT-A001", "Spec 或测试点 YAML 不存在") features = [item for item in docs["tp"].get("features") or [] if isinstance(item, dict)] points = _points(docs["tp"]) covered = all( any(point.get("feature_id") == item.get("id") or point.get("feature") == item.get("name") for point in points) and str(item.get("name") or "") in docs["spec"] for item in features ) return check("UT-A001", {"covered": bool(features) and covered}, "特性覆盖") def ut_a002_interface_coverage(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params docs = _docs(context) if docs is None or not docs["tp"] or not docs["spec"]: return unavailable("UT-A002", "Spec 或测试点 YAML 不存在") section = docs["spec"].split("接口", 1)[-1] if "接口" in docs["spec"] else "" names = re.findall(r"^\|\s*([A-Za-z_][\w.]*)\s*\|", section, re.MULTILINE) points = [item for item in _points(docs["tp"]) if "接口" in str(item.get("feature") or "") or item.get("coverage_type") == "Interface"] interface_points = docs["tp"].get("testpoints", {}).get("interface") if isinstance(docs["tp"].get("testpoints"), dict) else [] texts = " ".join(str(item.get("testpoint") or "") for item in list(interface_points or []) + points) if not names: return unavailable("UT-A002", "Spec 里没有接口表") return check("UT-A002", {"interfaces": all(name in texts for name in names)}, "接口覆盖") def ut_a003_tp_to_case(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params docs = _docs(context) if docs is None or not docs["tp"]: return unavailable("UT-A003", "测试点 YAML 不存在") points = {item.get("id") for item in _points(docs["tp"]) if item.get("id")} cited = set() for case in _cases(docs["tp"]): for step in case.get("steps") or []: if isinstance(step, dict): cited.update(step.get("source_tp_ids") or []) cited.update(case.get("purpose_testpoints") or []) if not points: return unavailable("UT-A003", "没有测试点 ID") return check("UT-A003", {"mapped": points <= cited}, "测试点到用例") def ut_a004_plan_ids(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params docs = _docs(context) if docs is None or not docs["tp"] or not docs["plan"]: return unavailable("UT-A004", "测试点或计划 YAML 不存在") known = {item.get("id") for item in _points(docs["tp"]) if item.get("id")} import json cited = _ids(json.dumps(docs["plan"], ensure_ascii=False)) if not cited: return unavailable("UT-A004", "计划里没有测试点 ID") return check("UT-A004", {"valid": cited <= known}, "计划引用的测试点") def _feature_supported(docs: dict[str, Any], key: str) -> bool: features = [item for item in docs["tp"].get("features") or [] if isinstance(item, dict)] blob = "" node = docs["plan"].get("stimulus_checking_requirements") if isinstance(node, dict): import json blob = json.dumps(node.get(key) or [], ensure_ascii=False) return bool(features) and all( str(item.get("id")) in blob or str(item.get("name")) in blob for item in features ) def ut_a005_stimulus(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params docs = _docs(context) if docs is None or not docs["tp"] or not docs["plan"]: return unavailable("UT-A005", "测试点或计划 YAML 不存在") return check("UT-A005", {"stimulus": _feature_supported(docs, "stimulus_requirements")}, "激励需求") def ut_a006_checking(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params docs = _docs(context) if docs is None or not docs["tp"] or not docs["plan"]: return unavailable("UT-A006", "测试点或计划 YAML 不存在") return check("UT-A006", {"checking": _feature_supported(docs, "checking_requirements")}, "比对需求") def ut_a007_either(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params docs = _docs(context) if docs is None or not docs["tp"] or not docs["plan"]: return unavailable("UT-A007", "测试点或计划 YAML 不存在") features = [item for item in docs["tp"].get("features") or [] if isinstance(item, dict)] import json node = docs["plan"].get("stimulus_checking_requirements") blob = json.dumps(node, ensure_ascii=False) if isinstance(node, dict) else "" covered = bool(features) and all(str(item.get("id")) in blob or str(item.get("name")) in blob for item in features) return check("UT-A007", {"either": covered}, "激励或比对") def ut_a008_coverpoints(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params docs = _docs(context) if docs is None or not docs["tp"] or not docs["plan"]: return unavailable("UT-A008", "测试点或计划 YAML 不存在") functional = [] groups = docs["tp"].get("testpoints") if isinstance(groups, dict): functional = [item for item in groups.get("functional") or [] if isinstance(item, dict)] import json blob = json.dumps(docs["plan"].get("coverage_model") or {}, ensure_ascii=False) mapped = bool(functional) and all( str(item.get("id")) in blob or "cp_" in str(item.get("coverage_desc") or "") for item in functional ) return check("UT-A008", {"mapped": mapped}, "功能覆盖映射") def ut_a009_inventory_files(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params docs = _docs(context) if docs is None or docs["env"] is None or docs["inventory"] is None or not docs["inventory"].is_file(): return unavailable("UT-A009", "用例清单不存在") names = [ line.split("logical_name:", 1)[1].strip().strip("'\"") for line in docs["inventory"].read_text(encoding="utf-8", errors="replace").splitlines() if "logical_name:" in line ] files = [(docs["env"] / "tc" / f"tc_{name}.sv").is_file() for name in names] return check("UT-A009", {"files": bool(names) and all(files)}, "用例文件") def ut_a010_component_files(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params docs = _docs(context) if docs is None or not docs["plan"] or docs["env"] is None: return unavailable("UT-A010", "计划或环境不存在") import json files = re.findall(r'"file"\s*:\s*"([^"]+)"', json.dumps(docs["plan"])) if not files: return unavailable("UT-A010", "计划里没有组件 file 字段") present = [] for name in files: path = Path(name) if not path.is_absolute(): path = docs["env"] / name body = path.read_text(encoding="utf-8", errors="replace") if path.is_file() else "" present.append(any(token in body for token in ("module ", "class ", "function ", "task "))) return check("UT-A010", {"implemented": all(present)}, "组件文件") def ut_a011_covergroups(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params docs = _docs(context) if docs is None or not docs["plan"] or docs["tb"] is None or not docs["tb"].is_file(): return unavailable("UT-A011", "计划或 TB 不存在") model = docs["plan"].get("coverage_model") if isinstance(docs["plan"].get("coverage_model"), dict) else {} names = [item.get("name") for item in model.get("covergroups") or [] if isinstance(item, dict) and item.get("name")] body = docs["tb"].read_text(encoding="utf-8", errors="replace") if not names: return unavailable("UT-A011", "计划里没有 covergroup") return check("UT-A011", {"present": all(str(name) in body for name in names)}, "covergroup") def ut_a012_properties(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-A012", "计划或 checker 不存在") design = docs["plan"].get("component_design") if isinstance(docs["plan"].get("component_design"), dict) else {} names = [item.get("name") for item in design.get("assertions") or [] if isinstance(item, dict) and item.get("name")] body = docs["checker"].read_text(encoding="utf-8", errors="replace") if not names: return unavailable("UT-A012", "计划里没有断言") return check("UT-A012", {"present": all(str(name) in body for name in names)}, "property") def ut_a013_watchdogs(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params docs = _docs(context) if docs is None or docs["tb"] is None or not docs["tb"].is_file(): return unavailable("UT-A013", "TB 不存在") body = docs["tb"].read_text(encoding="utf-8", errors="replace").lower() return check("UT-A013", { "cycle": "watchdog" in body or "超时" in body, "idle": "idle" in body or "无进展" in body, "deadlock": "deadlock" in body or "死锁" in body, "fatal": "$fatal" in body or "$finish" in body, }, "watchdog") UT_PHASE_EVALUATORS = { item[0]: measurement(item[0], item[1], item[2]) for item in ( ("ut_a001_feature_coverage", ut_a001_feature_coverage, "UT-A001"), ("ut_a002_interface_coverage", ut_a002_interface_coverage, "UT-A002"), ("ut_a003_tp_to_case", ut_a003_tp_to_case, "UT-A003"), ("ut_a004_plan_ids", ut_a004_plan_ids, "UT-A004"), ("ut_a005_stimulus", ut_a005_stimulus, "UT-A005"), ("ut_a006_checking", ut_a006_checking, "UT-A006"), ("ut_a007_either", ut_a007_either, "UT-A007"), ("ut_a008_coverpoints", ut_a008_coverpoints, "UT-A008"), ("ut_a009_inventory_files", ut_a009_inventory_files, "UT-A009"), ("ut_a010_component_files", ut_a010_component_files, "UT-A010"), ("ut_a011_covergroups", ut_a011_covergroups, "UT-A011"), ("ut_a012_properties", ut_a012_properties, "UT-A012"), ("ut_a013_watchdogs", ut_a013_watchdogs, "UT-A013"), ) }