Skip to content

Commit 391a589

Browse files
committed
Add tests for String#intern() where the interning does not happen
1 parent 2892724 commit 391a589

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

tests/Packages/JavaLangStringTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,45 @@ public function testIntern()
121121
$this->assertSame($intern, $literal);
122122
}
123123

124+
public function testNotInterned()
125+
{
126+
ob_start();
127+
$this->initiatedJavaClasses['JavaLangStringTest']
128+
->getInvoker()
129+
->getStatic()
130+
->getMethods()
131+
->call(
132+
'notInterned'
133+
);
134+
[ $intern, $literal ] = array_filter(explode("\n", ob_get_clean()));
135+
136+
$this->assertIsNumeric($intern);
137+
$this->assertIsNumeric($literal);
138+
139+
$this->assertNotSame($intern, $literal);
140+
}
141+
142+
public function testNotInternedAfterLiteral()
143+
{
144+
ob_start();
145+
$this->initiatedJavaClasses['JavaLangStringTest']
146+
->getInvoker()
147+
->getStatic()
148+
->getMethods()
149+
->call(
150+
'notInternedAfterLiteral'
151+
);
152+
[ $intern, $literal1, $literal2 ] = array_filter(explode("\n", ob_get_clean()));
153+
154+
$this->assertIsNumeric($intern);
155+
$this->assertIsNumeric($literal1);
156+
$this->assertIsNumeric($literal2);
157+
158+
$this->assertNotSame($intern, $literal1);
159+
$this->assertNotSame($intern, $literal2);
160+
$this->assertSame($literal1, $literal2);
161+
}
162+
124163
public function testReplace()
125164
{
126165
ob_start();

tests/fixtures/java/JavaLangStringTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ public static void intern()
2929
System.out.println(System.identityHashCode("test"));
3030
}
3131

32+
public static void notInterned()
33+
{
34+
String te = "te";
35+
String st = "st";
36+
37+
String test = te + st;
38+
System.out.println(System.identityHashCode(test));
39+
40+
System.out.println(System.identityHashCode("test"));
41+
}
42+
43+
public static void notInternedAfterLiteral()
44+
{
45+
String te = "te";
46+
String st = "st";
47+
48+
String test = te + st;
49+
System.out.println(System.identityHashCode(test));
50+
System.out.println(System.identityHashCode("test"));
51+
52+
test.intern();
53+
System.out.println(System.identityHashCode("test"));
54+
}
55+
3256
public static void replace(String a, String b, String c)
3357
{
3458
System.out.print(a.replace(b, c));

0 commit comments

Comments
 (0)