< prev index next >

test/jdk/java/lang/invoke/VarHandles/VarHandleTestAccessDouble.java

Print this page
rev 52865 : [mq]: 8210031

@@ -48,10 +48,18 @@
 
     final double final_v = 1.0d;
 
     double v;
 
+    static final double static_final_v2 = 1.0d;
+
+    static double static_v2;
+
+    final double final_v2 = 1.0d;
+
+    double v2;
+
     VarHandle vhFinalField;
 
     VarHandle vhField;
 
     VarHandle vhStaticField;

@@ -59,10 +67,45 @@
     VarHandle vhStaticFinalField;
 
     VarHandle vhArray;
 
 
+    VarHandle[] allocate(boolean same) {
+        List<VarHandle> vhs = new ArrayList<>();
+
+        String postfix = same ? "" : "2";
+        VarHandle vh;
+        try {
+            vh = MethodHandles.lookup().findVarHandle(
+                    VarHandleTestAccessDouble.class, "final_v" + postfix, double.class);
+            vhs.add(vh);
+
+            vh = MethodHandles.lookup().findVarHandle(
+                    VarHandleTestAccessDouble.class, "v" + postfix, double.class);
+            vhs.add(vh);
+
+            vh = MethodHandles.lookup().findStaticVarHandle(
+                VarHandleTestAccessDouble.class, "static_final_v" + postfix, double.class);
+            vhs.add(vh);
+
+            vh = MethodHandles.lookup().findStaticVarHandle(
+                VarHandleTestAccessDouble.class, "static_v" + postfix, double.class);
+            vhs.add(vh);
+
+            if (same) {
+                vh = MethodHandles.arrayElementVarHandle(double[].class);
+            }
+            else {
+                vh = MethodHandles.arrayElementVarHandle(String[].class);
+            }
+            vhs.add(vh);
+        } catch (Exception e) {
+            throw new InternalError(e);
+        }
+        return vhs.toArray(new VarHandle[0]);
+    }
+
     @BeforeClass
     public void setup() throws Exception {
         vhFinalField = MethodHandles.lookup().findVarHandle(
                 VarHandleTestAccessDouble.class, "final_v", double.class);
 

@@ -87,10 +130,35 @@
         vhs.add(vhArray);
 
         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
     }
 
+    @Test
+    public void testEqualsAndHashCode() {
+        VarHandle[] vhs1 = allocate(true);
+        VarHandle[] vhs2 = allocate(true);
+
+        for (int i = 0; i < vhs1.length; i++) {
+            for (int j = 0; j < vhs1.length; j++) {
+                if (i == j) {
+                    assertEquals(vhs1[i], vhs1[i]);
+                    assertEquals(vhs1[i], vhs2[i]);
+                    assertEquals(vhs1[i].hashCode(), vhs2[i].hashCode());
+                }
+                else {
+                    assertNotEquals(vhs1[i], vhs1[j]);
+                    assertNotEquals(vhs1[i], vhs2[j]);
+                }
+            }
+        }
+
+        VarHandle[] vhs3 = allocate(false);
+        for (int i = 0; i < vhs1.length; i++) {
+            assertNotEquals(vhs1[i], vhs3[i]);
+        }
+    }
+
     @Test(dataProvider = "varHandlesProvider")
     public void testIsAccessModeSupported(VarHandle vh) {
         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
< prev index next >