comparison src/tests/gov/nasa/jpf/test/vm/reflection/FieldTest.java @ 0:61d41facf527

initial v8 import (history reset)
author Peter Mehlitz <Peter.C.Mehlitz@nasa.gov>
date Fri, 23 Jan 2015 10:14:01 -0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:61d41facf527
1 /*
2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
4 * All rights reserved.
5 *
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0.
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package gov.nasa.jpf.test.vm.reflection;
19
20 import gov.nasa.jpf.util.test.TestJPF;
21
22 import java.lang.reflect.Field;
23
24 import org.junit.Test;
25
26 public class FieldTest extends TestJPF {
27
28 int instInt = 42;
29 double instDouble = 42.0;
30 double primField = 42.0;
31 Object refField = new Integer(42);
32 int[] arrayField = new int[]{42};
33
34 static int statInt = 43;
35
36 @Test
37 public void testInstanceInt() {
38 if (verifyNoPropertyViolation()) {
39 try {
40 Class<?> cls = FieldTest.class;
41 Field f = cls.getField("instInt");
42
43 int i = f.getInt(this);
44 assert i == 42;
45
46 f.setInt(this, 43);
47 assert instInt == 43;
48
49 } catch (Throwable t) {
50 assert false : "unexpected exception: " + t;
51 }
52 }
53 }
54
55 @Test
56 public void testStaticInt() {
57 if (verifyNoPropertyViolation()) {
58 try {
59 Class<?> cls = FieldTest.class;
60 Field f = cls.getField("statInt");
61
62 int i = f.getInt(this);
63 assert i == 43;
64 System.out.println("statInt = " + i);
65
66 f.setInt(null, 44);
67 assert statInt == 44;
68
69 } catch (Throwable t) {
70 assert false : "unexpected exception: " + t;
71 }
72 }
73 }
74
75
76 @Test
77 public void testInstanceDouble() {
78 if (verifyNoPropertyViolation()) {
79 try {
80 Class<?> cls = FieldTest.class;
81 Field f = cls.getField("instDouble");
82
83 double d = f.getDouble(this);
84 assert d == 42.0;
85
86 f.setDouble(this, 43.0);
87 assert instDouble == 43.0;
88
89 } catch (Throwable t) {
90 assert false : "unexpected exception: " + t;
91 }
92 }
93 }
94
95 @Test
96 public void testSetPrimitive() {
97 if (verifyNoPropertyViolation()) {
98 try {
99 Class<?> cls = FieldTest.class;
100 Field f = cls.getField("primField");
101
102 double d = ((Double) f.get(this)).doubleValue();
103 assert d == 42.0;
104
105 f.set(this, new Double(43.0));
106 assert primField == 43.0;
107
108 } catch (Throwable t) {
109 assert false : "unexpected exception: " + t;
110 }
111 }
112 }
113
114 @Test
115 public void testSetReference() {
116 if (verifyNoPropertyViolation()) {
117
118 try {
119 Class<?> cls = FieldTest.class;
120 Field f = cls.getField("refField");
121
122 Object o = f.get(this);
123 assert o == refField;
124
125 Object ob = new Double(43.0);
126 f.set(this, ob);
127 assert ob == refField;
128
129 } catch (Throwable t) {
130 assert false : "unexpected exception: " + t;
131 }
132 }
133 }
134
135 @Test
136 public void testSetArray() {
137 if (verifyNoPropertyViolation()) {
138
139 try {
140 Class<?> cls = FieldTest.class;
141 Field f = cls.getField("arrayField");
142
143 int[] a = (int[]) f.get(this);
144 assert a == arrayField;
145
146 int[] ar = new int[]{43};
147 f.set(this, ar);
148 assert ar == arrayField;
149
150 } catch (Throwable t) {
151 assert false : "unexpected exception: " + t;
152 }
153 }
154 }
155
156 //--- field enumeration
157 interface I {
158 static int I_S = 42;
159 }
160
161 static class X implements I {
162 public int pub_x_i; // 1
163 protected int prot_x_i;
164 public static int pub_x_s; // 2
165 static int prot_x_s;
166 }
167
168 static class Y extends X {
169 public static int pub_y_s; // 3
170 static int prot_y_s;
171 public int pub_y_i; // 4
172 protected int prot_y_i;
173 }
174
175 @Test
176 public void testGetFields(){
177 if (verifyNoPropertyViolation()){
178 Field[] publicFields = Y.class.getFields();
179 String[] fnames = {"pub_x_i", "pub_x_s", "pub_y_s", "pub_y_i", "I_S"};
180
181 assertTrue("wrong number of public fields", publicFields.length == fnames.length);
182
183 for (Field f : publicFields){
184 String fname = f.getName();
185 System.out.println(fname);
186
187 for (int i=0; i<fnames.length; i++){
188 if (fname.equals(fnames[i])){
189 fnames[i] = null;
190 }
191 }
192 }
193
194 for (int i=0; i<fnames.length; i++){
195 if (fnames[i] != null){
196 fail("unseen field: " + fnames[i]);
197 }
198 }
199 }
200 }
201 }