xref: /aosp_15_r20/art/test/003-omnibus-opcodes/src/InstField.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2008 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker public class InstField {
18*795d594fSAndroid Build Coastguard Worker     public boolean mBoolean1, mBoolean2;
19*795d594fSAndroid Build Coastguard Worker     public byte mByte1, mByte2;
20*795d594fSAndroid Build Coastguard Worker     public char mChar1, mChar2;
21*795d594fSAndroid Build Coastguard Worker     public short mShort1, mShort2;
22*795d594fSAndroid Build Coastguard Worker     public int mInt1, mInt2;
23*795d594fSAndroid Build Coastguard Worker     public float mFloat1, mFloat2;
24*795d594fSAndroid Build Coastguard Worker     public long mLong1, mLong2;
25*795d594fSAndroid Build Coastguard Worker     public double mDouble1, mDouble2;
26*795d594fSAndroid Build Coastguard Worker     public volatile long mVolatileLong1, mVolatileLong2;
27*795d594fSAndroid Build Coastguard Worker 
run()28*795d594fSAndroid Build Coastguard Worker     public void run() {
29*795d594fSAndroid Build Coastguard Worker         assignFields();
30*795d594fSAndroid Build Coastguard Worker         checkFields();
31*795d594fSAndroid Build Coastguard Worker         InstField.nullCheck(null);
32*795d594fSAndroid Build Coastguard Worker     }
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker     /*
35*795d594fSAndroid Build Coastguard Worker      * Check access to instance fields through a null pointer.
36*795d594fSAndroid Build Coastguard Worker      */
nullCheck(InstField nully)37*795d594fSAndroid Build Coastguard Worker     static public void nullCheck(InstField nully) {
38*795d594fSAndroid Build Coastguard Worker         System.out.println("InstField.nullCheck");
39*795d594fSAndroid Build Coastguard Worker         try {
40*795d594fSAndroid Build Coastguard Worker             int x = nully.mInt1;
41*795d594fSAndroid Build Coastguard Worker             Main.assertTrue(false);
42*795d594fSAndroid Build Coastguard Worker         } catch (NullPointerException npe) {
43*795d594fSAndroid Build Coastguard Worker             // good
44*795d594fSAndroid Build Coastguard Worker         }
45*795d594fSAndroid Build Coastguard Worker         try {
46*795d594fSAndroid Build Coastguard Worker             long l = nully.mLong1;
47*795d594fSAndroid Build Coastguard Worker             Main.assertTrue(false);
48*795d594fSAndroid Build Coastguard Worker         } catch (NullPointerException npe) {
49*795d594fSAndroid Build Coastguard Worker             // good
50*795d594fSAndroid Build Coastguard Worker         }
51*795d594fSAndroid Build Coastguard Worker         try {
52*795d594fSAndroid Build Coastguard Worker             nully.mInt1 = 5;
53*795d594fSAndroid Build Coastguard Worker             Main.assertTrue(false);
54*795d594fSAndroid Build Coastguard Worker         } catch (NullPointerException npe) {
55*795d594fSAndroid Build Coastguard Worker             // good
56*795d594fSAndroid Build Coastguard Worker         }
57*795d594fSAndroid Build Coastguard Worker         try {
58*795d594fSAndroid Build Coastguard Worker             nully.mLong1 = 17L;
59*795d594fSAndroid Build Coastguard Worker             Main.assertTrue(false);
60*795d594fSAndroid Build Coastguard Worker         } catch (NullPointerException npe) {
61*795d594fSAndroid Build Coastguard Worker             // good
62*795d594fSAndroid Build Coastguard Worker         }
63*795d594fSAndroid Build Coastguard Worker     }
64*795d594fSAndroid Build Coastguard Worker 
assignFields()65*795d594fSAndroid Build Coastguard Worker     public void assignFields() {
66*795d594fSAndroid Build Coastguard Worker         System.out.println("InstField assign...");
67*795d594fSAndroid Build Coastguard Worker         mBoolean1 = true;
68*795d594fSAndroid Build Coastguard Worker         mBoolean2 = false;
69*795d594fSAndroid Build Coastguard Worker         mByte1 = 127;
70*795d594fSAndroid Build Coastguard Worker         mByte2 = -128;
71*795d594fSAndroid Build Coastguard Worker         mChar1 = 32767;
72*795d594fSAndroid Build Coastguard Worker         mChar2 = 65535;
73*795d594fSAndroid Build Coastguard Worker         mShort1 = 32767;
74*795d594fSAndroid Build Coastguard Worker         mShort2 = -32768;
75*795d594fSAndroid Build Coastguard Worker         mInt1 = 65537;
76*795d594fSAndroid Build Coastguard Worker         mInt2 = -65537;
77*795d594fSAndroid Build Coastguard Worker         mFloat1 = 3.1415f;
78*795d594fSAndroid Build Coastguard Worker         mFloat2 = -1.0f / 0.0f;                // -inf
79*795d594fSAndroid Build Coastguard Worker         mLong1 = 1234605616436508552L;     // 0x1122334455667788
80*795d594fSAndroid Build Coastguard Worker         mLong2 = -1234605616436508552L;
81*795d594fSAndroid Build Coastguard Worker         mDouble1 = 3.1415926535;
82*795d594fSAndroid Build Coastguard Worker         mDouble2 = 1.0 / 0.0;               // +inf
83*795d594fSAndroid Build Coastguard Worker         mVolatileLong1 = mLong1 - 1;
84*795d594fSAndroid Build Coastguard Worker         mVolatileLong2 = mLong2 + 1;
85*795d594fSAndroid Build Coastguard Worker     }
86*795d594fSAndroid Build Coastguard Worker 
checkFields()87*795d594fSAndroid Build Coastguard Worker     public void checkFields() {
88*795d594fSAndroid Build Coastguard Worker         System.out.println("InstField check...");
89*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mBoolean1);
90*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(!mBoolean2);
91*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mByte1 == 127);
92*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mByte2 == -128);
93*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mChar1 == 32767);
94*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mChar2 == 65535);
95*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mShort1 == 32767);
96*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mShort2 == -32768);
97*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mInt1 == 65537);
98*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mInt2 == -65537);
99*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mFloat1 > 3.141f && mFloat1 < 3.142f);
100*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mFloat2 < mFloat1);
101*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mLong1 == 1234605616436508552L);
102*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mLong2 == -1234605616436508552L);
103*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mDouble1 > 3.141592653 && mDouble1 < 3.141592654);
104*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mDouble2 > mDouble1);
105*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mVolatileLong1 == 1234605616436508551L);
106*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(mVolatileLong2 == -1234605616436508551L);
107*795d594fSAndroid Build Coastguard Worker     }
108*795d594fSAndroid Build Coastguard Worker }
109