Skip to content

Commit

Permalink
Merge branch 'MDL-75757' of https://github.com/stronk7/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Sep 19, 2022
2 parents 359e7c7 + 6be384e commit a4c97a7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
7 changes: 4 additions & 3 deletions question/behaviour/deferredcbm/tests/behaviour_type_test.php
Expand Up @@ -18,6 +18,7 @@

use question_display_options;
use question_engine;
use question_testcase;

defined('MOODLE_INTERNAL') || die();

Expand Down Expand Up @@ -134,8 +135,8 @@ public function test_summarise_usage_max_mark_3() {
}

public function test_calculate_bonus() {
$this->assertEquals(0.05, $this->behaviourtype->calculate_bonus(1, 1/2));
$this->assertEquals(-0.01, $this->behaviourtype->calculate_bonus(2, 9/10));
$this->assertEquals(0, $this->behaviourtype->calculate_bonus(3, 1));
$this->assertEqualsWithDelta(0.05, $this->behaviourtype->calculate_bonus(1, 1 / 2), question_testcase::GRADE_DELTA);
$this->assertEqualsWithDelta(-0.01, $this->behaviourtype->calculate_bonus(2, 9 / 10), question_testcase::GRADE_DELTA);
$this->assertEqualsWithDelta(0, $this->behaviourtype->calculate_bonus(3, 1), question_testcase::GRADE_DELTA);
}
}
5 changes: 5 additions & 0 deletions question/engine/tests/helpers.php
Expand Up @@ -480,6 +480,11 @@ public function build_db_records(array $table) {

abstract class question_testcase extends advanced_testcase {

/**
* Tolerance accepted in some unit tests when float operations are involved.
*/
const GRADE_DELTA = 0.00000005;

public function assert($expectation, $compare, $notused = '') {

if (get_class($expectation) === 'question_pattern_expectation') {
Expand Down
11 changes: 6 additions & 5 deletions question/type/multianswer/tests/question_test.php
Expand Up @@ -19,6 +19,7 @@
use question_attempt_step;
use question_display_options;
use question_state;
use question_testcase;

defined('MOODLE_INTERNAL') || die();

Expand Down Expand Up @@ -199,7 +200,7 @@ public function test_compute_final_grade() {
2 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right),
);
$finalgrade = $question->compute_final_grade($responses, 1);
$this->assertEquals(1 / 3 * (1 - 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade);
$this->assertEqualsWithDelta(1 / 3 * (1 - 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade, question_testcase::GRADE_DELTA);

// Get subquestion 1 right at 3rd try and subquestion 2 right at 2nd try.
$responses = array(0 => array('sub1_answer' => 'Dog', 'sub2_answer' => $wrong),
Expand All @@ -208,7 +209,7 @@ public function test_compute_final_grade() {
3 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right),
);
$finalgrade = $question->compute_final_grade($responses, 1);
$this->assertEquals(1 / 3 * (1 - 2 * 0.2) + 2 / 3 * (1 - 0.2), $finalgrade);
$this->assertEqualsWithDelta(1 / 3 * (1 - 2 * 0.2) + 2 / 3 * (1 - 0.2), $finalgrade, question_testcase::GRADE_DELTA);

// Get subquestion 1 right at 4th try and subquestion 2 right at 1st try.
$responses = array(0 => array('sub1_answer' => 'Dog', 'sub2_answer' => $right),
Expand All @@ -217,7 +218,7 @@ public function test_compute_final_grade() {
3 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right),
);
$finalgrade = $question->compute_final_grade($responses, 1);
$this->assertEquals(1 / 3 * (1 - 3 * 0.2) + 2 / 3, $finalgrade);
$this->assertEqualsWithDelta(1 / 3 * (1 - 3 * 0.2) + 2 / 3, $finalgrade, question_testcase::GRADE_DELTA);

// Get subquestion 1 right at 4th try and subquestion 2 right 3rd try.
// Subquestion 2 was right at 1st try, but last change is at 3rd try.
Expand All @@ -227,7 +228,7 @@ public function test_compute_final_grade() {
3 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right),
);
$finalgrade = $question->compute_final_grade($responses, 1);
$this->assertEquals(1 / 3 * (1 - 3 * 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade);
$this->assertEqualsWithDelta(1 / 3 * (1 - 3 * 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade, question_testcase::GRADE_DELTA);

// Incomplete responses. Subquestion 1 is right at 4th try and subquestion 2 at 3rd try.
$responses = array(0 => array('sub1_answer' => 'Dog'),
Expand All @@ -236,7 +237,7 @@ public function test_compute_final_grade() {
3 => array('sub1_answer' => 'Owl', 'sub2_answer' => $right),
);
$finalgrade = $question->compute_final_grade($responses, 1);
$this->assertEquals(1 / 3 * (1 - 3 * 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade);
$this->assertEqualsWithDelta(1 / 3 * (1 - 3 * 0.2) + 2 / 3 * (1 - 2 * 0.2), $finalgrade, question_testcase::GRADE_DELTA);
}

/**
Expand Down

0 comments on commit a4c97a7

Please sign in to comment.