Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  
  18  /**
  19   * Unit tests for the numerical question definition class.
  20   *
  21   * @package moodlecore
  22   * @subpackage questiontypes
  23   * @copyright 2008 The Open University
  24   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  29  require_once($CFG->dirroot . '/question/type/numerical/question.php');
  30  
  31  
  32  class qtype_numerical_answer_test extends advanced_testcase {
  33      public function test_within_tolerance_nominal() {
  34          $answer = new qtype_numerical_answer(13, 7.0, 1.0, '', FORMAT_MOODLE, 1.0);
  35  
  36          $this->assertFalse($answer->within_tolerance(5.99));
  37          $this->assertTrue($answer->within_tolerance(6));
  38          $this->assertTrue($answer->within_tolerance(7));
  39          $this->assertTrue($answer->within_tolerance(8));
  40          $this->assertFalse($answer->within_tolerance(8.01));
  41      }
  42  
  43      public function test_within_tolerance_nominal_zero() {
  44          // Either an answer or tolerance of 0 requires special care. We still
  45          // don't want to end up comparing two floats for absolute equality.
  46  
  47          // Zero tol, non-zero answer.
  48          $answer = new qtype_numerical_answer(13, 1e-20, 1.0, '', FORMAT_MOODLE, 0.0);
  49          $this->assertFalse($answer->within_tolerance(0.9999999e-20));
  50          $this->assertTrue($answer->within_tolerance(1e-20));
  51          $this->assertFalse($answer->within_tolerance(1.0000001e-20));
  52  
  53          // Non-zero tol, zero answer.
  54          $answer = new qtype_numerical_answer(13, 0.0, 1.0, '', FORMAT_MOODLE, 1e-24);
  55          $this->assertFalse($answer->within_tolerance(-2e-24));
  56          $this->assertTrue($answer->within_tolerance(-1e-24));
  57          $this->assertTrue($answer->within_tolerance(0));
  58          $this->assertTrue($answer->within_tolerance(1e-24));
  59          $this->assertFalse($answer->within_tolerance(2e-24));
  60  
  61          // Zero tol, zero answer.
  62          $answer = new qtype_numerical_answer(13, 0.0, 1.0, '', FORMAT_MOODLE, 1e-24);
  63          $this->assertFalse($answer->within_tolerance(-1e-20));
  64          $this->assertTrue($answer->within_tolerance(-1e-35));
  65          $this->assertTrue($answer->within_tolerance(0));
  66          $this->assertTrue($answer->within_tolerance(1e-35));
  67          $this->assertFalse($answer->within_tolerance(1e-20));
  68  
  69          // Non-zero tol, non-zero answer.
  70          $answer = new qtype_numerical_answer(13, 1e-20, 1.0, '', FORMAT_MOODLE, 1e-24);
  71          $this->assertFalse($answer->within_tolerance(1.0002e-20));
  72          $this->assertTrue($answer->within_tolerance(1.0001e-20));
  73          $this->assertTrue($answer->within_tolerance(1e-20));
  74          $this->assertTrue($answer->within_tolerance(1.0001e-20));
  75          $this->assertFalse($answer->within_tolerance(1.0002e-20));
  76      }
  77  
  78      public function test_within_tolerance_blank() {
  79          $answer = new qtype_numerical_answer(13, 1234, 1.0, '', FORMAT_MOODLE, '');
  80          $this->assertTrue($answer->within_tolerance(1234));
  81          $this->assertFalse($answer->within_tolerance(1234.000001));
  82          $this->assertFalse($answer->within_tolerance(0));
  83      }
  84  
  85      public function test_within_tolerance_relative() {
  86          $answer = new qtype_numerical_answer(13, 7.0, 1.0, '', FORMAT_MOODLE, 0.1);
  87          $answer->tolerancetype = 1;
  88  
  89          $this->assertFalse($answer->within_tolerance(6.29));
  90          $this->assertTrue($answer->within_tolerance(6.3));
  91          $this->assertTrue($answer->within_tolerance(7));
  92          $this->assertTrue($answer->within_tolerance(7.7));
  93          $this->assertFalse($answer->within_tolerance(7.71));
  94      }
  95  
  96      public function test_within_tolerance_geometric() {
  97          $answer = new qtype_numerical_answer(13, 7.0, 1.0, '', FORMAT_MOODLE, 1.0);
  98          $answer->tolerancetype = 3;
  99  
 100          $this->assertFalse($answer->within_tolerance(3.49));
 101          $this->assertTrue($answer->within_tolerance(3.5));
 102          $this->assertTrue($answer->within_tolerance(7));
 103          $this->assertTrue($answer->within_tolerance(14));
 104          $this->assertFalse($answer->within_tolerance(14.01));
 105      }
 106  }