Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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/type/numerical/questiontype.php');
  29  
  30  class testable_qtype_numerical_answer_processor extends qtype_numerical_answer_processor {
  31      public function parse_response($response) {
  32          return parent::parse_response($response);
  33      }
  34  }
  35  
  36  class qtype_numerical_answer_processor_test extends advanced_testcase {
  37      public function test_parse_response() {
  38          $ap = new testable_qtype_numerical_answer_processor(
  39                  array('m' => 1, 'cm' => 100), false, '.', ',');
  40  
  41          $this->assertEquals(array('3', '142', '', ''), $ap->parse_response('3.142'));
  42          $this->assertEquals(array('', '2', '', ''), $ap->parse_response('.2'));
  43          $this->assertEquals(array('1', '', '', ''), $ap->parse_response('1.'));
  44          $this->assertEquals(array('1', '0', '', ''), $ap->parse_response('1.0'));
  45          $this->assertEquals(array('-1', '', '', ''), $ap->parse_response('-1.'));
  46          $this->assertEquals(array('+1', '0', '', ''), $ap->parse_response('+1.0'));
  47  
  48          $this->assertEquals(array('1', '', '4', ''), $ap->parse_response('1e4'));
  49          $this->assertEquals(array('3', '142', '-4', ''), $ap->parse_response('3.142E-4'));
  50          $this->assertEquals(array('', '2', '+2', ''), $ap->parse_response('.2e+2'));
  51          $this->assertEquals(array('1', '', '-1', ''), $ap->parse_response('1.e-1'));
  52          $this->assertEquals(array('1', '0', '0', ''), $ap->parse_response('1.0e0'));
  53  
  54          $this->assertEquals(array('3', '', '8', ''), $ap->parse_response('3x10^8'));
  55          $this->assertEquals(array('3', '', '8', ''), $ap->parse_response('3×10^8'));
  56          $this->assertEquals(array('3', '0', '8', ''), $ap->parse_response('3.0*10^8'));
  57          $this->assertEquals(array('3', '00', '-8', ''), $ap->parse_response('3.00x10**-8'));
  58          $this->assertEquals(array('0', '001', '7', ''), $ap->parse_response('0.001×10**7'));
  59  
  60          $this->assertEquals(array('1', '', '', 'm'), $ap->parse_response('1m'));
  61          $this->assertEquals(array('3', '142', '', 'm'), $ap->parse_response('3.142 m'));
  62          $this->assertEquals(array('', '2', '', 'm'), $ap->parse_response('.2m'));
  63          $this->assertEquals(array('1', '', '', 'cm'), $ap->parse_response('1.cm'));
  64          $this->assertEquals(array('1', '0', '', 'cm'), $ap->parse_response('1.0   cm'));
  65          $this->assertEquals(array('-1', '', '', 'm'), $ap->parse_response('-1.m'));
  66          $this->assertEquals(array('+1', '0', '', 'cm'), $ap->parse_response('+1.0cm'));
  67  
  68          $this->assertEquals(array('1', '', '4', 'm'), $ap->parse_response('1e4 m'));
  69          $this->assertEquals(array('3', '142', '-4', 'cm'), $ap->parse_response('3.142E-4  cm'));
  70          $this->assertEquals(array('', '2', '+2', 'm'), $ap->parse_response('.2e+2m'));
  71          $this->assertEquals(array('1', '', '-1', 'm'), $ap->parse_response('1.e-1 m'));
  72          $this->assertEquals(array('1', '0', '0', 'cm'), $ap->parse_response('1.0e0cm'));
  73  
  74          $this->assertEquals(array('1000000', '', '', ''),
  75                  $ap->parse_response('1,000,000'));
  76          $this->assertEquals(array('1000', '00', '', 'm'),
  77                  $ap->parse_response('1,000.00 m'));
  78  
  79          $this->assertEquals(array(null, null, null, null), $ap->parse_response('frog'));
  80          $this->assertEquals(array('3', '', '', 'frogs'), $ap->parse_response('3 frogs'));
  81          $this->assertEquals(array(null, null, null, null), $ap->parse_response('. m'));
  82          $this->assertEquals(array(null, null, null, null), $ap->parse_response('.e8 m'));
  83          $this->assertEquals(array(null, null, null, null), $ap->parse_response(','));
  84      }
  85  
  86      protected function verify_value_and_unit($exectedval, $expectedunit, $expectedmultiplier,
  87              qtype_numerical_answer_processor $ap, $input, $separateunit = null) {
  88          list($val, $unit, $multiplier) = $ap->apply_units($input, $separateunit);
  89          if (is_null($exectedval)) {
  90              $this->assertNull($val);
  91          } else {
  92              $this->assertEqualsWithDelta($exectedval, $val, 0.0001);
  93          }
  94          $this->assertEquals($expectedunit, $unit);
  95          if (is_null($expectedmultiplier)) {
  96              $this->assertNull($multiplier);
  97          } else {
  98              $this->assertEqualsWithDelta($expectedmultiplier, $multiplier, 0.0001);
  99          }
 100      }
 101  
 102      public function test_apply_units() {
 103          $ap = new qtype_numerical_answer_processor(
 104                  array('m/s' => 1, 'c' => 3.3356409519815E-9,
 105                          'mph' => 2.2369362920544), false, '.', ',');
 106  
 107          $this->verify_value_and_unit(3e8, 'm/s', 1, $ap, '3x10^8 m/s');
 108          $this->verify_value_and_unit(3e8, '', null, $ap, '3x10^8');
 109          $this->verify_value_and_unit(1, 'c', 299792458, $ap, '1c');
 110          $this->verify_value_and_unit(1, 'mph', 0.44704, $ap, '0001.000 mph');
 111  
 112          $this->verify_value_and_unit(1, 'frogs', null, $ap, '1 frogs');
 113          $this->verify_value_and_unit(null, null, null, $ap, '. m/s');
 114      }
 115  
 116      public function test_apply_units_separate_unit() {
 117          $ap = new qtype_numerical_answer_processor(
 118                  array('m/s' => 1, 'c' => 3.3356409519815E-9,
 119                          'mph' => 2.2369362920544), false, '.', ',');
 120  
 121          $this->verify_value_and_unit(3e8, 'm/s', 1, $ap, '3x10^8', 'm/s');
 122          $this->verify_value_and_unit(3e8, '', null, $ap, '3x10^8', '');
 123          $this->verify_value_and_unit(1, 'c', 299792458, $ap, '1', 'c');
 124          $this->verify_value_and_unit(1, 'mph', 0.44704, $ap, '0001.000', 'mph');
 125  
 126          $this->verify_value_and_unit(1, 'frogs', null, $ap, '1', 'frogs');
 127          $this->verify_value_and_unit(null, null, null, $ap, '.', 'm/s');
 128      }
 129  
 130      public function test_euro_style() {
 131          $ap = new qtype_numerical_answer_processor(array(), false, ',', ' ');
 132  
 133          $this->assertEquals(array(-1000, '', null), $ap->apply_units('-1 000'));
 134          $this->assertEquals(array(3.14159, '', null), $ap->apply_units('3,14159'));
 135      }
 136  
 137      public function test_percent() {
 138          $ap = new qtype_numerical_answer_processor(array('%' => 100), false, '.', ',');
 139  
 140          $this->assertEquals(array('3', '%', 0.01), $ap->apply_units('3%'));
 141          $this->assertEquals(array('1e-6', '%', 0.01), $ap->apply_units('1e-6 %'));
 142          $this->assertEquals(array('100', '', null), $ap->apply_units('100'));
 143      }
 144  
 145  
 146      public function test_currency() {
 147          $ap = new qtype_numerical_answer_processor(array('$' => 1, '£' => 1), true, '.', ',');
 148  
 149          $this->assertEquals(array('1234.56', '£', 1), $ap->apply_units('£1,234.56'));
 150          $this->assertEquals(array('100', '$', 1), $ap->apply_units('$100'));
 151          $this->assertEquals(array('100', '$', 1), $ap->apply_units('$100.'));
 152          $this->assertEquals(array('100.00', '$', 1), $ap->apply_units('$100.00'));
 153          $this->assertEquals(array('100', '', null), $ap->apply_units('100'));
 154          $this->assertEquals(array('100', 'frog', null), $ap->apply_units('frog 100'));
 155      }
 156  }