BcCompTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Yuloh\BcCompPolyfill;
  3. class BcCompTest extends \PHPUnit_Framework_TestCase
  4. {
  5. public function bcCompData()
  6. {
  7. return [
  8. ['1', '2', 0, -1, 'lt'],
  9. ['2', '1', 0, 1, 'gt'],
  10. ['2', '2', 0, 0, 'eq'],
  11. ['123', '1', 0, 1, 'gt when left operand is longer'],
  12. ['1', '123', 0, -1, 'lt when left operand is shorter'],
  13. ['100.12', '100.13', 0, 0, 'eq when scale is 0 but mantissa is less than'],
  14. ['100.12', '100.13', 1, 0, 'eq when eq up to last decimal place checked'],
  15. ['100.12', '100.13', 2, -1, 'lt with two decimal places checked and last place is lt'],
  16. ['100.12', '100.13', 5, -1, 'lt when scale is larger than both operands'],
  17. ['1', '-1', 0, 1, 'gt when right operand is negative'],
  18. ['-1', '1', 0, -1, 'lt when left operand is negative'],
  19. ['-1', '-1', 0, 0, 'eq when both operands are negative'],
  20. ['-1', '-2', 0, 1, 'gt when both operands are negative'],
  21. ['-2', '-1', 0, -1, 'lt when both operands are negative'],
  22. ['-20', '-1', 0, -1, 'lt when both operands are negative and left operand is longer'],
  23. ['-2', '-10', 0, 1, 'gt when both operands are negative and left operand is shorter'],
  24. ['-200.12', '-200.13', 2, 1, 'gt when both operands are negative with mantissa'],
  25. ['-' . PHP_INT_MAX . '99', '-' . PHP_INT_MAX . '99', 0, 0, 'eq when negative and larger than PHP_INT_MAX'],
  26. ['-' . PHP_INT_MAX . '99', '-' . PHP_INT_MAX . '98', 0, -1, 'lt when negative and larger than PHP_INT_MAX'],
  27. [PHP_INT_MAX . '99', PHP_INT_MAX . '99', 0, 0, 'eq when larger than PHP_INT_MAX'],
  28. [PHP_INT_MAX . '99', PHP_INT_MAX . '97', 0, 1, 'gt when larger than PHP_INT_MAX'],
  29. [PHP_INT_MAX . '99.999', PHP_INT_MAX . '99.997', 5, 1, 'lt when larger than PHP_INT_MAX with mantissa'],
  30. ['riffraff', '1', 0, -1, 'comparison when left operand is nonsense'],
  31. ['1', 'kodos', 0, 1, 'comparison when right operand is nonsense'],
  32. ['riffraff', 'kodos', 0, 0, 'comparison when both operands are nonsense'],
  33. ['1.2.3', '1', 5, -1, 'comparison when left operand has too many decimal separators'],
  34. ['1', '1.2.3', 5, 1, 'comparison when right operand has too many decimal separators'],
  35. ['1.12', '1.15', 1.6, 0, 'float scale should be cast to int, not rounded'],
  36. ['1a', '1b', 0, 0, 'comparison when both operands have a letter in a number'],
  37. ['0', '-0', 0, 0, 'comparison of 0 and -0'],
  38. ];
  39. }
  40. /**
  41. * @dataProvider bcCompData
  42. */
  43. public function testBcComp($leftOperand, $rightOperand, $scale, $result, $msg)
  44. {
  45. $realLibraryResult = \bccomp($leftOperand, $rightOperand, $scale);
  46. $this->assertSame($result, $realLibraryResult, 'The actual function result should match the expected result');
  47. $actual = \Yuloh\BcCompPolyfill\bccomp($leftOperand, $rightOperand, $scale);
  48. $this->assertSame($result, $actual, $msg);
  49. }
  50. /**
  51. * @expectedException PHPUnit_Framework_Error_Warning
  52. */
  53. public function testInvalidScale()
  54. {
  55. \Yuloh\BcCompPolyfill\bccomp(1, 1, []);
  56. }
  57. public function fuzzTestData()
  58. {
  59. $data = [];
  60. for ($i = 0; $i < 1000; $i++) {
  61. $leftOperand = $this->randOperand();
  62. $rightOperand = $this->randOperand();
  63. $scale = rand(0, 200);
  64. $result = \bccomp($leftOperand, $rightOperand, $scale);
  65. $data[] = [$leftOperand, $rightOperand, $scale, $result];
  66. }
  67. return $data;
  68. }
  69. /**
  70. * @dataProvider fuzzTestData
  71. */
  72. public function testWithRandomData($leftOperand, $rightOperand, $scale, $result)
  73. {
  74. $actual = \Yuloh\BcCompPolyfill\bccomp($leftOperand, $rightOperand, $scale);
  75. $this->assertSame($result, $actual);
  76. }
  77. public function randOperand()
  78. {
  79. $negative = rand() % 2 === 0 ? '-' : '';
  80. $characteristic = rand();
  81. $mantissa = rand() % 2 === 0 ? '.' . rand() : '';
  82. return $negative . $characteristic . $mantissa;
  83. }
  84. }