@@ -113,6 +113,7 @@ public function providerExpressions()
113113 ['tanh(1.5) ' ],
114114
115115 ['0.1 + 0.2 ' ],
116+ ['0.1 + 0.2 - 0.3 ' ],
116117 ['1 + 2 ' ],
117118
118119 ['0.1 - 0.2 ' ],
@@ -246,7 +247,246 @@ public function providerExpressions()
246247 ['max(1,2,4.9,3) ' ],
247248 ['min(1,2,4.9,3) ' ],
248249 ['max([1,2,4.9,3]) ' ],
249- ['min([1,2,4.9,3]) ' ]
250+ ['min([1,2,4.9,3]) ' ],
251+
252+ ['4 % 4 ' ],
253+ ['7 % 4 ' ],
254+ ['99 % 4 ' ],
255+ ['123 % 7 ' ],
256+ ];
257+ }
258+
259+ /**
260+ * @dataProvider bcMathExpressions
261+ */
262+ public function testBCMathCalculating (string $ expression , string $ expected = '' ) : void
263+ {
264+ $ calculator = new MathExecutor ();
265+ $ calculator ->useBCMath ();
266+
267+ if ('' === $ expected )
268+ {
269+ $ expected = $ expression ;
270+ }
271+
272+ /** @var float $phpResult */
273+ eval ('$phpResult = ' . $ expected . '; ' );
274+
275+ try {
276+ $ result = $ calculator ->execute ($ expression );
277+ } catch (Exception $ e ) {
278+ $ this ->fail (\sprintf ('Exception: %s (%s:%d), expression was: %s ' , \get_class ($ e ), $ e ->getFile (), $ e ->getLine (), $ expression ));
279+ }
280+ $ this ->assertEquals ($ phpResult , $ result , "Expression was: {$ expression }" );
281+ }
282+
283+ /**
284+ * Expressions data provider
285+ *
286+ * Most tests can go in here. The idea is that each expression will be evaluated by MathExecutor and by PHP with eval.
287+ * The results should be the same. If they are not, then the test fails. No need to add extra test unless you are doing
288+ * something more complex and not a simple mathmatical expression.
289+ */
290+ public function bcMathExpressions ()
291+ {
292+ return [
293+ ['-5 ' ],
294+ ['-5+10 ' ],
295+ ['4-5 ' ],
296+ ['4 -5 ' ],
297+ ['(4*2)-5 ' ],
298+ ['(4*2) - 5 ' ],
299+ ['4*-5 ' ],
300+ ['4 * -5 ' ],
301+ ['+5 ' ],
302+ ['+(3+2) ' ],
303+ ['+(+3+2) ' ],
304+ ['+(-3+2) ' ],
305+ ['-5 ' ],
306+ ['-(-5) ' ],
307+ ['-(+5) ' ],
308+ ['+(-5) ' ],
309+ ['+(+5) ' ],
310+ ['-(3+2) ' ],
311+ ['-(-3+-2) ' ],
312+
313+ ['abs(1.5) ' ],
314+ ['acos(0.15) ' ],
315+ ['acosh(1.5) ' ],
316+ ['asin(0.15) ' ],
317+ ['atan(0.15) ' ],
318+ ['atan2(1.5, 3.5) ' ],
319+ ['atanh(0.15) ' ],
320+ ['bindec("10101") ' ],
321+ ['ceil(1.5) ' ],
322+ ['cos(1.5) ' ],
323+ ['cosh(1.5) ' ],
324+ ['decbin("15") ' ],
325+ ['dechex("15") ' ],
326+ ['decoct("15") ' ],
327+ ['deg2rad(1.5) ' ],
328+ ['exp(1.5) ' ],
329+ ['expm1(1.5) ' ],
330+ ['floor(1.5) ' ],
331+ ['fmod(1.5, 3.5) ' ],
332+ ['hexdec("abcdef") ' ],
333+ ['hypot(1.5, 3.5) ' ],
334+ ['intdiv(10, 2) ' ],
335+ ['log(1.5) ' ],
336+ ['log10(1.5) ' ],
337+ ['log1p(1.5) ' ],
338+ ['max(1.5, 3.5) ' ],
339+ ['min(1.5, 3.5) ' ],
340+ ['octdec("15") ' ],
341+ ['pi() ' ],
342+ ['pow(1.5, 3.5) ' ],
343+ ['rad2deg(1.5) ' ],
344+ ['round(1.5) ' ],
345+ ['sin(1.5) ' ],
346+ ['sin(12) ' ],
347+ ['+sin(12) ' ],
348+ ['-sin(12) ' , '0.53 ' ],
349+ ['sinh(1.5) ' ],
350+ ['sqrt(1.5) ' ],
351+ ['tan(1.5) ' ],
352+ ['tanh(1.5) ' ],
353+
354+ ['0.1 + 0.2 ' ],
355+ ['0.1 + 0.2 - 0.3 ' ],
356+ ['1 + 2 ' ],
357+
358+ ['0.1 - 0.2 ' ],
359+ ['1 - 2 ' ],
360+
361+ ['0.1 * 2 ' ],
362+ ['1 * 2 ' ],
363+
364+ ['0.1 / 0.2 ' ],
365+ ['1 / 2 ' ],
366+
367+ ['2 * 2 + 3 * 3 ' ],
368+ ['2 * 2 / 3 * 3 ' , '3.99 ' ],
369+ ['2 / 2 / 3 / 3 ' , '0.11 ' ],
370+ ['2 / 2 * 3 / 3 ' ],
371+ ['2 / 2 * 3 * 3 ' ],
372+
373+ ['1 + 0.6 - 3 * 2 / 50 ' ],
374+
375+ ['(5 + 3) * -1 ' ],
376+
377+ ['-2- 2*2 ' ],
378+ ['2- 2*2 ' ],
379+ ['2-(2*2) ' ],
380+ ['(2- 2)*2 ' ],
381+ ['2 + 2*2 ' ],
382+ ['2+ 2*2 ' ],
383+ ['2+2*2 ' ],
384+ ['(2+2)*2 ' ],
385+ ['(2 + 2)*-2 ' ],
386+ ['(2+-2)*2 ' ],
387+
388+ ['1 + 2 * 3 / (min(1, 5) + 2 + 1) ' ],
389+ ['1 + 2 * 3 / (min(1, 5) - 2 + 5) ' ],
390+ ['1 + 2 * 3 / (min(1, 5) * 2 + 1) ' ],
391+ ['1 + 2 * 3 / (min(1, 5) / 2 + 1) ' ],
392+ ['1 + 2 * 3 / (min(1, 5) / 2 * 1) ' ],
393+ ['1 + 2 * 3 / (min(1, 5) / 2 / 1) ' ],
394+ ['1 + 2 * 3 / (3 + min(1, 5) + 2 + 1) ' , '1.85 ' ],
395+ ['1 + 2 * 3 / (3 - min(1, 5) - 2 + 1) ' ],
396+ ['1 + 2 * 3 / (3 * min(1, 5) * 2 + 1) ' , '1.85 ' ],
397+ ['1 + 2 * 3 / (3 / min(1, 5) / 2 + 1) ' ],
398+
399+ ['(1 + 2) * 3 / (3 / min(1, 5) / 2 + 1) ' ],
400+
401+ ['sin(10) * cos(50) / min(10, 20/2) ' , '-0.05 ' ],
402+ ['sin(10) * cos(50) / min(10, (20/2)) ' , '-0.05 ' ],
403+ ['sin(10) * cos(50) / min(10, (max(10,20)/2)) ' , '-0.05 ' ],
404+
405+ ['1 + "2" / 3 ' , '1.66 ' ],
406+ ["1.5 + '2.5' / 4 " , '2.12 ' ],
407+ ['1.5 + "2.5" * ".5" ' ],
408+
409+ ['-1 + -2 ' ],
410+ ['-1+-2 ' ],
411+ ['-1- -2 ' ],
412+ ['-1/-2 ' ],
413+ ['-1*-2 ' ],
414+
415+ ['(1+2+3+4-5)*7/100 ' ],
416+ ['(-1+2+3+4- 5)*7/100 ' ],
417+ ['(1+2+3+4- 5)*7/100 ' ],
418+ ['( 1 + 2 + 3 + 4 - 5 ) * 7 / 100 ' ],
419+
420+ ['1 && 0 ' ],
421+ ['1 && 0 && 1 ' ],
422+ ['1 || 0 ' ],
423+ ['1 && 0 || 1 ' ],
424+
425+ ['5 == 3 ' ],
426+ ['5 == 5 ' ],
427+ ['5 != 3 ' ],
428+ ['5 != 5 ' ],
429+ ['5 > 3 ' ],
430+ ['3 > 5 ' ],
431+ ['3 >= 5 ' ],
432+ ['3 >= 3 ' ],
433+ ['3 < 5 ' ],
434+ ['5 < 3 ' ],
435+ ['3 <= 5 ' ],
436+ ['5 <= 5 ' ],
437+ ['10 < 9 || 4 > (2+1) ' ],
438+ ['10 < 9 || 4 > (-2+1) ' ],
439+ ['10 < 9 || 4 > (2+1) && 5 == 5 || 4 != 6 || 3 >= 4 || 3 <= 7 ' ],
440+
441+ ['1 + 5 == 3 + 1 ' ],
442+ ['1 + 5 == 5 + 1 ' ],
443+ ['1 + 5 != 3 + 1 ' ],
444+ ['1 + 5 != 5 + 1 ' ],
445+ ['1 + 5 > 3 + 1 ' ],
446+ ['1 + 3 > 5 + 1 ' ],
447+ ['1 + 3 >= 5 + 1 ' ],
448+ ['1 + 3 >= 3 + 1 ' ],
449+ ['1 + 3 < 5 + 1 ' ],
450+ ['1 + 5 < 3 + 1 ' ],
451+ ['1 + 3 <= 5 + 1 ' ],
452+ ['1 + 5 <= 5 + 1 ' ],
453+
454+ ['(-4) ' ],
455+ ['(-4 + 5) ' ],
456+ ['(3 * 1) ' ],
457+ ['(-3 * -1) ' ],
458+ ['1 + (-3 * -1) ' ],
459+ ['1 + ( -3 * 1) ' ],
460+ ['1 + (3 *-1) ' ],
461+ ['1 - 0 ' ],
462+ ['1-0 ' ],
463+
464+ ['-(1.5) ' ],
465+ ['-log(4) ' , '-1.38 ' ],
466+ ['0-acosh(1.5) ' , '-0.96 ' ],
467+ ['-acosh(1.5) ' , '-0.96 ' ],
468+ ['-(-4) ' ],
469+ ['-(-4 + 5) ' ],
470+ ['-(3 * 1) ' ],
471+ ['-(-3 * -1) ' ],
472+ ['-1 + (-3 * -1) ' ],
473+ ['-1 + ( -3 * 1) ' ],
474+ ['-1 + (3 *-1) ' ],
475+ ['-1 - 0 ' ],
476+ ['-1-0 ' ],
477+ ['-(4*2)-5 ' ],
478+ ['-(4*-2)-5 ' ],
479+ ['-(-4*2) - 5 ' ],
480+ ['-4*-5 ' ],
481+ ['max(1,2,4.9,3) ' ],
482+ ['min(1,2,4.9,3) ' ],
483+ ['max([1,2,4.9,3]) ' ],
484+ ['min([1,2,4.9,3]) ' ],
485+
486+ ['4 % 4 ' ],
487+ ['7 % 4 ' ],
488+ ['99 % 4 ' ],
489+ ['123 % 7 ' ],
250490 ];
251491 }
252492
0 commit comments