"""Unit tests for MATH-Adv gold extraction and external complexity mapping.""" from __future__ import annotations import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) from causal_difficulty.llm_cache import ( answers_equal, extract_boxed, extract_gold_math, extract_pred_math, normalize_math_answer, validate_cached_response, ) from math_adv_pipeline import compute_complexity_from_levels, parse_level def test_extract_boxed_nested(): sol = r"Therefore $\boxed{\frac{1}{2}}$ is the answer." assert extract_boxed(sol) == r"\frac{1}{2}" def test_extract_gold_math_from_solution(): sol = ( "We compute step by step and conclude " r"$\boxed{2}$ vertical asymptotes." ) assert extract_gold_math(sol) == "2" def test_answers_equal_numeric_and_latex(): assert answers_equal("2", "2") assert answers_equal(" $2$ ", "2") assert not answers_equal("3", "2") def test_normalize_math_answer_strips_latex_spacing(): assert normalize_math_answer(r"\frac{1}{2}") == r"\frac{1}{2}" def test_parse_level_strings(): assert parse_level("Level 3") == 3 assert parse_level(4) == 4 def test_compute_complexity_from_levels(): scores = compute_complexity_from_levels([1, 3, 5]) assert scores[0] == 0.05 assert abs(scores[1] - 0.5) < 1e-9 assert abs(scores[2] - 0.95) < 1e-9 def test_extract_pred_math_prefers_answer_line(): text = "Work shown here.\nANSWER: 42" assert extract_pred_math(text) == "42" def test_extract_pred_math_falls_back_to_boxed(): text = r"Therefore the result is $\boxed{\frac{1}{2}}$." assert extract_pred_math(text) == r"\frac{1}{2}" def test_validate_competition_accepts_boxed_without_answer_line(): text = r"Long solution without ANSWER tag but with $\boxed{7}$ at end." ok, reason = validate_cached_response(text, style="competition") assert ok, reason