Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_dadd.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _dadd implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::add($value1, $value2));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_dmul.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _dmul implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::multiply($value1, $value2, 8));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_dsub.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _dsub implements OperationInterface

public function execute(): void
{
$leftValue = $this->getStack();
$rightValue = $this->getStack();
$leftValue = $this->getStack();

$this->pushStack(BinaryTool::sub($leftValue, $rightValue, 8));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_iadd.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _iadd implements OperationInterface

public function execute(): void
{
$leftValue = $this->getStack();
$rightValue = $this->getStack();
$leftValue = $this->getStack();

$this->pushStack(BinaryTool::add($leftValue, $rightValue));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_iand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _iand implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::andBits($value1, $value2, 4));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_if_acmpeq.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function execute(): void
{
$offset = $this->readShort();

$leftOperand = $this->getStack();
$rightOperand = $this->getStack();
$leftOperand = $this->getStack();

if ($leftOperand === $rightOperand) {
$this->setOffset($this->getProgramCounter() + $offset);
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_if_acmpne.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function execute(): void
{
$offset = $this->readShort();

$leftOperand = $this->getStack();
$rightOperand = $this->getStack();
$leftOperand = $this->getStack();

if ($leftOperand !== $rightOperand) {
$this->setOffset($this->getProgramCounter() + $offset);
Expand Down
4 changes: 2 additions & 2 deletions src/Kernel/Mnemonics/_if_icmpge.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public function execute(): void
{
$offset = $this->readShort();

$leftOperand = $this->getStack();
$rightOperand = $this->getStack();
$leftOperand = $this->getStack();

if ($leftOperand <= $rightOperand) {
if ($leftOperand >= $rightOperand) {
$this->setOffset($this->getProgramCounter() + $offset);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Kernel/Mnemonics/_if_icmpgt.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public function execute(): void
{
$offset = $this->readShort();

$leftOperand = $this->getStack();
$rightOperand = $this->getStack();
$leftOperand = $this->getStack();

if ($leftOperand < $rightOperand) {
if ($leftOperand > $rightOperand) {
$this->setOffset($this->getProgramCounter() + $offset);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Kernel/Mnemonics/_if_icmplt.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public function execute(): void
{
$offset = $this->readShort();

$leftOperand = $this->getStack();
$rightOperand = $this->getStack();
$leftOperand = $this->getStack();

if ($rightOperand < $leftOperand) {
if ($leftOperand < $rightOperand) {
$this->setOffset($this->getProgramCounter() + $offset);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_if_icmpne.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function execute(): void
{
$offset = $this->readShort();

$leftOperand = $this->getStack();
$rightOperand = $this->getStack();
$leftOperand = $this->getStack();

if ($leftOperand != $rightOperand) {
$this->setOffset($this->getProgramCounter() + $offset);
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_imul.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _imul implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::multiply($value1, $value2, 4));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_ior.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _ior implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::orBits($value1, $value2, 4));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_ishl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _ishl implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::shiftLeft($value1, $value2));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_ishr.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _ishr implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::shiftRight($value1, $value2, 4));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_isub.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _isub implements OperationInterface

public function execute(): void
{
$leftValue = $this->getStack();
$rightValue = $this->getStack();
$leftValue = $this->getStack();

$this->pushStack(BinaryTool::sub($leftValue, $rightValue, 4));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_iushr.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _iushr implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::unsignedShiftRight($value1, $value2, 4));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_ixor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _ixor implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::xorBits($value1, $value2, 4));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_ladd.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _ladd implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::add($value1, $value2));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_land.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _land implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::andBits($value1, $value2, 8));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_lmul.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _lmul implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::multiply($value1, $value2, 8));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_lor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _lor implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::orBits($value1, $value2, 8));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_lshl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _lshl implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::shiftLeft($value1, $value2));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_lshr.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _lshr implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::shiftRight($value1, $value2, 8));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_lsub.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _lsub implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::sub($value1, $value2, 8));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Mnemonics/_lushr.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ final class _lushr implements OperationInterface

public function execute(): void
{
$value1 = $this->getStack();
$value2 = $this->getStack();
$value1 = $this->getStack();

$this->pushStack(BinaryTool::unsignedShiftRight($value1, $value2, 8));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Utilities/BinaryTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ final public static function unsignedShiftRight($value1, $value2, $bytes)
{
$value1 = (int) $value1;
$value2 = (int) $value2;
$bits = sprintf('%0' . ($bytes * 8) . 's', base_convert($value2, 10, 2));
$bits = sprintf('%0' . ($bytes * 8) . 's', base_convert($value1, 10, 2));

$bits = sprintf('%0' . ($bytes * 8) . 's', substr($bits, 0, strlen($bits) - $value1));
$bits = sprintf('%0' . ($bytes * 8) . 's', substr($bits, 0, strlen($bits) - $value2));

if ($bits === '') {
$bits = '0';
Expand Down
85 changes: 85 additions & 0 deletions tests/BinaryOperatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
namespace PHPJava\Tests;

use PHPUnit\Framework\TestCase;

class BinaryOperatorTest extends Base
{
protected $fixtures = [
'BinaryOperatorTest',
];

private function call($method, $value1, $value2)
{
$calculatedValue = $this->initiatedJavaClasses['BinaryOperatorTest']
->getInvoker()
->getStatic()
->getMethods()
->call($method, $value1, $value2);

return $calculatedValue->getValue();
}

public function testIntAdd()
{
$actual = $this->call('intAdd', 5, 3);
$this->assertEquals(8, $actual);
}

public function testIntSub()
{
$actual = $this->call('intSub', 5, 3);
$this->assertEquals(2, $actual);
}

public function testIntMul()
{
$actual = $this->call('intMul', 5, 3);
$this->assertEquals(15, $actual);
}

public function testIntShl()
{
$actual = $this->call('intShl', 3, 2);
$this->assertEquals(12, $actual);
}

public function testIntShr()
{
$actual = $this->call('intShr', 12, 2);
$this->assertEquals(3, $actual);
}

public function testIntUshr()
{
$actual = $this->call('intUshr', 12, 2);
$this->assertEquals(3, $actual);
}

public function testIntAnd()
{
$value1 = (int) base_convert('0011', 2, 10);
$value2 = (int) base_convert('0101', 2, 10);
$expect = (int) base_convert('0001', 2, 10);
$actual = $this->call('intAnd', $value1, $value2);
$this->assertEquals($expect, $actual);
}

public function testIntOr()
{
$value1 = (int) base_convert('0011', 2, 10);
$value2 = (int) base_convert('0101', 2, 10);
$expect = (int) base_convert('0111', 2, 10);
$actual = $this->call('intOr', $value1, $value2);
$this->assertEquals($expect, $actual);
}

public function testIntXor()
{
$value1 = (int) base_convert('0011', 2, 10);
$value2 = (int) base_convert('0101', 2, 10);
$expect = (int) base_convert('0110', 2, 10);
$actual = $this->call('intXor', $value1, $value2);
$this->assertEquals($expect, $actual);
}
}
Loading