NumberTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Drupal\Tests\Component\Utility;
  3. use Drupal\Component\Utility\Number;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * Tests number manipulation utilities.
  7. *
  8. * @group Utility
  9. *
  10. * @coversDefaultClass \Drupal\Component\Utility\Number
  11. *
  12. * @see \Drupal\Component\Utility\Number
  13. */
  14. class NumberTest extends TestCase {
  15. /**
  16. * Tests Number::validStep() without offset.
  17. *
  18. * @dataProvider providerTestValidStep
  19. * @covers ::validStep
  20. *
  21. * @param numeric $value
  22. * The value argument for Number::validStep().
  23. * @param numeric $step
  24. * The step argument for Number::validStep().
  25. * @param bool $expected
  26. * Expected return value from Number::validStep().
  27. */
  28. public function testValidStep($value, $step, $expected) {
  29. $return = Number::validStep($value, $step);
  30. $this->assertEquals($expected, $return);
  31. }
  32. /**
  33. * Tests Number::validStep() with offset.
  34. *
  35. * @dataProvider providerTestValidStepOffset
  36. * @covers ::validStep
  37. *
  38. * @param numeric $value
  39. * The value argument for Number::validStep().
  40. * @param numeric $step
  41. * The step argument for Number::validStep().
  42. * @param numeric $offset
  43. * The offset argument for Number::validStep().
  44. * @param bool $expected
  45. * Expected return value from Number::validStep().
  46. */
  47. public function testValidStepOffset($value, $step, $offset, $expected) {
  48. $return = Number::validStep($value, $step, $offset);
  49. $this->assertEquals($expected, $return);
  50. }
  51. /**
  52. * Provides data for self::testNumberStep().
  53. *
  54. * @see \Drupal\Tests\Component\Utility\Number::testValidStep
  55. */
  56. public static function providerTestValidStep() {
  57. return [
  58. // Value and step equal.
  59. [10.3, 10.3, TRUE],
  60. // Valid integer steps.
  61. [42, 21, TRUE],
  62. [42, 3, TRUE],
  63. // Valid float steps.
  64. [42, 10.5, TRUE],
  65. [1, 1 / 3, TRUE],
  66. [-100, 100 / 7, TRUE],
  67. [1000, -10, TRUE],
  68. // Valid and very small float steps.
  69. [1000.12345, 1e-10, TRUE],
  70. [3.9999999999999, 1e-13, TRUE],
  71. // Invalid integer steps.
  72. [100, 30, FALSE],
  73. [-10, 4, FALSE],
  74. // Invalid float steps.
  75. [6, 5 / 7, FALSE],
  76. [10.3, 10.25, FALSE],
  77. // Step mismatches very close to being valid.
  78. [70 + 9e-7, 10 + 9e-7, FALSE],
  79. [1936.5, 3e-8, FALSE],
  80. ];
  81. }
  82. /**
  83. * Data provider for \Drupal\Test\Component\Utility\NumberTest::testValidStepOffset().
  84. *
  85. * @see \Drupal\Test\Component\Utility\NumberTest::testValidStepOffset()
  86. */
  87. public static function providerTestValidStepOffset() {
  88. return [
  89. // Try obvious fits.
  90. [11.3, 10.3, 1, TRUE],
  91. [100, 10, 50, TRUE],
  92. [-100, 90 / 7, -10, TRUE],
  93. [2 / 7 + 5 / 9, 1 / 7, 5 / 9, TRUE],
  94. // Ensure a small offset is still invalid.
  95. [10.3, 10.3, 0.0001, FALSE],
  96. [1 / 5, 1 / 7, 1 / 11, FALSE],
  97. // Try negative values and offsets.
  98. [1000, 10, -5, FALSE],
  99. [-10, 4, 0, FALSE],
  100. [-10, 4, -4, FALSE],
  101. ];
  102. }
  103. /**
  104. * Tests the alphadecimal conversion functions.
  105. *
  106. * @dataProvider providerTestConversions
  107. * @covers ::intToAlphadecimal
  108. * @covers ::alphadecimalToInt
  109. *
  110. * @param int $value
  111. * The integer value.
  112. * @param string $expected
  113. * The expected alphadecimal value.
  114. */
  115. public function testConversions($value, $expected) {
  116. $this->assertSame(Number::intToAlphadecimal($value), $expected);
  117. $this->assertSame($value, Number::alphadecimalToInt($expected));
  118. }
  119. /**
  120. * Data provider for testConversions().
  121. *
  122. * @see testConversions()
  123. *
  124. * @return array
  125. * An array containing:
  126. * - The integer value.
  127. * - The alphadecimal value.
  128. */
  129. public function providerTestConversions() {
  130. return [
  131. [0, '00'],
  132. [1, '01'],
  133. [10, '0a'],
  134. [20, '0k'],
  135. [35, '0z'],
  136. [36, '110'],
  137. [100, '12s'],
  138. ];
  139. }
  140. }