]> git.mjollnir.org Git - moodle.git/commitdiff
More unit tests for numerical question type.
authortjhunt <tjhunt>
Thu, 27 Jul 2006 08:32:50 +0000 (08:32 +0000)
committertjhunt <tjhunt>
Thu, 27 Jul 2006 08:32:50 +0000 (08:32 +0000)
question/type/numerical/simpletest/testquestiontype.php

index 824ae5f9b99c11b13b6f7df0e9ce64c0b1e6c6bf..4cb1bb16336ef267d6170782bf4ed800ca0801f6 100644 (file)
@@ -16,6 +16,7 @@ require_once($CFG->libdir . '/simpletestlib.php');
 require_once($CFG->dirroot . '/question/type/numerical/questiontype.php');
 
 class question_numerical_qtype_test extends UnitTestCase {
+    var $tolerance = 0.00000001;        
     var $qtype;
     
     function setUp() {
@@ -67,9 +68,80 @@ class question_numerical_qtype_test extends UnitTestCase {
 //    }
 
     function test_get_tolerance_interval() {
+        $answer = new stdClass;
+        $answer->tolerance = 0.01;
+        $answer->tolerancetype = 'relative';
+        $answer->answer = 1.0;
+        $this->qtype->get_tolerance_interval($answer);
+        $this->assertWithinMargin($answer->min, 0.99, $this->tolerance);
+        $this->assertWithinMargin($answer->max, 1.01, $this->tolerance);
+
+        $answer = new stdClass;
+        $answer->tolerance = 0.01;
+        $answer->tolerancetype = 'relative';
+        $answer->answer = 10.0;
+        $this->qtype->get_tolerance_interval($answer);
+        $this->assertWithinMargin($answer->min, 9.9, $this->tolerance);
+        $this->assertWithinMargin($answer->max, 10.1, $this->tolerance);
+
+        $answer = new stdClass;
+        $answer->tolerance = 0.01;
+        $answer->tolerancetype = 'nominal';
+        $answer->answer = 1.0;
+        $this->qtype->get_tolerance_interval($answer);
+        $this->assertWithinMargin($answer->min, 0.99, $this->tolerance);
+        $this->assertWithinMargin($answer->max, 1.01, $this->tolerance);
+
+        $answer = new stdClass;
+        $answer->tolerance = 2.0;
+        $answer->tolerancetype = 'nominal';
+        $answer->answer = 10.0;
+        $this->qtype->get_tolerance_interval($answer);
+        $this->assertWithinMargin($answer->min, 8, $this->tolerance);
+        $this->assertWithinMargin($answer->max, 12, $this->tolerance);
+
+        $answer = new stdClass; // Test default tolerance 0.
+        $answer->tolerancetype = 'nominal';
+        $answer->answer = 0.0;
+        $this->qtype->get_tolerance_interval($answer);
+        $this->assertWithinMargin($answer->min, 0, $this->tolerance);
+        $this->assertWithinMargin($answer->max, 0, $this->tolerance);
+        $this->assertTrue($answer->max - $answer->min > 0);
+        
+
+        $answer = new stdClass; // Test default type nominal.
+        $answer->tolerance = 1.0;
+        $answer->answer = 1.0;
+        $this->qtype->get_tolerance_interval($answer);
+        $this->assertWithinMargin($answer->min, 0, $this->tolerance);
+        $this->assertWithinMargin($answer->max, 2, $this->tolerance);
+
+        $answer = new stdClass;
+        $answer->tolerance = 1.0;
+        $answer->tolerancetype = 'geometric';
+        $answer->answer = 1.0;
+        $this->qtype->get_tolerance_interval($answer);
+        $this->assertWithinMargin($answer->min, 0.5, $this->tolerance);
+        $this->assertWithinMargin($answer->max, 2.0, $this->tolerance);
     }
 
     function test_apply_unit() {
+        $units = array(
+            (object) array('unit' => 'm', 'multiplier' => 1),
+            (object) array('unit' => 'cm', 'multiplier' => 100),
+            (object) array('unit' => 'mm', 'multiplier' => 1000),
+            (object) array('unit' => 'inch', 'multiplier' => 1.0/0.0254)
+        );
+        
+        $this->assertWithinMargin($this->qtype->apply_unit('1', $units), 1, $this->tolerance);
+        $this->assertWithinMargin($this->qtype->apply_unit('1.0', $units), 1, $this->tolerance);
+        $this->assertWithinMargin($this->qtype->apply_unit('-1e0', $units), -1, $this->tolerance);
+        $this->assertWithinMargin($this->qtype->apply_unit('100m', $units), 100, $this->tolerance);
+        $this->assertWithinMargin($this->qtype->apply_unit('1cm', $units), 0.01, $this->tolerance);
+        $this->assertWithinMargin($this->qtype->apply_unit('12inch', $units), .3048, $this->tolerance);
+        $this->assertIdentical($this->qtype->apply_unit('1km', $units), false);
+        $this->assertWithinMargin($this->qtype->apply_unit('-100', array()), -100, $this->tolerance);
+        $this->assertIdentical($this->qtype->apply_unit('1000 miles', array()), false);
     }
 
 //    function test_backup() {