Skip to content

Commit 5b9add7

Browse files
authored
Merge pull request #134 from php-java/system/indentityhashcode-test
Add tests for #133
2 parents 0ae5e23 + 4dcd728 commit 5b9add7

File tree

6 files changed

+109
-0
lines changed

6 files changed

+109
-0
lines changed

tests/Packages/JavaLangStringTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ public function testConcat()
8585
$this->assertEquals('abcdef', $value);
8686
}
8787

88+
public function testHashCode()
89+
{
90+
ob_start();
91+
$this->initiatedJavaClasses['JavaLangStringTest']
92+
->getInvoker()
93+
->getStatic()
94+
->getMethods()
95+
->call(
96+
'testHashCode'
97+
);
98+
99+
$values = array_filter(explode("\n", ob_get_clean()));
100+
$this->assertCount(3, $values);
101+
$this->assertSame('2728214739616339340', $values[0]);
102+
$this->assertSame('2728214739616339340', $values[1]);
103+
$this->assertSame('2728214739616339340', $values[2]);
104+
}
105+
88106
public function testReplace()
89107
{
90108
ob_start();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace PHPJava\Tests\Packages;
3+
4+
use PHPJava\Core\JavaArchive;
5+
use PHPJava\Tests\Base;
6+
7+
class JavaLangSystemTest extends Base
8+
{
9+
protected $fixtures = [
10+
'JavaLangSystemTest',
11+
];
12+
13+
public function testIdentityHashCode()
14+
{
15+
ob_start();
16+
$this->initiatedJavaClasses['JavaLangSystemTest']
17+
->getInvoker()
18+
->getStatic()
19+
->getMethods()
20+
->call(
21+
'identityHashCode'
22+
);
23+
24+
$values = array_filter(explode("\n", ob_get_clean()));
25+
$this->assertCount(2, $values);
26+
27+
$hashCodes = [];
28+
29+
foreach ($values as $value) {
30+
$this->assertIsNumeric($value);
31+
$this->assertNotContains($value, $hashCodes);
32+
$hashCodes[] = $value;
33+
}
34+
}
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace PHPJava\Tests\Packages;
3+
4+
use PHPJava\Core\JavaArchive;
5+
use PHPJava\Tests\Base;
6+
7+
class JavaUtilObjectsTest extends Base
8+
{
9+
protected $fixtures = [
10+
'JavaUtilObjectsTest',
11+
];
12+
13+
public function testHashCode()
14+
{
15+
ob_start();
16+
$this->initiatedJavaClasses['JavaUtilObjectsTest']
17+
->getInvoker()
18+
->getStatic()
19+
->getMethods()
20+
->call(
21+
'testHashCode'
22+
);
23+
24+
$values = array_filter(explode("\n", ob_get_clean()));
25+
$this->assertCount(3, $values);
26+
$this->assertSame('2728214739616339340', $values[0]);
27+
$this->assertSame('2728214739616339340', $values[1]);
28+
$this->assertSame('2728214739616339340', $values[2]);
29+
}
30+
}

tests/fixtures/java/JavaLangStringTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ public static void concat(String a, String b)
1010
System.out.print(a.concat(b));
1111
}
1212

13+
public static void testHashCode()
14+
{
15+
System.out.println("hello, world".hashCode());
16+
System.out.println("HELLO, WORLD".toLowerCase().hashCode());
17+
System.out.println((new String("hello, world")).hashCode());
18+
}
19+
1320
public static void replace(String a, String b, String c)
1421
{
1522
System.out.print(a.replace(b, c));
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class JavaLangSystemTest
2+
{
3+
public static void identityHashCode()
4+
{
5+
System.out.println(System.identityHashCode("Hello, World"));
6+
System.out.println(System.identityHashCode(new String("Hello, World")));
7+
}
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import java.util.Objects;
2+
3+
class JavaUtilObjectsTest
4+
{
5+
public static void testHashCode()
6+
{
7+
System.out.println(Objects.hashCode("hello, world"));
8+
System.out.println(Objects.hashCode("HELLO, WORLD".toLowerCase()));
9+
System.out.println(Objects.hashCode(new String("hello, world")));
10+
}
11+
}

0 commit comments

Comments
 (0)