EvalMathTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * EvalMathTest.php
  4. *
  5. * @author dbojdo - Daniel Bojdo <daniel.bojdo@8x8.com>
  6. * Created on 02 12, 2016, 17:17
  7. * Copyright (C) 8x8
  8. */
  9. namespace Webit\Util\EvalMath\Tests;
  10. use Webit\Util\EvalMath\EvalMath;
  11. class EvalMathTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @var EvalMath
  15. */
  16. private $evalMath;
  17. protected function setUp()
  18. {
  19. $this->evalMath = new EvalMath();
  20. }
  21. /**
  22. * @test
  23. * @dataProvider moduloOperatorData
  24. */
  25. public function shouldSupportModuloOperator($formula, $values, $expectedResult)
  26. {
  27. foreach ($values as $k => $v) {
  28. $this->evalMath->v[$k] = $v;
  29. }
  30. $this->assertEquals($expectedResult, $this->evalMath->evaluate($formula));
  31. }
  32. public function moduloOperatorData()
  33. {
  34. return array(
  35. array(
  36. 'a%b', // 9%3 => 0
  37. array('a' => 9, 'b' => 3),
  38. 0
  39. ),
  40. array(
  41. 'a%b', // 10%3 => 1
  42. array('a' => 10, 'b' => 3),
  43. 1
  44. ),
  45. array(
  46. '10-a%(b+c*d)', // 10-10%(7-2*2) => 9
  47. array('a' => '10', 'b' => 7, 'c'=> -2, 'd' => 2),
  48. 9
  49. )
  50. );
  51. }
  52. /**
  53. * @test
  54. * @dataProvider doubleMinusData
  55. */
  56. public function shouldConsiderDoubleMinusAsPlus($formula, $values, $expectedResult)
  57. {
  58. foreach ($values as $k => $v) {
  59. $this->evalMath->v[$k] = $v;
  60. }
  61. $this->assertEquals(
  62. $expectedResult,
  63. $this->evalMath->evaluate($formula)
  64. );
  65. }
  66. public function doubleMinusData()
  67. {
  68. return array(
  69. array(
  70. 'a+b*c--d', // 1+2*3--4 => 1+6+4 => 11
  71. array(
  72. 'a' => 1,
  73. 'b' => 2,
  74. 'c' => 3,
  75. 'd' => 4
  76. ),
  77. 11
  78. ),
  79. array(
  80. 'a+b*c--d', // 1+2*3---4 => 1+6-4 => 3
  81. array(
  82. 'a' => 1,
  83. 'b' => 2,
  84. 'c' => 3,
  85. 'd' => -4
  86. ),
  87. 3
  88. )
  89. );
  90. }
  91. }