You are here

public function CalculatorTest::testRounding in Commerce Core 8.2

@covers ::ceil @covers ::floor @covers ::round

File

modules/price/tests/src/Unit/CalculatorTest.php, line 48

Class

CalculatorTest
Tests the Calculator class.

Namespace

Drupal\Tests\commerce_price\Unit

Code

public function testRounding() {
  $this
    ->assertEquals('5', Calculator::ceil('4.4'));
  $this
    ->assertEquals('-4', Calculator::ceil('-4.4'));
  $this
    ->assertEquals('4', Calculator::floor('4.8'));
  $this
    ->assertEquals('-5', Calculator::floor('-4.8'));
  $rounding_data = [
    [
      '1.95583',
      2,
      PHP_ROUND_HALF_UP,
      '1.96',
    ],
    [
      '9.4',
      0,
      PHP_ROUND_HALF_UP,
      '9',
    ],
    [
      '9.6',
      0,
      PHP_ROUND_HALF_UP,
      '10',
    ],
    [
      '9.5',
      0,
      PHP_ROUND_HALF_UP,
      '10',
    ],
    [
      '9.5',
      0,
      PHP_ROUND_HALF_DOWN,
      '9',
    ],
    [
      '9.5',
      0,
      PHP_ROUND_HALF_EVEN,
      '10',
    ],
    [
      '9.5',
      0,
      PHP_ROUND_HALF_ODD,
      '9',
    ],
    [
      '8.5',
      0,
      PHP_ROUND_HALF_UP,
      '9',
    ],
    [
      '8.5',
      0,
      PHP_ROUND_HALF_DOWN,
      '8',
    ],
    [
      '8.5',
      0,
      PHP_ROUND_HALF_EVEN,
      '8',
    ],
    [
      '8.5',
      0,
      PHP_ROUND_HALF_ODD,
      '9',
    ],
    [
      '1.55',
      1,
      PHP_ROUND_HALF_UP,
      '1.6',
    ],
    [
      '1.54',
      1,
      PHP_ROUND_HALF_UP,
      '1.5',
    ],
    [
      '-1.55',
      1,
      PHP_ROUND_HALF_UP,
      '-1.6',
    ],
    [
      '-1.54',
      1,
      PHP_ROUND_HALF_UP,
      '-1.5',
    ],
    [
      '1.55',
      1,
      PHP_ROUND_HALF_DOWN,
      '1.5',
    ],
    [
      '1.54',
      1,
      PHP_ROUND_HALF_DOWN,
      '1.5',
    ],
    [
      '-1.55',
      1,
      PHP_ROUND_HALF_DOWN,
      '-1.5',
    ],
    [
      '-1.54',
      1,
      PHP_ROUND_HALF_DOWN,
      '-1.5',
    ],
    [
      '1.55',
      1,
      PHP_ROUND_HALF_EVEN,
      '1.6',
    ],
    [
      '1.54',
      1,
      PHP_ROUND_HALF_EVEN,
      '1.5',
    ],
    [
      '-1.55',
      1,
      PHP_ROUND_HALF_EVEN,
      '-1.6',
    ],
    [
      '-1.54',
      1,
      PHP_ROUND_HALF_EVEN,
      '-1.5',
    ],
    [
      '1.55',
      1,
      PHP_ROUND_HALF_ODD,
      '1.5',
    ],
    [
      '1.54',
      1,
      PHP_ROUND_HALF_ODD,
      '1.5',
    ],
    [
      '-1.55',
      1,
      PHP_ROUND_HALF_ODD,
      '-1.5',
    ],
    [
      '-1.54',
      1,
      PHP_ROUND_HALF_ODD,
      '-1.5',
    ],
  ];
  foreach ($rounding_data as $item) {
    $this
      ->assertEquals($item[3], Calculator::round($item[0], $item[1], $item[2]));
  }
}