3 * Unit tests for (some of) question/type/numerical/questiontype.php.
5 * @copyright © 2006 The Open University
6 * @author T.J.Hunt@open.ac.uk
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 if (!defined('MOODLE_INTERNAL')) {
12 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
15 require_once($CFG->dirroot
. '/question/type/numerical/questiontype.php');
17 class question_numerical_qtype_test
extends UnitTestCase
{
18 var $tolerance = 0.00000001;
22 $this->qtype
= new question_numerical_qtype();
29 function test_name() {
30 $this->assertEqual($this->qtype
->name(), 'numerical');
33 // function test_get_question_options() {
36 // function test_get_numerical_units() {
39 // function test_get_default_numerical_unit() {
42 // function test_save_question_options() {
45 // function test_save_numerical_units() {
48 // function test_delete_question() {
51 // function test_compare_responses() {
54 // function test_test_response() {
57 // function test_check_response(){
60 // function test_grade_responses() {
63 // function test_get_correct_responses() {
66 // function test_get_all_responses() {
69 function test_get_tolerance_interval() {
70 $answer = new stdClass
;
71 $answer->tolerance
= 0.01;
72 $answer->tolerancetype
= 'relative';
73 $answer->answer
= 1.0;
74 $this->qtype
->get_tolerance_interval($answer);
75 $this->assertWithinMargin($answer->min
, 0.99, $this->tolerance
);
76 $this->assertWithinMargin($answer->max
, 1.01, $this->tolerance
);
78 $answer = new stdClass
;
79 $answer->tolerance
= 0.01;
80 $answer->tolerancetype
= 'relative';
81 $answer->answer
= 10.0;
82 $this->qtype
->get_tolerance_interval($answer);
83 $this->assertWithinMargin($answer->min
, 9.9, $this->tolerance
);
84 $this->assertWithinMargin($answer->max
, 10.1, $this->tolerance
);
86 $answer = new stdClass
;
87 $answer->tolerance
= 0.01;
88 $answer->tolerancetype
= 'nominal';
89 $answer->answer
= 1.0;
90 $this->qtype
->get_tolerance_interval($answer);
91 $this->assertWithinMargin($answer->min
, 0.99, $this->tolerance
);
92 $this->assertWithinMargin($answer->max
, 1.01, $this->tolerance
);
94 $answer = new stdClass
;
95 $answer->tolerance
= 2.0;
96 $answer->tolerancetype
= 'nominal';
97 $answer->answer
= 10.0;
98 $this->qtype
->get_tolerance_interval($answer);
99 $this->assertWithinMargin($answer->min
, 8, $this->tolerance
);
100 $this->assertWithinMargin($answer->max
, 12, $this->tolerance
);
102 $answer = new stdClass
; // Test default tolerance 0.
103 $answer->tolerancetype
= 'nominal';
104 $answer->answer
= 0.0;
105 $this->qtype
->get_tolerance_interval($answer);
106 $this->assertWithinMargin($answer->min
, 0, $this->tolerance
);
107 $this->assertWithinMargin($answer->max
, 0, $this->tolerance
);
109 $answer = new stdClass
; // Test default type nominal.
110 $answer->tolerance
= 1.0;
111 $answer->answer
= 1.0;
112 $this->qtype
->get_tolerance_interval($answer);
113 $this->assertWithinMargin($answer->min
, 0, $this->tolerance
);
114 $this->assertWithinMargin($answer->max
, 2, $this->tolerance
);
116 $answer = new stdClass
;
117 $answer->tolerance
= 1.0;
118 $answer->tolerancetype
= 'geometric';
119 $answer->answer
= 1.0;
120 $this->qtype
->get_tolerance_interval($answer);
121 $this->assertWithinMargin($answer->min
, 0.5, $this->tolerance
);
122 $this->assertWithinMargin($answer->max
, 2.0, $this->tolerance
);
125 function test_apply_unit() {
127 (object) array('unit' => 'm', 'multiplier' => 1),
128 (object) array('unit' => 'cm', 'multiplier' => 100),
129 (object) array('unit' => 'mm', 'multiplier' => 1000),
130 (object) array('unit' => 'inch', 'multiplier' => 1.0/0.0254)
133 $this->assertWithinMargin($this->qtype
->apply_unit('1', $units), 1, $this->tolerance
);
134 $this->assertWithinMargin($this->qtype
->apply_unit('1.0', $units), 1, $this->tolerance
);
135 $this->assertWithinMargin($this->qtype
->apply_unit('-1e0', $units), -1, $this->tolerance
);
136 $this->assertWithinMargin($this->qtype
->apply_unit('100m', $units), 100, $this->tolerance
);
137 $this->assertWithinMargin($this->qtype
->apply_unit('1cm', $units), 0.01, $this->tolerance
);
138 $this->assertWithinMargin($this->qtype
->apply_unit('12inch', $units), .3048, $this->tolerance
);
139 $this->assertIdentical($this->qtype
->apply_unit('1km', $units), false);
140 $this->assertWithinMargin($this->qtype
->apply_unit('-100', array()), -100, $this->tolerance
);
141 $this->assertIdentical($this->qtype
->apply_unit('1000 miles', array()), false);
144 // function test_backup() {
147 // function test_restore() {