Files

74 lines
2.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""分数判分:小学五年级的核心缺口,这一组是它的护栏。
原则没变——宁可不算,不可算错:
- 只有「整题就是一个算式」时我们才自己算,带一个汉字就不算;
- 判分用 Fraction 精确比较,0.75 与 3/4 视为同一个答案。
"""
import unittest
from backend import judging
from backend.tencent_ocr import _frac_chain_answer as chain
class FractionCompareTest(unittest.TestCase):
def test_equivalent_forms(self):
ok, _ = judging.judge_calc("3/4", "0.75")
self.assertTrue(ok)
ok, _ = judging.judge_calc("0.75", "3/4")
self.assertTrue(ok)
ok, _ = judging.judge_calc("2/8", "1/4")
self.assertTrue(ok, "约分后相等也算对")
def test_mixed_number(self):
ok, msg = judging.judge_calc("1又1/2", "1.5")
self.assertTrue(ok, msg)
def test_wrong_answer_message(self):
ok, msg = judging.judge_calc("3/4", "2/3")
self.assertFalse(ok)
self.assertIn("3/4", msg)
self.assertIn("2/3", msg)
def test_zero_denominator_refused(self):
self.assertIsNone(judging.to_qty("3/0"))
ok, _ = judging.judge_calc("3/4", "3/0")
self.assertIsNone(ok, "分母为 0 应当拒答,而不是判错")
def test_decimal_and_int_still_work(self):
self.assertTrue(judging.judge_calc("83", "83")[0])
self.assertTrue(judging.judge_calc("2.5", "2.50")[0])
class FractionArithmeticTest(unittest.TestCase):
def test_exact_fraction_result(self):
val = judging.safe_eval("3/4*2/5")
self.assertEqual(judging._fmt(val), "3/10")
def test_integer_stays_integer(self):
self.assertEqual(judging._fmt(judging.safe_eval("36+47")), "83")
def test_divide_by_zero(self):
self.assertIsNone(judging.safe_eval("3/0"))
class ChainAnswerTest(unittest.TestCase):
def test_pure_fraction_expression(self):
self.assertEqual(chain("1/4×3/5="), "3/20")
self.assertEqual(chain("3. 3/4×2/5="), "3/10")
def test_decimal_not_eaten_as_numbering(self):
# 「2.5×4」曾把开头的 2. 当题号,剩下 5×4 算出 20
self.assertEqual(chain("2.5×4=( )"), "10")
def test_chinese_text_never_derived(self):
self.assertIsNone(chain("2.5/8吨的4/7是()吨"))
self.assertIsNone(chain("1. 计算:36 + 47 =()"))
def test_two_equals_rejected(self):
# 孩子已经写了答案的卷面:「1/4×3/5=1×3/4×5=3/20」不能拿来当题
self.assertIsNone(chain("1/4×3/5=1×3/4×5=3/20"))
if __name__ == "__main__":
unittest.main()