Skip to content

Commit fe5e77f

Browse files
committed
Add FizzBuzz test for compiler
1 parent a3c73b5 commit fe5e77f

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
22
namespace PHPJava\Compiler\Emulator\Mnemonics;
33

4-
use PHPJava\Exceptions\NotImplementedException;
5-
64
class _ifge extends AbstractOperationCode implements OperationCodeInterface
75
{
86
use \PHPJava\Compiler\Emulator\Traits\GeneralProcessor;
97

108
public function execute(): void
119
{
12-
throw new NotImplementedException(__CLASS__);
10+
$this->accumulator
11+
->popFromOperandStack();
12+
13+
$this->addFrame();
1314
}
1415
}

src/Compiler/Lang/Assembler/Traits/NodeConvertible.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ public function convertNodeToOpCode(Node $node): int
2525
case \PhpParser\Node\Expr\BinaryOp\BooleanOr::class:
2626
return OpCode::_ior;
2727
case \PhpParser\Node\Expr\BinaryOp\Greater::class:
28-
return OpCode::_iflt;
28+
return OpCode::_ifle;
2929
case \PhpParser\Node\Expr\BinaryOp\Smaller::class:
30-
return OpCode::_ifgt;
30+
return OpCode::_ifge;
3131
case \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual::class:
32-
return OpCode::_ifle;
32+
return OpCode::_iflt;
3333
case \PhpParser\Node\Expr\BinaryOp\SmallerOrEqual::class:
34-
return OpCode::_ifge;
34+
return OpCode::_ifgt;
3535
case \PhpParser\Node\Expr\BinaryOp\Identical::class:
3636
return OpCode::_ifne;
3737
case \PhpParser\Node\Expr\BinaryOp\NotIdentical::class:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
class TestFizzBuzz
4+
{
5+
/**
6+
* @param \PHPJava\Packages\java\lang\_String[] $args
7+
*/
8+
public static function main($args)
9+
{
10+
for ($i = 1; $i <= 100; $i++) {
11+
if (($i % 3) === 0) {
12+
echo 'Fizz';
13+
}
14+
if (($i % 5) === 0) {
15+
echo 'Buzz';
16+
}
17+
if (($i % 3) !== 0 && ($i % 5) !== 0) {
18+
echo $i;
19+
}
20+
echo "\n";
21+
}
22+
}
23+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
namespace PHPJava\Tests\Cases\Compiler;
3+
4+
use PHPJava\Compiler\Lang\PackageAssembler;
5+
use PHPJava\Compiler\Lang\Stream\FileStream;
6+
use PHPJava\Compiler\Lang\Stream\StreamReaderInterface;
7+
use PHPJava\Tests\Cases\Base;
8+
9+
class FizzBuzzTest extends Base
10+
{
11+
private function getDistributeDirectory(): string
12+
{
13+
return __DIR__ . '/../caches/compiler';
14+
}
15+
16+
private function getClassNameByTestName(string $name): string
17+
{
18+
return ucfirst(explode('::', $name)[1]);
19+
}
20+
21+
private function getFileStream(string $className): StreamReaderInterface
22+
{
23+
return (new FileStream(__DIR__ . '/Codes/' . $className . '.php'))
24+
->setDistributeDirectory($this->getDistributeDirectory());
25+
}
26+
27+
private function runJavaTest(string $testName): array
28+
{
29+
$name = $this->getClassNameByTestName($testName);
30+
(new PackageAssembler($this->getFileStream($name)))
31+
->assemble();
32+
33+
exec(
34+
'cd ' . $this->getDistributeDirectory() . ' && java ' . $name,
35+
$output,
36+
$return
37+
);
38+
39+
return [$output, $return];
40+
}
41+
42+
public function testFizzBuzz()
43+
{
44+
[$output, $return] = $this->runJavaTest(__METHOD__);
45+
46+
$this->assertSame(0, $return);
47+
$this->assertEquals(
48+
file_get_contents(__DIR__ . '/../templates/FizzBuzzTest.txt'),
49+
implode("\n", $output) . "\n"
50+
);
51+
}
52+
}

0 commit comments

Comments
 (0)