From fad28a74512fec9079de0ee96ee7a19ba351183c Mon Sep 17 00:00:00 2001 From: tjhunt Date: Thu, 27 Jul 2006 08:32:50 +0000 Subject: [PATCH] More unit tests for numerical question type. --- .../numerical/simpletest/testquestiontype.php | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/question/type/numerical/simpletest/testquestiontype.php b/question/type/numerical/simpletest/testquestiontype.php index 824ae5f9b9..4cb1bb1633 100644 --- a/question/type/numerical/simpletest/testquestiontype.php +++ b/question/type/numerical/simpletest/testquestiontype.php @@ -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() { -- 2.39.5