Skip to content

Commit 280f39e

Browse files
committed
Add negation tests
1 parent 60c2762 commit 280f39e

7 files changed

Lines changed: 206 additions & 19 deletions

File tree

src/Kernel/Mnemonics/_dneg.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace PHPJava\Kernel\Mnemonics;
33

4-
use PHPJava\Exceptions\NotImplementedException;
4+
use PHPJava\Kernel\Types\_Double;
5+
use PHPJava\Utilities\BinaryTool;
6+
use PHPJava\Utilities\Extractor;
57

68
final class _dneg implements OperationInterface
79
{
@@ -10,6 +12,14 @@ final class _dneg implements OperationInterface
1012

1113
public function execute(): void
1214
{
13-
throw new NotImplementedException(__CLASS__);
15+
$value = Extractor::getRealValue(
16+
$this->popFromOperandStack()
17+
);
18+
19+
$this->pushToOperandStack(
20+
_Double::get(
21+
BinaryTool::negate($value)
22+
)
23+
);
1424
}
1525
}

src/Kernel/Mnemonics/_fneg.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace PHPJava\Kernel\Mnemonics;
33

4-
use PHPJava\Exceptions\NotImplementedException;
4+
use PHPJava\Kernel\Types\_Float;
5+
use PHPJava\Utilities\BinaryTool;
6+
use PHPJava\Utilities\Extractor;
57

68
final class _fneg implements OperationInterface
79
{
@@ -10,6 +12,14 @@ final class _fneg implements OperationInterface
1012

1113
public function execute(): void
1214
{
13-
throw new NotImplementedException(__CLASS__);
15+
$value = Extractor::getRealValue(
16+
$this->popFromOperandStack()
17+
);
18+
19+
$this->pushToOperandStack(
20+
_Float::get(
21+
BinaryTool::negate($value)
22+
)
23+
);
1424
}
1525
}

src/Kernel/Mnemonics/_ineg.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace PHPJava\Kernel\Mnemonics;
33

4+
use PHPJava\Kernel\Types\_Int;
45
use PHPJava\Utilities\BinaryTool;
6+
use PHPJava\Utilities\Extractor;
57

68
final class _ineg implements OperationInterface
79
{
@@ -10,8 +12,14 @@ final class _ineg implements OperationInterface
1012

1113
public function execute(): void
1214
{
13-
$value = $this->popFromOperandStack();
15+
$value = Extractor::getRealValue(
16+
$this->popFromOperandStack()
17+
);
1418

15-
$this->pushToOperandStack(BinaryTool::negate($value, 4));
19+
$this->pushToOperandStack(
20+
_Int::get(
21+
BinaryTool::negate($value)
22+
)
23+
);
1624
}
1725
}

src/Kernel/Mnemonics/_lneg.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace PHPJava\Kernel\Mnemonics;
33

4-
use PHPJava\Exceptions\NotImplementedException;
4+
use PHPJava\Kernel\Types\_Long;
5+
use PHPJava\Utilities\BinaryTool;
6+
use PHPJava\Utilities\Extractor;
57

68
final class _lneg implements OperationInterface
79
{
@@ -10,6 +12,14 @@ final class _lneg implements OperationInterface
1012

1113
public function execute(): void
1214
{
13-
throw new NotImplementedException(__CLASS__);
15+
$value = Extractor::getRealValue(
16+
$this->popFromOperandStack()
17+
);
18+
19+
$this->pushToOperandStack(
20+
_Long::get(
21+
BinaryTool::negate($value)
22+
)
23+
);
1424
}
1525
}

src/Utilities/BinaryTool.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,9 @@ final public static function toSigned($value, $bytes)
5252
return '-' . base_convert(self::addOneBit(self::reverseBits($convert)), 2, 10);
5353
}
5454

55-
final public static function negate($value, $bytes)
55+
final public static function negate($value)
5656
{
57-
$value = (int) $value;
58-
$value = base_convert((string) $value, 10, 2);
59-
60-
if (sprintf('%0' . $bytes . 's', $value) === str_repeat('0', $bytes)) {
61-
// zero number was overflow
62-
return '0';
63-
}
64-
65-
$convert = self::addOneBit(self::reverseBits($value));
66-
return ($convert[0] === '1' ? '-' : '') . base_convert($convert, 2, 10);
57+
return $value * -1;
6758
}
6859

6960
final public static function multiply($value1, $value2)

tests/NegationTest.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
namespace PHPJava\Tests;
3+
4+
use PHPJava\Kernel\Types\_Double;
5+
use PHPJava\Kernel\Types\_Float;
6+
use PHPJava\Kernel\Types\_Int;
7+
use PHPJava\Kernel\Types\_Long;
8+
9+
class NegationTest extends Base
10+
{
11+
protected $fixtures = [
12+
'NegationTest',
13+
];
14+
15+
private function call($method, ...$arguments)
16+
{
17+
$calculatedValue = $this->initiatedJavaClasses['NegationTest']
18+
->getInvoker()
19+
->getStatic()
20+
->getMethods()
21+
->call(
22+
$method,
23+
...$arguments
24+
);
25+
26+
return $calculatedValue->getValue();
27+
}
28+
29+
public function testNegateIntPattern1()
30+
{
31+
$actual = $this->call(
32+
'negateInt',
33+
_Int::get(123)
34+
);
35+
$this->assertEquals('-123', $actual);
36+
}
37+
38+
public function testNegateIntPattern2()
39+
{
40+
$actual = $this->call(
41+
'negateInt',
42+
_Int::get(-123)
43+
);
44+
$this->assertEquals('123', $actual);
45+
}
46+
47+
public function testNegateDoublePattern1()
48+
{
49+
$actual = $this->call(
50+
'negateDouble',
51+
_Double::get(123)
52+
);
53+
$this->assertEquals('-123', $actual);
54+
}
55+
56+
public function testNegateDoublePattern2()
57+
{
58+
$actual = $this->call(
59+
'negateDouble',
60+
_Double::get(-123)
61+
);
62+
$this->assertEquals('123', $actual);
63+
}
64+
65+
public function testNegateDoublePattern3()
66+
{
67+
$actual = $this->call(
68+
'negateDouble',
69+
_Double::get(123.5)
70+
);
71+
$this->assertEquals('-123.5', $actual);
72+
}
73+
74+
public function testNegateDoublePattern4()
75+
{
76+
$actual = $this->call(
77+
'negateDouble',
78+
_Double::get(-123.5)
79+
);
80+
$this->assertEquals('123.5', $actual);
81+
}
82+
83+
public function testNegateFloatPattern1()
84+
{
85+
$actual = $this->call(
86+
'negateFloat',
87+
_Float::get(123)
88+
);
89+
$this->assertEquals('-123', $actual);
90+
}
91+
92+
public function testNegateFloatPattern2()
93+
{
94+
$actual = $this->call(
95+
'negateFloat',
96+
_Float::get(-123)
97+
);
98+
$this->assertEquals('123', $actual);
99+
}
100+
101+
public function testNegateFloatPattern3()
102+
{
103+
$actual = $this->call(
104+
'negateFloat',
105+
_Float::get(123.5)
106+
);
107+
$this->assertEquals('-123.5', $actual);
108+
}
109+
110+
public function testNegateFloatPattern4()
111+
{
112+
$actual = $this->call(
113+
'negateFloat',
114+
_Float::get(-123.5)
115+
);
116+
$this->assertEquals('123.5', $actual);
117+
}
118+
119+
public function testNegateLongPattern1()
120+
{
121+
$actual = $this->call(
122+
'negateLong',
123+
_Long::get(123)
124+
);
125+
$this->assertEquals('-123', $actual);
126+
}
127+
128+
public function testNegateLongPattern2()
129+
{
130+
$actual = $this->call(
131+
'negateLong',
132+
_Long::get(-123)
133+
);
134+
$this->assertEquals('123', $actual);
135+
}
136+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class NegationTest
2+
{
3+
public static int negateInt(int a)
4+
{
5+
return -a;
6+
}
7+
8+
public static double negateDouble(double a)
9+
{
10+
return -a;
11+
}
12+
13+
public static float negateFloat(float a)
14+
{
15+
return -a;
16+
}
17+
18+
public static long negateLong(long a)
19+
{
20+
return -a;
21+
}
22+
}

0 commit comments

Comments
 (0)