Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace qtype_numerical;
18
 
19
use qtype_numerical_answer;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
25
require_once($CFG->dirroot . '/question/type/numerical/question.php');
26
 
27
/**
28
 * Unit tests for the numerical question definition class.
29
 *
30
 * @package   qtype_numerical
31
 * @category  test
32
 * @copyright 2008 The Open University
33
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
1441 ariadna 35
final class answer_test extends \advanced_testcase {
11 efrain 36
    public function test_within_tolerance_nominal(): void {
1 efrain 37
        $answer = new qtype_numerical_answer(13, 7.0, 1.0, '', FORMAT_MOODLE, 1.0);
38
 
39
        $this->assertFalse($answer->within_tolerance(5.99));
40
        $this->assertTrue($answer->within_tolerance(6));
41
        $this->assertTrue($answer->within_tolerance(7));
42
        $this->assertTrue($answer->within_tolerance(8));
43
        $this->assertFalse($answer->within_tolerance(8.01));
44
    }
45
 
11 efrain 46
    public function test_within_tolerance_nominal_zero(): void {
1 efrain 47
        // Either an answer or tolerance of 0 requires special care. We still
48
        // don't want to end up comparing two floats for absolute equality.
49
 
50
        // Zero tol, non-zero answer.
51
        $answer = new qtype_numerical_answer(13, 1e-20, 1.0, '', FORMAT_MOODLE, 0.0);
52
        $this->assertFalse($answer->within_tolerance(0.9999999e-20));
53
        $this->assertTrue($answer->within_tolerance(1e-20));
54
        $this->assertFalse($answer->within_tolerance(1.0000001e-20));
55
 
56
        // Non-zero tol, zero answer.
57
        $answer = new qtype_numerical_answer(13, 0.0, 1.0, '', FORMAT_MOODLE, 1e-24);
58
        $this->assertFalse($answer->within_tolerance(-2e-24));
59
        $this->assertTrue($answer->within_tolerance(-1e-24));
60
        $this->assertTrue($answer->within_tolerance(0));
61
        $this->assertTrue($answer->within_tolerance(1e-24));
62
        $this->assertFalse($answer->within_tolerance(2e-24));
63
 
64
        // Zero tol, zero answer.
65
        $answer = new qtype_numerical_answer(13, 0.0, 1.0, '', FORMAT_MOODLE, 1e-24);
66
        $this->assertFalse($answer->within_tolerance(-1e-20));
67
        $this->assertTrue($answer->within_tolerance(-1e-35));
68
        $this->assertTrue($answer->within_tolerance(0));
69
        $this->assertTrue($answer->within_tolerance(1e-35));
70
        $this->assertFalse($answer->within_tolerance(1e-20));
71
 
72
        // Non-zero tol, non-zero answer.
73
        $answer = new qtype_numerical_answer(13, 1e-20, 1.0, '', FORMAT_MOODLE, 1e-24);
74
        $this->assertFalse($answer->within_tolerance(1.0002e-20));
75
        $this->assertTrue($answer->within_tolerance(1.0001e-20));
76
        $this->assertTrue($answer->within_tolerance(1e-20));
77
        $this->assertTrue($answer->within_tolerance(1.0001e-20));
78
        $this->assertFalse($answer->within_tolerance(1.0002e-20));
79
    }
80
 
11 efrain 81
    public function test_within_tolerance_blank(): void {
1 efrain 82
        $answer = new qtype_numerical_answer(13, 1234, 1.0, '', FORMAT_MOODLE, '');
83
        $this->assertTrue($answer->within_tolerance(1234));
84
        $this->assertFalse($answer->within_tolerance(1234.000001));
85
        $this->assertFalse($answer->within_tolerance(0));
86
    }
87
 
11 efrain 88
    public function test_within_tolerance_relative(): void {
1 efrain 89
        $answer = new qtype_numerical_answer(13, 7.0, 1.0, '', FORMAT_MOODLE, 0.1);
90
        $answer->tolerancetype = 1;
91
 
92
        $this->assertFalse($answer->within_tolerance(6.29));
93
        $this->assertTrue($answer->within_tolerance(6.3));
94
        $this->assertTrue($answer->within_tolerance(7));
95
        $this->assertTrue($answer->within_tolerance(7.7));
96
        $this->assertFalse($answer->within_tolerance(7.71));
97
    }
98
 
11 efrain 99
    public function test_within_tolerance_geometric(): void {
1 efrain 100
        $answer = new qtype_numerical_answer(13, 7.0, 1.0, '', FORMAT_MOODLE, 1.0);
101
        $answer->tolerancetype = 3;
102
 
103
        $this->assertFalse($answer->within_tolerance(3.49));
104
        $this->assertTrue($answer->within_tolerance(3.5));
105
        $this->assertTrue($answer->within_tolerance(7));
106
        $this->assertTrue($answer->within_tolerance(14));
107
        $this->assertFalse($answer->within_tolerance(14.01));
108
 
109
        // Geometric tolerance, negative answer.
110
        $answer = new qtype_numerical_answer(13, -7.0, 1.0, '', FORMAT_MOODLE, 1.0);
111
        $answer->tolerancetype = 3;
112
 
113
        $this->assertFalse($answer->within_tolerance(-3.49));
114
        $this->assertTrue($answer->within_tolerance(-3.5));
115
        $this->assertTrue($answer->within_tolerance(-7));
116
        $this->assertTrue($answer->within_tolerance(-14));
117
        $this->assertFalse($answer->within_tolerance(-14.01));
118
    }
119
}