|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Test for the evaluation utils module.""" |
| 3 | + |
| 4 | +import unittest |
| 5 | + |
| 6 | +from trinity.utils.eval_utils import is_equiv |
| 7 | +from trinity.utils.math_eval_utils import extract_answer, verify_math_answer |
| 8 | + |
| 9 | + |
| 10 | +class TestMathEvalUtils(unittest.TestCase): |
| 11 | + def test_extract_answer(self): |
| 12 | + test_cases = [ |
| 13 | + ("The answer is \\boxed{42}", "42", "Basic boxed extraction"), |
| 14 | + ("The result is \\boxed{\\frac{1}{2}}", "\\frac{1}{2}", "Boxed with LaTeX"), |
| 15 | + ("Therefore, the final answer is 100.", "100", "English 'answer is' extraction"), |
| 16 | + ("My final answer is: 3.14", "3.14", "English 'answer is' with colon"), |
| 17 | + ("所以,答案是x^2", "x^2", "Chinese 'answer is' extraction"), |
| 18 | + ( |
| 19 | + "The cost is 10 dollars and the profit is 20 dollars.", |
| 20 | + "20", |
| 21 | + "Extract the last number", |
| 22 | + ), |
| 23 | + ( |
| 24 | + "There are 1,000 apples and 2,000 oranges.", |
| 25 | + "2000", |
| 26 | + "Extract the last number with commas", |
| 27 | + ), |
| 28 | + ("The probability is 0.75.", "0.75", "Extract the last decimal"), |
| 29 | + ("This sentence has no answer.", None, "No answer case"), |
| 30 | + ("The box is empty \\boxed{}", None, "Empty boxed"), |
| 31 | + (12345, None, "Input is not a string"), |
| 32 | + ] |
| 33 | + |
| 34 | + for i, (input_str, expected_output, description) in enumerate(test_cases): |
| 35 | + with self.subTest(f"Case {i+1}: {description}"): |
| 36 | + actual_output = extract_answer(input_str) |
| 37 | + self.assertEqual( |
| 38 | + actual_output, |
| 39 | + expected_output, |
| 40 | + "Failed on input: '{input_str}'\nExpected: '{expected_output}', Got: '{actual_output}'", |
| 41 | + ) |
| 42 | + |
| 43 | + def test_verify_math_answer(self): |
| 44 | + test_cases = [ |
| 45 | + ("The answer is \\boxed{42}", "42", True, "Simple integer equality"), |
| 46 | + ("The result is 1,000.", "1000", True, "Number with commas"), |
| 47 | + ("The answer is -50.", "-50", True, "Negative number equality"), |
| 48 | + ("The solution is 5", "x=5", True, "Equivalence of value and equation"), |
| 49 | + ("The answer is \\boxed{42}", "43", False, "Simple numerical inequality"), |
| 50 | + ("The answer is \\boxed{x+1}", "x-1", False, "Symbolic expression inequality"), |
| 51 | + ( |
| 52 | + "The matrix is \\boxed{\\begin{pmatrix}1 & 1 \\\\ 0 & 1\\end{pmatrix}}", |
| 53 | + "\\begin{pmatrix}1&0\\\\0&1\\end{pmatrix}", |
| 54 | + False, |
| 55 | + "Matrix inequality", |
| 56 | + ), |
| 57 | + ("The speed is 50 km/h", "50", True, "Judgment after stripping units"), |
| 58 | + ] |
| 59 | + |
| 60 | + for i, (response, ground_truth, expected_correct, description) in enumerate(test_cases): |
| 61 | + with self.subTest(f"Case {i+1}: {description}"): |
| 62 | + accuracy, details = verify_math_answer(response, ground_truth) |
| 63 | + is_correct = accuracy == 1.0 |
| 64 | + self.assertEqual( |
| 65 | + is_correct, |
| 66 | + expected_correct, |
| 67 | + f"Failed on response: '{response}' with truth: '{ground_truth}'\n" |
| 68 | + f"Expected correct: {expected_correct}, Got: {is_correct}\nDetails: {details}", |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + unittest.main() |
| 74 | + |
| 75 | + |
| 76 | +class TestEvalUtils(unittest.TestCase): |
| 77 | + def test_is_equiv(self): |
| 78 | + test_cases = [ |
| 79 | + # str1, str2, expected_output, description |
| 80 | + (" 123 ", "123", True, "Equivalence with whitespace"), |
| 81 | + ("50%", "50", True, "Equivalence with percentage sign"), |
| 82 | + ("$50", "50", True, "Equivalence with dollar sign"), |
| 83 | + ("hello", "world", False, "Basic inequality"), |
| 84 | + ("123", "1234", False, "Numerical inequality"), |
| 85 | + (None, None, True, "Both inputs are None"), |
| 86 | + ("Some string", None, False, "One input is None (str1)"), |
| 87 | + (None, "Some string", False, "One input is None (str2)"), |
| 88 | + ] |
| 89 | + |
| 90 | + for i, (str1, str2, expected_output, description) in enumerate(test_cases): |
| 91 | + with self.subTest(f"Case {i+1}: {description}"): |
| 92 | + actual_output = is_equiv(str1, str2) |
| 93 | + self.assertEqual( |
| 94 | + actual_output, |
| 95 | + expected_output, |
| 96 | + f"Failed on inputs: ('{str1}', '{str2}')\nExpected: {expected_output}, Got: {actual_output}", |
| 97 | + ) |
0 commit comments