FloatComparatorTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Math\Test\Unit;
  8. use Magento\Framework\Math\FloatComparator;
  9. use PHPUnit\Framework\TestCase;
  10. class FloatComparatorTest extends TestCase
  11. {
  12. /**
  13. * @var FloatComparator
  14. */
  15. private $comparator;
  16. /**
  17. * @inheritdoc
  18. */
  19. protected function setUp()
  20. {
  21. $this->comparator = new FloatComparator();
  22. }
  23. /**
  24. * Checks a case when `a` and `b` are equal.
  25. *
  26. * @param float $a
  27. * @param float $b
  28. * @param bool $expected
  29. * @dataProvider eqDataProvider
  30. */
  31. public function testEq(float $a, float $b, bool $expected)
  32. {
  33. self::assertEquals($expected, $this->comparator->equal($a, $b));
  34. }
  35. /**
  36. * Gets list of variations to compare equal float.
  37. *
  38. * @return array
  39. */
  40. public function eqDataProvider(): array
  41. {
  42. return [
  43. [10, 10.00001, true],
  44. [10, 10.000001, true],
  45. [10.0000099, 10.00001, true],
  46. [1, 1.0001, false],
  47. [1, -1.00001, false],
  48. ];
  49. }
  50. /**
  51. * Checks a case when `a` > `b`.
  52. *
  53. * @param float $a
  54. * @param float $b
  55. * @param bool $expected
  56. * @dataProvider gtDataProvider
  57. */
  58. public function testGt(float $a, float $b, bool $expected)
  59. {
  60. self::assertEquals($expected, $this->comparator->greaterThan($a, $b));
  61. }
  62. /**
  63. * Gets list of variations to compare if `a` > `b`.
  64. *
  65. * @return array
  66. */
  67. public function gtDataProvider(): array
  68. {
  69. return [
  70. [10, 10.00001, false],
  71. [10, 10.000001, false],
  72. [10.0000099, 10.00001, false],
  73. [1.0001, 1, true],
  74. [1, -1.00001, true],
  75. ];
  76. }
  77. /**
  78. * Checks a case when `a` >= `b`.
  79. *
  80. * @param float $a
  81. * @param float $b
  82. * @param bool $expected
  83. * @dataProvider gteDataProvider
  84. */
  85. public function testGte(float $a, float $b, bool $expected)
  86. {
  87. self::assertEquals($expected, $this->comparator->greaterThanOrEqual($a, $b));
  88. }
  89. /**
  90. * Gets list of variations to compare if `a` >= `b`.
  91. *
  92. * @return array
  93. */
  94. public function gteDataProvider(): array
  95. {
  96. return [
  97. [10, 10.00001, true],
  98. [10, 10.000001, true],
  99. [10.0000099, 10.00001, true],
  100. [1.0001, 1, true],
  101. [1, -1.00001, true],
  102. [1.0001, 1.001, false],
  103. ];
  104. }
  105. }