summaryrefslogtreecommitdiff
path: root/extensions/ParserFunctions/tests/ExpressionTest.php
blob: 169a9cb41016d09ab98505e4c5494fd0c6dc1f25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
class ExpressionTest extends MediaWikiTestCase {

	/**
	 * @var ExprParser
	 */
	protected $parser;

	protected function setUp() {
		parent::setUp();
		$this->parser = new ExprParser();
	}

	/**
	 * @dataProvider provideExpressions
	 */
	function testExpression( $input, $expected ) {
		$this->assertEquals(
			$expected,
			$this->parser->doExpression( $input )
		);
	}

	function provideExpressions() {
		return array(
			array( '1 or 0', '1' ),
			array( 'not (1 and 0)', '1' ),
			array( 'not 0', '1' ),
			array( '4 < 5', '1' ),
			array( '-5 < 2', '1' ),
			array( '-2 <= -2', '1' ),
			array( '4 > 3', '1' ),
			array( '4 > -3', '1' ),
			array( '5 >= 2', '1' ),
			array( '2 >= 2', '1' ),
			array( '1 != 2', '1' ),
			array( '-4 * -4 = 4 * 4', '1' ),
			array( 'not (1 != 1)', '1' ),
			array( '1 + 1', '2' ),
			array( '-1 + 1', '0' ),
			array( '+1 + 1', '2' ),
			array( '4 * 4', '16' ),
			array( '(1/3) * 3', '1' ),
			array( '3 / 1.5', '2' ),
			array( '3 / 0.2', '15' ),
			array( '3 / ( 2.0 * 0.1 )', '15' ),
			array( '3 / ( 2.0 / 10 )', '15' ),
			array( '3 / (- 0.2 )', '-15' ),
			array( '3 / abs( 0.2 )', '15' ),
			array( '3 mod 2', '1' ),
			array( '1e4', '10000' ),
			array( '1e-2', '0.01' ),
			array( '4.0 round 0', '4' ),
			array( 'ceil 4', '4' ),
			array( 'floor 4', '4' ),
			array( '4.5 round 0', '5' ),
			array( '4.2 round 0', '4' ),
			array( '-4.2 round 0', '-4' ),
			array( '-4.5 round 0', '-5' ),
			array( '-2.0 round 0', '-2' ),
			array( 'ceil -3', '-3' ),
			array( 'floor -6.0', '-6' ),
			array( 'ceil 4.2', '5' ),
			array( 'ceil -4.5', '-4' ),
			array( 'floor -4.5', '-5' ),
			array( 'abs(-2)', '2' ),
			array( 'ln(exp(1))', '1' ),
			array( 'trunc(4.5)', '4' ),
			array( 'trunc(-4.5)', '-4' ),
			array( '123 fmod (2^64-1)', '123' ),
			array( '5.7 mod 1.3', '0' ),
			array( '5.7 fmod 1.3', '0.5' ),
		);
	}
}