Permalink
Browse files

MDL-31837 qtype_numerical: fix old-style unit tests.

  • Loading branch information...
1 parent 6d24c26 commit 9345e7bfb8aabd41af98874dd02985d6655b59b0 @timhunt timhunt committed Aug 28, 2012
Showing with 35 additions and 5 deletions.
  1. +35 −5 question/type/numerical/simpletest/testanswer.php
View
40 question/type/numerical/simpletest/testanswer.php
@@ -37,16 +37,46 @@ public function test_within_tolerance_nominal() {
$this->assertFalse($answer->within_tolerance(8.01));
}
+ public function test_within_tolerance_nominal_zero() {
+ // Either an answer or tolerance of 0 requires special care. We still
+ // don't want to end up comparing two floats for absolute equality.
+
+ // Zero tol, non-zero answer.
+ $answer = new qtype_numerical_answer(13, 1e-20, 1.0, '', FORMAT_MOODLE, 0.0);
+ $this->assertFalse($answer->within_tolerance(0.9999999e-20));
+ $this->assertTrue($answer->within_tolerance(1e-20));
+ $this->assertFalse($answer->within_tolerance(1.0000001e-20));
+
+ // Non-zero tol, zero answer.
+ $answer = new qtype_numerical_answer(13, 0.0, 1.0, '', FORMAT_MOODLE, 1e-24);
+ $this->assertFalse($answer->within_tolerance(-2e-24));
+ $this->assertTrue($answer->within_tolerance(-1e-24));
+ $this->assertTrue($answer->within_tolerance(0));
+ $this->assertTrue($answer->within_tolerance(1e-24));
+ $this->assertFalse($answer->within_tolerance(2e-24));
+
+ // Zero tol, zero answer.
+ $answer = new qtype_numerical_answer(13, 0.0, 1.0, '', FORMAT_MOODLE, 1e-24);
+ $this->assertFalse($answer->within_tolerance(-1e-20));
+ $this->assertTrue($answer->within_tolerance(-1e-35));
+ $this->assertTrue($answer->within_tolerance(0));
+ $this->assertTrue($answer->within_tolerance(1e-35));
+ $this->assertFalse($answer->within_tolerance(1e-20));
+
+ // Non-zero tol, non-zero answer.
+ $answer = new qtype_numerical_answer(13, 1e-20, 1.0, '', FORMAT_MOODLE, 1e-24);
+ $this->assertFalse($answer->within_tolerance(1.0002e-20));
+ $this->assertTrue($answer->within_tolerance(1.0001e-20));
+ $this->assertTrue($answer->within_tolerance(1e-20));
+ $this->assertTrue($answer->within_tolerance(1.0001e-20));
+ $this->assertFalse($answer->within_tolerance(1.0002e-20));
+ }
+
public function test_within_tolerance_blank() {
$answer = new qtype_numerical_answer(13, 1234, 1.0, '', FORMAT_MOODLE, '');
$this->assertTrue($answer->within_tolerance(1234));
$this->assertFalse($answer->within_tolerance(1234.000001));
$this->assertFalse($answer->within_tolerance(0));
-
- $answer = new qtype_numerical_answer(13, 0, 1.0, '', FORMAT_MOODLE, '');
- $this->assertTrue($answer->within_tolerance(0));
- $this->assertFalse($answer->within_tolerance(pow(10, -1 * ini_get('precision') + 1)));
- $this->assertTrue($answer->within_tolerance(pow(10, -1 * ini_get('precision'))));
}
public function test_within_tolerance_relative() {

0 comments on commit 9345e7b

Please sign in to comment.