Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/question/type/numerical/simpletest/testanswerprocessor.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 
00027 require_once($CFG->dirroot . '/question/type/numerical/questiontype.php');
00028 
00029 class testable_qtype_numerical_answer_processor extends qtype_numerical_answer_processor {
00030     public function parse_response($response) {
00031         return parent::parse_response($response);
00032     }
00033 }
00034 
00035 class qtype_numerical_answer_processor_test extends UnitTestCase {
00036     public function test_parse_response() {
00037         $ap = new testable_qtype_numerical_answer_processor(
00038                 array('m' => 1, 'cm' => 100), false, '.', ',');
00039 
00040         $this->assertEqual(array('3', '142', '', ''), $ap->parse_response('3.142'));
00041         $this->assertEqual(array('', '2', '', ''), $ap->parse_response('.2'));
00042         $this->assertEqual(array('1', '', '', ''), $ap->parse_response('1.'));
00043         $this->assertEqual(array('1', '0', '', ''), $ap->parse_response('1.0'));
00044         $this->assertEqual(array('-1', '', '', ''), $ap->parse_response('-1.'));
00045         $this->assertEqual(array('+1', '0', '', ''), $ap->parse_response('+1.0'));
00046 
00047         $this->assertEqual(array('1', '', '4', ''), $ap->parse_response('1e4'));
00048         $this->assertEqual(array('3', '142', '-4', ''), $ap->parse_response('3.142E-4'));
00049         $this->assertEqual(array('', '2', '+2', ''), $ap->parse_response('.2e+2'));
00050         $this->assertEqual(array('1', '', '-1', ''), $ap->parse_response('1.e-1'));
00051         $this->assertEqual(array('1', '0', '0', ''), $ap->parse_response('1.0e0'));
00052 
00053         $this->assertEqual(array('3', '', '8', ''), $ap->parse_response('3x10^8'));
00054         $this->assertEqual(array('3', '', '8', ''), $ap->parse_response('3×10^8'));
00055         $this->assertEqual(array('3', '0', '8', ''), $ap->parse_response('3.0*10^8'));
00056         $this->assertEqual(array('3', '00', '-8', ''), $ap->parse_response('3.00x10**-8'));
00057         $this->assertEqual(array('0', '001', '7', ''), $ap->parse_response('0.001×10**7'));
00058 
00059         $this->assertEqual(array('1', '', '', 'm'), $ap->parse_response('1m'));
00060         $this->assertEqual(array('3', '142', '', 'm'), $ap->parse_response('3.142 m'));
00061         $this->assertEqual(array('', '2', '', 'm'), $ap->parse_response('.2m'));
00062         $this->assertEqual(array('1', '', '', 'cm'), $ap->parse_response('1.cm'));
00063         $this->assertEqual(array('1', '0', '', 'cm'), $ap->parse_response('1.0   cm'));
00064         $this->assertEqual(array('-1', '', '', 'm'), $ap->parse_response('-1.m'));
00065         $this->assertEqual(array('+1', '0', '', 'cm'), $ap->parse_response('+1.0cm'));
00066 
00067         $this->assertEqual(array('1', '', '4', 'm'), $ap->parse_response('1e4 m'));
00068         $this->assertEqual(array('3', '142', '-4', 'cm'), $ap->parse_response('3.142E-4  cm'));
00069         $this->assertEqual(array('', '2', '+2', 'm'), $ap->parse_response('.2e+2m'));
00070         $this->assertEqual(array('1', '', '-1', 'm'), $ap->parse_response('1.e-1 m'));
00071         $this->assertEqual(array('1', '0', '0', 'cm'), $ap->parse_response('1.0e0cm'));
00072 
00073         $this->assertEqual(array('1000000', '', '', ''),
00074                 $ap->parse_response('1,000,000'));
00075         $this->assertEqual(array('1000', '00', '', 'm'),
00076                 $ap->parse_response('1,000.00 m'));
00077 
00078         $this->assertEqual(array(null, null, null, null), $ap->parse_response('frog'));
00079         $this->assertEqual(array('3', '', '', 'frogs'), $ap->parse_response('3 frogs'));
00080         $this->assertEqual(array(null, null, null, null), $ap->parse_response('. m'));
00081         $this->assertEqual(array(null, null, null, null), $ap->parse_response('.e8 m'));
00082         $this->assertEqual(array(null, null, null, null), $ap->parse_response(','));
00083     }
00084 
00085     protected function verify_value_and_unit($exectedval, $expectedunit, $expectedmultiplier,
00086             qtype_numerical_answer_processor $ap, $input, $separateunit = null) {
00087         list($val, $unit, $multiplier) = $ap->apply_units($input, $separateunit);
00088         if (is_null($exectedval)) {
00089             $this->assertNull($val);
00090         } else {
00091             $this->assertWithinMargin($exectedval, $val, 0.0001);
00092         }
00093         $this->assertEqual($expectedunit, $unit);
00094         if (is_null($expectedmultiplier)) {
00095             $this->assertNull($multiplier);
00096         } else {
00097             $this->assertWithinMargin($expectedmultiplier, $multiplier, 0.0001);
00098         }
00099     }
00100 
00101     public function test_apply_units() {
00102         $ap = new qtype_numerical_answer_processor(
00103                 array('m/s' => 1, 'c' => 3.3356409519815E-9,
00104                         'mph' => 2.2369362920544), false, '.', ',');
00105 
00106         $this->verify_value_and_unit(3e8, 'm/s', 1, $ap, '3x10^8 m/s');
00107         $this->verify_value_and_unit(3e8, '', null, $ap, '3x10^8');
00108         $this->verify_value_and_unit(1, 'c', 299792458, $ap, '1c');
00109         $this->verify_value_and_unit(1, 'mph', 0.44704, $ap, '0001.000 mph');
00110 
00111         $this->verify_value_and_unit(1, 'frogs', null, $ap, '1 frogs');
00112         $this->verify_value_and_unit(null, null, null, $ap, '. m/s');
00113     }
00114 
00115     public function test_apply_units_separate_unit() {
00116         $ap = new qtype_numerical_answer_processor(
00117                 array('m/s' => 1, 'c' => 3.3356409519815E-9,
00118                         'mph' => 2.2369362920544), false, '.', ',');
00119 
00120         $this->verify_value_and_unit(3e8, 'm/s', 1, $ap, '3x10^8', 'm/s');
00121         $this->verify_value_and_unit(3e8, '', null, $ap, '3x10^8', '');
00122         $this->verify_value_and_unit(1, 'c', 299792458, $ap, '1', 'c');
00123         $this->verify_value_and_unit(1, 'mph', 0.44704, $ap, '0001.000', 'mph');
00124 
00125         $this->verify_value_and_unit(1, 'frogs', null, $ap, '1', 'frogs');
00126         $this->verify_value_and_unit(null, null, null, $ap, '.', 'm/s');
00127     }
00128 
00129     public function test_euro_style() {
00130         $ap = new qtype_numerical_answer_processor(array(), false, ',', ' ');
00131 
00132         $this->assertEqual(array(-1000, '', null), $ap->apply_units('-1 000'));
00133         $this->assertEqual(array(3.14159, '', null), $ap->apply_units('3,14159'));
00134     }
00135 
00136     public function test_percent() {
00137         $ap = new qtype_numerical_answer_processor(array('%' => 100), false, '.', ',');
00138 
00139         $this->assertEqual(array('3', '%', 0.01), $ap->apply_units('3%'));
00140         $this->assertEqual(array('1e-6', '%', 0.01), $ap->apply_units('1e-6 %'));
00141         $this->assertEqual(array('100', '', null), $ap->apply_units('100'));
00142     }
00143 
00144 
00145     public function test_currency() {
00146         $ap = new qtype_numerical_answer_processor(array('$' => 1, '£' => 1), true, '.', ',');
00147 
00148         $this->assertEqual(array('1234.56', '£', 1), $ap->apply_units('£1,234.56'));
00149         $this->assertEqual(array('100', '$', 1), $ap->apply_units('$100'));
00150         $this->assertEqual(array('100', '$', 1), $ap->apply_units('$100.'));
00151         $this->assertEqual(array('100.00', '$', 1), $ap->apply_units('$100.00'));
00152         $this->assertEqual(array('100', '', null), $ap->apply_units('100'));
00153         $this->assertEqual(array('100', 'frog', null), $ap->apply_units('frog 100'));
00154     }
00155 }
 All Data Structures Namespaces Files Functions Variables Enumerations