1 common.test CommonValidNumberStepUnitTestCase::testNumberStep()

Tests valid_number_step() without offset.

File

core/modules/simpletest/tests/common.test, line 2359
Tests for common.inc functionality.

Class

CommonValidNumberStepUnitTestCase
Tests number step validation by valid_number_step().

Code

function testNumberStep() {
  // Value and step equal.
  $this->assertTrue(valid_number_step(10.3, 10.3));

  // Valid integer steps.
  $this->assertTrue(valid_number_step(42, 21));
  $this->assertTrue(valid_number_step(42, 3));

  // Valid float steps.
  $this->assertTrue(valid_number_step(42, 10.5));
  $this->assertTrue(valid_number_step(1000, -10));

  // Valid and very small float steps.
  $this->assertTrue(valid_number_step(1000.12345, 1e-10));
  $this->assertTrue(valid_number_step(1936.5, 3e-8));
  $this->assertTrue(valid_number_step(3.9999999999999, 1e-13));

  // Valid and confirmed to fail with the former approach.
  $this->assertTrue(valid_number_step(20.123456789, 1.0E-10));
  $this->assertTrue(valid_number_step(9999.1933172003, 1.0E-10));
  $this->assertTrue(valid_number_step(199.200001, 1.0E-10));
  $this->assertTrue(valid_number_step(1109.87, 1.0E-10));
  $this->assertTrue(valid_number_step(41239412.55, 1.0E-2));
  $this->assertTrue(valid_number_step(13517282.20, 1.0E-2));
  $this->assertTrue(valid_number_step(12345678.12, 1.0E-2));

  // Invalid integer steps.
  $this->assertFalse(valid_number_step(100, 30));
  $this->assertFalse(valid_number_step(-10, 4));

  // Invalid float steps.
  $this->assertFalse(valid_number_step(6, 5 / 7));
  $this->assertFalse(valid_number_step(10.3, 10.25));
  $this->assertFalse(valid_number_step(1, 1 / 3));
  $this->assertFalse(valid_number_step(-100, 100 / 7));

  // Step mismatches very close to beeing valid.
  $this->assertFalse(valid_number_step(70 + 9e-7, 10 + 9e-7));
  $this->assertFalse(valid_number_step(1109.87, 0.07));
  $this->assertFalse(valid_number_step(41239412.11, 2));
  $this->assertFalse(valid_number_step(13517283.0001, 3));
}