Extend the VarHandle test of instance field of type long

Bug: b/247076137
Change-Id: I5981e70ade88c414ab993e5d39b14ee4d7f9b072
diff --git a/src/test/examplesJava9/varhandle/InstanceLongField.java b/src/test/examplesJava9/varhandle/InstanceLongField.java
index dcca70e..fac0ef3 100644
--- a/src/test/examplesJava9/varhandle/InstanceLongField.java
+++ b/src/test/examplesJava9/varhandle/InstanceLongField.java
@@ -10,14 +10,69 @@
 
   private long field;
 
+  private static void checkJavaLangInvokeWrongMethodTypeException(RuntimeException e) {
+    if (e.getClass().getCanonicalName().equals("java.lang.invoke.WrongMethodTypeException")
+        || e.getMessage().equals("java.lang.invoke.WrongMethodTypeException")) {
+      return;
+    }
+    throw e;
+  }
+
   public static void testSet(VarHandle varHandle) {
-    System.out.println("testGet");
+    System.out.println("testSet");
 
     InstanceLongField instance = new InstanceLongField();
+    System.out.println((long) varHandle.get(instance));
 
+    // Long value.
+    varHandle.set(instance, (long) 1);
+    System.out.println((long) varHandle.get(instance));
+    varHandle.set(instance, Long.valueOf(2));
     System.out.println(varHandle.get(instance));
-    varHandle.set(instance, 1);
-    System.out.println(varHandle.get(instance));
+
+    // Long compatible values.
+    varHandle.set(instance, (byte) 3);
+    System.out.println((long) varHandle.get(instance));
+    varHandle.set(instance, Byte.valueOf((byte) 4));
+    System.out.println((long) varHandle.get(instance));
+    varHandle.set(instance, '0');
+    System.out.println((long) varHandle.get(instance));
+    varHandle.set(instance, Character.valueOf('1'));
+    System.out.println((long) varHandle.get(instance));
+    varHandle.set(instance, (short) 5);
+    System.out.println((long) varHandle.get(instance));
+    varHandle.set(instance, Short.valueOf((short) 6));
+    System.out.println((long) varHandle.get(instance));
+    varHandle.set(instance, (int) 7);
+    System.out.println((long) varHandle.get(instance));
+    varHandle.set(instance, Integer.valueOf(8));
+    System.out.println((long) varHandle.get(instance));
+
+    // Long non-compatible values.
+    try {
+      varHandle.set(instance, true);
+    } catch (RuntimeException e) {
+      checkJavaLangInvokeWrongMethodTypeException(e);
+      System.out.println(varHandle.get(instance));
+    }
+    try {
+      varHandle.set(instance, "3");
+    } catch (RuntimeException e) {
+      checkJavaLangInvokeWrongMethodTypeException(e);
+      System.out.println(varHandle.get(instance));
+    }
+    try {
+      varHandle.set(instance, 3.0f);
+    } catch (RuntimeException e) {
+      checkJavaLangInvokeWrongMethodTypeException(e);
+      System.out.println(varHandle.get(instance));
+    }
+    try {
+      varHandle.set(instance, 3.0);
+    } catch (RuntimeException e) {
+      checkJavaLangInvokeWrongMethodTypeException(e);
+      System.out.println(varHandle.get(instance));
+    }
   }
 
   public static void testCompareAndSet(VarHandle varHandle) {
@@ -25,10 +80,108 @@
 
     InstanceLongField instance = new InstanceLongField();
 
-    varHandle.compareAndSet(instance, 1, 2);
+    // Long value.
+    varHandle.compareAndSet(instance, 1L, 2L);
+    System.out.println((long) varHandle.get(instance));
+    varHandle.compareAndSet(instance, 0L, 1L);
+    System.out.println((long) varHandle.get(instance));
+    varHandle.compareAndSet(instance, Long.valueOf(1), 2);
+    System.out.println((long) varHandle.get(instance));
+    varHandle.compareAndSet(instance, 2, Long.valueOf(3));
+    System.out.println((long) varHandle.get(instance));
+    varHandle.compareAndSet(instance, Long.valueOf(3), Long.valueOf(4));
+    System.out.println((long) varHandle.get(instance));
+
+    // Long compatible values.
+    varHandle.compareAndSet(instance, (byte) 4, 5);
     System.out.println(varHandle.get(instance));
-    varHandle.compareAndSet(instance, 0, 1);
+    varHandle.compareAndSet(instance, 5, (byte) 6);
     System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, (byte) 6, (byte) 7);
+    System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, Byte.valueOf((byte) 7), (byte) 8);
+    System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, (byte) 8, Byte.valueOf((byte) 9));
+    System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, Byte.valueOf((byte) 9), Byte.valueOf((byte) 10));
+    System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, 10, '0');
+    System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, '0', 49);
+    System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, '1', '2');
+    System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, (byte) 50, '3');
+    System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, '3', (byte) 52);
+    System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, '4', '5');
+    System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, '5', (int) 11);
+    System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, (int) 11, (int) 12);
+    System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, (int) 12, Integer.valueOf(13));
+    System.out.println(varHandle.get(instance));
+    varHandle.compareAndSet(instance, Integer.valueOf(13), Integer.valueOf(14));
+    System.out.println(varHandle.get(instance));
+
+    // Long non-compatible values.
+    try {
+      varHandle.compareAndSet(instance, 6, 7.0f);
+    } catch (RuntimeException e) {
+      checkJavaLangInvokeWrongMethodTypeException(e);
+      System.out.println(varHandle.get(instance));
+    }
+    try {
+      varHandle.compareAndSet(instance, 6.0f, 7);
+    } catch (RuntimeException e) {
+      checkJavaLangInvokeWrongMethodTypeException(e);
+      System.out.println(varHandle.get(instance));
+    }
+    try {
+      varHandle.compareAndSet(instance, 6.0f, 7.0f);
+    } catch (RuntimeException e) {
+      checkJavaLangInvokeWrongMethodTypeException(e);
+      System.out.println(varHandle.get(instance));
+    }
+    try {
+      varHandle.compareAndSet(instance, 6, 7.0);
+    } catch (RuntimeException e) {
+      checkJavaLangInvokeWrongMethodTypeException(e);
+      System.out.println(varHandle.get(instance));
+    }
+    try {
+      varHandle.compareAndSet(instance, 6.0, 7);
+    } catch (RuntimeException e) {
+      checkJavaLangInvokeWrongMethodTypeException(e);
+      System.out.println(varHandle.get(instance));
+    }
+    try {
+      varHandle.compareAndSet(instance, 6.0, 7.0);
+    } catch (RuntimeException e) {
+      checkJavaLangInvokeWrongMethodTypeException(e);
+      System.out.println(varHandle.get(instance));
+    }
+
+    try {
+      varHandle.compareAndSet(instance, 6, "7");
+    } catch (RuntimeException e) {
+      checkJavaLangInvokeWrongMethodTypeException(e);
+      System.out.println(varHandle.get(instance));
+    }
+    try {
+      varHandle.compareAndSet(instance, "6", 7);
+    } catch (RuntimeException e) {
+      checkJavaLangInvokeWrongMethodTypeException(e);
+      System.out.println(varHandle.get(instance));
+    }
+    try {
+      varHandle.compareAndSet(instance, "6", "7");
+    } catch (RuntimeException e) {
+      checkJavaLangInvokeWrongMethodTypeException(e);
+      System.out.println(varHandle.get(instance));
+    }
   }
 
   public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {