diff --git a/common/lib/capa/capa/tests/test_responsetypes.py b/common/lib/capa/capa/tests/test_responsetypes.py index 7a43fff4c9..f7848ca094 100644 --- a/common/lib/capa/capa/tests/test_responsetypes.py +++ b/common/lib/capa/capa/tests/test_responsetypes.py @@ -438,6 +438,36 @@ class FormulaResponseTest(ResponseTest): self.assert_grade(problem, incorrect, 'incorrect', msg="Failed on function {0}; the given, incorrect answer was {1} but graded 'correct'".format(func, incorrect)) + def test_grade_infinity(self): + # This resolves a bug where a problem with relative tolerance would + # pass with any arbitrarily large student answer. + + sample_dict = {'x' : (1,2)} + + # Test problem + problem = self.build_problem(sample_dict=sample_dict, + num_samples=10, + tolerance="1%", + answer="x") + # Expect such a large answer to be marked incorrect + input_formula = "x*1e999" + self.assert_grade(problem, input_formula, "incorrect") + + def test_grade_nan(self): + # attempt to produce a value which causes the student's answer to be + # evaluated to nan. See if this is resolved correctly. + + sample_dict = {'x' : (1,2)} + + # Test problem + problem = self.build_problem(sample_dict=sample_dict, + num_samples=10, + tolerance="1%", + answer="x") + # Expect an incorrect answer (+ nan) to be marked incorrect + input_formula = "10*x + 0*1e999" # right now this evaluates to 'nan' for a given x + self.assert_grade(problem, input_formula, "incorrect") + class StringResponseTest(ResponseTest): from response_xml_factory import StringResponseXMLFactory @@ -714,6 +744,28 @@ class NumericalResponseTest(ResponseTest): incorrect_responses = ["", "4.5", "3.5", "0"] self.assert_multiple_grade(problem, correct_responses, incorrect_responses) + def test_grade_infinity(self): + # This resolves a bug where a problem with relative tolerance would + # pass with any arbitrarily large student answer. + problem = self.build_problem(question_text="What is 2 + 2 approximately?", + explanation="The answer is 4", + answer=4, + tolerance="10%") + correct_responses = [] + incorrect_responses = ["1e999"] + self.assert_multiple_grade(problem, correct_responses, incorrect_responses) + + def test_grade_nan(self): + # attempt to produce a value which causes the student's answer to be + # evaluated to nan. See if this is resolved correctly. + problem = self.build_problem(question_text="What is 2 + 2 approximately?", + explanation="The answer is 4", + answer=4, + tolerance="10%") + correct_responses = [] + incorrect_responses = ["0*1e999"] # right now this evaluates to 'nan' for a given x + self.assert_multiple_grade(problem, correct_responses, incorrect_responses) + def test_grade_with_script(self): script_text = "computed_response = math.sqrt(4)" problem = self.build_problem(question_text="What is sqrt(4)?",