29
29
import java .lang .invoke .MethodHandles ;
30
30
import java .lang .invoke .MethodType ;
31
31
import java .lang .invoke .VarHandle ;
32
+ import java .lang .reflect .Field ;
32
33
import java .util .AbstractMap ;
33
34
import java .util .AbstractSet ;
34
35
import java .util .ArrayList ;
35
36
import java .util .Arrays ;
36
37
import java .util .HashMap ;
38
+ import java .util .HashSet ;
37
39
import java .util .Iterator ;
38
40
import java .util .LinkedHashMap ;
41
+ import java .util .LinkedHashSet ;
39
42
import java .util .List ;
40
43
import java .util .Map ;
41
44
import java .util .Set ;
48
51
49
52
/*
50
53
* @test
51
- * @bug 8186958 8210280 8281631 8285386
54
+ * @bug 8186958 8210280 8281631 8285386 8284780
52
55
* @modules java.base/java.util:open
53
56
* @summary White box tests for HashMap-related internals around table sizing
54
57
* @run testng/othervm -Xmx2g WhiteBoxResizeTest
@@ -58,6 +61,7 @@ public class WhiteBoxResizeTest {
58
61
final MethodHandle TABLE_SIZE_FOR ;
59
62
final VarHandle HM_TABLE ;
60
63
final VarHandle WHM_TABLE ;
64
+ final VarHandle HS_MAP ;
61
65
62
66
public WhiteBoxResizeTest () throws ReflectiveOperationException {
63
67
MethodHandles .Lookup hmlookup = MethodHandles .privateLookupIn (HashMap .class , MethodHandles .lookup ());
@@ -67,6 +71,9 @@ public WhiteBoxResizeTest() throws ReflectiveOperationException {
67
71
68
72
MethodHandles .Lookup whmlookup = MethodHandles .privateLookupIn (WeakHashMap .class , MethodHandles .lookup ());
69
73
WHM_TABLE = whmlookup .unreflectVarHandle (WeakHashMap .class .getDeclaredField ("table" ));
74
+
75
+ MethodHandles .Lookup hslookup = MethodHandles .privateLookupIn (HashSet .class , MethodHandles .lookup ());
76
+ HS_MAP = hslookup .unreflectVarHandle (HashSet .class .getDeclaredField ("map" ));
70
77
}
71
78
72
79
/*
@@ -328,15 +335,17 @@ public void populatedCapacity(String label, // unused, included for diagnostics
328
335
Object [] rsc (String label ,
329
336
int size ,
330
337
int expectedCapacity ,
331
- Supplier <Map < String , String > > supplier ) {
338
+ Supplier <Capacitiable > supplier ) {
332
339
return new Object []{label , size , expectedCapacity , supplier };
333
340
}
334
341
335
342
List <Object []> genRequestedSizeCases (int size , int cap ) {
336
343
return Arrays .asList (
337
- rsc ("rshm" , size , cap , () -> HashMap .newHashMap (size )),
338
- rsc ("rslm" , size , cap , () -> LinkedHashMap .newLinkedHashMap (size )),
339
- rsc ("rswm" , size , cap , () -> WeakHashMap .newWeakHashMap (size ))
344
+ rsc ("rshm" , size , cap , () -> new MapCapacitiable (HashMap .newHashMap (size ))),
345
+ rsc ("rslm" , size , cap , () -> new MapCapacitiable (LinkedHashMap .newLinkedHashMap (size ))),
346
+ rsc ("rswm" , size , cap , () -> new MapCapacitiable (WeakHashMap .newWeakHashMap (size ))),
347
+ rsc ("rshs" , size , cap , () -> new SetCapacitiable (HashSet .newHashSet (size ))),
348
+ rsc ("rsls" , size , cap , () -> new SetCapacitiable (LinkedHashSet .newLinkedHashSet (size )))
340
349
);
341
350
}
342
351
@@ -364,9 +373,57 @@ public Iterator<Object[]> requestedSizeCases() {
364
373
public void requestedSize (String label , // unused, included for diagnostics
365
374
int size , // unused, included for diagnostics
366
375
int expectedCapacity ,
367
- Supplier <Map <String , String >> s ) {
368
- Map <String , String > map = s .get ();
369
- map .put ("" , "" );
370
- assertEquals (capacity (map ), expectedCapacity );
376
+ Supplier <Capacitiable > s ) {
377
+ Capacitiable capacitiable = s .get ();
378
+ capacitiable .init ();
379
+ assertEquals (capacitiable .capacity (), expectedCapacity );
380
+ }
381
+
382
+ interface Capacitiable {
383
+
384
+ void init ();
385
+
386
+ int capacity ();
387
+
388
+ }
389
+
390
+ class MapCapacitiable implements Capacitiable {
391
+
392
+ private final Map <String , String > content ;
393
+
394
+ public MapCapacitiable (Map <String , String > content ) {
395
+ this .content = content ;
396
+ }
397
+
398
+ @ Override
399
+ public void init () {
400
+ content .put ("" , "" );
401
+ }
402
+
403
+ @ Override
404
+ public int capacity () {
405
+ return table (content ).length ;
406
+ }
407
+ }
408
+
409
+ class SetCapacitiable implements Capacitiable {
410
+
411
+ private final Set <String > content ;
412
+
413
+ public SetCapacitiable (Set <String > content ) {
414
+ this .content = content ;
415
+ }
416
+
417
+ @ Override
418
+ public void init () {
419
+ content .add ("" );
420
+ }
421
+
422
+ @ Override
423
+ public int capacity () {
424
+ HashMap <?, ?> hashMap = (HashMap <?, ?>) HS_MAP .get (content );
425
+ return table (hashMap ).length ;
426
+ }
371
427
}
428
+
372
429
}
0 commit comments