1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import java.lang.invoke.MethodHandles;
18 import java.lang.invoke.VarHandle;
19 
20 public class VarHandleFpCasTests {
21     public static class FieldFloatTest extends VarHandleUnitTest {
22         private static final VarHandle vh;
23 
24         public static class A {
25             public float field;
26         }
27 
28         static {
29             try {
30                 vh = MethodHandles.lookup().findVarHandle(A.class, "field", float.class);
31             } catch (Exception e) {
32                 throw new RuntimeException(e);
33             }
34         }
35 
36         @Override
doTest()37         protected void doTest() {
38             A a = new A();
39             assertTrue(vh.compareAndSet(a, 0.0f, 1.0f));
40             assertTrue(vh.compareAndSet(a, 1.0f, 0.0f));
41             assertFalse(vh.compareAndSet(a, -0.0f, Float.NaN));
42             assertTrue(vh.compareAndSet(a, 0.0f, Float.NaN));
43             assertTrue(vh.compareAndSet(a, Float.NaN, 0.0f));
44         }
45 
main(String[] args)46         public static void main(String[] args) {
47             new FieldFloatTest().run();
48         }
49     }
50 
51     public static class FieldDoubleTest extends VarHandleUnitTest {
52         private static final VarHandle vh;
53 
54         public static class A {
55             public double field;
56         }
57 
58         static {
59             try {
60                 vh = MethodHandles.lookup().findVarHandle(A.class, "field", double.class);
61             } catch (Exception e) {
62                 throw new RuntimeException(e);
63             }
64         }
65 
66         @Override
doTest()67         protected void doTest() {
68             A a = new A();
69             assertTrue(vh.compareAndSet(a, 0.0, 1.0));
70             assertTrue(vh.compareAndSet(a, 1.0, 0.0));
71             assertFalse(vh.compareAndSet(a, -0.0, Double.NaN));
72             assertTrue(vh.compareAndSet(a, 0.0, Double.NaN));
73             assertTrue(vh.compareAndSet(a, Double.NaN, 0.0));
74         }
75 
main(String[] args)76         public static void main(String[] args) {
77             new FieldDoubleTest().run();
78         }
79     }
80 
main(String[] args)81     public static void main(String[] args) {
82         FieldFloatTest.main(args);
83         FieldDoubleTest.main(args);
84     }
85 }
86