Skip to content

Commit

Permalink
Add test cases to ensure cases of inf and nan are accounted for by op…
Browse files Browse the repository at this point in the history
…timizer (address #16)
  • Loading branch information
Muqsit committed Nov 3, 2022
1 parent 14442ca commit 082ccf3
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion tests/muqsit/arithmexp/OptimizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,25 @@ public function testConstantPropagationOptimization() : void{
self::assertExpressionsEqual($expected, $actual);
}

public function testDivisionConstantFoldingForEqualOperandsReturningNan() : void{
public function testDivisionConstantFoldingForRightOperandNan() : void{
$expression = $this->parser->parse("min(1, 2) / nan");
self::assertInstanceOf(ConstantExpression::class, $expression);
self::assertNan($expression->evaluate());
}

public function testSubtractionConstantFoldingBetweenInf() : void{
$expression = $this->parser->parse("inf - inf");
self::assertInstanceOf(ConstantExpression::class, $expression);
self::assertNan($expression->evaluate());
}

public function testSubtractionConstantFoldingBetweenNan() : void{
$expression = $this->parser->parse("nan - nan");
self::assertInstanceOf(ConstantExpression::class, $expression);
self::assertNan($expression->evaluate());
}

public function testDivisionConstantFoldingForFunctionCallOperandsReturningNan() : void{
$expression = $this->parser->parse("sqrt(-1) / sqrt(-1)");
self::assertInstanceOf(ConstantExpression::class, $expression);
self::assertNan($expression->evaluate());
Expand Down Expand Up @@ -204,6 +222,16 @@ public function testDivisionOperatorStrengthReductionForEqualOperandsWithGroupin
self::assertEquals(1, $expression->evaluate());
}

public function testMultiplicationOperatorStrengthReductionForNanOperands() : void{
$expression = $this->parser->parse("nan * x");
self::assertInstanceOf(ConstantExpression::class, $expression);
self::assertNan($expression->evaluate());

$expression = $this->parser->parse("x * nan");
self::assertInstanceOf(ConstantExpression::class, $expression);
self::assertNan($expression->evaluate());
}

public function testDivisionOperatorStrengthReductionForCommutativelyEqualOperands() : void{
$expression = $this->parser->parse("(x + y + z) / (y + x + z)");
self::assertInstanceOf(ConstantExpression::class, $expression);
Expand All @@ -222,6 +250,12 @@ public function testDivisionOperatorStrengthReductionForLeftOperandZeroWithGroup
self::assertEquals(0, $expression->evaluate());
}

public function testDivisionOperatorStrengthReductionForRightOperandNan() : void{
$expression = $this->parser->parse("mt_rand(1, 2) / nan");
self::assertInstanceOf(ConstantExpression::class, $expression);
self::assertNan($expression->evaluate());
}

public function testDivisionOperatorStrengthReductionForRightOperandOne() : void{
$actual = $this->parser->parse("x / 1 + y / 1");
$expected = $this->unoptimized_parser->parse("x + y");
Expand Down Expand Up @@ -288,6 +322,16 @@ public function testAdditionOperatorStrengthReductionForOperandZeroWithGrouping(
self::assertExpressionsEqual($expected, $actual);
}

public function testAdditionOperatorStrengthReductionForNanOperands() : void{
$expression = $this->parser->parse("nan + x");
self::assertInstanceOf(ConstantExpression::class, $expression);
self::assertNan($expression->evaluate());

$expression = $this->parser->parse("x + nan");
self::assertInstanceOf(ConstantExpression::class, $expression);
self::assertNan($expression->evaluate());
}

public function testAdditionOperatorStrengthReductionForNegativeLeftOperand() : void{
$actual = $this->parser->parse("-x + y");
$expected = $this->unoptimized_parser->parse("y - x");
Expand Down Expand Up @@ -354,6 +398,16 @@ public function testSubtractionOperatorStrengthReductionForCommutativeFunctions(
self::assertEquals(0, $expression->evaluate());
}

public function testSubtractionOperatorStrengthReductionForNanOperands() : void{
$expression = $this->parser->parse("nan - x");
self::assertInstanceOf(ConstantExpression::class, $expression);
self::assertNan($expression->evaluate());

$expression = $this->parser->parse("x - nan");
self::assertInstanceOf(ConstantExpression::class, $expression);
self::assertNan($expression->evaluate());
}

public function testSubtractionOperatorStrengthReductionForNegativeLeftOperand() : void{
$actual = $this->parser->parse("-x - y");

Expand Down

0 comments on commit 082ccf3

Please sign in to comment.