Download testing/agent_evaluation/evaluators/ut_assert.py from yuhanqin/ut-evaluation: direct link, hf CLI and curl.
- Browser
- Download file 3.64 kB
-
https://huggingface.co/datasets/yuhanqin/ut-evaluation/resolve/main/testing/agent_evaluation/evaluators/ut_assert.py
- Command line
-
hf download hf://datasets/yuhanqin/ut-evaluation/testing/agent_evaluation/evaluators/ut_assert.py
-
curl -L -o ut_assert.py https://huggingface.co/datasets/yuhanqin/ut-evaluation/resolve/main/testing/agent_evaluation/evaluators/ut_assert.py
3.64 kB
| """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"), | |
| ) | |
| } | |